use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testStatedWithShadowAndDeleteException.
@Test(timeout = 10000)
public void testStatedWithShadowAndDeleteException() {
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" + "";
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();
// TMS is now enabled
assertNotNull(fh1.getEqualityKey());
assertNotNull(fh2.getEqualityKey());
// EqualtyKey shows both are stated
assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
assertEquals(EqualityKey.STATED, fh2.getEqualityKey().getStatus());
// Only fh1 has a logical
assertEquals(1, fh1.getEqualityKey().getBeliefSet().size());
assertNull(fh2.getEqualityKey().getBeliefSet());
// Get the logical Handle too
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
EqualityKey key = jfh1.getEqualityKey();
assertSame(fh1.getEqualityKey(), key);
assertNotSame(fh1, jfh1);
assertEquals(2, key.size());
assertSame(jfh1, key.getLogicalFactHandle());
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class TruthMaintenanceTest method testStatedShadowLogicalThenLogicalOnly.
@Test(timeout = 10000)
public void testStatedShadowLogicalThenLogicalOnly() {
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");
FactHandle g1 = session.insert("go1");
session.fireAllRules();
// This removes the stated position, but it should still be logical now and exist
session.delete(fh1, FactHandle.State.STATED);
session.insert("go2");
session.fireAllRules();
// fh1 is invalid and no longer in the WM. However it's now reverted to it's Justified version and will still be there
assertFalse(fh1.isValid());
// Make sure f1 is still there, but logical only now
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
InternalFactHandle jfh1 = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem().get("f1").getLogicalFactHandle();
assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
assertSame(jfh1, session.getFactHandle("f1"));
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class MarshallingTest method testSerializeAdd3.
/*
* Works Fine if both the scenarios mentioned above are skipped.
*/
@Test
public void testSerializeAdd3() throws Exception {
// Create a rulebase, a session, and test it
Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1_0.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
kBase = SerializationHelper.serializeObject(kBase);
List results = new ArrayList();
KieSession session = kBase.newKieSession();
session.setGlobal("results", results);
InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie1 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
session.fireAllRules();
assertEquals(1, results.size());
assertEquals(stilton1.getObject(), results.get(0));
// serialize session and rulebase
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
// dynamic add pkgs
kpkgs = loadKnowledgePackages("../test_Dynamic3_0.drl");
kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
InternalFactHandle bob1 = (InternalFactHandle) session.insert(new Person("bob", 20));
InternalFactHandle bob2 = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(4, results.size());
assertEquals(stilton2.getObject(), results.get(1));
assertEquals(bob2.getObject(), results.get(2));
assertEquals(bob1.getObject(), results.get(3));
// serialize session and rulebase
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
// dynamic add pkgs
kpkgs = loadKnowledgePackages("../test_Dynamic1_2.drl");
kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
InternalFactHandle stilton3 = (InternalFactHandle) session.insert(new Cheese("stilton", 40));
InternalFactHandle brie3 = (InternalFactHandle) session.insert(new Cheese("brie", 40));
InternalFactHandle bob3 = (InternalFactHandle) session.insert(new Person("bob", 40));
InternalFactHandle bob4 = (InternalFactHandle) session.insert(new Person("bob", 40));
InternalFactHandle addr1 = (InternalFactHandle) session.insert(new Address("bangalore"));
InternalFactHandle addr2 = (InternalFactHandle) session.insert(new Address("India"));
session.fireAllRules();
assertEquals(9, results.size());
assertEquals(stilton3.getObject(), results.get(4));
assertEquals(bob4.getObject(), results.get(5));
assertEquals(bob3.getObject(), results.get(6));
assertEquals(addr2.getObject(), results.get(7));
assertEquals(addr1.getObject(), results.get(8));
// serialize session and rulebase
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
InternalFactHandle stilton4 = (InternalFactHandle) session.insert(new Cheese("stilton", 50));
InternalFactHandle brie4 = (InternalFactHandle) session.insert(new Cheese("brie", 50));
InternalFactHandle bob5 = (InternalFactHandle) session.insert(new Person("bob", 50));
InternalFactHandle bob6 = (InternalFactHandle) session.insert(new Person("bob", 50));
InternalFactHandle addr3 = (InternalFactHandle) session.insert(new Address("Tripura"));
InternalFactHandle addr4 = (InternalFactHandle) session.insert(new Address("Agartala"));
session.fireAllRules();
assertEquals(14, results.size());
assertEquals(stilton4.getObject(), results.get(9));
assertEquals(bob6.getObject(), results.get(10));
assertEquals(bob5.getObject(), results.get(11));
assertEquals(addr4.getObject(), results.get(12));
assertEquals(addr3.getObject(), results.get(13));
// serialize session and rulebase
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.dispose();
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class AddRemoveRulesTest method testRemoveRuleChangeFHFirstLeftTuple.
@Test
public void testRemoveRuleChangeFHFirstLeftTuple() {
final String[] rules = getRules1Pattern();
final KieSession kieSession = TestUtil.createSession(rules);
try {
final List resultsList = new ArrayList();
kieSession.setGlobal("list", resultsList);
TestUtil.insertFacts(kieSession, 3);
kieSession.fireAllRules();
Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME);
resultsList.clear();
TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE1_NAME);
kieSession.fireAllRules();
final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
assertNotNull(fh1.getFirstLeftTuple());
} finally {
kieSession.dispose();
}
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasMiddle.
@Test
public void testRemoveChildLeftTupleThatWasMiddle() {
final String[] rules = getRules3Pattern();
final KieSession kieSession = TestUtil.createSession(rules);
try {
final List resultsList = new ArrayList();
kieSession.setGlobal("list", resultsList);
TestUtil.insertFacts(kieSession, 3);
kieSession.fireAllRules();
Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME, TestUtil.RULE3_NAME);
resultsList.clear();
TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE2_NAME);
kieSession.fireAllRules();
final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
final LeftTuple lt = fh1.getFirstLeftTuple().getFirstChild();
assertSame(lt, fh1.getFirstLeftTuple().getLastChild());
assertEquals(1, lt.getTupleSink().getAssociatedRuleSize());
assertTrue(lt.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
final LeftTuple peer = lt.getPeer();
assertEquals(1, peer.getTupleSink().getAssociatedRuleSize());
assertTrue(peer.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE3_NAME)));
} finally {
kieSession.dispose();
}
}
Aggregations