Search in sources :

Example 1 with JunctionTree

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;
}
Also used : BayesPackage(org.drools.beliefs.bayes.assembler.BayesPackage) BayesInstance(org.drools.beliefs.bayes.BayesInstance) JunctionTree(org.drools.beliefs.bayes.JunctionTree) ResourceType(org.kie.api.io.ResourceType) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 2 with JunctionTree

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()));
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) BayesInstance(org.drools.beliefs.bayes.BayesInstance) JunctionTreeClique(org.drools.beliefs.bayes.JunctionTreeClique) JunctionTree(org.drools.beliefs.bayes.JunctionTree) Test(org.junit.Test)

Example 3 with JunctionTree

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()));
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) BayesInstance(org.drools.beliefs.bayes.BayesInstance) JunctionTreeClique(org.drools.beliefs.bayes.JunctionTreeClique) JunctionTree(org.drools.beliefs.bayes.JunctionTree) Test(org.junit.Test)

Example 4 with JunctionTree

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()));
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) BayesInstance(org.drools.beliefs.bayes.BayesInstance) JunctionTreeClique(org.drools.beliefs.bayes.JunctionTreeClique) JunctionTree(org.drools.beliefs.bayes.JunctionTree) Test(org.junit.Test)

Example 5 with JunctionTree

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);
}
Also used : BayesPackage(org.drools.beliefs.bayes.assembler.BayesPackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) JunctionTree(org.drools.beliefs.bayes.JunctionTree) ResourceType(org.kie.api.io.ResourceType) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

JunctionTree (org.drools.beliefs.bayes.JunctionTree)5 BayesInstance (org.drools.beliefs.bayes.BayesInstance)4 Test (org.junit.Test)4 JunctionTreeBuilder (org.drools.beliefs.bayes.JunctionTreeBuilder)3 JunctionTreeClique (org.drools.beliefs.bayes.JunctionTreeClique)3 BayesPackage (org.drools.beliefs.bayes.assembler.BayesPackage)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)2 ResourceType (org.kie.api.io.ResourceType)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1