Search in sources :

Example 66 with Cheese

use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.

the class QueryTest method testQuery.

@Test
public void testQuery() throws Exception {
    KieBase kbase = loadKnowledgeBase("simple_query_test.drl");
    KieSession session = createKieSession(kbase);
    final Cheese stilton = new Cheese("stinky", 5);
    FactHandle factHandle = session.insert(stilton);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    String queryName = "simple query";
    org.kie.api.runtime.rule.QueryResults results = getQueryResults(session, queryName);
    assertEquals(1, results.size());
    QueryResultsRow row = results.iterator().next();
    if (row instanceof FlatQueryResultRow) {
        FlatQueryResultRow flatRow = (FlatQueryResultRow) row;
        assertEquals(0, flatRow.getIdentifiers().size());
    } else if (row instanceof QueryResultsRowImpl) {
        QueryResultsRowImpl rowImpl = (QueryResultsRowImpl) row;
        assertEquals(0, rowImpl.getDeclarations().size());
    }
}
Also used : FlatQueryResultRow(org.drools.core.runtime.rule.impl.FlatQueryResultRow) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) QueryResults(org.kie.api.runtime.rule.QueryResults) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) QueryResultsRowImpl(org.drools.core.QueryResultsRowImpl) Test(org.junit.Test)

Example 67 with Cheese

use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsWithExists.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsWithExists() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionWithExists.drl");
    KieSession ksession = kbase.newKieSession();
    try {
        final Person p1 = new Person("p1", "stilton", 20);
        p1.setStatus("europe");
        FactHandle c1FactHandle = ksession.insert(p1);
        final Person p2 = new Person("p2", "stilton", 30);
        p2.setStatus("europe");
        FactHandle c2FactHandle = ksession.insert(p2);
        final Person p3 = new Person("p3", "stilton", 40);
        p3.setStatus("europe");
        FactHandle c3FactHandle = ksession.insert(p3);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        // all 3 in europe, so, 2 cheese
        Collection cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(2, cheeseList.size());
        // europe=[ 1, 2 ], america=[ 3 ]
        p3.setStatus("america");
        c3FactHandle = getFactHandle(c3FactHandle, ksession);
        ksession.update(c3FactHandle, p3);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(1, cheeseList.size());
        // europe=[ 1 ], america=[ 2, 3 ]
        p2.setStatus("america");
        c2FactHandle = getFactHandle(c2FactHandle, ksession);
        ksession.update(c2FactHandle, p2);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(1, cheeseList.size());
        // europe=[ ], america=[ 1, 2, 3 ]
        p1.setStatus("america");
        c1FactHandle = getFactHandle(c1FactHandle, ksession);
        ksession.update(c1FactHandle, p1);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(2, cheeseList.size());
        // europe=[ 2 ], america=[ 1, 3 ]
        p2.setStatus("europe");
        c2FactHandle = getFactHandle(c2FactHandle, ksession);
        ksession.update(c2FactHandle, p2);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(1, cheeseList.size());
        // europe=[ 1, 2 ], america=[ 3 ]
        p1.setStatus("europe");
        c1FactHandle = getFactHandle(c1FactHandle, ksession);
        ksession.update(c1FactHandle, p1);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(1, cheeseList.size());
        // europe=[ 1, 2, 3 ], america=[ ]
        p3.setStatus("europe");
        c3FactHandle = getFactHandle(c3FactHandle, ksession);
        ksession.update(c3FactHandle, p3);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
        assertEquals(2, cheeseList.size());
    } finally {
        ksession.dispose();
    }
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 68 with Cheese

use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsNot.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsNot() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsNot.drl");
    KieSession ksession = kbase.newKieSession();
    try {
        final Person a = new Person("a");
        final Cheese cheese = new Cheese("brie", 1);
        ksession.setGlobal("cheese", cheese);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        Collection list = ksession.getObjects();
        assertEquals("i was not asserted by not a => i.", 1, list.size());
        assertEquals("i was not asserted by not a => i.", cheese, list.iterator().next());
        FactHandle h = ksession.insert(a);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        // no need to fire rules, assertion alone removes justification for i,
        // so it should be deleted.
        // workingMemory.fireAllRules();
        ksession.fireAllRules();
        list = ksession.getObjects();
        assertEquals("a was not asserted or i not deleted.", 1, list.size());
        assertEquals("a was asserted.", a, list.iterator().next());
        assertFalse("i was not rectracted.", list.contains(cheese));
        // no rules should fire, but nevertheless...
        // workingMemory.fireAllRules();
        assertEquals("agenda should be empty.", 0, ((InternalAgenda) ((StatefulKnowledgeSessionImpl) ksession).getAgenda()).getAgendaGroupsManager().agendaSize());
        h = getFactHandle(h, ksession);
        ksession.delete(h);
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        list = ksession.getObjects();
        assertEquals("i was not asserted by not a => i.", 1, list.size());
        assertEquals("i was not asserted by not a => i.", cheese, list.iterator().next());
    } finally {
        ksession.dispose();
    }
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 69 with Cheese

use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsBacking.

@Test(timeout = 10000)
public void testLogicalInsertionsBacking() throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsBacking.drl", getClass()), ResourceType.DRL);
    Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kpkgs);
    KieSession session = createKnowledgeSession(kbase);
    try {
        final Cheese cheese1 = new Cheese("c", 1);
        final Cheese cheese2 = new Cheese(cheese1.getType(), 1);
        FactHandle h1 = session.insert(cheese1);
        session.fireAllRules();
        session = getSerialisedStatefulKnowledgeSession(session, true);
        Collection<?> list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
        assertEquals(1, list.size());
        // probably dangerous, as contains works with equals, not identity
        assertEquals(cheese1.getType(), list.iterator().next());
        FactHandle h2 = session.insert(cheese2);
        session.fireAllRules();
        session = getSerialisedStatefulKnowledgeSession(session, true);
        list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
        assertEquals(1, list.size());
        assertEquals(cheese1.getType(), list.iterator().next());
        assertEquals(3, session.getObjects().size());
        h1 = getFactHandle(h1, session);
        session.delete(h1);
        session = getSerialisedStatefulKnowledgeSession(session, true);
        session.fireAllRules();
        session = getSerialisedStatefulKnowledgeSession(session, true);
        list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
        assertEquals("cheese-type " + cheese1.getType() + " was deleted, but should not. Backed by cheese2 => type.", 1, list.size());
        assertEquals("cheese-type " + cheese1.getType() + " was deleted, but should not. Backed by cheese2 => type.", cheese1.getType(), list.iterator().next());
        h2 = getFactHandle(h2, session);
        session.delete(h2);
        session = getSerialisedStatefulKnowledgeSession(session, true);
        session.fireAllRules();
        session = getSerialisedStatefulKnowledgeSession(session, true);
        list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
        assertEquals("cheese-type " + cheese1.getType() + " was not deleted, but should have. Neither  cheese1 => type nor cheese2 => type is true.", 0, list.size());
    } finally {
        session.dispose();
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ClassObjectFilter(org.drools.core.ClassObjectFilter) KiePackage(org.kie.api.definition.KiePackage) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 70 with Cheese

use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.

the class MarshallingTest method testMultiRuleMultiJoinNodePatternsWithHalt.

@Test
public void testMultiRuleMultiJoinNodePatternsWithHalt() throws Exception {
    String rule1 = "package org.drools.compiler.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule \"Rule 1\"\n" + "when\n" + "    $c : Cheese( ) \n" + "    $p : Person( cheese == $c ) \n" + "then\n" + "    list.add( $p );\n" + "end";
    String rule2 = "package org.drools.compiler.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "import " + Cell.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule \"Rule 2\"\n" + "when\n" + "    $c : Cheese( ) \n" + "    $p : Person( cheese == $c ) \n" + "    $x : Cell( value == $p.age ) \n" + "then\n" + "    list.add( $x );\n" + "end";
    String rule3 = "package org.drools.compiler.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + FactA.class.getCanonicalName() + "\n" + "import " + FactB.class.getCanonicalName() + "\n" + "import " + FactC.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule \"Rule 3\"\n" + "when\n" + "    $a : FactA( field2 > 10 ) \n" + "    $b : FactB( f2 >= $a.field2 ) \n" + "    $p : Person( name == \"darth vadar\" ) \n" + "    $c : FactC( f2 >= $b.f2 ) \n" + "then\n" + "    list.add( $c );\n" + "    drools.halt();\n" + "end";
    KieBase kBase = loadKnowledgeBaseFromString(rule1, rule2, rule3);
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    Cheese stilton = new Cheese("stilton", 25);
    Cheese brie = new Cheese("brie", 49);
    Person bobba = new Person("bobba fet", 30);
    bobba.setCheese(stilton);
    Person vadar = new Person("darth vadar", 38);
    Person c3po = new Person("c3p0", 17);
    c3po.setCheese(stilton);
    Person r2d2 = new Person("r2d2", 58);
    r2d2.setCheese(brie);
    session.insert(stilton);
    session.insert(bobba);
    session.insert(vadar);
    session.insert(brie);
    session.insert(c3po);
    session.insert(r2d2);
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    list = (List) session.getGlobal("list");
    assertEquals(3, list.size());
    assertTrue(list.contains(r2d2));
    assertTrue(list.contains(c3po));
    assertTrue(list.contains(bobba));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    Cell cell30 = new Cell(30);
    session.insert(cell30);
    Cell cell58 = new Cell(58);
    session.insert(cell58);
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(5, list.size());
    assertTrue(list.contains(cell30));
    assertTrue(list.contains(cell58));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.insert(new FactA(15));
    session.insert(new FactB(20));
    FactC factC27 = new FactC(27);
    session.insert(factC27);
    FactC factC52 = new FactC(52);
    session.insert(factC52);
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    session.fireAllRules();
    assertEquals(7, list.size());
    assertTrue(list.contains(factC52));
    assertTrue(list.contains(factC27));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) Cell(org.drools.mvel.compiler.Cell) FactA(org.drools.mvel.compiler.FactA) FactB(org.drools.mvel.compiler.FactB) FactC(org.drools.mvel.compiler.FactC) Test(org.junit.Test)

Aggregations

Cheese (org.drools.mvel.compiler.Cheese)136 Test (org.junit.Test)129 KieSession (org.kie.api.runtime.KieSession)112 KieBase (org.kie.api.KieBase)96 ArrayList (java.util.ArrayList)90 List (java.util.List)65 Person (org.drools.mvel.compiler.Person)47 FactHandle (org.kie.api.runtime.rule.FactHandle)29 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)24 KiePackage (org.kie.api.definition.KiePackage)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)20 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)14 Command (org.kie.api.command.Command)10 ExecutionResults (org.kie.api.runtime.ExecutionResults)10 PackageDescr (org.drools.drl.ast.descr.PackageDescr)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)6 Cheesery (org.drools.mvel.compiler.Cheesery)6 ClassObjectFilter (org.drools.core.ClassObjectFilter)5 ClassObjectType (org.drools.core.base.ClassObjectType)5