Search in sources :

Example 56 with Cheese

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

the class AlphaNetworkModifyTest method testModifyWithLiaToEval.

@Test
public void testModifyWithLiaToEval() {
    final String drl = "package org.simple \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "import " + Pet.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule x1 \n" + "when \n" + "    $pe : Person() \n" + "    $ch : Cheese() \n" + "    $ca : Pet() \n" + "then \n" + "end  \n" + "rule x2 \n" + "when \n" + "    $ch : Cheese() \n" + "    $ca : Pet() \n" + "    $pe : Person() \n" + "then \n" + "end  \n" + "rule x3 \n" + "when \n" + "    $ch : Cheese() \n" + "then \n" + "end  \n" + "rule x4 \n" + "when \n" + "    $ch : Cheese() \n" + "    eval( $ch != null ) \n" + "then \n" + "end  \n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("alpha-network-modify-test", kieBaseTestConfiguration, drl);
    final KieSession wm = kbase.newKieSession();
    try {
        final ObjectTypeNode otnPerson = getObjectTypeNode(kbase, "Person");
        final ObjectTypeNode otnCheese = getObjectTypeNode(kbase, "Cheese");
        final ObjectTypeNode otnPet = getObjectTypeNode(kbase, "Pet");
        assertNotNull(otnPerson);
        assertNotNull(otnCheese);
        assertNotNull(otnPet);
        assertEquals(0, otnPerson.getOtnIdCounter());
        assertEquals(0, otnCheese.getOtnIdCounter());
        assertEquals(0, otnPet.getOtnIdCounter());
        wm.insert(new Person());
        wm.insert(new Pet("yyy"));
        wm.insert(new Cheese());
        wm.fireAllRules();
        assertEquals(2, otnPerson.getOtnIdCounter());
        assertEquals(4, otnCheese.getOtnIdCounter());
        assertEquals(2, otnPet.getOtnIdCounter());
    } finally {
        wm.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Cheese(org.drools.testcoverage.common.model.Cheese) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.testcoverage.common.model.Person) Pet(org.drools.testcoverage.common.model.Pet) Test(org.junit.Test)

Example 57 with Cheese

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

the class AccumulateMvelDialectTest method testMVELAccumulate.

// See https://issues.jboss.org/browse/DROOLS-2733
@Test(timeout = 10000)
public void testMVELAccumulate() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("accumulate-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_AccumulateMVEL.drl");
    final KieSession wm = kbase.newKieSession();
    try {
        final List<?> results = new ArrayList<>();
        wm.setGlobal("results", results);
        wm.insert(new Person("Bob", "stilton", 20));
        wm.insert(new Person("Mark", "provolone"));
        wm.insert(new Cheese("stilton", 10));
        wm.insert(new Cheese("brie", 5));
        wm.insert(new Cheese("provolone", 150));
        wm.fireAllRules();
        assertEquals(165, results.get(0));
        assertEquals(10, results.get(1));
        assertEquals(150, results.get(2));
        assertEquals(10, results.get(3));
        assertEquals(210, results.get(4));
    } finally {
        wm.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 58 with Cheese

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

the class AccumulateMvelDialectTest method testAccumulateWithSameSubnetwork.

@Test
public void testAccumulateWithSameSubnetwork() {
    final String drl = "package org.drools.compiler.test;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list; \n" + "rule r1 salience 100 \n" + "    when\n" + "        $person      : Person( name == 'Alice', $likes : likes )\n" + "        $total       : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + "                                                min($c.getPrice()) )\n" + "    then\n" + "        list.add( 'r1' + ':' + $total);\n" + "end\n" + "rule r2 \n" + "    when\n" + "        $person      : Person( name == 'Alice', $likes : likes )\n" + "        $total       : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + "                                                max($c.getPrice()) )\n" + "    then\n" + "        list.add( 'r2' + ':' + $total);\n" + "end\n" + "";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("accumulate-test", kieBaseTestConfiguration, drl);
    final KieSession wm = kbase.newKieSession();
    try {
        final List list = new ArrayList();
        wm.setGlobal("list", list);
        // Check the network formation, to ensure the RiaNode is shared.
        final ObjectTypeNode cheeseOtn = KieUtil.getObjectTypeNode(kbase, Cheese.class);
        assertNotNull(cheeseOtn);
        final ObjectSink[] oSinks = cheeseOtn.getObjectSinkPropagator().getSinks();
        assertEquals(1, oSinks.length);
        final JoinNode cheeseJoin = (JoinNode) oSinks[0];
        final LeftTupleSink[] ltSinks = cheeseJoin.getSinkPropagator().getSinks();
        assertEquals(1, ltSinks.length);
        final RightInputAdapterNode rian = (RightInputAdapterNode) ltSinks[0];
        // RiaNode is shared, if this has two outputs
        assertEquals(2, rian.getObjectSinkPropagator().size());
        wm.insert(new Cheese("stilton", 10));
        wm.insert(new Person("Alice", "brie"));
        wm.insert(new Person("Bob", "stilton"));
        wm.fireAllRules();
        assertEquals(2, list.size());
        assertEquals("r1:10", list.get(0));
        assertEquals("r2:10", list.get(1));
    } finally {
        wm.dispose();
    }
}
Also used : JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) Cheese(org.drools.testcoverage.common.model.Cheese) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ObjectSink(org.drools.core.reteoo.ObjectSink) Person(org.drools.testcoverage.common.model.Person) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode) Test(org.junit.Test)

Example 59 with Cheese

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

the class AccumulateTest method execTestAccumulateReverseModifyMultiPattern.

private void execTestAccumulateReverseModifyMultiPattern(final String fileName) {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("accumulate-test", kieBaseTestConfiguration, fileName);
    final KieSession wm = kbase.newKieSession();
    try {
        final List<?> results = new ArrayList<>();
        wm.setGlobal("results", results);
        final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 2), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 16), new Cheese("provolone", 8) };
        final Person bob = new Person("Bob", "stilton");
        final Person mark = new Person("Mark", "provolone");
        final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
        for (int i = 0; i < cheese.length; i++) {
            cheeseHandles[i] = wm.insert(cheese[i]);
        }
        final FactHandle bobHandle = wm.insert(bob);
        wm.insert(mark);
        // ---------------- 1st scenario
        wm.fireAllRules();
        // no fire, as per rule constraints
        assertEquals(0, results.size());
        // ---------------- 2nd scenario
        final int index = 1;
        cheese[index].setPrice(9);
        wm.update(cheeseHandles[index], cheese[index]);
        wm.fireAllRules();
        // 1 fire
        assertEquals(1, results.size());
        assertEquals(32, ((Cheesery) results.get(results.size() - 1)).getTotalAmount());
        // ---------------- 3rd scenario
        bob.setLikes("brie");
        wm.update(bobHandle, bob);
        wm.fireAllRules();
        // 2 fires
        assertEquals(2, results.size());
        assertEquals(39, ((Cheesery) results.get(results.size() - 1)).getTotalAmount());
        // ---------------- 4th scenario
        wm.delete(cheeseHandles[3]);
        wm.fireAllRules();
        // should not have fired as per constraint
        assertEquals(2, results.size());
    } finally {
        wm.dispose();
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person)

Example 60 with Cheese

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

the class AccumulateTest method testAccumulateGlobals.

@Test(timeout = 10000)
public void testAccumulateGlobals() {
    final String drl = "package org.drools.compiler\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "global Integer globalValue;\n" + "\n" + "rule \"Accumulate globals\"\n" + "    when\n" + "        $totalAmount : Number() from accumulate(  Cheese( type == \"brie\" ),\n" + "                                                  init( int total = 0; ),\n" + "                                                  action( total += globalValue.intValue(); ),\n" + "                                                  reverse( total -= globalValue.intValue(); ),\n" + "                                                  result( new Integer( total ) ) );\n" + "    then\n" + "        results.add($totalAmount);\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("accumulate-test", kieBaseTestConfiguration, drl);
    final KieSession wm = kbase.newKieSession();
    try {
        final List<?> results = new ArrayList<>();
        wm.setGlobal("results", results);
        wm.setGlobal("globalValue", 50);
        wm.insert(new Cheese("stilton", 10));
        wm.insert(new Cheese("brie", 5));
        wm.insert(new Cheese("provolone", 150));
        wm.insert(new Cheese("brie", 20));
        wm.fireAllRules();
        assertEquals(1, results.size());
        assertEquals(100, results.get(0));
    } finally {
        wm.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) 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