Search in sources :

Example 1 with Bif

use of org.drools.beliefs.bayes.model.Bif in project drools by kiegroup.

the class ParserTest method testSprinklerLoadBif.

@Test
public void testSprinklerLoadBif() {
    Bif bif = (Bif) XmlBifParser.loadBif(ParserTest.class.getResource("Garden.xmlbif"));
    Network network = bif.getNetwork();
    assertEquals("Garden", network.getName());
    assertEquals("package = org.drools.beliefs.bayes.integration", network.getProperties().get(0));
    Map<String, Variable> varMap = varToMap(network.getVariables());
    assertEquals(4, varMap.size());
    Variable var = varMap.get("WetGrass");
    assertEquals("WetGrass", var.getName());
    assertEquals(2, var.getOutComes().size());
    assertEquals(var.getOutComes(), Arrays.asList(new String[] { "false", "true" }));
    assertEquals("position = (0,10)", var.getProperties().get(0));
    var = varMap.get("Cloudy");
    assertEquals("Cloudy", var.getName());
    assertEquals(2, var.getOutComes().size());
    assertEquals(var.getOutComes(), Arrays.asList(new String[] { "false", "true" }));
    assertEquals("position = (0,-10)", var.getProperties().get(0));
    var = varMap.get("Sprinkler");
    assertEquals("Sprinkler", var.getName());
    assertEquals(2, var.getOutComes().size());
    assertEquals(var.getOutComes(), Arrays.asList(new String[] { "false", "true" }));
    assertEquals("position = (13,0)", var.getProperties().get(0));
    var = varMap.get("Rain");
    assertEquals("Rain", var.getName());
    assertEquals(2, var.getOutComes().size());
    assertEquals(var.getOutComes(), Arrays.asList(new String[] { "false", "true" }));
    assertEquals("position = (-12,0)", var.getProperties().get(0));
    Map<String, Definition> defMap = defToMap(network.getDefinitions());
    assertEquals(4, defMap.size());
    Definition def = defMap.get("WetGrass");
    assertEquals("WetGrass", def.getName());
    assertEquals(2, def.getGiven().size());
    assertEquals(def.getGiven(), Arrays.asList(new String[] { "Sprinkler", "Rain" }));
    assertEquals("1.0 0.0 0.1 0.9 0.1 0.9 0.01 0.99", def.getProbabilities());
    def = defMap.get("Cloudy");
    assertEquals("Cloudy", def.getName());
    assertNull(def.getGiven());
    assertEquals("0.5 0.5", def.getProbabilities().trim());
    def = defMap.get("Sprinkler");
    assertEquals("Sprinkler", def.getName());
    assertEquals(1, def.getGiven().size());
    assertEquals("Cloudy", def.getGiven().get(0));
    assertEquals("0.5 0.5 0.9 0.1", def.getProbabilities().trim());
    def = defMap.get("Rain");
    assertEquals("Rain", def.getName());
    assertNull(def.getGiven());
    assertEquals("0.5 0.5", def.getProbabilities().trim());
}
Also used : Variable(org.drools.beliefs.bayes.model.Variable) BayesVariable(org.drools.beliefs.bayes.BayesVariable) BayesNetwork(org.drools.beliefs.bayes.BayesNetwork) Network(org.drools.beliefs.bayes.model.Network) Definition(org.drools.beliefs.bayes.model.Definition) Bif(org.drools.beliefs.bayes.model.Bif) Test(org.junit.Test)

Example 2 with Bif

use of org.drools.beliefs.bayes.model.Bif in project drools by kiegroup.

the class BayesAssemblerService method addResource.

@Override
public void addResource(Object kbuilder, Resource resource, ResourceType type, ResourceConfiguration configuration) throws Exception {
    BayesNetwork network;
    JunctionTreeBuilder builder;
    KnowledgeBuilder kb = (KnowledgeBuilder) kbuilder;
    Bif bif = XmlBifParser.loadBif(resource, kb.getErrors());
    if (bif == null) {
        return;
    }
    try {
        network = XmlBifParser.buildBayesNetwork(bif);
    } catch (Exception e) {
        kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
        return;
    }
    try {
        builder = new JunctionTreeBuilder(network);
    } catch (Exception e) {
        kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to build Junction Tree:\n" + e.toString()));
        return;
    }
    KnowledgeBuilderImpl kbuilderImpl = (KnowledgeBuilderImpl) kbuilder;
    PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(new PackageDescr(network.getPackageName()));
    InternalKnowledgePackage kpkgs = pkgReg.getPackage();
    Map<ResourceType, ResourceTypePackage> rpkg = kpkgs.getResourceTypePackages();
    BayesPackage bpkg = (BayesPackage) rpkg.get(ResourceType.BAYES);
    if (bpkg == null) {
        bpkg = new BayesPackage();
        rpkg.put(ResourceType.BAYES, bpkg);
    }
    bpkg.addJunctionTree(network.getName(), builder.build());
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) ResourceType(org.kie.api.io.ResourceType) BayesNetwork(org.drools.beliefs.bayes.BayesNetwork) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Bif(org.drools.beliefs.bayes.model.Bif) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 3 with Bif

use of org.drools.beliefs.bayes.model.Bif in project drools by kiegroup.

the class ParserTest method testSprinklerBuildBayesNework.

@Test
public void testSprinklerBuildBayesNework() {
    Bif bif = (Bif) XmlBifParser.loadBif(ParserTest.class.getResource("Garden.xmlbif"));
    BayesNetwork network = XmlBifParser.buildBayesNetwork(bif);
    Map<String, GraphNode<BayesVariable>> map = nodeToMap(network);
    GraphNode<BayesVariable> node = map.get("WetGrass");
    BayesVariable wetGrass = node.getContent();
    assertEquals(Arrays.asList(new String[] { "false", "true" }), Arrays.asList(wetGrass.getOutcomes()));
    assertEquals(2, wetGrass.getGiven().length);
    assertEquals(Arrays.asList(wetGrass.getGiven()), Arrays.asList(new String[] { "Sprinkler", "Rain" }));
    assertTrue(Arrays.deepEquals(new double[][] { { 1.0, 0.0 }, { 0.1, 0.9 }, { 0.1, 0.9 }, { 0.01, 0.99 } }, wetGrass.getProbabilityTable()));
    node = map.get("Sprinkler");
    BayesVariable sprinkler = node.getContent();
    assertEquals(Arrays.asList(new String[] { "false", "true" }), Arrays.asList(sprinkler.getOutcomes()));
    assertEquals(1, sprinkler.getGiven().length);
    assertEquals("Cloudy", sprinkler.getGiven()[0]);
    assertTrue(Arrays.deepEquals(new double[][] { { 0.5, 0.5 }, { 0.9, 0.1 } }, sprinkler.getProbabilityTable()));
    node = map.get("Cloudy");
    BayesVariable cloudy = node.getContent();
    assertEquals(Arrays.asList(new String[] { "false", "true" }), Arrays.asList(cloudy.getOutcomes()));
    assertEquals(0, cloudy.getGiven().length);
    assertTrue(Arrays.deepEquals(new double[][] { { 0.5, 0.5 } }, cloudy.getProbabilityTable()));
    node = map.get("Rain");
    BayesVariable rain = node.getContent();
    assertEquals(Arrays.asList(new String[] { "false", "true" }), Arrays.asList(rain.getOutcomes()));
    assertEquals(0, rain.getGiven().length);
    assertTrue(Arrays.deepEquals(new double[][] { { 0.5, 0.5 } }, rain.getProbabilityTable()));
}
Also used : BayesVariable(org.drools.beliefs.bayes.BayesVariable) GraphNode(org.drools.beliefs.graph.GraphNode) BayesNetwork(org.drools.beliefs.bayes.BayesNetwork) Bif(org.drools.beliefs.bayes.model.Bif) Test(org.junit.Test)

Aggregations

BayesNetwork (org.drools.beliefs.bayes.BayesNetwork)3 Bif (org.drools.beliefs.bayes.model.Bif)3 BayesVariable (org.drools.beliefs.bayes.BayesVariable)2 Test (org.junit.Test)2 JunctionTreeBuilder (org.drools.beliefs.bayes.JunctionTreeBuilder)1 Definition (org.drools.beliefs.bayes.model.Definition)1 Network (org.drools.beliefs.bayes.model.Network)1 Variable (org.drools.beliefs.bayes.model.Variable)1 GraphNode (org.drools.beliefs.graph.GraphNode)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)1 ResourceType (org.kie.api.io.ResourceType)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1