entry points with the or condition throw an exception

huangapple go评论68阅读模式
英文:

entry points with the or conditon throw a exception

问题

我想实现一个类似以下规则的规则(在流模式下):

declare EventTest
  @role( event )
  @timestamp( callDateTime )
  @duration( callDuration )
  @expires( 1h35m )
end

rule "Event2"
    when
        $m : EventTest( originNumber == "123", originNumber : originNumber )  from entry-point "ATM Stream"
        or
        $m1 : EventTest( originNumber == "456" )  from entry-point "ATM Stream"
    then
        System.out.println( originNumber );
end

在这个规则中,你有两个条件,一个是检查 originNumber 是否等于 "123",另一个是检查 originNumber 是否等于 "456",并且两者都在 "ATM Stream" 入口点中触发。

你提到当设置成 "和" 条件时正常工作,但设置成 "或" 条件时出现问题。Drools 确实不支持入口点的 "或" 条件,但你可以通过不同的方式实现这个逻辑规则。

一种方法是将两个条件拆分为两个规则,并在需要时触发相应的规则。例如:

rule "Event2 Condition 1"
when
    $m : EventTest( originNumber == "123", originNumber : originNumber )  from entry-point "ATM Stream"
then
    System.out.println( originNumber );
end

rule "Event2 Condition 2"
when
    $m1 : EventTest( originNumber == "456" )  from entry-point "ATM Stream"
then
    System.out.println( originNumber );
end

这样,你可以分别触发满足条件的规则。

希望这个解决方案能够帮助你实现你的逻辑规则。如果你有其他问题,请随时提问。

英文:

I want to implement a rule likes below(in stream mode):


declare EventTest
  @role( event )
  @timestamp( callDateTime )
  @duration( callDuration )
  @expires( 1h35m )
end

rule "Event2"
    when
        $m : EventTest( originNumber == "123", originNumber : originNumber )  from entry-point "ATM Stream"
        or
        $m1 : EventTest( originNumber == "456" )  from entry-point "ATM Stream"
    then
        System.out.println( originNumber );
end
public class EventTest {
    private String  originNumber;
    private String  destinationNumber;
    private Timestamp callDateTime;
    private long    callDuration;  // in milliseconds

    // Constructors, getters, and setters


    public EventTest(String originNumber,String destinationNumber,Timestamp callDateTime,long callDuration) {
        this.originNumber = originNumber;
        this.destinationNumber = destinationNumber;
        this.callDateTime = callDateTime;
        this.callDuration = callDuration;
    }

    public String getOriginNumber() {
        return originNumber;
    }

    public void setOriginNumber(String originNumber) {
        this.originNumber = originNumber;
    }

    public String getDestinationNumber() {
        return destinationNumber;
    }

    public void setDestinationNumber(String destinationNumber) {
        this.destinationNumber = destinationNumber;
    }

    public Timestamp getCallDateTime() {
        return callDateTime;
    }

    public void setCallDateTime(Timestamp callDateTime) {
        this.callDateTime = callDateTime;
    }

    public long getCallDuration() {
        return callDuration;
    }

    public void setCallDuration(long callDuration) {
        this.callDuration = callDuration;
    }
}

public class EventExample {
    public static final void main(final String[] args) throws Exception{
        // KieServices is the factory for all KIE services
        KieServices ks = KieServices.Factory.get();

        // From the kie services, a container is created from the classpath
        KieContainer kc = ks.getKieClasspathContainer();
        execute( kc );
    }

    public static void execute( KieContainer kc) throws Exception{
        // From the container, a session is created based on
        // its definition and configuration in the META-INF/kmodule.xml file

        KieSession ksession = kc.newKieSession("EventKS");


LocalTime.now().plusSeconds(3).atDate(zoneId));

        EntryPoint atmStream  = ksession.getEntryPoint("ATM Stream");


        new Thread( new Runnable() {
            @Override
            public void run() {
                ksession.fireUntilHalt();
            }
        } ).start();


        atmStream.insert(new EventTest("123","456", Timestamp.valueOf(LocalDateTime.now()),30));



        int i = 0;
        while (true){
            i++;
            atmStream.insert(new EventTest("456","789",Timestamp.valueOf(LocalDateTime.now().plusSeconds(4)),30));

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (i == 5){
                break;
            }
        }

        atmStream.insert("test string aaaa");

        ksession.halt();
        ksession.dispose();

    }
}

it throws a exception

Exception in thread "main" java.lang.RuntimeException: Error while creating KieBase[Message [id=1, kieBase=EventKS, level=ERROR, path=/home/dai/download/drools-examples-master/drools-simple-demo/target/classes/rules/Event.drl, line=45, column=0
   text=Rule Compilation error originNumber cannot be resolved to a variable]]
	at org.drools.compiler.kie.builder.impl.KieContainerImpl.getKieBase(KieContainerImpl.java:379)
	at org.drools.compiler.kie.builder.impl.KieContainerImpl.getKieBaseFromKieSessionModel(KieContainerImpl.java:577)
	at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:553)
	at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:523)
	at com.neo.drools.EventExample.execute(EventExample.java:35)
	at com.neo.drools.EventExample.main(EventExample.java:28)

when i set it to and ,it's normal and output the following log

123
123
123
123
123

it seams drools doesn't support the or condition for entry points ,the CompositeFactPattern also can not set or type with entry points facts,but the and condition is no problem.how can i achive this logic rule?

答案1

得分: 1

尝试执行以下规则并检查:

规则 "Event2"
    $m : EventTest( originNumber == "123", originNumber : originNumber ) 从入口点 "ATM Stream"
    $m1 : EventTest( originNumber == "456", originNumber : originNumber ) 从入口点 "ATM Stream"
然后
    System.out.println( originNumber );
结束
英文:

Try executing below rule and check :

rule "Event2"
    when
        $m : EventTest( originNumber == "123", originNumber : originNumber )  from entry-point "ATM Stream"
        or
        $m1 : EventTest( originNumber == "456",originNumber : originNumber )  from entry-point "ATM Stream"
    then
        System.out.println( originNumber );
end

huangapple
  • 本文由 发表于 2020年1月3日 14:54:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定