Search in sources :

Example 66 with KieHelper

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);
}
Also used : KieBase(org.kie.api.KieBase) 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 67 with KieHelper

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();
}
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 68 with KieHelper

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();
    }
}
Also used : ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) 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) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Toy(org.drools.compiler.oopath.model.Toy) Test(org.junit.Test)

Example 69 with KieHelper

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 70 with KieHelper

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());
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) 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