use of org.drools.core.ClassObjectFilter 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);
kbase = SerializationHelper.serializeObject(kbase);
KieSession session = createKnowledgeSession(kbase);
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.retract(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.retract(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());
}
use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class TruthMaintenanceTest method testDeleteLogicalAssertion.
@Test
public void testDeleteLogicalAssertion() {
// BZ-1317026
String drl = "rule R1 when\n" + "then\n" + " insertLogical( \"test\" ); \n" + "end\n";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
ksession.fireAllRules();
Collection<FactHandle> fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
assertEquals(1, fhs.size());
for (FactHandle fh : fhs) {
ksession.delete(fh);
}
fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
assertEquals(0, fhs.size());
}
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());
}
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());
}
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());
}
}
}
Aggregations