簡體   English   中英

在WSO2 CEP中使用左右參數編寫自定義條件

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

我想擴展我們的需求中的Wso2 CEP產品,並嘗試編寫此wso2 cep官方鏈接中指示的自定義條件。

我能夠編寫一個擴展類來擴展“ 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;
    }
}

當我將此擴展方法用作:

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

在這種情況下,如果名稱字段的開頭帶有“ A”,我希望startswithA方法返回true。 但是,當我在CEP中運行此查詢時,整個事件都落入我的execute函數中,即沒有跡象表明我發送的“ name”字段已作為參數發送給startswithA方法。

我如何理解流的哪個字段作為參數發送到擴展方法?

我也想寫條件

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

我怎樣才能做到這一點?

在“ AbstractGenericConditionExecutor”中,還有另一種方法,可在執行程序實例化時為您提供一組包含在參數中的表達式執行程序:

public void setExpressionExecutors(List<ExpressionExecutor> expressionExecutors)

您不必重寫此方法並存儲列表,它已經作為名為expressionExecutors的列表存儲在“ AbastractGenericConditionExecutor”中。 您可以將事件傳遞給這些執行程序,以按順序從事件中檢索相關值。

例如,如果在查詢中包含變量(如“名稱”)(作為索引0的參數),則列表中的索引0處將獲得“ VariableExpressionExecutor”,該變量將獲取變量的值從事件。 同樣,對於像“ A”這樣的常量,您將獲得一個不同的執行程序,該執行程序在調用時將為您提供值“ A”。

要添加到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;
}

編寫查詢時,它類似於

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

您可以擴展此行為以獲得支持的擴展

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

以及鍵入查詢。

HTH,

拉桑塔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM