Search in sources :

Example 61 with Cheese

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

the class AccumulateTest method testAccumulateWithAndOrCombinations.

@Test(timeout = 10000)
public void testAccumulateWithAndOrCombinations() {
    // JBRULES-3482
    // once this compils, update it to actually assert on correct outputs.
    final String drl = "package org.drools.compiler.test;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "rule \"Class cast causer\"\n" + "    when\n" + "        $person      : Person( $likes : likes )\n" + "        $total       : Number() from accumulate( $p : Person(likes != $likes, $l : likes) and $c : Cheese( type == $l ),\n" + "                                                min($c.getPrice()) )\n" + "        ($p2 : Person(name == 'nobody') or $p2 : Person(name == 'Doug'))\n" + "    then\n" + "        System.out.println($p2.getName());\n" + "end\n";
    // read in the source
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("accumulate-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        ksession.insert(new Cheese("stilton", 10));
        ksession.insert(new Person("Alice", "brie"));
        ksession.insert(new Person("Bob", "stilton"));
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) 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 62 with Cheese

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

the class AccumulateTest method execTestAccumulateMultipleFunctions.

private void execTestAccumulateMultipleFunctions(final String fileName) {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("accumulate-test", kieBaseTestConfiguration, fileName);
    final KieSession ksession = kbase.newKieSession();
    try {
        final AgendaEventListener ael = mock(AgendaEventListener.class);
        ksession.addEventListener(ael);
        final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 17), 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] = ksession.insert(cheese[i]);
        }
        final FactHandle bobHandle = ksession.insert(bob);
        // ---------------- 1st scenario
        ksession.fireAllRules();
        final ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
        Mockito.verify(ael).afterMatchFired(cap.capture());
        Match activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
        Mockito.reset(ael);
        // ---------------- 2nd scenario
        final int index = 1;
        cheese[index].setPrice(9);
        ksession.update(cheeseHandles[index], cheese[index]);
        ksession.fireAllRules();
        Mockito.verify(ael).afterMatchFired(cap.capture());
        activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(24));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(5));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(8));
        Mockito.reset(ael);
        // ---------------- 3rd scenario
        bob.setLikes("brie");
        ksession.update(bobHandle, bob);
        ksession.fireAllRules();
        Mockito.verify(ael).afterMatchFired(cap.capture());
        activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(32));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(15));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(16));
        Mockito.reset(ael);
        // ---------------- 4th scenario
        ksession.delete(cheeseHandles[3]);
        ksession.fireAllRules();
        Mockito.verify(ael).afterMatchFired(cap.capture());
        activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(17));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(17));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(17));
    } finally {
        ksession.dispose();
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match)

Example 63 with Cheese

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

the class AccumulateTest method testAccumulateMultipleFunctionsConstraint.

@Test(timeout = 10000)
public void testAccumulateMultipleFunctionsConstraint() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("accumulate-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_AccumulateMultipleFunctionsConstraint.drl");
    final KieSession ksession = kbase.newKieSession();
    try {
        final AgendaEventListener ael = mock(AgendaEventListener.class);
        ksession.addEventListener(ael);
        final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 3), new Cheese("brie", 17), 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] = ksession.insert(cheese[i]);
        }
        final FactHandle bobHandle = ksession.insert(bob);
        // ---------------- 1st scenario
        ksession.fireAllRules();
        final ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
        Mockito.verify(ael).afterMatchFired(cap.capture());
        Match activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
        Mockito.reset(ael);
        // ---------------- 2nd scenario
        final int index = 1;
        cheese[index].setPrice(9);
        ksession.update(cheeseHandles[index], cheese[index]);
        ksession.fireAllRules();
        Mockito.verify(ael, Mockito.never()).afterMatchFired(Mockito.any(AfterMatchFiredEvent.class));
        Mockito.reset(ael);
        // ---------------- 3rd scenario
        bob.setLikes("brie");
        ksession.update(bobHandle, bob);
        ksession.fireAllRules();
        Mockito.verify(ael).afterMatchFired(cap.capture());
        activation = cap.getValue().getMatch();
        assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(20));
        assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
        assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(10));
    } finally {
        ksession.dispose();
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 64 with Cheese

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

the class AccumulateTest method execTestAccumulateMin.

private void execTestAccumulateMin(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", 8), new Cheese("stilton", 10), new Cheese("stilton", 9), new Cheese("brie", 4), new Cheese("brie", 1), 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 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(3);
        wm.update(cheeseHandles[index], cheese[index]);
        wm.fireAllRules();
        // 1 fire
        assertEquals(1, results.size());
        assertEquals(3, ((Number) results.get(results.size() - 1)).intValue());
        // ---------------- 3rd scenario
        bob.setLikes("brie");
        wm.update(bobHandle, bob);
        wm.fireAllRules();
        // 2 fires
        assertEquals(2, results.size());
        assertEquals(1, ((Number) results.get(results.size() - 1)).intValue());
        // ---------------- 4th scenario
        wm.delete(cheeseHandles[3]);
        wm.delete(cheeseHandles[4]);
        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 65 with Cheese

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

the class AccumulateTest method testAccumulateReverseModify.

@Test()
public void testAccumulateReverseModify() {
    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" + "    when\n" + "        $person      : Person( $likes : likes )\n" + "        $cheesery    : Cheesery( totalAmount > 20 )\n" + "                               from accumulate( $cheese : Cheese( type == $likes ),\n" + "                                                init( Cheesery cheesery = new Cheesery(); ),\n" + "                                                action( cheesery.addCheese( $cheese ); ),\n" + "                                                reverse( cheesery.removeCheese( $cheese ); ),\n" + "                                                result( cheesery ) );\n" + "    then\n" + "        //System.out.println($person.getName() +\" is spending a lot buying cheese ( US$ \"+$cheesery.getTotalAmount()+\" )!\");\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);
        cheese[3].setPrice(20);
        wm.update(cheeseHandles[3], cheese[3]);
        wm.fireAllRules();
        // 2 fires
        assertEquals(2, results.size());
        assertEquals(36, ((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) 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)

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