Search in sources :

Example 21 with Cheese

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

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

the class ActivationTest method noDormantCheckOnModifies.

/**
 * Tests improper deactivation of already activated rule on the agenda. See
 * BZ 862325.
 */
@Test
public void noDormantCheckOnModifies() throws Exception {
    AgendaEventListener ael = mock(AgendaEventListener.class);
    session.addEventListener(ael);
    session.setGlobal("LOGGER", LOGGER);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(getCommands().newInsert(new Person("Bob", 19)));
    commands.add(getCommands().newInsert(new Cheese("brie", 10)));
    commands.add(getCommands().newFireAllRules());
    session.execute(getCommands().newBatchExecution(commands, null));
    // both rules should fire exactly once
    verify(ael, times(2)).afterMatchFired(any(AfterMatchFiredEvent.class));
    // no cancellations should have happened
    verify(ael, never()).matchCancelled(any(MatchCancelledEvent.class));
}
Also used : Command(org.kie.api.command.Command) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieSessionTest(org.drools.testcoverage.common.KieSessionTest) Test(org.junit.Test)

Example 23 with Cheese

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

the class TimerAndCalendarWithRealTimeTest method testDuration.

@Test(timeout = 15000)
public void testDuration() throws Exception {
    final String drl = "package org.drools.compiler.test;\n" + "\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule delayed\n" + "    duration 100\n" + "    when\n" + "        cheese : Cheese( )\n" + "    then\n" + "        list.add( cheese );\n" + "end ";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("timer-and-calendar-test", kieBaseTestConfiguration, drl);
    ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Cheese brie = new Cheese("brie", 12);
    ksession.insert(brie);
    ksession.fireAllRules();
    // now check for update
    assertEquals(0, list.size());
    awaitUntilRulesThatFiredAre(1);
    // now check for update
    assertEquals(1, list.size());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Cheese(org.drools.testcoverage.common.model.Cheese) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Test(org.junit.Test)

Example 24 with Cheese

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

the class TimerAndCalendarWithRealTimeTest method testDurationWithNoLoop.

@Test(timeout = 10000)
public void testDurationWithNoLoop() throws Exception {
    final String drl = "package org.drools.compiler.test;\n" + "\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule delayed\n" + "    timer 100\n" + "    no-loop true\n" + "    when\n" + "        cheese : Cheese( )\n" + "    then\n" + "        list.add( cheese );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("timer-and-calendar-test", kieBaseTestConfiguration, drl);
    ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Cheese brie = new Cheese("brie", 12);
    ksession.insert(brie);
    ksession.fireAllRules();
    // now check for update
    assertEquals(0, list.size());
    awaitUntilRulesThatFiredAre(1);
    // now check for update
    assertEquals(1, list.size());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 25 with Cheese

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

the class JoinNodeRangeIndexingTest method testCoercionBigDecimalVsInt.

@Test
public void testCoercionBigDecimalVsInt() {
    final String drl = "package org.drools.compiler.integrationtests;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Primitives.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R\n" + "    when\n" + "        Cheese($price : price)\n" + "        p : Primitives(bigDecimal < $price)\n" + "    then\n" + "        list.add( p );\n" + "end";
    // Integer is coerced to BigDecimal
    final KieBase kbase = getKieBaseWithRangeIndexOption(drl);
    assertIndexedTrue(kbase, Primitives.class);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List<Primitives> list = new ArrayList<>();
        ksession.setGlobal("list", list);
        final Primitives bd42 = new Primitives();
        bd42.setBigDecimal(new BigDecimal("42"));
        ksession.insert(bd42);
        final Primitives bd43 = new Primitives();
        bd43.setBigDecimal(new BigDecimal("43"));
        ksession.insert(bd43);
        ksession.insert(new Cheese("gorgonzola", 43));
        ksession.fireAllRules();
        Assertions.assertThat(list).containsExactly(bd42);
    } 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) Primitives(org.drools.testcoverage.common.model.Primitives) BigDecimal(java.math.BigDecimal) 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