Uploaded image for project: 'Drools'
  1. Drools
  2. DROOLS-574

str operator in 'accumulate', with operand from 'from', results in NPE at KB build

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 6.2.0.Beta2
    • 6.1.0.Final
    • None
    • None

      This is an exercise for "group by" words.

      Consider the following pattern for realizing a "group by" by means of from/accumulate/count : the goal is to count Messages which starts with preferred keyword, if the count for a keyword is >= 2, then rule should fire.

      declare GroupByString
      	groupId : String
      	groups : String[]
      end
      
      rule "init words of my interest"
      no-loop
      when
      then
      	GroupByString grp = new GroupByString();
      	grp.setGroupId("wordGroup");
      	grp.setGroups(new String[]{"ciao", "bella"});
      	insert(grp);
      end
      
      rule "group by word and count if >=2 then "
      no-loop
      when
      	$group : GroupByString( groupId == "wordGroup")
      	$word : String() from $group.groups
      	accumulate ( $msg : Message( text str[startsWith] $word ) over window:time( 5m ) ;
      				 $list : collectList( $msg ),
      				 $count : count( $msg );
      				 $count >= 2
      				 ) 				 
      then
      	System.out.println("group by " + $word + " count is "+ $count + " list: " + $list);
      	insert(new Integer($count.intValue()));
      end
      

      However trying to compile the KB with the kie-maven-plugin fails with this NPE and stacktrace (snippet):

      java.lang.NullPointerException
      	at org.drools.compiler.rule.builder.dialect.java.JavaAccumulateBuilder.buildExternalFunctionCall(JavaAccumulateBuilder.java:182)
      	at org.drools.compiler.rule.builder.dialect.java.JavaAccumulateBuilder.build(JavaAccumulateBuilder.java:101)
      	at org.drools.compiler.rule.builder.dialect.java.JavaAccumulateBuilder.build(JavaAccumulateBuilder.java:66)
      	at org.drools.compiler.rule.builder.PatternBuilder.build(PatternBuilder.java:320)
      	at org.drools.compiler.rule.builder.PatternBuilder.build(PatternBuilder.java:138)
      	at org.drools.compiler.rule.builder.GroupElementBuilder.build(GroupElementBuilder.java:66)
      	at org.drools.compiler.rule.builder.RuleBuilder.build(RuleBuilder.java:89)
      	at org.drools.compiler.builder.impl.KnowledgeBuilderImpl.addRule(KnowledgeBuilderImpl.java:1652)
      	at org.drools.compiler.builder.impl.KnowledgeBuilderImpl.compileRules(KnowledgeBuilderImpl.java:968)
      

      I've also noticed the following 2 aspects:

      1-/ If you use another operator instead of str startsWith, for instance you replace with the contains operators, it does compile the KB successfully. In this scenario would change the exercise to "count Messages which contains the preferred keyword", but it works:

      rule "group by word and count if >=2 then "
      no-loop
      when
      	$group : GroupByString( groupId == "wordGroup")
      	$word : String() from $group.groups
      	accumulate ( $msg : Message( text contains $word ) over window:time( 5m ) ;
      				 $list : collectList( $msg ),
      				 $count : count( $msg );
      				 $count >= 2
      				 ) 				 
      then
      	System.out.println("group by " + $word + " count is "+ $count + " list: " + $list);
      	insert(new Integer($count.intValue()));
      end
      

      2-/ If you use an hard-coded operand in the str startsWith, it works too. For instance if you hard-code the rule as:

      rule "group by word and count if >=2 then "
      no-loop
      when
      	accumulate ( $msg : Message( text str[startsWith] "ciao" ) over window:time( 5m ) ;
      				 $list : collectList( $msg ),
      				 $count : count( $msg );
      				 $count >= 2
      				 ) 				 
      then
      	insert(new Integer($count.intValue()));
      end
      

      It does not give NPE at KB build and executes as expected.

      For my application it's okay to deal with the first option above, but anyway I thought worthy to report a JIRA. I will attach reproducer. Ciao

              mfusco@redhat.com Mario Fusco
              mmortari@redhat.com Matteo Mortari
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: