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 org.drools.compiler.Person\n" + "import org.drools.compiler.Cheese\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 = loadKnowledgeBaseFromString(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 TruthMaintenanceTest method testStatedDeleteLogicalAssertionFromRule.
@Test
public void testStatedDeleteLogicalAssertionFromRule() {
// 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, org.kie.api.runtime.rule.FactHandle.State.STATED ); \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(1, fhs.size());
}
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();
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, 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.retract(h);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
assertEquals(0, ksession.getObjects().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());
}
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());
}
Aggregations