Search in sources :

Example 61 with KieHelper

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);
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) Future(java.util.concurrent.Future) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man)

Example 62 with KieHelper

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");
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) Woman(org.drools.compiler.oopath.model.Woman) Child(org.drools.compiler.oopath.model.Child) Toy(org.drools.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 63 with KieHelper

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");
}
Also used : ArrayList(java.util.ArrayList) BabyBoy(org.drools.compiler.oopath.model.BabyBoy) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) Woman(org.drools.compiler.oopath.model.Woman) BabyGirl(org.drools.compiler.oopath.model.BabyGirl) Toy(org.drools.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 64 with KieHelper

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();
}
Also used : SensorEvent(org.drools.compiler.oopath.model.SensorEvent) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Variable(org.kie.api.runtime.rule.Variable) Test(org.junit.Test) Room(org.drools.compiler.oopath.model.Room) Thing(org.drools.compiler.oopath.model.Thing) ResourceType(org.kie.api.io.ResourceType) Collectors(java.util.stream.Collectors) List(java.util.List) QueryResults(org.kie.api.runtime.rule.QueryResults) StreamSupport(java.util.stream.StreamSupport) KieSession(org.kie.api.runtime.KieSession) KieHelper(org.kie.internal.utils.KieHelper) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Thing(org.drools.compiler.oopath.model.Thing) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 65 with KieHelper

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");
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.compiler.oopath.model.Man) Woman(org.drools.compiler.oopath.model.Woman) Child(org.drools.compiler.oopath.model.Child) Toy(org.drools.compiler.oopath.model.Toy) Test(org.junit.Test)

Aggregations

KieHelper (org.kie.internal.utils.KieHelper)467 Test (org.junit.Test)427 KieSession (org.kie.api.runtime.KieSession)355 ArrayList (java.util.ArrayList)194 KieBase (org.kie.api.KieBase)152 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)77 Person (org.drools.compiler.Person)61 FactHandle (org.kie.api.runtime.rule.FactHandle)55 List (java.util.List)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)41 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)38 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)37 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)34 Man (org.drools.compiler.oopath.model.Man)29 Child (org.drools.compiler.oopath.model.Child)24 Woman (org.drools.compiler.oopath.model.Woman)23 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)18 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)18 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)18