创建 Esper 的 epl 实例 [英] Creating instances of Esper's epl

查看:47
本文介绍了创建 Esper 的 epl 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩 Esper,学习如何使用更高级的概念.我有一个程序可以触发 3 种不同股票的模拟股票事件.我目前有一个带有 match_recognize 模式 EPL 的模块,如下所示:

I am playing around with Esper, learning how to use more advanced concepts. I have a program that fires mock stock events of 3 different stocks. I currently have a module with match_recognize pattern EPL that looks like this:

module queries;

import events.*;
import configDemo.*;
import annotations.*;

create schema MyTickEvent as TickEvent;

@Name('compareStocks') 
@Description('Compare the difference amount between two stocks')
@Subscriber(className='configDemo.MySubscriber')
select * from TickEvent
match_recognize (
measures A.currentPrice as a_currentPrice, B.currentPrice as b_currentPrice, 
A.stockName as a_stockName, B.stockName as b_stockName
pattern (A C* B)
define
A as A.stockName = firstStock,
B as A.currentPrice - B.currentPrice >= difference and B.stockName = 
secondStock
);

如您所见,其中包含三个变量 - firstStock、secondStock、差异.我接受用户的输入,然后在 java 代码中设置变量,如下所示:

As you can see there are three variables in it - firstStock, secondStock, difference. I take the user's input and then set the variables within the java code as such:

    System.out.println("Please enter 3 char stock name for the first stock: ");
    System.out.println("Available stocks: IBM, YAH, MIC");
    first = scanner.nextLine();
    engineHelper.getAdmin().getConfiguration().addVariable("firstStock", String.class, first);

    System.out.println("Please enter 3 char stock name for the second stock: ");
    second = scanner.nextLine();
    engineHelper.getAdmin().getConfiguration().addVariable("secondStock", String.class, second);

    System.out.println("Please enter integer value for stock difference: ");
    difference = scanner.nextInt();
    engineHelper.getAdmin().getConfiguration().addVariable("difference", Integer.class, difference);

如果我只想一次跟踪一对股票,那效果很好.我正在努力为多对找到一种方法.我希望能够动态创建/删除/启动/停止对.例如,假设我有 YAH、APP、MIC、GOO 股票.事件开始运行,我决定要跟踪 MIC/GOO 之间超过 X 量的差异.然后我决定我也想以不同的数量跟踪 APP/GOO.数据将是这样的:

That works fine if I only want to keep track of one stock pair at a time. I am struggling to find a way of doing that for multiple pairs. I want to be able to dynamically create/delete/start/stop pairs. For examples, let's say I have YAH, APP, MIC, GOO stocks. The events start running and I decide that I want to track for a difference of more than X amount between MIC/GOO. Then I decide I want to track APP/GOO too for a different amount. The data would be something like this:

[IBM,YAH,5][GOO,APP,3]....

[IBM, YAH, 5] [GOO, APP, 3] ....

关于如何做到这一点的任何建议?我想我需要用一组新变量创建一个新的 EPL 实例.我可以在 java 代码中轻松做到这一点,但我想尽可能远离它.我想使用模块文件.由于它本质上是相同的 EPL,因此有一种方法可以将其用作具有不同股票对的多个实例"的模板.

Any suggestions of how to do that? I imagine I would need to create a new instance of the EPL with a new set of variables. I can easily do that within java code but I want to stay away from it as much as possible. I want to use module files. Since it is essentially the same EPL, it would make sense to have a way of using it as a template with multiple "instances" for different stock pairs.

或者,是否有其他方法可以有效地实现这一目标?

Alternatively, are there other ways of achieving this efficiently?

我让它工作了,我注意到错误来自文件中不再存在的文本,所以我删除了它并重新编写它并且它起作用了.现在一切都已成功部署,这是它的外观:

I got it working, I noticed the errors come from text that no longer exists in the file so I deleted it and rewrote it and it worked. All is deployed successfully now and this is how it looks:

module context;

import events.*;
import configDemo.*;
import annotations.*;
import main.*;
import subscribers.*;


create schema InitEvent(firstStock string, secondStock string, bias double);

create context TwoStocksContext
initiated by InitEvent as initEvent;


@Name('compareStocks') 
@Description('Compare the difference between two different stocks and make a 
decision')
@Subscriber(className='subscribers.MySubscriber')
context TwoStocksContext 
select * from TickEvent
match_recognize (
measures A.currentPrice as a_currentPrice, B.currentPrice as b_currentPrice, A.stockCode as a_stockCode, B.stockCode as b_stockCode
pattern (A C* B)
define
A as A.stockCode =  context.initEvent.firstStock,
B as A.currentPrice - B.currentPrice >=  context.initEvent.bias and 
B.stockCode =  context.initEvent.secondStock
);

推荐答案

您可以让 Esper 使用上下文来分配语句的多个分区.将变量放入Init"-Event 并发送此事件以分配每个变量.一个例子

You can get Esper to allocate multiple partitions of a statement using contexts. Put the variables into an "Init"-Event and send this event to allocate each. An example

create schema InitEvent(firststock string, secondstock string, diff double);

create context AnalyzePerFirstAndSecond initiated by InitEvent as initEvent; // add terminated here if needed

context AnalyzePerFirstAndSecond select .... define A as A.stock = context.initEvent.firststock....

文档章节链接以获得更完整的解决方案...http://esper.espertech.com/release-7.0.0/esper-reference/html_single/index.html#perf-tips-27

Doc chapter link for more complete solution... http://esper.espertech.com/release-7.0.0/esper-reference/html_single/index.html#perf-tips-27

这篇关于创建 Esper 的 epl 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆