use of org.drools.beliefs.bayes.runtime.BayesRuntime 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, ep.getTruthMaintenanceSystem());
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.beliefs.bayes.runtime.BayesRuntime in project drools by kiegroup.
the class BayesRuntimeTest method testBayesRuntimeManager.
@Test
public void testBayesRuntimeManager() throws Exception {
KnowledgeBuilderImpl kbuilder = new KnowledgeBuilderImpl();
kbuilder.add(ResourceFactory.newClassPathResource("Garden.xmlbif", AssemblerTest.class), ResourceType.BAYES);
InternalKnowledgeBase kbase = getKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kbase.newKieSession();
BayesRuntime bayesRuntime = ksession.getKieRuntime(BayesRuntime.class);
BayesInstance<Garden> instance = bayesRuntime.createInstance(Garden.class);
assertNotNull(instance);
}
Aggregations