Search in sources :

Example 41 with ClassObjectFilter

use of org.drools.core.ClassObjectFilter in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsAccumulatorPattern.

@Test(timeout = 10000)
public void testLogicalInsertionsAccumulatorPattern() throws Exception {
    // JBRULES-449
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsAccumulatorPattern.drl");
    kbase = SerializationHelper.serializeObject(kbase);
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("ga", "a");
    ksession.setGlobal("gb", "b");
    ksession.setGlobal("gs", new Short((short) 3));
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    FactHandle h = ksession.insert(new Integer(6));
    assertEquals(1, ksession.getObjects().size());
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    assertEquals("There should be 2 CheeseEqual in Working Memory, 1 justified, 1 stated", 2, ksession.getObjects(new ClassObjectFilter(CheeseEqual.class)).size());
    assertEquals(6, ksession.getObjects().size());
    h = getFactHandle(h, ksession);
    ksession.retract(h);
    ksession.fireAllRules();
    for (Object o : ksession.getObjects()) {
        System.out.println(o);
    }
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(CheeseEqual.class)).size());
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(Short.class)).size());
    assertEquals(0, ksession.getObjects().size());
}
Also used : CheeseEqual(org.drools.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 42 with ClassObjectFilter

use of org.drools.core.ClassObjectFilter in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsSelfreferencing.

// (timeout=10000)
@Test
public // @Ignore
void testLogicalInsertionsSelfreferencing() throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsSelfreferencing.drl", getClass()), ResourceType.DRL);
    Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    final KieSession session = createKnowledgeSession(kbase);
    final Person b = new Person("b");
    final Person a = new Person("a");
    session.setGlobal("b", b);
    FactHandle h1 = session.insert(a);
    session.fireAllRules();
    Collection<?> list = session.getObjects(new ClassObjectFilter(a.getClass()));
    assertEquals(2, list.size());
    assertTrue(list.contains(a));
    assertTrue(list.contains(b));
    session.retract(h1);
    session.fireAllRules();
    list = session.getObjects(new ClassObjectFilter(a.getClass()));
    assertEquals("b was deleted, but it should not have. Is backed by b => b being true.", 1, list.size());
    assertEquals("b was deleted, but it should not have. Is backed by b => b being true.", b, list.iterator().next());
    h1 = session.getFactHandle(b);
    assertSame(((InternalFactHandle) h1).getEqualityKey().getLogicalFactHandle(), h1);
    ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem().delete(h1);
    session.fireAllRules();
    list = session.getObjects(new ClassObjectFilter(a.getClass()));
    assertEquals(0, list.size());
}
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) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 43 with ClassObjectFilter

use of org.drools.core.ClassObjectFilter in project drools by kiegroup.

the class DefeasibilityTest method testManyDefeasibles.

@Test(timeout = 10000)
public void testManyDefeasibles() {
    String drl = "package org.drools.defeasible; " + "declare Fact " + "     fact: String @key " + "end " + " " + "rule init " + "     when " + "     then " + "         insert( new Fact( 'one' ) ); " + "         insert( new Fact( 'two' ) ); " + "         insert( new Fact( 'two' ) ); " + "end " + " " + "rule rule1 " + "     @Defeasible " + "     enabled true " + "     when " + "         Fact( \"one\"; ) " + "     then " + "         System.out.println(\"one causes wibble\"); " + "         insertLogical( new Fact( \"wibble\") ); " + "end " + " " + "rule rule2 " + "     @Defeasible " + "     when " + "         Fact( \"two\"; ) " + "     then " + "         System.out.println(\"two causes wibble\"); " + "         insertLogical( new Fact( \"wibble\") ); " + "end " + " " + "rule rule3 " + "     @Defeater " + "     @Defeats( \"rule2\" ) " + "     when " + "         Fact( \"two\"; ) " + "     then " + "         System.out.println(\"two negates wibble\"); " + "         insertLogical( new Fact( \"wibble\"), \"neg\" ); " + "end";
    KieSession session = getSessionFromString(drl);
    session.fireAllRules();
    FactType factType = session.getKieBase().getFactType("org.drools.defeasible", "Fact");
    for (Object o : session.getObjects(new ClassObjectFilter(factType.getFactClass()))) {
        if ("wibble".equals(factType.get(o, "fact"))) {
            InternalFactHandle handle = (InternalFactHandle) session.getFactHandle(o);
            DefeasibleBeliefSet dbs = (DefeasibleBeliefSet) handle.getEqualityKey().getBeliefSet();
            assertEquals(3, dbs.size());
            assertTrue(dbs.isConflicting());
        }
    }
}
Also used : DefeasibleBeliefSet(org.drools.core.beliefsystem.defeasible.DefeasibleBeliefSet) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 44 with ClassObjectFilter

use of org.drools.core.ClassObjectFilter in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsAccumulatorPattern.

@Test(timeout = 10000)
public void testLogicalInsertionsAccumulatorPattern() throws Exception {
    // JBRULES-449
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsAccumulatorPattern.drl");
    KieSession ksession = kbase.newKieSession();
    try {
        ksession.setGlobal("ga", "a");
        ksession.setGlobal("gb", "b");
        ksession.setGlobal("gs", new Short((short) 3));
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        FactHandle h = ksession.insert(new Integer(6));
        assertEquals(1, ksession.getObjects().size());
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        assertEquals("There should be 2 CheeseEqual in Working Memory, 1 justified, 1 stated", 2, ksession.getObjects(new ClassObjectFilter(CheeseEqual.class)).size());
        assertEquals(6, ksession.getObjects().size());
        h = getFactHandle(h, ksession);
        ksession.delete(h);
        ksession.fireAllRules();
        for (Object o : ksession.getObjects()) {
            System.out.println(o);
        }
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        assertEquals(0, ksession.getObjects(new ClassObjectFilter(CheeseEqual.class)).size());
        assertEquals(0, ksession.getObjects(new ClassObjectFilter(Short.class)).size());
        assertEquals(0, ksession.getObjects().size());
    } finally {
        ksession.dispose();
    }
}
Also used : CheeseEqual(org.drools.mvel.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 45 with ClassObjectFilter

use of org.drools.core.ClassObjectFilter in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsDynamicRule.

@Test
public void testLogicalInsertionsDynamicRule() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsDynamicRule.drl", getClass()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
    kbase.addPackages(kpkgs);
    KieSession ksession = createKnowledgeSession(kbase);
    try {
        final Cheese c1 = new Cheese("a", 1);
        final Cheese c2 = new Cheese("b", 2);
        final Cheese c3 = new Cheese("c", 3);
        List list;
        ksession.insert(c1);
        FactHandle h = ksession.insert(c2);
        ksession.insert(c3);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        // Check logical Insertions where made for c2 and c3
        list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
        assertEquals(2, list.size());
        assertFalse(list.contains(new Person(c1.getType())));
        assertTrue(list.contains(new Person(c2.getType())));
        assertTrue(list.contains(new Person(c3.getType())));
        // this rule will make a logical assertion for c1 too
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsDynamicRule2.drl", getClass()), ResourceType.DRL);
        if (kbuilder.hasErrors()) {
            fail(kbuilder.getErrors().toString());
        }
        Collection<KiePackage> kpkgs2 = kbuilder.getKnowledgePackages();
        kbase.addPackages(kpkgs2);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        kbase = (InternalKnowledgeBase) ksession.getKieBase();
        // check all now have just one logical assertion each
        list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
        assertEquals(3, list.size());
        assertTrue(list.contains(new Person(c1.getType())));
        assertTrue(list.contains(new Person(c2.getType())));
        assertTrue(list.contains(new Person(c3.getType())));
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        // check the packages are correctly populated
        assertEquals(3, kbase.getKiePackages().size());
        KiePackage test = null, test2 = null;
        // different JVMs return the package list in different order
        for (KiePackage kpkg : kbase.getKiePackages()) {
            if (kpkg.getName().equals("org.drools.compiler.test")) {
                test = kpkg;
            } else if (kpkg.getName().equals("org.drools.compiler.test2")) {
                test2 = kpkg;
            }
        }
        assertNotNull(test);
        assertNotNull(test2);
        assertEquals("rule1", test.getRules().iterator().next().getName());
        assertEquals("rule2", test2.getRules().iterator().next().getName());
        // now remove the first rule
        kbase.removeRule(test.getName(), test.getRules().iterator().next().getName());
        // different JVMs return the package list in different order
        for (KiePackage kpkg : kbase.getKiePackages()) {
            if (kpkg.getName().equals("org.drools.compiler.test")) {
                test = kpkg;
            } else if (kpkg.getName().equals("org.drools.compiler.test2")) {
                test2 = kpkg;
            }
        }
        assertNotNull(test);
        assertNotNull(test2);
        // Check the rule was correctly remove
        assertEquals(0, test.getRules().size());
        assertEquals(1, test2.getRules().size());
        assertEquals("rule2", test2.getRules().iterator().next().getName());
        list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
        assertEquals("removal of the rule should result in retraction of c3's logical assertion", 2, list.size());
        assertTrue("c1's logical assertion should not be deleted", list.contains(new Person(c1.getType())));
        assertTrue("c2's logical assertion should  not be deleted", list.contains(new Person(c2.getType())));
        assertFalse("c3's logical assertion should be  deleted", list.contains(new Person(c3.getType())));
        c2.setPrice(3);
        h = getFactHandle(h, ksession);
        ksession.update(h, c2);
        ksession.fireAllRules();
        ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
        list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
        assertEquals("c2 now has a higher price, its logical assertion should  be cancelled", 1, list.size());
        assertFalse("The logical assertion cor c2 should have been deleted", list.contains(new Person(c2.getType())));
        assertTrue("The logical assertion  for c1 should exist", list.contains(new Person(c1.getType())));
        // different JVMs return the package list in different order
        for (KiePackage kpkg : kbase.getKiePackages()) {
            if (kpkg.getName().equals("org.drools.compiler.test")) {
                test = kpkg;
            } else if (kpkg.getName().equals("org.drools.compiler.test2")) {
                test2 = kpkg;
            }
        }
        assertNotNull(test);
        assertNotNull(test2);
        kbase.removeRule(test2.getName(), test2.getRules().iterator().next().getName());
        // different JVMs return the package list in different order
        for (KiePackage kpkg : kbase.getKiePackages()) {
            if (kpkg.getName().equals("org.drools.compiler.test")) {
                test = kpkg;
            } else if (kpkg.getName().equals("org.drools.compiler.test2")) {
                test2 = kpkg;
            }
        }
        assertNotNull(test);
        assertNotNull(test2);
        assertEquals(0, test.getRules().size());
        assertEquals(0, test2.getRules().size());
        list = new ArrayList(ksession.getObjects(new ClassObjectFilter(Person.class)));
        assertEquals(0, list.size());
    } finally {
        ksession.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) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

ClassObjectFilter (org.drools.core.ClassObjectFilter)55 KieSession (org.kie.api.runtime.KieSession)44 Test (org.junit.Test)40 FactHandle (org.kie.api.runtime.rule.FactHandle)29 InternalFactHandle (org.drools.core.common.InternalFactHandle)24 KieBase (org.kie.api.KieBase)19 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)17 ArrayList (java.util.ArrayList)15 List (java.util.List)14 Collection (java.util.Collection)12 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)11 Person (org.drools.compiler.Person)8 KiePackage (org.kie.api.definition.KiePackage)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)8 HashMap (java.util.HashMap)7 KieHelper (org.kie.internal.utils.KieHelper)6 Cheese (org.drools.compiler.Cheese)5 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 RegistryContext (org.drools.core.command.impl.RegistryContext)4 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)4