Search in sources :

Example 11 with Cheese

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

the class RuleFlowGroupTest method testRuleFlowGroupWithLockOnActivate.

@Test
public void testRuleFlowGroupWithLockOnActivate() {
    // JBRULES-3590
    final String drl = "import " + Person.class.getCanonicalName() + ";\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "rule R1\n" + "ruleflow-group \"group1\"\n" + "lock-on-active true\n" + "when\n" + "   $p : Person()\n" + "then\n" + "   $p.setName(\"John\");\n" + "   update ($p);\n" + "end\n" + "rule R2\n" + "ruleflow-group \"group1\"\n" + "lock-on-active true\n" + "when\n" + "   $p : Person( name == null )\n" + "   forall ( Cheese ( type == \"cheddar\" ))\n" + "then\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("ruleflowgroup-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        ksession.insert(new Person());
        ksession.insert(new Cheese("gorgonzola"));
        ((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("group1");
        assertEquals(1, ksession.fireAllRules());
    } finally {
        ksession.dispose();
    }
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) KieBase(org.kie.api.KieBase) Cheese(org.drools.testcoverage.common.model.Cheese) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 12 with Cheese

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

the class ExceptionTest method testReturnValueException.

@Test
public void testReturnValueException() {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + TestException.class.getCanonicalName() + ";\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "function String throwException( ) {\n" + "    throw new TestException( \"this should throw an exception\" );\n" + "}\n" + "\n" + "rule \"Throw ReturnValue Exception\"\n" + "    when\n" + "        Cheese( type == ( throwException( ) ) )\n" + "    then\n" + "\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("exception-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final Cheese brie = new Cheese("brie", 12);
        Assertions.assertThatThrownBy(() -> {
            ksession.insert(brie);
            ksession.fireAllRules();
        }).hasRootCauseInstanceOf(TestException.class);
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Test(org.junit.Test)

Example 13 with Cheese

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

the class OrTest method testOrWithReturnValueRestriction.

@Test
public void testOrWithReturnValueRestriction() {
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + " \n" + "rule \"r1\"\n" + "when\n" + "    Cheese( type == \"brie\", $price : price )\n" + "    Cheese( type == \"stilton\", price == 10 || == ( $price % 10 ) )\n" + "then\n" + "    // noop\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("or-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        ksession.insert(new Cheese("brie", 18));
        ksession.insert(new Cheese("stilton", 8));
        ksession.insert(new Cheese("brie", 28));
        final int fired = ksession.fireAllRules();
        assertEquals(2, fired);
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Test(org.junit.Test)

Example 14 with Cheese

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

the class OrTest method testOr.

@Test
public void testOr() {
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "\n" + "rule \"literal test rule\"\n" + "    when\n" + "        Cheese(type == \"stilton\" ) or Cheese(type == \"cheddar\")\n" + "    then\n" + "        list.add(\"got cheese\");\n" + "end   \n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("or-test", kieBaseTestConfiguration, drl);
    final KieSession session = kbase.newKieSession();
    try {
        final List list = new ArrayList();
        session.setGlobal("list", list);
        final Cheese cheddar = new Cheese("cheddar", 5);
        final FactHandle h = session.insert(cheddar);
        session.fireAllRules();
        // just one added
        assertEquals("got cheese", list.get(0));
        assertEquals(1, list.size());
        session.delete(h);
        session.fireAllRules();
        // still just one
        assertEquals(1, list.size());
        session.insert(new Cheese("stilton", 5));
        session.fireAllRules();
        // now have one more
        assertEquals(2, ((List) session.getGlobal("list")).size());
    } finally {
        session.dispose();
    }
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) 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) Test(org.junit.Test)

Example 15 with Cheese

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

the class OrTest method testOrWithBinding.

@Test
public void testOrWithBinding() {
    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" + "\n" + "rule \"MyRule\"\n" + "    when\n" + "        c : (Cheese( type == \"stilton\") or\n" + "             Cheese( type == \"brie\" ) or\n" + "             Cheese( type == \"muzzarella\" ) )\n" + "        p : Person()\n" + "    then\n" + "        results.add(c);\n" + "        results.add(p);\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);
        final Person hola = new Person("hola");
        ksession.insert(hola);
        ksession.fireAllRules();
        assertEquals(0, list.size());
        final Cheese brie = new Cheese("brie");
        ksession.insert(brie);
        ksession.fireAllRules();
        assertEquals(2, list.size());
        assertTrue(list.contains(hola));
        assertTrue(list.contains(brie));
    } 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) 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