use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class PropertySpecificTest method testPropSpecOnPatternWithThis.
@Test(timeout = 5000)
public void testPropSpecOnPatternWithThis() throws Exception {
String rule = "package org.drools.compiler.integrationtests\n" + "declare A\n" + " @propertyReactive\n" + " i : int\n" + "end\n" + "declare B\n" + " @propertyReactive\n" + " a : A\n" + "end\n" + "rule R1\n" + "when\n" + " $b : B();\n" + " $a : A(this == $b.a);\n" + "then\n" + " modify($b) { setA(null) };\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString(rule);
KieSession ksession = kbase.newKieSession();
FactType factTypeA = kbase.getFactType("org.drools.compiler.integrationtests", "A");
Object factA = factTypeA.newInstance();
factTypeA.set(factA, "i", 1);
ksession.insert(factA);
FactType factTypeB = kbase.getFactType("org.drools.compiler.integrationtests", "B");
Object factB = factTypeB.newInstance();
factTypeB.set(factB, "a", factA);
ksession.insert(factB);
int rules = ksession.fireAllRules();
assertEquals(1, rules);
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class PropertySpecificTest method testWatchNothing.
@Test
public void testWatchNothing() throws Exception {
String rule = "package org.drools.compiler.integrationtests\n" + "dialect \"mvel\"\n" + "declare A\n" + " s : String\n" + "end\n" + "declare B\n" + " @propertyReactive\n" + " on : boolean\n" + " s : String\n" + "end\n" + "rule R1\n" + "when\n" + " A($s : s)\n" + " $b : B(s != $s) @watch( !* )\n" + "then\n" + " modify($b) { setS($s) }\n" + "end\n" + "rule R2\n" + "when\n" + " $b : B(on == false)\n" + "then\n" + " modify($b) { setOn(true) }\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString(rule);
KieSession ksession = kbase.newKieSession();
FactType factTypeA = kbase.getFactType("org.drools.compiler.integrationtests", "A");
Object factA = factTypeA.newInstance();
factTypeA.set(factA, "s", "y");
ksession.insert(factA);
FactType factTypeB = kbase.getFactType("org.drools.compiler.integrationtests", "B");
Object factB = factTypeB.newInstance();
factTypeB.set(factB, "on", false);
factTypeB.set(factB, "s", "x");
ksession.insert(factB);
int rules = ksession.fireAllRules();
assertEquals(2, rules);
assertEquals(true, factTypeB.get(factB, "on"));
assertEquals("y", factTypeB.get(factB, "s"));
ksession.dispose();
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class PropertySpecificTest method testClassReactive.
@Test
public void testClassReactive() throws Exception {
String rule = "package org.drools.compiler.integrationtests\n" + "global java.util.concurrent.atomic.AtomicInteger counter\n" + "declare B\n" + " @classReactive\n" + " on : boolean\n" + " s : String\n" + "end\n" + "rule R1\n" + "when\n" + " $b : B(s == \"test\")\n" + "then\n" + " modify($b) { setOn(true) }\n" + " if (counter.incrementAndGet() > 10) throw new RuntimeException();\n" + "end\n";
KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
config.setOption(PropertySpecificOption.ALWAYS);
KieBase kbase = loadKnowledgeBaseFromString(config, rule);
KieSession ksession = kbase.newKieSession();
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
FactType factTypeB = kbase.getFactType("org.drools.compiler.integrationtests", "B");
Object factB = factTypeB.newInstance();
factTypeB.set(factB, "s", "test");
factTypeB.set(factB, "on", false);
ksession.insert(factB);
Assertions.assertThatThrownBy(() -> ksession.fireAllRules()).isInstanceOf(RuntimeException.class).hasMessageContaining("Exception executing consequence for rule \"R1\"");
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class PropertySpecificTest method testBetaWith2RTNSinksExec.
private void testBetaWith2RTNSinksExec(boolean addInfiniteLoopWatch) throws Exception {
String rule = "package org.drools.compiler.integrationtests\n" + "global java.util.concurrent.atomic.AtomicInteger counter\n" + "declare A\n" + " @propertyReactive\n" + " x : int\n" + "end\n" + "declare B\n" + " @propertyReactive\n" + "end\n" + "declare C\n" + " @propertyReactive\n" + " y : int\n" + "end\n" + "rule R1 when\n" + " A ( x == 1 )\n" + " B ( )\n" + (addInfiniteLoopWatch ? " $c : C ( ) @watch(y)\n" : " $c : C ( )\n") + "then " + " modify( $c ) { setY( 2 ) };\n" + " if (counter.incrementAndGet() > 10) throw new RuntimeException();\n" + "end;\n" + "rule R2 when\n" + " A ( x == 1 )\n" + " B ( )\n" + " C ( ) @watch(y)\n" + "then end;\n" + "rule InitA when\n" + " $a : A ( x == 0 )\n" + "then\n" + " modify( $a ) { setX( 1 ) };\n" + "end;\t\n" + "rule InitC salience 1 when\n" + " $c : C ( y == 0 )\n" + "then\n" + " modify( $c ) { setY( 1 ) };\n" + "end;\n";
KieBase kbase = loadKnowledgeBaseFromString(rule);
KieSession ksession = kbase.newKieSession();
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
FactType factTypeA = kbase.getFactType("org.drools.compiler.integrationtests", "A");
Object factA = factTypeA.newInstance();
factTypeA.set(factA, "x", 0);
ksession.insert(factA);
FactType factTypeB = kbase.getFactType("org.drools.compiler.integrationtests", "B");
Object factB = factTypeB.newInstance();
ksession.insert(factB);
FactType factTypeC = kbase.getFactType("org.drools.compiler.integrationtests", "C");
Object factC = factTypeC.newInstance();
factTypeC.set(factC, "y", 0);
ksession.insert(factC);
try {
ksession.fireAllRules();
} finally {
assertEquals(2, factTypeC.get(factC, "y"));
ksession.dispose();
}
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class PropertySpecificTest method testConfig.
@Test(timeout = 5000)
public void testConfig() throws Exception {
String rule = "package org.drools.compiler.integrationtests\n" + "declare A\n" + " i : int\n" + " j : int\n" + "end\n" + "rule R1\n" + "when\n" + " $a : A(i == 1)\n" + "then\n" + " modify($a) { setJ(2) };\n" + "end\n";
KnowledgeBuilderConfiguration config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
config.setOption(PropertySpecificOption.ALWAYS);
KieBase kbase = loadKnowledgeBaseFromString(config, rule);
KieSession ksession = kbase.newKieSession();
FactType typeA = kbase.getFactType("org.drools.compiler.integrationtests", "A");
Object a = typeA.newInstance();
typeA.set(a, "i", 1);
ksession.insert(a);
int rules = ksession.fireAllRules();
assertEquals(1, rules);
}
Aggregations