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;
}
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()));
}
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()));
}
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()));
}
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()));
}
Aggregations