entry points with the or condition throw an exception

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

entry points with the or conditon throw a exception

问题

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

  1. declare EventTest
  2. @role( event )
  3. @timestamp( callDateTime )
  4. @duration( callDuration )
  5. @expires( 1h35m )
  6. end
  7. rule "Event2"
  8. when
  9. $m : EventTest( originNumber == "123", originNumber : originNumber ) from entry-point "ATM Stream"
  10. or
  11. $m1 : EventTest( originNumber == "456" ) from entry-point "ATM Stream"
  12. then
  13. System.out.println( originNumber );
  14. end

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

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

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

  1. rule "Event2 Condition 1"
  2. when
  3. $m : EventTest( originNumber == "123", originNumber : originNumber ) from entry-point "ATM Stream"
  4. then
  5. System.out.println( originNumber );
  6. end
  7. rule "Event2 Condition 2"
  8. when
  9. $m1 : EventTest( originNumber == "456" ) from entry-point "ATM Stream"
  10. then
  11. System.out.println( originNumber );
  12. end

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

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

英文:

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

  1. declare EventTest
  2. @role( event )
  3. @timestamp( callDateTime )
  4. @duration( callDuration )
  5. @expires( 1h35m )
  6. end
  7. rule "Event2"
  8. when
  9. $m : EventTest( originNumber == "123", originNumber : originNumber ) from entry-point "ATM Stream"
  10. or
  11. $m1 : EventTest( originNumber == "456" ) from entry-point "ATM Stream"
  12. then
  13. System.out.println( originNumber );
  14. end
  1. public class EventTest {
  2. private String originNumber;
  3. private String destinationNumber;
  4. private Timestamp callDateTime;
  5. private long callDuration; // in milliseconds
  6. // Constructors, getters, and setters
  7. public EventTest(String originNumber,String destinationNumber,Timestamp callDateTime,long callDuration) {
  8. this.originNumber = originNumber;
  9. this.destinationNumber = destinationNumber;
  10. this.callDateTime = callDateTime;
  11. this.callDuration = callDuration;
  12. }
  13. public String getOriginNumber() {
  14. return originNumber;
  15. }
  16. public void setOriginNumber(String originNumber) {
  17. this.originNumber = originNumber;
  18. }
  19. public String getDestinationNumber() {
  20. return destinationNumber;
  21. }
  22. public void setDestinationNumber(String destinationNumber) {
  23. this.destinationNumber = destinationNumber;
  24. }
  25. public Timestamp getCallDateTime() {
  26. return callDateTime;
  27. }
  28. public void setCallDateTime(Timestamp callDateTime) {
  29. this.callDateTime = callDateTime;
  30. }
  31. public long getCallDuration() {
  32. return callDuration;
  33. }
  34. public void setCallDuration(long callDuration) {
  35. this.callDuration = callDuration;
  36. }
  37. }
  38. public class EventExample {
  39. public static final void main(final String[] args) throws Exception{
  40. // KieServices is the factory for all KIE services
  41. KieServices ks = KieServices.Factory.get();
  42. // From the kie services, a container is created from the classpath
  43. KieContainer kc = ks.getKieClasspathContainer();
  44. execute( kc );
  45. }
  46. public static void execute( KieContainer kc) throws Exception{
  47. // From the container, a session is created based on
  48. // its definition and configuration in the META-INF/kmodule.xml file
  49. KieSession ksession = kc.newKieSession("EventKS");
  50. LocalTime.now().plusSeconds(3).atDate(zoneId));
  51. EntryPoint atmStream = ksession.getEntryPoint("ATM Stream");
  52. new Thread( new Runnable() {
  53. @Override
  54. public void run() {
  55. ksession.fireUntilHalt();
  56. }
  57. } ).start();
  58. atmStream.insert(new EventTest("123","456", Timestamp.valueOf(LocalDateTime.now()),30));
  59. int i = 0;
  60. while (true){
  61. i++;
  62. atmStream.insert(new EventTest("456","789",Timestamp.valueOf(LocalDateTime.now().plusSeconds(4)),30));
  63. try {
  64. Thread.sleep(1000);
  65. } catch (InterruptedException e) {
  66. e.printStackTrace();
  67. }
  68. if (i == 5){
  69. break;
  70. }
  71. }
  72. atmStream.insert("test string aaaa");
  73. ksession.halt();
  74. ksession.dispose();
  75. }
  76. }

it throws a exception

  1. 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
  2. text=Rule Compilation error originNumber cannot be resolved to a variable]]
  3. at org.drools.compiler.kie.builder.impl.KieContainerImpl.getKieBase(KieContainerImpl.java:379)
  4. at org.drools.compiler.kie.builder.impl.KieContainerImpl.getKieBaseFromKieSessionModel(KieContainerImpl.java:577)
  5. at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:553)
  6. at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:523)
  7. at com.neo.drools.EventExample.execute(EventExample.java:35)
  8. 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

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

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

Try executing below rule and check :

  1. rule "Event2"
  2. when
  3. $m : EventTest( originNumber == "123", originNumber : originNumber ) from entry-point "ATM Stream"
  4. or
  5. $m1 : EventTest( originNumber == "456",originNumber : originNumber ) from entry-point "ATM Stream"
  6. then
  7. System.out.println( originNumber );
  8. 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:

确定