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

Rule Unit unbind using `run` causes wrong lookup of DS on the wrong unit

XMLWordPrintable

    • 2018 Week 39-41
    • 5
    • NEW
    • NEW

      Please reference test testGuardAndRunBack

          public static class MainHouseUnit implements RuleUnit {
      
              private DataSource<Date> now;
              private DataSource<String> part;
              private DataSource<Boolean> switch1;
      
              public MainHouseUnit() {
                  super();
              }
      
              public DataSource<Date> getNow() {
                  return now;
              }
      
              public DataSource<String> getPart() {
                  return part;
              }
      
              public DataSource<Boolean> getSwitch1() {
                  return switch1;
              }
      
          }
      
          public static class DayPartUnit implements RuleUnit {
      
              private DataSource<Date> now;
              private DataSource<Date> aScopedDS;
              private DataSource<String> part;
      
              public DayPartUnit() {
                  super();
              }
      
              public DataSource<Date> getNow() {
                  return now;
              }
      
              public DataSource<String> getPart() {
                  return part;
              }
      
              public DataSource<Date> getaScopedDS() {
                  return aScopedDS;
              }
      
          }
      
          public static class SwitchUnit implements RuleUnit {
      
              private DataSource<String> part;
              private DataSource<Boolean> switch1;
      
              public SwitchUnit() {
                  super();
              }
      
              public DataSource<String> getPart() {
                  return part;
              }
      
              public DataSource<Boolean> getSwitch1() {
                  return switch1;
              }
      
          }
      
          private KieBase kieBaseMainGuardSubunitRunBackToMain(boolean currentStyle) {
              // use "hammer" approach with external multiple call to fire, or "drools.run()" approach in rules.
              System.out.println("Running with style: " + currentStyle);
              String drl1 = "package org.drools.compiler.integrationtests\n" +
                            "unit " + getCanonicalSimpleName(MainHouseUnit.class) + "\n" +
                            "import " + DayPartUnit.class.getCanonicalName() + "\n" +
                            "import " + SwitchUnit.class.getCanonicalName() + "\n" +
                            "rule GuardDayPartUnit when\n" +
                            "    Object() from now \n" +
                            "    not( String() from part ) \n" +
                            "then\n" +
                            "    System.out.println(\"Guarding DayPartUnit\");\n" +
                            "    drools.guard(DayPartUnit.class);\n" +
                            "end\n" +
                            "rule GuardSwitchUnit when\n" +
                            "    String() from part \n" +
                            "    not( Boolean() from switch1 ) \n" +
                            "then\n" +
                            "    System.out.println(\"Guarding SwitchUnit\");\n" +
                            "    drools.guard(SwitchUnit.class);\n" +
                            "end\n";
      
              String drl2 = "package org.drools.compiler.integrationtests\n" +
                            "unit " + getCanonicalSimpleName(DayPartUnit.class) + "\n" +
                            "import " + MainHouseUnit.class.getCanonicalName() + "\n" +
                            "rule doDayPartUnit when\n" +
                            "    $n : Object() from now \n" +
                            "then\n" +
                            "    System.out.println(\"Inside DayPartUnit: \"+$n);\n" +
                            "    part.insert(\"Morning\");\n" +
                            (currentStyle ? "//" : "") + " drools.run(MainHouseUnit.class);\n" +
                            "end\n";
      
              String drl3 = "package org.drools.compiler.integrationtests\n" +
                            "unit " + getCanonicalSimpleName(SwitchUnit.class) + "\n" +
                            "import " + MainHouseUnit.class.getCanonicalName() + "\n" +
                            "rule doSwitchUnit when\n" +
                            "    $n : String() from part \n" +
                            "then\n" +
                            "    System.out.println(\"Inside SwitchUnit: \"+$n);\n" +
                            "    switch1.insert(true);\n" +
                            (currentStyle ? "//" : "") + " drools.run(MainHouseUnit.class);\n" +
                            "end\n";
      
              KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL)
                                             .addContent(drl2, ResourceType.DRL)
                                             .addContent(drl3, ResourceType.DRL)
                                             .build();
      
              return kbase;
          }
      
          @Test
          public void testMainGuardSubunitRunBackToMain_usingHammerStyle() throws Exception {
      
              KieBase kbase = kieBaseMainGuardSubunitRunBackToMain(true);
              RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
      
              executor.newDataSource("now", LocalDateTime.now());
              executor.newDataSource("part");
              executor.newDataSource("aScopedDS");
              DataSource<Boolean> switch1 = executor.newDataSource("switch1");
      
              RuleUnit adultUnit = new MainHouseUnit();
              executor.run(adultUnit);
      
              // need a second, "hammer" run.
              executor.run(adultUnit);
      
              assertEquals(true, switch1.iterator().next());
          }
      
          @Test
          public void testMainGuardSubunitRunBackToMain_usingRunStyle() throws Exception {
      
              KieBase kbase = kieBaseMainGuardSubunitRunBackToMain(false);
              RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
      
              executor.newDataSource("now", LocalDateTime.now());
              executor.newDataSource("part");
              executor.newDataSource("aScopedDS");
              DataSource<Boolean> switch1 = executor.newDataSource("switch1");
      
              RuleUnit adultUnit = new MainHouseUnit();
              executor.run(adultUnit); // FAILs with trying to lookup method getaScopedDS() on the MainHouseUnit (which is not correct unit). 
      
              // does NOT need a second, "hammer" run.
      
              assertEquals(true, switch1.iterator().next());
          }
      
          public static class EmptyUnit implements RuleUnit {
      
              public EmptyUnit() {
                  // no-args constructor.
              }
          }
      
          public static class StringDSUnit implements RuleUnit {
      
              private DataSource<String> strings;
      
              public StringDSUnit() {
                  // no-args constructor.
              }
      
              public DataSource<String> getStrings() {
                  return strings;
              }
      
          }
      
          @Test
          public void testGuardAndRunBack() {
              String drl1 = "package org.drools.compiler.integrationtests\n" +
                            "unit " + getCanonicalSimpleName(EmptyUnit.class) + "\n" +
                            "import " + StringDSUnit.class.getCanonicalName() + "\n" +
                            "rule RGuard when\n" +
                            "then\n" +
                            "    System.out.println(\"Guarding StringDSUnit\");\n" +
                            "    drools.guard(StringDSUnit.class);\n" +
                            "end\n";
      
              String drl2 = "package org.drools.compiler.integrationtests\n" +
                            "unit " + getCanonicalSimpleName(StringDSUnit.class) + "\n" +
                            "import " + EmptyUnit.class.getCanonicalName() + "\n" +
                            "rule RGoBack when\n" +
                            "then\n" +
                            "    System.out.println(\"Inside StringDSUnit: \");\n" +
                            "    drools.run(EmptyUnit.class);\n" +
                            "end\n";
      
              KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL)
                                             .addContent(drl2, ResourceType.DRL)
                                             .build();
              RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
      
              executor.newDataSource("strings", "abc", "xyz");
      
              RuleUnit adultUnit = new EmptyUnit();
              executor.run(adultUnit); // FAILs with trying to lookup method getStrings() on the EmptyUnit (which is not correct unit). 
      
          }
      

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

                Created:
                Updated:
                Resolved: