简体   繁体   English

在WSO2 CEP中使用左右参数编写自定义条件

[英]Writing a custom condition in WSO2 CEP with left and right argument

I want to extend the Wso2 CEP product in our needs and try to write a custom condition as indicated in this official wso2 cep link . 我想扩展我们的需求中的Wso2 CEP产品,并尝试编写此wso2 cep官方链接中指示的自定义条件。

I am able to write an extension class that extends "org.wso2.siddhi.core.executor.conditon.AbstractGenericConditionExecutor" and implement its abstract method as indicated below: 我能够编写一个扩展类来扩展“ org.wso2.siddhi.core.executor.conditon.AbstractGenericConditionExecutor”并实现其抽象方法,如下所示:

    @SiddhiExtension(namespace = "myext", function = "startswithA")
public class StringUtils extends
        org.wso2.siddhi.core.executor.conditon.AbstractGenericConditionExecutor {

    static Log log = LogFactory.getLog(StringUtils.class);

    @Override
    public boolean execute(AtomicEvent atomicEvent) {
        log.error("Entered the execute method");
        log.error("Atomic event to string: " + atomicEvent.toString());

        return true;
    }
}

when i use this extensioned method as: 当我将此扩展方法用作:

from allEventsStream[myext:startswithA(name)]
insert into selectedEventsStream *;

In this situation, i want that startswithA method returns true if the name field has 'A' at the begining of it. 在这种情况下,如果名称字段的开头带有“ A”,我希望startswithA方法返回true。 However when i run this query in CEP the whole event drops into my execute function ie there is no sign to show that i send "name" field is sent to startswithA method as argument. 但是,当我在CEP中运行此查询时,整个事件都落入我的execute函数中,即没有迹象表明我发送的“ name”字段已作为参数发送给startswithA方法。

How can i understand which field of the stream is sent to my extended method as argument? 我如何理解流的哪个字段作为参数发送到扩展方法?

Also i want to write conditions like 我也想写条件

from allEventsStream[myext:startswith('A', name)]
insert into selectedEventsStream *;

How can i achive this? 我怎样才能做到这一点?

In 'AbstractGenericConditionExecutor' there's another method that gives you the set of expression executors that are included in the parameters when executor instantiates: 在“ AbstractGenericConditionExecutor”中,还有另一种方法,可在执行程序实例化时为您提供一组包含在参数中的表达式执行程序:

public void setExpressionExecutors(List<ExpressionExecutor> expressionExecutors)

You don't necessarily have to override this method and store the list, it is already stored there in the 'AbastractGenericConditionExecutor' as a list named expressionExecutors . 您不必重写此方法并存储列表,它已经作为名为expressionExecutors的列表存储在“ AbastractGenericConditionExecutor”中。 You can pass the event to these executors to retrieve the relevant values from the event in order. 您可以将事件传递给这些执行程序,以按顺序从事件中检索相关值。

For an example, if you include a variable (like 'name') in the query (as a parameter at index 0), you'll get a 'VariableExpressionExecutor' in the list at index 0 that will fetch you the value of the variable from the event. 例如,如果在查询中包含变量(如“名称”)(作为索引0的参数),则列表中的索引0处将获得“ VariableExpressionExecutor”,该变量将获取变量的值从事件。 Similarly for a constant like 'A', you'll get a different executor that will give you the value 'A' when called. 同样,对于像“ A”这样的常量,您将获得一个不同的执行程序,该执行程序在调用时将为您提供值“ A”。

To add to Rajeev's answer, if you want to filter all the names that starts with 'A', you can override the execute method of your custom Siddhi extension similar to the following segment. 要添加到Rajeev的答案中,如果要过滤以'A'开头的所有名称,则可以覆盖自定义Siddhi扩展名的execute方法,类似于以下部分。

@Override
public boolean execute(AtomicEvent atomicEvent) {
    if(!this.expressionExecutors.isEmpty()) {
        String name = (String)this.expressionExecutors.get(0).execute(atomicEvent);
        if(name.startsWith("A")) {
            return true;
        }
    }
    return false;
}

When writing the query, it would be similar to 编写查询时,它类似于

from allEventStream[myext:startsWithA(name)]
insert into filteredStream *;

You can extend this behaviour to achieve an extension that supports 您可以扩展此行为以获得支持的扩展

from allEventsStream[myext:startswith('A', name)]

type queries as well. 以及键入查询。

HTH, HTH,

Lasantha 拉桑塔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM