use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class ParallelEvaluationTest method getMultipleParallelKieSessionsWithUpdatesCallable.
private Callable<Void> getMultipleParallelKieSessionsWithUpdatesCallable(KieBase kBase) {
return new Callable<Void>() {
@Override
public Void call() {
KieSession ksession = kBase.newKieSession();
assertThat(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda()).as("Parallel agenda has to be enabled").isTrue();
List<Integer> list = new DebugList<Integer>();
ksession.setGlobal("list", list);
FactHandle[] fhs = new FactHandle[10];
fhs = insertFacts(ksession, 10);
ksession.fireAllRules();
assertThat(list.size()).isEqualTo(10);
list.clear();
for (int i = 0; i < 10; i++) {
ksession.update(fhs[i], i);
}
ksession.fireAllRules();
assertThat(list.size()).isEqualTo(10);
return null;
}
};
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class ParallelEvaluationTest method testWithUpdates.
@Test(timeout = 10000L)
public void testWithUpdates() {
StringBuilder sb = new StringBuilder(400);
sb.append("global java.util.List list;\n");
for (int i = 0; i < 10; i++) {
sb.append(getRule(i, ""));
}
KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES).newKieSession();
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
List<Integer> list = new DebugList<Integer>();
ksession.setGlobal("list", list);
FactHandle[] fhs = new FactHandle[10];
for (int i = 0; i < 10; i++) {
fhs[i] = ksession.insert(i);
ksession.insert("" + i);
}
ksession.fireAllRules();
assertEquals(10, list.size());
list.clear();
for (int i = 0; i < 10; i++) {
ksession.update(fhs[i], i);
}
ksession.fireAllRules();
assertEquals(10, list.size());
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class PolymorphismTest method testModifySubclass2.
@Test
public void testModifySubclass2() {
// DROOLS-1501
String drl = "import " + A.class.getCanonicalName() + "\n;" + "import " + B.class.getCanonicalName() + "\n;" + "import " + C.class.getCanonicalName() + "\n;" + "import " + D.class.getCanonicalName() + "\n;" + "\n" + "rule Rd when\n" + " $a: D(id == 0)\n" + " $d: C($id : id == 0)\n" + "then\n" + " modify($d) { setId($id+1) };\n" + "end\n" + "rule Rc when\n" + " $a: D(id == 1)\n" + " $c: B($id : id == 1)\n" + "then\n" + " modify($c) { setId($id+1) };\n" + "end\n" + "rule Rb when\n" + " $a: D(id == 2)\n" + " $b: A($id : id == 2)\n" + "then\n" + " modify($b) { setId($id+1) };\n" + "end\n" + "rule Ra when\n" + " $a: D(id == 3)\n" + "then\n" + " delete($a);\n" + "end";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM).newKieSession();
FactHandle fh = ksession.insert(new D(0));
ksession.fireAllRules();
assertEquals(0, ksession.getObjects().size());
System.out.println(fh);
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class PropertyReactivityBlockerTest method testA_Working.
@Test()
public void testA_Working() {
// DROOLS-644
String drl = "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + " $p1 : Person( name == \"Mario\", $a1: age ) \n" + " $p2 : Person( age > $a1 ) \n" + "then\n" + " list.add(\"t0\");\n" + "end\n";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
ReteDumper.dumpRete(ksession);
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
Person mario = new Person("Mario", 40);
Person mark = new Person("Mark", 37);
FactHandle fh_mario = ksession.insert(mario);
ksession.insert(mark);
ksession.fireAllRules();
mario.setAge(35);
ksession.update(fh_mario, mario, "age");
int x = ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("t0", list.get(0));
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class PropertyReactivityTest method testWatchFieldInExternalPatternWithNoConstraints.
@Test
public void testWatchFieldInExternalPatternWithNoConstraints() {
// DROOLS-2333
final String drl = "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + " $p1 : Person()\n" + " $p2 : Person( name == \"Mark\", age > $p1.age ) \n" + "then\n" + " list.add(\"t0\");\n" + "end\n" + "rule Z when\n" + " $p1 : Person( name == \"Mario\" ) \n" + "then\n" + " modify($p1) { setAge(35); } \n" + "end\n";
// making the default explicit:
final KieSession ksession = new KieHelper(PropertySpecificOption.ALWAYS).addContent(drl, ResourceType.DRL).build().newKieSession();
final List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
final Person mario = new Person("Mario", 40);
final Person mark = new Person("Mark", 37);
final FactHandle fh_mario = ksession.insert(mario);
ksession.insert(mark);
final int x = ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("t0", list.get(0));
}
Aggregations