Search in sources :

Example 1 with BayesModeFactory

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

the class BayesBeliefSystemTest method testBayes.

@Test
public void testBayes() {
    String drl = "package org.drools.defeasible; " + "import " + Garden.class.getCanonicalName() + "; \n" + "import " + PropertyReference.class.getCanonicalName() + "; \n" + "global " + BayesModeFactory.class.getCanonicalName() + " bsFactory; \n" + "dialect 'mvel'; \n" + " " + "rule rule1 when " + "        String( this == 'rule1') \n" + "    g : Garden()" + "then " + "    System.out.println(\"rule 1\"); \n" + "    insertLogical( new PropertyReference(g, 'cloudy'), bsFactory.create( new double[] {1.0,0.0} ) ); \n " + "end " + "rule rule2 when " + "        String( this == 'rule2') \n" + "    g : Garden()" + "then " + "    System.out.println(\"rule2\"); \n" + "    insertLogical( new PropertyReference(g, 'sprinkler'), bsFactory.create( new double[] {1.0,0.0} ) ); \n " + "end " + "rule rule3 when " + "        String( this == 'rule3') \n" + "    g : Garden()" + "then " + "    System.out.println(\"rule3\"); \n" + "    insertLogical( new PropertyReference(g, 'sprinkler'), bsFactory.create( new double[] {1.0,0.0} ) ); \n " + "end " + "rule rule4 when " + "        String( this == 'rule4') \n" + "    g : Garden()" + "then " + "    System.out.println(\"rule4\"); \n" + "    insertLogical( new PropertyReference(g, 'sprinkler'), bsFactory.create( new double[] {0.0,1.0} ) ); \n " + "end " + "\n";
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) getSessionFromString(drl);
    NamedEntryPoint ep = (NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId());
    BayesBeliefSystem bayesBeliefSystem = new BayesBeliefSystem(ep, ep.getTruthMaintenanceSystem());
    BayesModeFactoryImpl bayesModeFactory = new BayesModeFactoryImpl(bayesBeliefSystem);
    ksession.setGlobal("bsFactory", bayesModeFactory);
    BayesRuntime bayesRuntime = ksession.getKieRuntime(BayesRuntime.class);
    BayesInstance<Garden> instance = bayesRuntime.createInstance(Garden.class);
    assertNotNull(instance);
    assertTrue(instance.isDecided());
    instance.globalUpdate();
    Garden garden = instance.marginalize();
    assertTrue(garden.isWetGrass());
    FactHandle fh = ksession.insert(garden);
    FactHandle fh1 = ksession.insert("rule1");
    ksession.fireAllRules();
    assertTrue(instance.isDecided());
    // rule1 has added evidence, update the bayes network
    instance.globalUpdate();
    garden = instance.marginalize();
    // grass was wet before rule1 and continues to be wet
    assertTrue(garden.isWetGrass());
    // applies 2 logical insertions
    FactHandle fh2 = ksession.insert("rule2");
    ksession.fireAllRules();
    assertTrue(instance.isDecided());
    instance.globalUpdate();
    garden = instance.marginalize();
    // new evidence means grass is no longer wet
    assertFalse(garden.isWetGrass());
    // adds an additional support for the sprinkler, belief set of 2
    FactHandle fh3 = ksession.insert("rule3");
    ksession.fireAllRules();
    assertTrue(instance.isDecided());
    instance.globalUpdate();
    garden = instance.marginalize();
    // nothing has changed
    assertFalse(garden.isWetGrass());
    // rule4 introduces a conflict, and the BayesFact becomes undecided
    FactHandle fh4 = ksession.insert("rule4");
    ksession.fireAllRules();
    assertFalse(instance.isDecided());
    try {
        instance.globalUpdate();
        fail("The BayesFact is undecided, it should throw an exception, as it cannot be updated.");
    } catch (Exception e) {
    // this should fail
    }
    // the conflict is resolved, so it should be decided again
    ksession.delete(fh4);
    ksession.fireAllRules();
    assertTrue(instance.isDecided());
    instance.globalUpdate();
    garden = instance.marginalize();
    // back to grass is not wet
    assertFalse(garden.isWetGrass());
    // takes the sprinkler belief set back to 1
    ksession.delete(fh2);
    ksession.fireAllRules();
    instance.globalUpdate();
    garden = instance.marginalize();
    // still grass is not wet
    assertFalse(garden.isWetGrass());
    // no sprinkler support now
    ksession.delete(fh3);
    ksession.fireAllRules();
    instance.globalUpdate();
    garden = instance.marginalize();
    // grass is wet again
    assertTrue(garden.isWetGrass());
}
Also used : BayesBeliefSystem(org.drools.beliefs.bayes.BayesBeliefSystem) FactHandle(org.kie.api.runtime.rule.FactHandle) BayesModeFactory(org.drools.beliefs.bayes.BayesModeFactory) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) BayesModeFactoryImpl(org.drools.beliefs.bayes.BayesModeFactoryImpl) BayesRuntime(org.drools.beliefs.bayes.runtime.BayesRuntime) Test(org.junit.Test)

Aggregations

BayesBeliefSystem (org.drools.beliefs.bayes.BayesBeliefSystem)1 BayesModeFactory (org.drools.beliefs.bayes.BayesModeFactory)1 BayesModeFactoryImpl (org.drools.beliefs.bayes.BayesModeFactoryImpl)1 BayesRuntime (org.drools.beliefs.bayes.runtime.BayesRuntime)1 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)1 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)1 Test (org.junit.Test)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1