Search in sources :

Example 1 with ClassObjectFilter

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

the class TruthMaintenanceTest method testLogicalInsertionsUpdateEqual.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsUpdateEqual() throws Exception {
    // calling update on a justified FH, states it
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsUpdateEqual.drl");
    KieSession ksession = kbase.newKieSession();
    final Person p = new Person("person");
    p.setAge(2);
    FactHandle h = ksession.insert(p);
    assertEquals(1, ksession.getObjects().size());
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    assertEquals(2, ksession.getObjects().size());
    Collection l = ksession.getObjects(new ClassObjectFilter(CheeseEqual.class));
    assertEquals(1, l.size());
    assertEquals(3, ((CheeseEqual) l.iterator().next()).getPrice());
    h = getFactHandle(h, ksession);
    ksession.retract(h);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    Collection list = ksession.getObjects();
    // CheeseEqual was updated, making it stated, so it wouldn't have been logically deleted
    assertEquals(1, list.size());
    assertEquals(new CheeseEqual("person", 3), list.iterator().next());
    FactHandle fh = ksession.getFactHandle(list.iterator().next());
    ksession.retract(fh);
    list = ksession.getObjects();
    assertEquals(0, list.size());
    TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    final java.lang.reflect.Field field = tms.getClass().getDeclaredField("equalityKeyMap");
    field.setAccessible(true);
    final ObjectHashMap m = (ObjectHashMap) field.get(tms);
    field.setAccessible(false);
    assertEquals("assertMap should be empty", 0, m.size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectHashMap(org.drools.core.util.ObjectHashMap) CheeseEqual(org.drools.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with ClassObjectFilter

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

the class TruthMaintenanceTest method testDeleteLogicalAssertionFromRule.

@Test
public void testDeleteLogicalAssertionFromRule() {
    // BZ-1317026
    String drl = "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + "    insertLogical( \"test\" ); \n" + "end\n" + "rule R2 when\n" + "    $s : String()\n" + "then\n" + "    list.add( $s ); \n" + "    delete( $s ); \n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("test", list.get(0));
    Collection<FactHandle> fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
    assertEquals(0, fhs.size());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 3 with ClassObjectFilter

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

the class TruthMaintenanceTest method testLogicalInsertionsNoLoop.

// (timeout=10000)
@Test
public void testLogicalInsertionsNoLoop() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsNoLoop.drl");
    KieSession ksession = kbase.newKieSession();
    final List l = new ArrayList();
    final Person a = new Person("a");
    ksession.setGlobal("a", a);
    ksession.setGlobal("l", l);
    ksession.fireAllRules();
    assertEquals("a still in WM", 0, ksession.getObjects(new ClassObjectFilter(a.getClass())).size());
    assertEquals("Rule should not loop", 1, l.size());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 4 with ClassObjectFilter

use of org.drools.core.ClassObjectFilter 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();
    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 = SerializationHelper.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 = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.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 = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.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 = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.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 = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.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 = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.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 = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(2, cheeseList.size());
}
Also used : 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) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 5 with ClassObjectFilter

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

the class TruthMaintenanceTest method testLogicalInsertionsDynamicRule.

// timeout=10000)
@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);
    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);
    kbase = SerializationHelper.serializeObject(kbase);
    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());
    kbase = SerializationHelper.serializeObject(kbase);
    // 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());
}
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) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.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