use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathBindTest method testBindInteger.
public void testBindInteger(final boolean fireUntilHalt) throws InterruptedException, ExecutionException {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Adult( $age: /age )\n" + "then\n" + " list.add( $age );\n" + "end\n";
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
Future fireUntilHaltFuture = null;
if (fireUntilHalt) {
fireUntilHaltFuture = startFireUntilHaltThread(ksession);
}
final List<Integer> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Man bob = new Man("Bob", 40);
ksession.insert(bob);
if (fireUntilHalt) {
waitForResultAndStopFireUntilHalt(list, ksession, fireUntilHaltFuture);
} else {
ksession.fireAllRules();
Assertions.assertThat(list).hasSize(1);
}
Assertions.assertThat(list).contains(40);
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathMultilevelTest method testClassThreeLevelPath.
@Test
public void testClassThreeLevelPath() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Man( $toyName: /wife/children/toys/name )\n" + "then\n" + " list.add( $toyName );\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);
alice.addChild(charlie);
charlie.addToy(new Toy("car"));
charlie.addToy(new Toy("ball"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathCastTest method testInlineCast.
@Test
public void testInlineCast() {
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#BabyGirl/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 BabyBoy charlie = new BabyBoy("Charles", 12);
final BabyGirl debbie = new BabyGirl("Debbie", 8);
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("doll");
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathQueriesTest method testQueryFromCode.
@Test
public void testQueryFromCode() {
final String drl = "import org.drools.compiler.oopath.model.Thing;\n" + "query isContainedIn( Thing $x, Thing $y )\n" + " $y := /$x/children\n" + "or\n" + " ( $z := /$x/children and isContainedIn( $z, $y; ) )\n" + "end\n";
final Thing smartphone = new Thing("smartphone");
final List<String> itemList = Arrays.asList(new String[] { "display", "keyboard", "processor" });
itemList.stream().map(item -> new Thing(item)).forEach((thing) -> smartphone.addChild(thing));
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
ksession.insert(smartphone);
final QueryResults queryResults = ksession.getQueryResults("isContainedIn", new Object[] { smartphone, Variable.v });
final List<String> resultList = StreamSupport.stream(queryResults.spliterator(), false).map(row -> ((Thing) row.get("$y")).getName()).collect(Collectors.toList());
assertThat(resultList).as("Query does not contain all items").containsAll(itemList);
ksession.dispose();
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class OOPathReactiveTests method testReactiveOnBeta.
@Test
public void testReactiveOnBeta() {
final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + " $i : Integer()\n" + " Man( $toy: /wife/children[age > $i]?/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(10);
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("car", "ball");
list.clear();
debbie.setAge(11);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("doll");
}
Aggregations