Search in sources :

Example 1 with BayesNetwork

use of org.drools.beliefs.bayes.BayesNetwork 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 2 with BayesNetwork

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

the class XmlBifParser method buildBayesNetwork.

public static BayesNetwork buildBayesNetwork(Bif bif) {
    String name = bif.getNetwork().getName();
    String packageName = "default";
    List<String> props = bif.getNetwork().getProperties();
    if (props != null) {
        for (String prop : props) {
            prop = prop.trim();
            if (prop.startsWith("package")) {
                packageName = prop.substring(prop.indexOf('=') + 1).trim();
            }
        }
    }
    BayesNetwork graph = new BayesNetwork(name, packageName);
    Map<String, GraphNode<BayesVariable>> map = new HashMap<String, GraphNode<BayesVariable>>();
    for (Definition def : bif.getNetwork().getDefinitions()) {
        GraphNode<BayesVariable> node = graph.addNode();
        BayesVariable var = buildVariable(def, bif.getNetwork(), node.getId());
        node.setContent(var);
        map.put(var.getName(), node);
    }
    for (Entry<String, GraphNode<BayesVariable>> entry : map.entrySet()) {
        GraphNode<BayesVariable> node = entry.getValue();
        BayesVariable var = node.getContent();
        if (var.getGiven() != null && var.getGiven().length > 0) {
            for (String given : var.getGiven()) {
                GraphNode<BayesVariable> givenNode = map.get(given);
                EdgeImpl e = new EdgeImpl();
                e.setOutGraphNode(givenNode);
                e.setInGraphNode(node);
            }
        }
    }
    return graph;
}
Also used : BayesVariable(org.drools.beliefs.bayes.BayesVariable) HashMap(java.util.HashMap) EdgeImpl(org.drools.beliefs.graph.impl.EdgeImpl) GraphNode(org.drools.beliefs.graph.GraphNode) BayesNetwork(org.drools.beliefs.bayes.BayesNetwork)

Example 3 with BayesNetwork

use of org.drools.beliefs.bayes.BayesNetwork 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 BayesVariable (org.drools.beliefs.bayes.BayesVariable)2 Bif (org.drools.beliefs.bayes.model.Bif)2 GraphNode (org.drools.beliefs.graph.GraphNode)2 HashMap (java.util.HashMap)1 JunctionTreeBuilder (org.drools.beliefs.bayes.JunctionTreeBuilder)1 EdgeImpl (org.drools.beliefs.graph.impl.EdgeImpl)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 Test (org.junit.Test)1 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)1 ResourceType (org.kie.api.io.ResourceType)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1