[jboss-jira] [JBoss JIRA] (DROOLS-2133) Rule Unit unbind using `run` causes wrong lookup of DS on the wrong unit
Matteo Mortari (JIRA)
issues at jboss.org
Tue Nov 14 07:40:01 EST 2017
Matteo Mortari created DROOLS-2133:
--------------------------------------
Summary: Rule Unit unbind using `run` causes wrong lookup of DS on the wrong unit
Key: DROOLS-2133
URL: https://issues.jboss.org/browse/DROOLS-2133
Project: Drools
Issue Type: Bug
Components: core engine
Reporter: Matteo Mortari
Assignee: Mario Fusco
Please reference test {{testGuardAndRunBack}}
{code:java}
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).
}
{code}
--
This message was sent by Atlassian JIRA
(v7.5.0#75005)
More information about the jboss-jira
mailing list