Search code examples
wso2complex-event-processing

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.

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:

    @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. However when i run this query in CEP the whole event drops into my execute function i.e. there is no sign to show that i send "name" field is sent to startswithA method as argument.

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?


Solution

  • In 'AbstractGenericConditionExecutor' there's another method that gives you the set of expression executors that are included in the parameters when executor instantiates:

    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. 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. Similarly for a constant like 'A', you'll get a different executor that will give you the value 'A' when called.