use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathReactiveTests method testSingleFireOnReactiveChange.
@Test
public void testSingleFireOnReactiveChange() {
// DROOLS-1302
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children[age > 10]/toys )\n" + "then\n" + " list.add( $toy );\n" + "end\n";
final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
final KieSession ksession = kbase.newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
ksession.insert(bob);
ksession.fireAllRules();
list.clear();
final Child eleonor = new Child("Eleonor", 10);
alice.addChild(eleonor);
final Toy toy = new Toy("eleonor toy 1");
eleonor.addToy(toy);
eleonor.setAge(11);
ksession.fireAllRules();
Assertions.assertThat(list).hasSize(1);
list.clear();
toy.setName("eleonor toy 2");
ksession.fireAllRules();
Assertions.assertThat(list).hasSize(1);
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathReactiveTests method testNonReactivePart.
@Test
public void testNonReactivePart() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children[age > 10]?/toys )\n" + "then\n" + " list.add( $toy.getName() );\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 10);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
list.clear();
charlie.addToy(new Toy("robot"));
ksession.fireAllRules();
Assertions.assertThat(list).isEmpty();
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathReactiveTests method testReactiveDeleteOnLia.
@Test
public void testReactiveDeleteOnLia() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toy: /wife/children[age > 10]/toys )\n" + "then\n" + " list.add( $toy.getName() );\n" + "end\n";
final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
final KieSession ksession = kbase.newKieSession();
final EntryPointNode epn = ((InternalKnowledgeBase) ksession.getKieBase()).getRete().getEntryPointNodes().values().iterator().next();
final ObjectTypeNode otn = epn.getObjectTypeNodes().values().iterator().next();
final LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
final ReactiveFromNode from1 = (ReactiveFromNode) lian.getSinkPropagator().getSinks()[0];
final ReactiveFromNode from2 = (ReactiveFromNode) from1.getSinkPropagator().getSinks()[0];
final ReactiveFromNode from3 = (ReactiveFromNode) from2.getSinkPropagator().getSinks()[0];
final BetaMemory betaMemory = ((InternalWorkingMemory) ksession).getNodeMemory(from3).getBetaMemory();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 11);
alice.addChild(charlie);
alice.addChild(debbie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
debbie.addToy(new Toy("doll"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball", "doll");
final TupleMemory tupleMemory = betaMemory.getLeftTupleMemory();
Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(2);
Iterator<LeftTuple> it = tupleMemory.iterator();
for (LeftTuple next = it.next(); next != null; next = it.next()) {
final Object obj = next.getFactHandle().getObject();
Assertions.assertThat(obj == charlie || obj == debbie).isTrue();
}
list.clear();
debbie.setAge(10);
ksession.fireAllRules();
Assertions.assertThat(list).hasSize(0);
;
Assertions.assertThat(betaMemory.getLeftTupleMemory().size()).isEqualTo(1);
it = tupleMemory.iterator();
for (LeftTuple next = it.next(); next != null; next = it.next()) {
final Object obj = next.getFactHandle().getObject();
Assertions.assertThat(obj == charlie).isTrue();
}
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class Misc2Test method testFireUntilHaltWithForceEagerActivation.
@Test(timeout = 10000L)
public void testFireUntilHaltWithForceEagerActivation() throws InterruptedException {
String drl = "global java.util.List list\n" + "rule \"String detector\"\n" + " when\n" + " $s : String( )\n" + " then\n" + " list.add($s);\n" + "end";
KieSessionConfiguration config = KieServices.Factory.get().newKieSessionConfiguration();
config.setOption(ForceEagerActivationOption.YES);
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession(config, null);
final Integer monitor = 42;
int factsNr = 5;
List<String> list = new NotifyingList<String>(factsNr, new Runnable() {
@Override
public void run() {
synchronized (monitor) {
monitor.notifyAll();
}
}
});
ksession.setGlobal("list", list);
// thread for firing until halt
ExecutorService thread = Executors.newSingleThreadExecutor();
thread.submit(new Runnable() {
@Override
public void run() {
ksession.fireUntilHalt();
}
});
for (int i = 0; i < factsNr; i++) {
ksession.insert("" + i);
}
// wait for rule to fire
synchronized (monitor) {
if (list.size() < factsNr) {
monitor.wait();
}
}
assertEquals(factsNr, list.size());
ksession.halt();
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class Misc2Test method testJittingFunctionReturningAnInnerClass.
@Test
public void testJittingFunctionReturningAnInnerClass() {
// DROOLS-1166
String drl = "import " + java.util.function.Function.class.getCanonicalName() + "\n" + "function Function<String, Integer> f() {\n" + " return new Function<String, Integer>() {\n" + " public Integer apply(String s) {\n" + " return s.length();\n" + " }\n" + " };\n" + "}\n" + "\n" + "rule R when\n" + " $s : String( f().apply(this) > 3 )\n" + "then\n" + "end\n";
KieSession kieSession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
kieSession.insert("test");
assertEquals(1, kieSession.fireAllRules());
}
Aggregations