use of org.drools.beliefs.bayes.JunctionTree in project drools by kiegroup.
the class BayesRuntimeImpl method createInstance.
// @Override
// public BayesInstance createBayesFact(Class cls) {
// // using the two-tone pattern, to ensure only one is created
// BayesInstance instance = instances.get( cls.getName() );
// if ( instance == null ) {
// instance = createInstance(cls);
// }
//
// return instance;
// }
public BayesInstance createInstance(Class cls) {
// synchronised using the two-tone pattern, to ensure only one is created
// BayesInstance instance = instances.get( cls.getName() );
// if ( instance != null ) {
// return instance;
// }
InternalKnowledgePackage kpkg = (InternalKnowledgePackage) runtime.getKieBase().getKiePackage(cls.getPackage().getName());
Map<ResourceType, ResourceTypePackage> map = kpkg.getResourceTypePackages();
BayesPackage bayesPkg = (BayesPackage) map.get(ResourceType.BAYES);
JunctionTree jtree = bayesPkg.getJunctionTree(cls.getSimpleName());
BayesInstance instance = new BayesInstance(jtree, cls);
return instance;
}
use of org.drools.beliefs.bayes.JunctionTree in project drools by kiegroup.
the class SprinkerTest method testNoEvidence.
@Test
public void testNoEvidence() {
JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
JunctionTree jTree = jtBuilder.build();
JunctionTreeClique jtNode = jTree.getRoot();
BayesInstance bayesInstance = new BayesInstance(jTree);
bayesInstance.globalUpdate();
assertArray(new double[] { 0.5, 0.5 }, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution()));
assertArray(new double[] { 0.5, 0.5 }, scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution()));
assertArray(new double[] { 0.7, 0.3 }, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()));
assertArray(new double[] { 0.353, 0.647 }, scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution()));
}
use of org.drools.beliefs.bayes.JunctionTree in project drools by kiegroup.
the class SprinkerTest method testGrassWetEvidence.
@Test
public void testGrassWetEvidence() {
JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
JunctionTree jTree = jtBuilder.build();
JunctionTreeClique jtNode = jTree.getRoot();
BayesInstance bayesInstance = new BayesInstance(jTree);
bayesInstance.setLikelyhood("WetGrass", new double[] { 1.0, 0.0 });
bayesInstance.globalUpdate();
assertArray(new double[] { 0.639, 0.361 }, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution()));
assertArray(new double[] { 0.881, 0.119 }, scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution()));
assertArray(new double[] { 0.938, 0.062 }, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()));
assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution()));
}
use of org.drools.beliefs.bayes.JunctionTree in project drools by kiegroup.
the class SprinkerTest method testSprinklerEvidence.
@Test
public void testSprinklerEvidence() {
JunctionTreeBuilder jtBuilder = new JunctionTreeBuilder(graph);
JunctionTree jTree = jtBuilder.build();
JunctionTreeClique jtNode = jTree.getRoot();
BayesInstance bayesInstance = new BayesInstance(jTree);
bayesInstance.setLikelyhood("Sprinkler", new double[] { 1.0, 0.0 });
bayesInstance.setLikelyhood("Cloudy", new double[] { 1.0, 0.0 });
bayesInstance.globalUpdate();
assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("Cloudy").getDistribution()));
assertArray(new double[] { 0.8, 0.2 }, scaleDouble(3, bayesInstance.marginalize("Rain").getDistribution()));
assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("Sprinkler").getDistribution()));
assertArray(new double[] { 0.82, 0.18 }, scaleDouble(3, bayesInstance.marginalize("WetGrass").getDistribution()));
}
use of org.drools.beliefs.bayes.JunctionTree in project drools by kiegroup.
the class WeaverTest method testBayesPackageWeaving.
@Test
public void testBayesPackageWeaving() throws Exception {
KnowledgeBuilderImpl kbuilder = new KnowledgeBuilderImpl();
kbuilder.add(ResourceFactory.newClassPathResource("Garden.xmlbif", AssemblerTest.class), ResourceType.BAYES);
InternalKnowledgeBase kbase = getKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
InternalKnowledgePackage kpkg = (InternalKnowledgePackage) kbase.getKiePackage("org.drools.beliefs.bayes.integration");
Map<ResourceType, ResourceTypePackage> map = kpkg.getResourceTypePackages();
BayesPackage existing = (BayesPackage) map.get(ResourceType.BAYES);
JunctionTree jtree = existing.getJunctionTree("Garden");
assertNotNull(jtree);
}
Aggregations