use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertionsWithModify.
@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsWithModify() throws Exception {
KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsWithUpdate.drl");
KieSession ksession = kbase.newKieSession();
try {
final Person p = new Person("person");
p.setAge(2);
FactHandle h = ksession.insert(p);
assertEquals(1, ksession.getObjects().size());
ksession.fireAllRules();
ksession = getSerialisedStatefulKnowledgeSession(ksession, false);
assertEquals(2, ksession.getObjects().size());
Collection l = ksession.getObjects(new ClassObjectFilter(CheeseEqual.class));
assertEquals(1, l.size());
assertEquals(2, ((CheeseEqual) l.iterator().next()).getPrice());
h = getFactHandle(h, ksession);
ksession.delete(h);
ksession = getSerialisedStatefulKnowledgeSession(ksession, false);
assertEquals(0, ksession.getObjects().size());
TruthMaintenanceSystem tms = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) ksession);
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());
} finally {
ksession.dispose();
}
}
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) ((TruthMaintenanceSystemEqualityKey) handle.getEqualityKey()).getBeliefSet();
assertEquals(3, dbs.size());
assertTrue(dbs.isConflicting());
}
}
}
use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class IncrementalCompilationTest method testRemoveRulesWithAccumulateAndLogicalAssertions.
@Test
public void testRemoveRulesWithAccumulateAndLogicalAssertions() {
// DROOLS-3554
final String DRL1 = "rule R1 when\n" + " accumulate(String($l : length > 1); $s : sum($l))\n" + "then\n" + " insertLogical($s);\n" + "end";
KieServices ks = KieServices.get();
final ReleaseId id = ks.newReleaseId("org.test", "logical", "1.0.0");
KieModuleModel model = ks.newKieModuleModel();
model.newKieBaseModel("kbase").newKieSessionModel("ksession").setDefault(true);
String kproj = model.toXML();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.generateAndWritePomXML(id);
kfs.writeKModuleXML(kproj);
kfs.write(ks.getResources().newReaderResource(new StringReader(DRL1)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
kieBuilder.buildAll(DrlProject.class);
KieModule kieModule = kieBuilder.getKieModule();
KieContainer kieContainer = ks.newKieContainer(kieModule.getReleaseId());
KieSession kieSession = kieContainer.newKieSession();
kieSession.insert("test");
kieSession.insert("test2");
assertEquals(2, kieSession.getObjects().size());
kieSession.fireAllRules();
assertEquals(3, kieSession.getObjects().size());
assertEquals(9, kieSession.getObjects(new ClassObjectFilter(Integer.class)).iterator().next());
ReleaseId id2 = ks.newReleaseId("org.test", "logical", "2.0.0");
KieFileSystem kfs2 = ks.newKieFileSystem();
KieBuilder kieBuilder2 = ks.newKieBuilder(kfs2);
kfs2.generateAndWritePomXML(id2);
kfs2.writeKModuleXML(kproj);
kieBuilder = ks.newKieBuilder(kfs2);
kieBuilder.buildAll(DrlProject.class);
kieModule = kieBuilder.getKieModule();
kieContainer.updateToVersion(id2);
kieSession.fireAllRules();
assertEquals(2, kieSession.getObjects().size());
assertTrue(kieSession.getObjects(new ClassObjectFilter(Integer.class)).isEmpty());
}
use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class Misc2Test method testPhreakTMS.
@Test
public void testPhreakTMS() {
// DROOLS-7
String str = "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "rule R when\n" + " Person( $age : age, $name : name == \"A\" )\n" + " not Person( age == $age + 1 )\n" + "then\n" + " insertLogical(new Cheese(\"gorgonzola\", 10));\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
Person p1 = new Person("A", 31);
FactHandle fh1 = ksession.insert(p1);
ksession.fireAllRules();
assertEquals(1, ksession.getObjects(new ClassObjectFilter(Cheese.class)).size());
Person p2 = new Person("A", 32);
FactHandle fh2 = ksession.insert(p2);
ksession.fireAllRules();
assertEquals(1, ksession.getObjects(new ClassObjectFilter(Cheese.class)).size());
}
use of org.drools.core.ClassObjectFilter in project drools by kiegroup.
the class StatefulSessionTest method testIterateObjects.
@Test
public void testIterateObjects() throws Exception {
KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_IterateObjects.drl");
KieSession ksession = kbase.newKieSession();
final List results = new ArrayList();
ksession.setGlobal("results", results);
ksession.insert(new Cheese("stilton", 10));
ksession.fireAllRules();
final Iterator events = ksession.getObjects(new ClassObjectFilter(PersonInterface.class)).iterator();
assertTrue(events.hasNext());
assertEquals(1, results.size());
assertEquals(results.get(0), events.next());
}
Aggregations