Search in sources :

Example 71 with KieHelper

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

the class Misc2Test method testDeletedRightTupleInChangedBucket.

@Test
public void testDeletedRightTupleInChangedBucket() {
    // PLANNER-488
    String drl = "import " + Person.class.getCanonicalName() + "\n" + "rule R when\n" + "    Person( $name: name, $age: age )\n" + "    not Person( happy, name == $name, age == $age-1 )\n" + "then\n" + "end";
    KieSession kieSession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    Person p1 = new Person("C", 1, true);
    Person p2 = new Person("B", 1, true);
    Person p3 = new Person("B", 2, true);
    Person p4 = new Person("A", 2);
    FactHandle fh1 = kieSession.insert(p1);
    FactHandle fh2 = kieSession.insert(p2);
    FactHandle fh3 = kieSession.insert(p3);
    FactHandle fh4 = kieSession.insert(p4);
    kieSession.fireAllRules();
    p4.setName("B");
    p4.setHappy(true);
    kieSession.update(fh4, p4);
    kieSession.fireAllRules();
    p3.setName("A");
    p3.setHappy(false);
    kieSession.update(fh3, p3);
    p1.setName("B");
    kieSession.update(fh1, p1);
    p2.setName("C");
    kieSession.update(fh2, p2);
    kieSession.fireAllRules();
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 72 with KieHelper

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

the class Misc2Test method checkJava8InRhs.

private void checkJava8InRhs(String expr) {
    assumeTrue(System.getProperty("java.version").startsWith("1.8"));
    // BZ-1199965
    String drl = "global java.util.List list;\n" + "rule \"Example with Lambda expression\"\n" + "    when\n" + "    then\n" + "        java.util.List<Integer> $list = java.util.Arrays.asList(1, 2, 3, 4);\n" + "        $list.forEach(" + expr + ");\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(4, list.size());
    assertTrue(list.containsAll(Arrays.asList(1, 2, 3, 4)));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession)

Example 73 with KieHelper

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

the class Misc2Test method testReportFailingConstraintOnError.

@Test
public void testReportFailingConstraintOnError() {
    // DROOLS-1071
    String drl = "import " + Person.class.getCanonicalName() + "\n" + "rule R when\n" + "    Person( name.startsWith(\"A\") )\n" + "then\n" + "end";
    KieSession kieSession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    for (int i = 0; i < 100; i++) {
        kieSession.insert(new Person("A" + i));
    }
    kieSession.fireAllRules();
    kieSession.insert(new Person(null));
    try {
        kieSession.fireAllRules();
        fail("Evaluation with null must throw a NPE");
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("name.startsWith(\"A\")"));
    }
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Person(org.drools.compiler.Person) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) Test(org.junit.Test)

Example 74 with KieHelper

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

the class Misc2Test method testDoubleNestedClass.

@Test
public void testDoubleNestedClass() {
    // DROOLS-815
    String drl = "import " + $X.$Y.class.getCanonicalName() + ";\n" + "global java.util.List list\n" + "rule R when\n" + "    $X.$Y($v : value)\n" + "then\n" + "    list.add($v);\n" + "end";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(new $X.$Y(42));
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(42, list.get(0));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 75 with KieHelper

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

the class Misc2Test method testBitwiseOperator.

@Test
public void testBitwiseOperator() {
    // DROOLS-585
    String drl = "global java.util.List list;\n" + "\n" + "rule R when\n" + "    $i : Integer( (intValue() & 5) != 0 )\n" + "then\n" + "    list.add($i);\n" + "end";
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieSession kieSession = helper.build().newKieSession();
    List<Integer> list = new ArrayList<Integer>();
    kieSession.setGlobal("list", list);
    kieSession.insert(3);
    kieSession.insert(2);
    kieSession.insert(6);
    kieSession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(asList(3, 6)));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) 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