Search in sources :

Example 1 with BayesInstance

use of org.drools.beliefs.bayes.BayesInstance 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 BayesInstance

use of org.drools.beliefs.bayes.BayesInstance in project drools by kiegroup.

the class EarthQuakeTest method testJoinCallsEvidence.

@Test
public void testJoinCallsEvidence() {
    BayesInstance bayesInstance = new BayesInstance(jTree);
    bayesInstance.setLikelyhood("JohnCalls", new double[] { 1.0, 0.0 });
    bayesInstance.globalUpdate();
    assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("JohnCalls").getDistribution()));
    assertArray(new double[] { 0.04, 0.96 }, scaleDouble(3, bayesInstance.marginalize("MaryCalls").getDistribution()));
    assertArray(new double[] { 0.016, 0.984 }, scaleDouble(3, bayesInstance.marginalize("Burglary").getDistribution()));
    assertArray(new double[] { 0.011, 0.989 }, scaleDouble(3, bayesInstance.marginalize("Earthquake").getDistribution()));
    assertArray(new double[] { 0.043, 0.957 }, scaleDouble(3, bayesInstance.marginalize("Alarm").getDistribution()));
}
Also used : BayesInstance(org.drools.beliefs.bayes.BayesInstance) Test(org.junit.Test)

Example 3 with BayesInstance

use of org.drools.beliefs.bayes.BayesInstance in project drools by kiegroup.

the class EarthQuakeTest method testEathQuakeEvidence.

@Test
public void testEathQuakeEvidence() {
    BayesInstance bayesInstance = new BayesInstance(jTree);
    bayesInstance.setLikelyhood("Earthquake", new double[] { 1.0, 0.0 });
    bayesInstance.globalUpdate();
    assertArray(new double[] { 0.297, 0.703 }, scaleDouble(3, bayesInstance.marginalize("JohnCalls").getDistribution()));
    assertArray(new double[] { 0.211, 0.789 }, scaleDouble(3, bayesInstance.marginalize("MaryCalls").getDistribution()));
    assertArray(new double[] { .001, 0.999 }, scaleDouble(3, bayesInstance.marginalize("Burglary").getDistribution()));
    assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("Earthquake").getDistribution()));
    assertArray(new double[] { 0.291, 0.709 }, scaleDouble(3, bayesInstance.marginalize("Alarm").getDistribution()));
}
Also used : BayesInstance(org.drools.beliefs.bayes.BayesInstance) Test(org.junit.Test)

Example 4 with BayesInstance

use of org.drools.beliefs.bayes.BayesInstance in project drools by kiegroup.

the class EarthQuakeTest method testEarthquakeAndJohnCallsEvidence.

@Test
public void testEarthquakeAndJohnCallsEvidence() {
    BayesInstance bayesInstance = new BayesInstance(jTree);
    bayesInstance.setLikelyhood("JohnCalls", new double[] { 1.0, 0.0 });
    bayesInstance.setLikelyhood("Earthquake", new double[] { 1.0, 0.0 });
    bayesInstance.globalUpdate();
    assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("JohnCalls").getDistribution()));
    assertArray(new double[] { 0.618, 0.382 }, scaleDouble(3, bayesInstance.marginalize("MaryCalls").getDistribution()));
    assertArray(new double[] { 0.003, 0.997 }, scaleDouble(3, bayesInstance.marginalize("Burglary").getDistribution()));
    assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("Earthquake").getDistribution()));
    assertArray(new double[] { 0.881, 0.119 }, scaleDouble(3, bayesInstance.marginalize("Alarm").getDistribution()));
}
Also used : BayesInstance(org.drools.beliefs.bayes.BayesInstance) Test(org.junit.Test)

Example 5 with BayesInstance

use of org.drools.beliefs.bayes.BayesInstance in project drools by kiegroup.

the class EarthQuakeTest method testAlarmEvidence.

@Test
public void testAlarmEvidence() {
    BayesInstance bayesInstance = new BayesInstance(jTree);
    bayesInstance.setLikelyhood("Alarm", new double[] { 1.0, 0.0 });
    bayesInstance.globalUpdate();
    assertArray(new double[] { 0.9, 0.1 }, scaleDouble(3, bayesInstance.marginalize("JohnCalls").getDistribution()));
    assertArray(new double[] { 0.7, 0.3 }, scaleDouble(3, bayesInstance.marginalize("MaryCalls").getDistribution()));
    assertArray(new double[] { 0.374, 0.626 }, scaleDouble(3, bayesInstance.marginalize("Burglary").getDistribution()));
    assertArray(new double[] { 0.231, 0.769 }, scaleDouble(3, bayesInstance.marginalize("Earthquake").getDistribution()));
    assertArray(new double[] { 1.0, 0.0 }, scaleDouble(3, bayesInstance.marginalize("Alarm").getDistribution()));
}
Also used : BayesInstance(org.drools.beliefs.bayes.BayesInstance) Test(org.junit.Test)

Aggregations

BayesInstance (org.drools.beliefs.bayes.BayesInstance)9 Test (org.junit.Test)7 JunctionTree (org.drools.beliefs.bayes.JunctionTree)4 JunctionTreeBuilder (org.drools.beliefs.bayes.JunctionTreeBuilder)4 JunctionTreeClique (org.drools.beliefs.bayes.JunctionTreeClique)3 BayesPackage (org.drools.beliefs.bayes.assembler.BayesPackage)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 Before (org.junit.Before)1 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)1 ResourceType (org.kie.api.io.ResourceType)1