use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class BayesBeliefSystemTest method testBayes.
@Test
public void testBayes() {
String drl = "package org.drools.defeasible; " + "import " + Garden.class.getCanonicalName() + "; \n" + "import " + PropertyReference.class.getCanonicalName() + "; \n" + "global " + BayesModeFactory.class.getCanonicalName() + " bsFactory; \n" + "dialect 'mvel'; \n" + " " + "rule rule1 when " + " String( this == 'rule1') \n" + " g : Garden()" + "then " + " System.out.println(\"rule 1\"); \n" + " insertLogical( new PropertyReference(g, 'cloudy'), bsFactory.create( new double[] {1.0,0.0} ) ); \n " + "end " + "rule rule2 when " + " String( this == 'rule2') \n" + " g : Garden()" + "then " + " System.out.println(\"rule2\"); \n" + " insertLogical( new PropertyReference(g, 'sprinkler'), bsFactory.create( new double[] {1.0,0.0} ) ); \n " + "end " + "rule rule3 when " + " String( this == 'rule3') \n" + " g : Garden()" + "then " + " System.out.println(\"rule3\"); \n" + " insertLogical( new PropertyReference(g, 'sprinkler'), bsFactory.create( new double[] {1.0,0.0} ) ); \n " + "end " + "rule rule4 when " + " String( this == 'rule4') \n" + " g : Garden()" + "then " + " System.out.println(\"rule4\"); \n" + " insertLogical( new PropertyReference(g, 'sprinkler'), bsFactory.create( new double[] {0.0,1.0} ) ); \n " + "end " + "\n";
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) getSessionFromString(drl);
NamedEntryPoint ep = (NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId());
BayesBeliefSystem bayesBeliefSystem = new BayesBeliefSystem(ep, TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep));
BayesModeFactoryImpl bayesModeFactory = new BayesModeFactoryImpl(bayesBeliefSystem);
ksession.setGlobal("bsFactory", bayesModeFactory);
BayesRuntime bayesRuntime = ksession.getKieRuntime(BayesRuntime.class);
BayesInstance<Garden> instance = bayesRuntime.createInstance(Garden.class);
assertNotNull(instance);
assertTrue(instance.isDecided());
instance.globalUpdate();
Garden garden = instance.marginalize();
assertTrue(garden.isWetGrass());
FactHandle fh = ksession.insert(garden);
FactHandle fh1 = ksession.insert("rule1");
ksession.fireAllRules();
assertTrue(instance.isDecided());
// rule1 has added evidence, update the bayes network
instance.globalUpdate();
garden = instance.marginalize();
// grass was wet before rule1 and continues to be wet
assertTrue(garden.isWetGrass());
// applies 2 logical insertions
FactHandle fh2 = ksession.insert("rule2");
ksession.fireAllRules();
assertTrue(instance.isDecided());
instance.globalUpdate();
garden = instance.marginalize();
// new evidence means grass is no longer wet
assertFalse(garden.isWetGrass());
// adds an additional support for the sprinkler, belief set of 2
FactHandle fh3 = ksession.insert("rule3");
ksession.fireAllRules();
assertTrue(instance.isDecided());
instance.globalUpdate();
garden = instance.marginalize();
// nothing has changed
assertFalse(garden.isWetGrass());
// rule4 introduces a conflict, and the BayesFact becomes undecided
FactHandle fh4 = ksession.insert("rule4");
ksession.fireAllRules();
assertFalse(instance.isDecided());
try {
instance.globalUpdate();
fail("The BayesFact is undecided, it should throw an exception, as it cannot be updated.");
} catch (Exception e) {
// this should fail
}
// the conflict is resolved, so it should be decided again
ksession.delete(fh4);
ksession.fireAllRules();
assertTrue(instance.isDecided());
instance.globalUpdate();
garden = instance.marginalize();
// back to grass is not wet
assertFalse(garden.isWetGrass());
// takes the sprinkler belief set back to 1
ksession.delete(fh2);
ksession.fireAllRules();
instance.globalUpdate();
garden = instance.marginalize();
// still grass is not wet
assertFalse(garden.isWetGrass());
// no sprinkler support now
ksession.delete(fh3);
ksession.fireAllRules();
instance.globalUpdate();
garden = instance.marginalize();
// grass is wet again
assertTrue(garden.isWetGrass());
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class RuleUnlinkingTest method testRuleSegmentLinking.
@Test
public void testRuleSegmentLinking() {
setUp(JOIN_NODE);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase);
BetaMemory bm = null;
List<PathMemory> list;
PathMemory rtn1Rs = (PathMemory) wm.getNodeMemory(rtn1);
PathMemory rtn2Rs = (PathMemory) wm.getNodeMemory(rtn2);
PathMemory rtn3Rs = (PathMemory) wm.getNodeMemory(rtn3);
DefaultFactHandle f1 = (DefaultFactHandle) wm.insert("test1");
SegmentUtilities.createSegmentMemory(liaNode, wm);
liaNode.assertObject(f1, context, wm);
n1.assertObject(f1, context, wm);
n3.assertObject(f1, context, wm);
n4.assertObject(f1, context, wm);
n8.assertObject(f1, context, wm);
assertFalse(rtn1Rs.isRuleLinked());
assertFalse(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
// Link in Rule1
bm = (BetaMemory) wm.getNodeMemory(n2);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
DefaultFactHandle f2 = (DefaultFactHandle) wm.insert("test2");
n2.assertObject(f2, context, wm);
assertTrue(bm.getSegmentMemory().isSegmentLinked());
assertTrue(rtn1Rs.isRuleLinked());
assertFalse(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
// Link in Rule2
bm = (BetaMemory) wm.getNodeMemory(n5);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n5.assertObject(f1, context, wm);
assertTrue(bm.getSegmentMemory().isSegmentLinked());
assertTrue(rtn1Rs.isRuleLinked());
assertTrue(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
// Link in Rule3
n6.assertObject(f1, context, wm);
n7.assertObject(f1, context, wm);
assertTrue(bm.getSegmentMemory().isSegmentLinked());
assertTrue(rtn1Rs.isRuleLinked());
assertTrue(rtn2Rs.isRuleLinked());
assertTrue(rtn3Rs.isRuleLinked());
// retract n2, should unlink all rules
n2.retractRightTuple(f2.getFirstRightTuple(), context, wm);
assertFalse(rtn1Rs.isRuleLinked());
assertFalse(rtn2Rs.isRuleLinked());
assertFalse(rtn3Rs.isRuleLinked());
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class NodeSegmentUnlinkingTest method testLiaNodeInitialisation.
@Test
public void testLiaNodeInitialisation() {
setUp(JOIN_NODE);
// Initialise from lian
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
SegmentUtilities.createSegmentMemory(liaNode, ksession);
liaNode.assertObject((InternalFactHandle) ksession.insert("str"), context, ksession);
LiaNodeMemory liaMem = (LiaNodeMemory) ksession.getNodeMemory(liaNode);
assertEquals(1, liaMem.getNodePosMaskBit());
assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());
BetaMemory bm1 = (BetaMemory) ksession.getNodeMemory(n1);
assertEquals(2, bm1.getNodePosMaskBit());
assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());
// Initialise from n1
kBase = KnowledgeBaseFactory.newKnowledgeBase();
ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
n1.assertObject((InternalFactHandle) ksession.insert("str"), context, ksession);
liaMem = (LiaNodeMemory) ksession.getNodeMemory(liaNode);
assertEquals(1, liaMem.getNodePosMaskBit());
assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());
bm1 = (BetaMemory) ksession.getNodeMemory(n1);
assertEquals(2, bm1.getNodePosMaskBit());
assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class NodeSegmentUnlinkingTest method testAllLinkedInWithExistsNodesOnly.
@Test
public void testAllLinkedInWithExistsNodesOnly() {
setUp(EXISTS_NODE);
// make sure it created ExistsNodes
assertEquals(ExistsNode.class, n3.getClass());
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
DefaultFactHandle f1 = (DefaultFactHandle) ksession.insert("test1");
n3.assertObject(f1, context, ksession);
BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n3);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n4.assertObject(f1, context, ksession);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n5.assertObject(f1, context, ksession);
assertFalse(bm.getSegmentMemory().isSegmentLinked());
n6.assertObject(f1, context, ksession);
// only after all 4 nodes are populated, is the segment linked in
assertTrue(bm.getSegmentMemory().isSegmentLinked());
}
use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.
the class NodeSegmentUnlinkingTest method testLiaNodeLinking.
@Test
public void testLiaNodeLinking() {
setUp(JOIN_NODE);
// Initialise from lian
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
SegmentUtilities.createSegmentMemory(liaNode, ksession);
InternalFactHandle fh1 = (InternalFactHandle) ksession.insert("str1");
n1.assertObject(fh1, context, ksession);
LiaNodeMemory liaMem = (LiaNodeMemory) ksession.getNodeMemory(liaNode);
assertEquals(1, liaMem.getNodePosMaskBit());
assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());
BetaMemory bm1 = (BetaMemory) ksession.getNodeMemory(n1);
assertEquals(2, bm1.getNodePosMaskBit());
assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());
// still unlinked
assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
// now linked
InternalFactHandle fh2 = (InternalFactHandle) ksession.insert("str2");
liaNode.assertObject(fh2, context, ksession);
assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
// test unlink after one retract
liaNode.retractLeftTuple(fh2.getFirstLeftTuple(), context, ksession);
assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
// check counter, after multiple asserts
InternalFactHandle fh3 = (InternalFactHandle) ksession.insert("str3");
InternalFactHandle fh4 = (InternalFactHandle) ksession.insert("str4");
liaNode.assertObject(fh3, context, ksession);
liaNode.assertObject(fh4, context, ksession);
assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
liaNode.retractLeftTuple(fh3.getFirstLeftTuple(), context, ksession);
assertTrue(liaMem.getSegmentMemory().isSegmentLinked());
liaNode.retractLeftTuple(fh4.getFirstLeftTuple(), context, ksession);
assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
}
Aggregations