Search in sources :

Example 96 with KieHelper

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

the class PassiveQueryTest method testPassiveQueryNodeSharing2.

@Test
public void testPassiveQueryNodeSharing2() throws Exception {
    String str = "global java.util.List list\n" + "query Q (Integer i)\n" + "    String( this == i.toString() )\n" + "end\n" + "rule R1a @Propagation(IMMEDIATE) when\n" + "    $i : Integer()\n" + "    ?Q( $i; )\n" + "then\n" + "    list.add( \"R1a\" );\n" + "end\n" + "rule R1b @Propagation(IMMEDIATE) when\n" + "    Long( $i : intValue )\n" + "    ?Q( $i; )\n" + "then\n" + "    list.add( \"R1b\" );\n" + "end\n" + "rule R2a when\n" + "    $i : Integer()\n" + "    ?Q( $i; )\n" + "then\n" + "    list.add( \"R2a\" );\n" + "end\n" + "rule R2b when\n" + "    Long( $i : intValue )\n" + "    ?Q( $i; )\n" + "then\n" + "    list.add( \"R2b\" );\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(1);
    ksession.insert(1L);
    ksession.insert("1");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(asList("R2a", "R2b")));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 97 with KieHelper

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

the class PassiveQueryTest method testReactiveQueryDataDriven.

@Test
public void testReactiveQueryDataDriven() throws Exception {
    String str = "global java.util.List list\n" + "query Q (Integer i)\n" + "    String( this == i.toString() )\n" + "end\n" + "rule R @Propagation(IMMEDIATE) when\n" + "    $i : Integer()\n" + "    Q( $i; )\n" + "then\n" + "    list.add( $i );\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    ksession.insert(1);
    ksession.insert("1");
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 98 with KieHelper

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

the class PassiveQueryTest method testPassiveQuery.

@Test
public void testPassiveQuery() throws Exception {
    String str = "global java.util.List list\n" + "query Q (Integer i)\n" + "    String( this == i.toString() )\n" + "end\n" + "rule R when\n" + "    $i : Integer()\n" + "    ?Q( $i; )\n" + "then\n" + "    list.add( $i );\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    ksession.insert(1);
    ksession.insert("2");
    ksession.fireAllRules();
    assertEquals(0, list.size());
    ksession.insert("1");
    ksession.fireAllRules();
    assertEquals(0, list.size());
    ksession.insert(2);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(2, (int) list.get(0));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 99 with KieHelper

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

the class PolymorphismTest method testModifySubclassOverWindow.

@Test
public void testModifySubclassOverWindow() {
    // DROOLS-1501
    String drl = "declare Number @role( event ) end\n" + "declare Integer @role( event ) end\n" + "\n" + "rule R1 no-loop when\n" + "    $i: Integer()\n" + "then\n" + "    update($i);\n" + "end\n" + "rule R2 when\n" + "    $n: Number() over window:length(1)\n" + "then\n" + "end";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM).newKieSession();
    ksession.insert(1);
    ksession.fireAllRules();
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 100 with KieHelper

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

the class PolymorphismTest method testModifySubclass2.

@Test
public void testModifySubclass2() {
    // DROOLS-1501
    String drl = "import " + A.class.getCanonicalName() + "\n;" + "import " + B.class.getCanonicalName() + "\n;" + "import " + C.class.getCanonicalName() + "\n;" + "import " + D.class.getCanonicalName() + "\n;" + "\n" + "rule Rd when\n" + "    $a: D(id == 0)\n" + "    $d: C($id : id == 0)\n" + "then\n" + "    modify($d) { setId($id+1) };\n" + "end\n" + "rule Rc when\n" + "    $a: D(id == 1)\n" + "    $c: B($id : id == 1)\n" + "then\n" + "    modify($c) { setId($id+1) };\n" + "end\n" + "rule Rb when\n" + "    $a: D(id == 2)\n" + "    $b: A($id : id == 2)\n" + "then\n" + "    modify($b) { setId($id+1) };\n" + "end\n" + "rule Ra when\n" + "    $a: D(id == 3)\n" + "then\n" + "    delete($a);\n" + "end";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM).newKieSession();
    FactHandle fh = ksession.insert(new D(0));
    ksession.fireAllRules();
    assertEquals(0, ksession.getObjects().size());
    System.out.println(fh);
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) 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