Search in sources :

Example 16 with Cheese

use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.

the class OrTest method testOrCE.

@Test
public void testOrCE() {
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "rule \"test or CE\"\n" + "when\n" + "    $c : Cheese( type == \"stilton\" )\n" + "    or\n" + "    (\n" + "        $c2 : Cheese( type == \"brie\" )\n" + "        and\n" + "        (\n" + "            $p : Person( likes == \"stilton\" )\n" + "            or\n" + "            $p : Person( name == \"bob\" )\n" + "        )\n" + "    ) \n" + "then\n" + "    results.add(\" OK \" );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("or-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List list = new ArrayList();
        ksession.setGlobal("results", list);
        ksession.insert(new Cheese("brie", 10));
        ksession.insert(new Person("bob"));
        ksession.fireAllRules();
        assertEquals("should have fired once", 1, list.size());
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 17 with Cheese

use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.

the class ExistsTest method testNodeSharingNotExists.

@Test
public void testNodeSharingNotExists() {
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "\n" + "rule rule1\n" + "when\n" + "       not Cheese( type == Cheese.STILTON )\n" + "then\n" + "       results.add( \"rule1\" );\n" + "end\n" + "\n" + "rule rule2\n" + "when\n" + "       exists Cheese( type == Cheese.STILTON )\n" + "then\n" + "       results.add( \"rule2\" );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("exists-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List list = new ArrayList();
        ksession.setGlobal("results", list);
        ksession.fireAllRules();
        assertEquals(1, list.size());
        assertEquals("rule1", list.get(0));
        ksession.insert(new Cheese("stilton", 10));
        ksession.fireAllRules();
        assertEquals(2, list.size());
        assertEquals("rule2", list.get(1));
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Cheese(org.drools.testcoverage.common.model.Cheese) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 18 with Cheese

use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.

the class FromTest method testFromSharingWithAccumulate.

@Test
public void testFromSharingWithAccumulate() {
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "\n" + "import java.util.List;\n" + "import java.util.ArrayList;\n" + "import " + Cheesery.class.getCanonicalName() + " ;\n" + "import " + Cheese.class.getCanonicalName() + " ;\n" + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "\n" + "rule R1\n" + "    when\n" + "        $cheesery : Cheesery()\n" + "        $list     : List( ) from accumulate( $cheese : Cheese( ) from $cheesery.getCheeses(),\n" + "                                             init( List l = new ArrayList(); ),\n" + "                                             action( l.add( $cheese ); )\n" + "                                             result( l ) )\n" + "    then\n" + "        output1.add( $list );\n" + "end\n" + "rule R2\n" + "    when\n" + "        $cheesery : Cheesery()\n" + "        $list     : List( ) from accumulate( $cheese : Cheese( ) from $cheesery.getCheeses(),\n" + "                                             init( List l = new ArrayList(); ),\n" + "                                             action( l.add( $cheese ); )\n" + "                                             result( l ) )\n" + "    then\n" + "        output2.add( $list );\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("from-test", kieBaseTestConfiguration, drl);
    EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNode(EntryPointId.DEFAULT);
    ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Cheesery.class));
    Sink[] otnSinks = otn.getSinks();
    assertEquals(1, otnSinks.length);
    LeftInputAdapterNode lia = (LeftInputAdapterNode) otnSinks[0];
    Sink[] liaSinks = lia.getSinks();
    // there must be only 1 shared from node
    assertEquals(1, Stream.of(liaSinks).filter(sink -> sink instanceof FromNode).count());
    final KieSession ksession = kbase.newKieSession();
    try {
        final List<?> output1 = new ArrayList<>();
        ksession.setGlobal("output1", output1);
        final List<?> output2 = new ArrayList<>();
        ksession.setGlobal("output2", output2);
        final Cheesery cheesery = new Cheesery();
        cheesery.addCheese(new Cheese("stilton", 8));
        cheesery.addCheese(new Cheese("provolone", 8));
        final FactHandle cheeseryHandle = ksession.insert(cheesery);
        ksession.fireAllRules();
        assertEquals(1, output1.size());
        assertEquals(2, ((List) output1.get(0)).size());
        assertEquals(1, output2.size());
        assertEquals(2, ((List) output2.get(0)).size());
        output1.clear();
        output2.clear();
        ksession.update(cheeseryHandle, cheesery);
        ksession.fireAllRules();
        assertEquals(1, output1.size());
        assertEquals(2, ((List) output1.get(0)).size());
        assertEquals(1, output2.size());
        assertEquals(2, ((List) output2.get(0)).size());
    } finally {
        ksession.dispose();
    }
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) FactHandle(org.kie.api.runtime.rule.FactHandle) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) Cheese(org.drools.testcoverage.common.model.Cheese) Cheesery(org.drools.testcoverage.common.model.Cheesery) FromNode(org.drools.core.reteoo.FromNode) EntryPointNode(org.drools.core.reteoo.EntryPointNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) Sink(org.drools.core.reteoo.Sink) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 19 with Cheese

use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.

the class EvalTest method testPredicateException.

@Test
public void testPredicateException() {
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "function boolean throwException(Object object) {\n" + "    throw new RuntimeException( \"this should throw an exception\" );\n" + "}\n" + "\n" + "rule \"Throw Predicate Exception\"\n" + "    when\n" + "        Cheese( type1:type, eval( throwException( type1 ) ) )\n" + "    then\n" + "\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("eval-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final Cheese brie = new Cheese("brie", 12);
        try {
            ksession.insert(brie);
            ksession.fireAllRules();
            fail("Should throw an Exception from the Predicate");
        } catch (final Exception e) {
            Throwable cause = e.getCause();
            if (cause instanceof InvocationTargetException) {
                cause = ((InvocationTargetException) cause).getTargetException();
            }
            assertTrue(cause.getMessage().contains("this should throw an exception"));
        }
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 20 with Cheese

use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.

the class RemoveRuleTest method testRuleRemovalWithJoinedRootPattern.

@Test
public void testRuleRemovalWithJoinedRootPattern() {
    String str = "";
    str += "package org.drools.compiler \n";
    str += "import " + Person.class.getCanonicalName() + ";\n";
    str += "import " + Cheese.class.getCanonicalName() + ";\n";
    str += "rule rule1 \n";
    str += "when \n";
    str += "  String() \n";
    str += "  Person() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule rule2 \n";
    str += "when \n";
    str += "  String() \n";
    str += "  Cheese() \n";
    str += "then \n";
    str += "end  \n";
    final KieServices kieServices = KieServices.get();
    final ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-remove-rule-with-joined-root-pattern", "1.0");
    KieUtil.getKieModuleFromDrls(releaseId, kieBaseTestConfiguration, str);
    final KieContainer kieContainer = kieServices.newKieContainer(releaseId);
    final KieBase kbase = kieContainer.getKieBase();
    final KieSession ksession = kbase.newKieSession();
    final DefaultFactHandle handle = (DefaultFactHandle) ksession.insert("hello");
    ksession.fireAllRules();
    LeftTuple leftTuple = handle.getFirstLeftTuple();
    assertNotNull(leftTuple);
    assertNotNull(leftTuple.getPeer());
    kbase.removeRule("org.drools.compiler", "rule2");
    leftTuple = handle.getFirstLeftTuple();
    assertNotNull(leftTuple);
    assertNull(leftTuple.getHandleNext());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) Cheese(org.drools.testcoverage.common.model.Cheese) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) Person(org.drools.testcoverage.common.model.Person) LeftTuple(org.drools.core.reteoo.LeftTuple) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

Cheese (org.drools.testcoverage.common.model.Cheese)97 KieBase (org.kie.api.KieBase)92 KieSession (org.kie.api.runtime.KieSession)88 Test (org.junit.Test)85 ArrayList (java.util.ArrayList)65 Person (org.drools.testcoverage.common.model.Person)47 List (java.util.List)33 FactHandle (org.kie.api.runtime.rule.FactHandle)22 Cheesery (org.drools.testcoverage.common.model.Cheesery)11 Arrays.asList (java.util.Arrays.asList)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 BigDecimal (java.math.BigDecimal)3 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)3 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)3 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)2