Search in sources :

Example 1 with Cheesery

use of org.drools.testcoverage.common.model.Cheesery 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 2 with Cheesery

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

the class MemberOfTest method testMemberOfAndNotMemberOf.

@Test
public void testMemberOfAndNotMemberOf() {
    final String drl = "package org.drools.compiler.test;\n" + "\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"Stilton is memberOf Cheesery\"\n" + "    salience 10\n" + "    when\n" + "        Cheesery( $cheeses : cheeses )\n" + "        cheese : Cheese( type memberOf $cheeses )\n" + "    then\n" + "        list.add( cheese );\n" + "end   \n" + "\n" + "rule \"Muzzarela is not memberOf Cheesery\"\n" + "    when\n" + "        Cheesery( $cheeses : cheeses )\n" + "        cheese : Cheese( type not memberOf $cheeses )\n" + "    then\n" + "        list.add( cheese );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("member-of-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List list = new ArrayList();
        ksession.setGlobal("list", list);
        final Cheese stilton = new Cheese("stilton", 12);
        final Cheese muzzarela = new Cheese("muzzarela", 10);
        final Cheese brie = new Cheese("brie", 15);
        ksession.insert(stilton);
        ksession.insert(muzzarela);
        final Cheesery cheesery = new Cheesery();
        cheesery.getCheeses().add(stilton);
        cheesery.getCheeses().add(brie);
        ksession.insert(cheesery);
        ksession.fireAllRules();
        assertEquals(2, list.size());
        assertEquals(stilton, list.get(0));
        assertEquals(muzzarela, list.get(1));
    } 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) Cheesery(org.drools.testcoverage.common.model.Cheesery) Test(org.junit.Test)

Example 3 with Cheesery

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

the class AlphaTest method testNPEOnMVELAlphaPredicates.

@Test
public void testNPEOnMVELAlphaPredicates() {
    final String drl = "package org.drools.compiler\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "\n" + "rule \"test NPE on mvel predicate\"\n" + "when\n" + "    $p : Person( cheese.type != null )\n" + "    $q : Cheese( ) from $p.cheese\n" + "then\n" + "    results.add( $q );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("alpha-test", kieBaseTestConfiguration, drl);
    final KieSession session = kbase.newKieSession();
    try {
        final List list = new ArrayList();
        session.setGlobal("results", list);
        final Cheese cheese = new Cheese("stilton", 10);
        final Cheesery cheesery = new Cheesery();
        cheesery.addCheese(cheese);
        final Person bob = new Person("bob", "stilton");
        final Cheese cheese2 = new Cheese();
        bob.setCheese(cheese2);
        final FactHandle p = session.insert(bob);
        session.insert(cheesery);
        session.fireAllRules();
        assertEquals("should not have fired", 0, list.size());
        cheese2.setType("stilton");
        session.update(p, bob);
        session.fireAllRules();
        assertEquals(1, list.size());
    } finally {
        session.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) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.testcoverage.common.model.Cheese) Cheesery(org.drools.testcoverage.common.model.Cheesery) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 4 with Cheesery

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

the class AccumulateTest method testAccumulateModifyMVEL.

@Test(timeout = 10000)
public void testAccumulateModifyMVEL() {
    final String drl = "package org.drools.compiler.test;\n" + "\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List results;\n" + "\n" + "rule \"Constraints everywhere\" salience 80\n" + "    dialect \"mvel\"\n" + "    when\n" + "        $person      : Person( $likes : likes )\n" + "        $cheesery    : Cheesery( totalAmount > 20 )\n" + "                               from accumulate( $cheese : Cheese( type == $likes ),\n" + "                                                init( cheesery = new Cheesery(); ),\n" + "                                                action( cheesery.addCheese( $cheese ); ),\n" + "                                                result( cheesery ) );\n" + "    then\n" + "        results.add( $cheesery );\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);
        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 FactHandle[] cheeseHandles = new FactHandle[cheese.length];
        for (int i = 0; i < cheese.length; i++) {
            cheeseHandles[i] = wm.insert(cheese[i]);
        }
        final org.kie.api.runtime.rule.FactHandle bobHandle = wm.insert(bob);
        // ---------------- 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(24, ((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(31, ((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) ArrayList(java.util.ArrayList) Cheese(org.drools.testcoverage.common.model.Cheese) Cheesery(org.drools.testcoverage.common.model.Cheesery) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 5 with Cheesery

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

the class AccumulateTest method testAccumulateWithFromChaining.

@Test(timeout = 10000)
public void testAccumulateWithFromChaining() {
    final String drl = "package org.drools.compiler\n" + "\n" + "import java.util.List;\n" + "import java.util.ArrayList;\n" + "\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List results;\n" + "\n" + "rule \"Accumulate with From Chaining\" salience 80\n" + "    when\n" + "        $cheesery : Cheesery()\n" + "        $person   : Person( $likes : likes )\n" + "        $list     : List( size > 2 )\n" + "                               from accumulate( $cheese : Cheese( type == $likes  ) from $cheesery.getCheeses(),\n" + "                                                init( List l = new ArrayList(); ),\n" + "                                                action( l.add( $cheese ); )\n" + "                                                result( l ) )\n" + "    then\n" + "        results.add( $list );\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);
        final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 8), new Cheese("stilton", 10), new Cheese("stilton", 9), new Cheese("brie", 4), new Cheese("brie", 1), new Cheese("provolone", 8) };
        final Cheesery cheesery = new Cheesery();
        for (final Cheese aCheese : cheese) {
            cheesery.addCheese(aCheese);
        }
        final FactHandle cheeseryHandle = wm.insert(cheesery);
        final Person bob = new Person("Bob", "stilton");
        final FactHandle bobHandle = wm.insert(bob);
        // ---------------- 1st scenario
        wm.fireAllRules();
        // one fire, as per rule constraints
        assertEquals(1, results.size());
        assertEquals(3, ((List) results.get(results.size() - 1)).size());
        // ---------------- 2nd scenario
        final int index = 1;
        cheese[index].setType("brie");
        wm.update(cheeseryHandle, cheesery);
        wm.fireAllRules();
        // no fire
        assertEquals(1, results.size());
        System.out.println(results);
        // ---------------- 3rd scenario
        bob.setLikes("brie");
        wm.update(bobHandle, bob);
        wm.fireAllRules();
        // 2 fires
        assertEquals(2, results.size());
        assertEquals(3, ((List) results.get(results.size() - 1)).size());
        // ---------------- 4th scenario
        cheesery.getCheeses().remove(cheese[3]);
        wm.update(cheeseryHandle, cheesery);
        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) Cheese(org.drools.testcoverage.common.model.Cheese) KieSession(org.kie.api.runtime.KieSession) Cheesery(org.drools.testcoverage.common.model.Cheesery) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)11 Cheesery (org.drools.testcoverage.common.model.Cheesery)11 Test (org.junit.Test)11 KieBase (org.kie.api.KieBase)11 KieSession (org.kie.api.runtime.KieSession)11 Cheese (org.drools.testcoverage.common.model.Cheese)10 List (java.util.List)7 Person (org.drools.testcoverage.common.model.Person)6 FactHandle (org.kie.api.runtime.rule.FactHandle)5 ClassObjectType (org.drools.core.base.ClassObjectType)1 EntryPointNode (org.drools.core.reteoo.EntryPointNode)1 FromNode (org.drools.core.reteoo.FromNode)1 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)1 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1 Sink (org.drools.core.reteoo.Sink)1