Search in sources :

Example 81 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class NamedConsequencesTest method testNamedConsequenceOnEvents.

@Test
public void testNamedConsequenceOnEvents() {
    // DROOLS-641
    String drl = "import " + StockTick.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "declare StockTick \n" + "    @role( event )" + "    @timestamp( time )\n" + "end\n" + "rule R when\n" + "    $s1 : StockTick( company == \"XXX\" )\n" + "    $s2 : StockTick( price > $s1.price ) do[t1]\n" + "    $s3 : StockTick( price < $s1.price )\n" + "then\n" + "    list.add( \"t0:\" + $s3.getCompany() );\n" + "then[t1]\n" + "    list.add( \"t1:\" + $s2.getCompany() );\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(new StockTick(1L, "XXX", 10, 0L));
    ksession.insert(new StockTick(2L, "YYY", 15, 1L));
    ksession.insert(new StockTick(3L, "ZZZ", 5, 2L));
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(asList("t1:YYY", "t0:ZZZ")));
}
Also used : StockTick(org.drools.compiler.StockTick) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 82 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class NamedConsequencesTest method testMvelInsertWithNamedConsequence.

@Test
public void testMvelInsertWithNamedConsequence() {
    // DROOLS-726
    String drl2 = "package org.drools.compiler\n" + "global java.util.concurrent.atomic.AtomicInteger counter\n" + "declare Output\n" + "    feedback: String\n" + "end\n" + "rule \"Move to next\" dialect \"mvel\"\n" + "   when\n" + "          $i: Integer()\n" + "          if ($i == 1) break[nextStep1]\n" + "   then\n" + "           insert(new Output(\"defualt\"));\n" + "   then[nextStep1]\n" + "           insert(new Output(\"step 1\"));\n" + "end\n" + "\n" + "rule \"Produce output\"\n" + "    when\n" + "        $output: Output()\n" + "    then\n" + "        System.out.println($output);\n" + "        retract($output);" + "        counter.incrementAndGet();\n" + "end\n";
    KieSession kSession = new KieHelper().addContent(drl2, ResourceType.DRL).build().newKieSession();
    AtomicInteger counter = new AtomicInteger(0);
    kSession.setGlobal("counter", counter);
    FactHandle messageHandle = kSession.insert(1);
    kSession.fireAllRules();
    kSession.delete(messageHandle);
    kSession.insert(2);
    kSession.fireAllRules();
    assertEquals(2, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FactHandle(org.kie.api.runtime.rule.FactHandle) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 83 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class NamedConsequencesTest method testQueryWithBreakingBranch.

@Test
public void testQueryWithBreakingBranch() throws Exception {
    // DROOLS-1115
    String drl = "import " + ListHolder.class.getCanonicalName() + ";\n" + "query q(ListHolder $listHolder)\n" + "    $listHolder := ListHolder()\n" + "    $i : Integer()\n" + "end\n" + "\n" + "rule R when\n" + "    q($listHolder;)\n" + "    $s : String() from $listHolder.list\n" + "    if (1==2) break[branch]\n" + "then\n" + "    $listHolder.getList().add(\"ok\");\n" + "then[branch]\n" + "    $listHolder.getList().add(\"ko\");\n" + "end\n" + "\n" + "rule Init when then insert(1); end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<String>();
    list.add("test");
    ksession.insert(new ListHolder(list));
    ksession.fireAllRules();
    assertEquals("ok", list.get(1));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 84 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class NamedConsequencesTest method testDeleteWithBreakingBranch.

@Test
public void testDeleteWithBreakingBranch() throws Exception {
    // DROOLS-1068
    String drl = "global java.util.List list;\n" + "rule R when\n" + "  Integer()\n" + "  if (true) break[branch]\n" + "  not Integer()\n" + "then\n" + "  list.add(\"main\");\n" + "then[branch]\n" + "  list.add(\"branch\");\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    FactHandle fh = ksession.insert(1);
    ksession.fireAllRules();
    ksession.delete(fh);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("branch", list.get(0));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 85 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class NodePositionInPathTest method test.

@Test
public void test() {
    String drl = "rule R1 when\n" + "    Integer()\n" + "    exists( Integer() and Integer() )\n" + "    String()\n" + "then\n" + "end\n" + "rule R2 when\n" + "    Integer()\n" + "    exists( Integer() and String() )\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    ReteDumper.dumpRete(((InternalKnowledgeBase) kbase));
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    LeftInputAdapterNode liaNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
        if (Integer.class == otnType) {
            liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
        }
    }
    assertEquals(0, liaNode.getPositionInPath());
    LeftTupleSink[] liaSinks = liaNode.getSinkPropagator().getSinks();
    BetaNode join1 = (BetaNode) liaSinks[0];
    assertEquals(1, join1.getPositionInPath());
    ExistsNode ex1 = (ExistsNode) liaSinks[1];
    assertEquals(1, ex1.getPositionInPath());
    BetaNode join2 = (BetaNode) ex1.getSinkPropagator().getSinks()[0];
    assertEquals(2, join2.getPositionInPath());
    RuleTerminalNode rtn1 = (RuleTerminalNode) join2.getSinkPropagator().getSinks()[0];
    assertEquals(3, rtn1.getPositionInPath());
    ExistsNode ex2 = (ExistsNode) liaSinks[2];
    assertEquals(1, ex2.getPositionInPath());
    RuleTerminalNode rtn2 = (RuleTerminalNode) ex2.getSinkPropagator().getSinks()[0];
    assertEquals(2, rtn2.getPositionInPath());
    BetaNode join3 = (BetaNode) join1.getSinkPropagator().getSinks()[0];
    assertEquals(2, join3.getPositionInPath());
    RightInputAdapterNode ria1 = (RightInputAdapterNode) join3.getSinkPropagator().getSinks()[0];
    assertEquals(3, ria1.getPositionInPath());
    BetaNode join4 = (BetaNode) join1.getSinkPropagator().getSinks()[1];
    assertEquals(2, join4.getPositionInPath());
    RightInputAdapterNode ria2 = (RightInputAdapterNode) join4.getSinkPropagator().getSinks()[0];
    assertEquals(3, ria2.getPositionInPath());
    LeftTupleNode[] rtn1PathNodes = rtn1.getPathNodes();
    assertEquals(4, rtn1PathNodes.length);
    checkNodePosition(rtn1PathNodes, liaNode);
    checkNodePosition(rtn1PathNodes, ex1);
    checkNodePosition(rtn1PathNodes, join2);
    checkNodePosition(rtn1PathNodes, rtn1);
    LeftTupleNode[] rtn2PathNodes = rtn2.getPathNodes();
    assertEquals(3, rtn2PathNodes.length);
    checkNodePosition(rtn2PathNodes, liaNode);
    checkNodePosition(rtn2PathNodes, ex2);
    checkNodePosition(rtn2PathNodes, rtn2);
    LeftTupleNode[] ria1PathNodes = ria1.getPathNodes();
    assertEquals(4, ria1PathNodes.length);
    checkNodePosition(ria1PathNodes, liaNode);
    checkNodePosition(ria1PathNodes, join1);
    checkNodePosition(ria1PathNodes, join3);
    checkNodePosition(ria1PathNodes, ria1);
    LeftTupleNode[] ria2PathNodes = ria2.getPathNodes();
    assertEquals(4, ria2PathNodes.length);
    checkNodePosition(ria2PathNodes, liaNode);
    checkNodePosition(ria2PathNodes, join1);
    checkNodePosition(ria2PathNodes, join4);
    checkNodePosition(ria2PathNodes, ria2);
}
Also used : Rete(org.drools.core.reteoo.Rete) BetaNode(org.drools.core.reteoo.BetaNode) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ExistsNode(org.drools.core.reteoo.ExistsNode) KieBase(org.kie.api.KieBase) LeftTupleNode(org.drools.core.reteoo.LeftTupleNode) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) 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