Uploaded image for project: 'Kogito'
  1. Kogito
  2. KOGITO-2124

Add a global variable use case in rule examples

XMLWordPrintable

    • Icon: Task Task
    • Resolution: Done
    • Icon: Major Major
    • None
    • 0.10.1
    • Examples
    • None
    • 3
    • 2020 Week 19-21 (from May 4)

      In old Drools, using a global variable to collect rule outcome is a popular use case.
      e.g.

      global java.util.List resultList;
      
      rule Hello
        when
          $p : Person(age > 20)
        then
          resultList.add("Hello " + $p.getName());
      end
      

      Add an example for such a use case so that users can follow a recommended approach. Probably A) SingletonStore.

      A) SingletonStore

      https://github.com/tkobayas/my-kogito-examples/tree/master/hello-singleton-store-declare

      declare Hello extends RuleUnitData
        result : SingletonStore<Result> = DataSource.createSingleton()
        messages : DataStore<String>
      end
      
      rule helloWorld
      when
          $r : /result
          $m : /messages
      then
          $r.setValue($m + " world");
      end
      
      query hello
        $r : /result
      end
      

      Concerns:

      • Need to refer the variable at LHS (I think it's acceptable)
      • Where should I insert the object firstly? (Currently, via POST payload)
      • Has a minor issue in generating from "declare" (Filed https://issues.redhat.com/browse/KOGITO-2127)

      B) Plain field in RuleUnitData

      A plain field in RuleUnitData can work as a global.

      public class LoanUnit implements RuleUnitData {
          private int maxAmount;
      

      However, it has some issues. Probably this approach is not recommended even if the issues will be fixed.
      https://github.com/tkobayas/my-kogito-examples/tree/master/hello-global

      • If you use a List as a plain field in RuleUnitData, it's considered as a data source (See ReflectiveRuleUnitDescription.getUnitVarType()) so failed in compiling RuleUnitInstance class.
        2020-05-08 14:54:26,536 ERROR [io.qua.dep.dev.DevModeMain] (main) Failed to start Quarkus: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
        	[error]: Build step org.kie.kogito.quarkus.deployment.KogitoAssetsProcessor#generateModel threw an exception: java.lang.IllegalStateException: src/main/java/org/kie/kogito/examples/HelloRuleUnitInstance.java (13:30) : cannot find symbol
          symbol:   method subscribe(org.kie.kogito.rules.units.EntryPointDataProcessor)
          location: interface java.util.List<java.lang.String>
        src/main/java/org/kie/kogito/examples/HelloRuleUnitInstance.java (13:30) : cannot find symbol
          symbol:   method subscribe(org.kie.kogito.rules.units.EntryPointDataProcessor)
          location: interface java.util.List<java.lang.String>
        	at org.kie.kogito.quarkus.deployment.KogitoAssetsProcessor.register(KogitoAssetsProcessor.java:314)
        	at org.kie.kogito.quarkus.deployment.KogitoAssetsProcessor.generateModel(KogitoAssetsProcessor.java:197)
        
      • Cannot write a query for global. So cannot generate REST endpoint.

              rhn-support-tkobayas Toshiya Kobayashi
              rhn-support-tkobayas Toshiya Kobayashi
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: