use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class GetFactHandlesInEntryPointCommand method execute.
public Collection<FactHandle> execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
EntryPoint ep = ksession.getEntryPoint(entryPoint);
Collection<FactHandle> disconnectedFactHandles = new ArrayList<FactHandle>();
if (filter != null) {
Collection<InternalFactHandle> factHandles = ep.getFactHandles(this.filter);
if (factHandles != null && disconnected) {
for (InternalFactHandle factHandle : factHandles) {
InternalFactHandle handle = factHandle.clone();
handle.disconnect();
disconnectedFactHandles.add(handle);
}
return disconnectedFactHandles;
} else {
return ksession.getFactHandles(this.filter);
}
} else {
Collection<InternalFactHandle> factHandles = ep.getFactHandles();
if (factHandles != null && disconnected) {
for (InternalFactHandle factHandle : factHandles) {
InternalFactHandle handle = factHandle.clone();
handle.disconnect();
disconnectedFactHandles.add(handle);
}
return disconnectedFactHandles;
} else {
return ksession.getFactHandles();
}
}
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testStatedShadowLogicalWithSingleOccurance.
@Test(timeout = 10000)
public void testStatedShadowLogicalWithSingleOccurance() {
String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + " String( this == 'go1' ) " + "then \n" + " insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + " String( this == 'go2' ) " + " s : String( this == 'f1' ) " + "then \n" + " list.add( s ); \n" + "end \n" + "";
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
session.insert("go1");
session.fireAllRules();
session.insert("go2");
session.fireAllRules();
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class MarshallingTest method testSerializeAdd.
@Test
public void testSerializeAdd() throws Exception {
Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
kBase = SerializationHelper.serializeObject(kBase);
KieSession session = kBase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
InternalFactHandle stilton = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie = (InternalFactHandle) session.insert(new Cheese("brie", 10));
session.fireAllRules();
assertEquals(list.size(), 1);
assertEquals("stilton", list.get(0));
// now recreate the rulebase, deserialize the session and test it
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
list = (List) session.getGlobal("list");
assertNotNull(list);
assertEquals(list.size(), 1);
assertEquals("stilton", list.get(0));
kpkgs = loadKnowledgePackages("../test_Dynamic3.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
kBase.addPackages(kpkgs);
InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
InternalFactHandle bob = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(list.size(), 3);
assertEquals(bob.getObject(), list.get(2));
assertEquals("stilton", list.get(1));
session.dispose();
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalThenStatedShadowSingleOccurance.
@Test(timeout = 10000)
public void testLogicalThenStatedShadowSingleOccurance() {
String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + " String( this == 'go1' ) " + "then \n" + " insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + " String( this == 'go2' ) " + " s : String( this == 'f1' ) " + "then \n" + " list.add( s ); \n" + "end \n" + "";
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
session.insert("go1");
session.fireAllRules();
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
session.insert("go2");
session.fireAllRules();
assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
assertNotSame(fh1, jfh1);
EqualityKey key = jfh1.getEqualityKey();
assertSame(fh1.getEqualityKey(), key);
assertNotSame(fh1, jfh1);
assertEquals(2, key.size());
assertSame(jfh1, key.getLogicalFactHandle());
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalWithStatedShadowThenDeleteLogicalThenDeleteStated.
@Test(timeout = 10000)
public void testLogicalWithStatedShadowThenDeleteLogicalThenDeleteStated() {
String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + " String( this == 'go1' ) " + "then \n" + " insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + " String( this in ('go2', 'go3', 'go4') ) " + " s : String( this == 'f1' ) " + "then \n" + " list.add( s ); \n" + "end \n" + "";
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
session.insert("go1");
session.fireAllRules();
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
session.insert("go2");
session.fireAllRules();
assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
assertEquals(1, fh1.getEqualityKey().getBeliefSet().size());
assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
assertNotSame(fh1, jfh1);
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
list.clear();
tms.delete(jfh1);
session.insert("go3");
session.fireAllRules();
assertNull(fh1.getEqualityKey().getBeliefSet());
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
list.clear();
session.delete(fh1);
session.insert("go4");
session.fireAllRules();
assertEquals(0, list.size());
}
Aggregations