Search in sources :

Example 91 with Cheese

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

the class OrTest method testEmptyIdentifier.

@Test
public void testEmptyIdentifier() {
    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" + "rule \"Or condition followed by fact\"\n" + "  when\n" + "    Cheese( type == \"stilton\" ) or Cheese( type == \"brie\" )\n" + "    Person( )\n" + "  then\n" + "    results.add(\"Or condition followed by fact is ok\");\n" + "end\n" + "\n" + "rule \"Fact followed by or condition\"\n" + "  when\n" + "    Person( )\n" + "    Cheese( type == \"stilton\" ) or Cheese( type == \"brie\" )\n" + "  then\n" + "    results.add(\"Fact followed by or condition is ok\");\n" + "end\n" + "\n" + "rule \"Single fact\"\n" + "  when\n" + "    Person( )\n" + "  then\n" + "    results.add(\"Single fact is ok\");\n" + "end\n" + "\n" + "rule \"Single or\"\n" + "  when\n" + "    Cheese( type == \"stilton\" ) or Cheese( type == \"brie\" )\n" + "  then\n" + "    results.add(\"Single or is ok\");\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("or-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List result = new ArrayList();
        ksession.setGlobal("results", result);
        final Person person = new Person("bob");
        final Cheese cheese = new Cheese("brie", 10);
        ksession.insert(person);
        ksession.insert(cheese);
        ksession.fireAllRules();
        assertEquals(4, result.size());
    } 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)

Example 92 with Cheese

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

the class ContainsTest method testContainsCheese.

@Test
public void testContainsCheese() {
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"Cheesery contains stilton\"\n" + "    salience 10\n" + "    when\n" + "        stilton : Cheese( type == \"stilton\" )\n" + "        Cheesery( cheeses contains stilton )\n" + "    then\n" + "        list.add( stilton );\n" + "end   \n" + "\n" + "rule \"Cheesery does not contain brie\"\n" + "    when\n" + "        brie : Cheese( type == \"brie\" )\n" + "        Cheesery( cheeses not contains brie )\n" + "    then\n" + "        list.add( brie );\n" + "end ";
    final KieBase kieBase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("contains-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kieBase.newKieSession();
    try {
        final List list = new ArrayList();
        ksession.setGlobal("list", list);
        final Cheese stilton = new Cheese("stilton", 12);
        ksession.insert(stilton);
        final Cheese brie = new Cheese("brie", 10);
        ksession.insert(brie);
        final Cheesery cheesery = new Cheesery();
        cheesery.getCheeses().add(stilton);
        ksession.insert(cheesery);
        ksession.fireAllRules();
        assertEquals(2, list.size());
        assertEquals(stilton, list.get(0));
        assertEquals(brie, 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 93 with Cheese

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

the class EvalTest method testEvalDefaultCompiler.

@Test
public void testEvalDefaultCompiler() {
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "global java.lang.Integer five;\n" + "rule \"eval rule test\"\n" + "    when\n" + "        $cheese : Cheese( $type:type == \"stilton\" )\n" + "        eval( $cheese.getPrice() == five.intValue() )\n" + "    then\n" + "        list.add( $cheese );\n" + "end";
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("eval-test", kieBaseTestConfiguration, drl);
    final KieContainer kieContainer = KieServices.get().newKieContainer(kieModule.getReleaseId());
    final KieBase kbase;
    kbase = kieContainer.getKieBase();
    final KieSession ksession = kbase.newKieSession();
    try {
        ksession.setGlobal("five", 5);
        final List list = new ArrayList();
        ksession.setGlobal("list", list);
        final Cheese stilton = new Cheese("stilton", 5);
        ksession.insert(stilton);
        ksession.fireAllRules();
        assertEquals(stilton, ((List) ksession.getGlobal("list")).get(0));
    } 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) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 94 with Cheese

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

the class FromTest method testNetworkBuildErrorAcrossEntryPointsAndFroms.

@Test
public void testNetworkBuildErrorAcrossEntryPointsAndFroms() {
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List list\n" + "rule rule1\n" + "when\n" + "         Cheese() from entry-point \"testep\"\n" + "    $p : Person() from list\n" + "then \n" + "  list.add( \"rule1\" ) ;\n" + "  insert( $p );\n" + "end\n" + "rule rule2\n" + "when\n" + "  $p : Person() \n" + "then \n" + "  list.add( \"rule2\" ) ;\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("from-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final EntryPoint ep = ksession.getEntryPoint("testep");
        final List list = new ArrayList();
        ksession.setGlobal("list", list);
        list.add(new Person("darth"));
        ep.insert(new Cheese("cheddar"));
        ksession.fireAllRules();
        assertEquals(3, list.size());
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Cheese(org.drools.testcoverage.common.model.Cheese) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 95 with Cheese

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

the class NotTest method testMissingRootBlockerEquality.

@Test
public void testMissingRootBlockerEquality() {
    // DROOLS-6636
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "\n" + "rule R1 when\n" + "    Cheese($type : type)\n" + "    not( Person( likes == $type, salary == null ) )\n" + "then\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("not-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        Cheese cheese = new Cheese("cheddar");
        Person p1 = new Person("John");
        p1.setLikes("cheddar");
        p1.setSalary(null);
        Person p2 = new Person("Paul");
        p2.setLikes("cheddar");
        p2.setSalary(null);
        Person p3 = new Person("George");
        p3.setLikes("cheddar");
        p3.setSalary(null);
        ksession.insert(cheese);
        InternalFactHandle handle1 = (InternalFactHandle) ksession.insert(p1);
        InternalFactHandle handle2 = (InternalFactHandle) ksession.insert(p2);
        InternalFactHandle handle3 = (InternalFactHandle) ksession.insert(p3);
        assertEquals(0, ksession.fireAllRules());
        InternalFactHandle blockerHandle = getBlockerFactHandle(ksession);
        // for example, it returns p3 "George"
        Person blockerFact = (Person) blockerHandle.getObject();
        // modify unrelated property
        blockerFact.setAge(40);
        ksession.update(blockerHandle, blockerFact, "age");
        assertEquals(0, ksession.fireAllRules());
        // now this fact should match but remaining 2 facts shouldn't
        blockerFact.setSalary(new BigDecimal(1000));
        ksession.update(blockerHandle, blockerFact, "salary");
        assertEquals(0, ksession.fireAllRules());
        // Then, modify remaining facts
        List<InternalFactHandle> handleList = new ArrayList<>();
        handleList.add(handle1);
        handleList.add(handle2);
        handleList.add(handle3);
        handleList.remove(blockerHandle);
        for (InternalFactHandle handle : handleList) {
            Person p = (Person) handle.getObject();
            p.setSalary(new BigDecimal(1000));
            ksession.update(handle, p, "salary");
        }
        assertEquals(1, ksession.fireAllRules());
    } finally {
        ksession.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) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.testcoverage.common.model.Person) 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