Search in sources :

Example 1 with DefeasibleBeliefSet

use of org.drools.core.beliefsystem.defeasible.DefeasibleBeliefSet in project drools by kiegroup.

the class DefeasibilityTest method testWMStatusOnNegativeDefeat.

@Test(timeout = 10000)
public void testWMStatusOnNegativeDefeat() {
    String droolsSource = "package org.drools.tms.test; " + "global java.util.List posList;" + "global java.util.List negList;" + "declare Bar value : int @key end " + "rule Top " + "@Defeasible " + "@Defeats( 'Sub' ) " + "when " + "   $i : Integer( this < 10 ) " + "then " + "   insertLogical( new Bar( $i ) ); " + "end " + "rule Sub " + "@Defeasible " + "when " + "   $i : Integer() " + "then " + "   insertLogical( new Bar( $i ), $i > 10 ? 'pos' : 'neg' ); " + "end " + "rule Sup " + "@Defeasible " + "@Defeats( 'Sub' ) " + "when " + "   $i : Integer( this > 10 ) " + "then " + "   insertLogical( new Bar( $i ), 'neg' ); " + "end " + "rule React_Pos " + "when " + "   $b : Bar() " + "then " + "   posList.add( $b ); " + "   System.out.println( ' ++++ ' + $b ); " + "end " + "rule React_Neg " + "when " + "   $b : Bar( _.neg )" + "then " + "   negList.add( $b ); " + "   System.out.println( ' ---- ' + $b ); " + "end " + "";
    KieSession session = getSessionFromString(droolsSource);
    List posList = new ArrayList();
    List negList = new ArrayList();
    session.setGlobal("posList", posList);
    session.setGlobal("negList", negList);
    session.insert(20);
    session.insert(5);
    session.fireAllRules();
    assertEquals(1, posList.size());
    assertEquals(1, negList.size());
    Object posBar = posList.get(0);
    InternalFactHandle posHandle = (InternalFactHandle) session.getFactHandle(posBar);
    DefeasibleBeliefSet dbs = (DefeasibleBeliefSet) posHandle.getEqualityKey().getBeliefSet();
    assertEquals(1, dbs.size());
    assertFalse(dbs.isNegated());
    assertTrue(dbs.isDecided());
    assertTrue(dbs.isPositive());
    assertSame(posHandle, dbs.getFactHandle());
    assertFalse(posHandle.isNegated());
    assertTrue(dbs.isDefeasiblyPosProveable());
    assertTrue(session.getObjects().contains(posBar));
    Object negBar = negList.get(0);
    InternalFactHandle negHandle = (InternalFactHandle) getNegativeHandles(session).get(0);
    dbs = (DefeasibleBeliefSet) negHandle.getEqualityKey().getBeliefSet();
    assertEquals(1, dbs.size());
    assertFalse(dbs.isPositive());
    assertTrue(dbs.isDecided());
    assertTrue(dbs.isNegated());
    assertSame(negHandle, dbs.getFactHandle());
    assertTrue(negHandle.isNegated());
    assertTrue(dbs.isDefeasiblyNegProveable());
    assertTrue(session.getObjects().contains(negBar));
}
Also used : DefeasibleBeliefSet(org.drools.core.beliefsystem.defeasible.DefeasibleBeliefSet) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 2 with DefeasibleBeliefSet

use of org.drools.core.beliefsystem.defeasible.DefeasibleBeliefSet in project drools by kiegroup.

the class DefeasibilityTest method testManyDefeasibles.

@Test(timeout = 10000)
public void testManyDefeasibles() {
    String drl = "package org.drools.defeasible; " + "declare Fact " + "     fact: String @key " + "end " + " " + "rule init " + "     when " + "     then " + "         insert( new Fact( 'one' ) ); " + "         insert( new Fact( 'two' ) ); " + "         insert( new Fact( 'two' ) ); " + "end " + " " + "rule rule1 " + "     @Defeasible " + "     enabled true " + "     when " + "         Fact( \"one\"; ) " + "     then " + "         System.out.println(\"one causes wibble\"); " + "         insertLogical( new Fact( \"wibble\") ); " + "end " + " " + "rule rule2 " + "     @Defeasible " + "     when " + "         Fact( \"two\"; ) " + "     then " + "         System.out.println(\"two causes wibble\"); " + "         insertLogical( new Fact( \"wibble\") ); " + "end " + " " + "rule rule3 " + "     @Defeater " + "     @Defeats( \"rule2\" ) " + "     when " + "         Fact( \"two\"; ) " + "     then " + "         System.out.println(\"two negates wibble\"); " + "         insertLogical( new Fact( \"wibble\"), \"neg\" ); " + "end";
    KieSession session = getSessionFromString(drl);
    session.fireAllRules();
    FactType factType = session.getKieBase().getFactType("org.drools.defeasible", "Fact");
    for (Object o : session.getObjects(new ClassObjectFilter(factType.getFactClass()))) {
        if ("wibble".equals(factType.get(o, "fact"))) {
            InternalFactHandle handle = (InternalFactHandle) session.getFactHandle(o);
            DefeasibleBeliefSet dbs = (DefeasibleBeliefSet) handle.getEqualityKey().getBeliefSet();
            assertEquals(3, dbs.size());
            assertTrue(dbs.isConflicting());
        }
    }
}
Also used : DefeasibleBeliefSet(org.drools.core.beliefsystem.defeasible.DefeasibleBeliefSet) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 3 with DefeasibleBeliefSet

use of org.drools.core.beliefsystem.defeasible.DefeasibleBeliefSet in project drools by kiegroup.

the class DefeasibilityTest method checkStatus.

private void checkStatus(EqualityKey key, int support, DefeasibilityStatus status) {
    assertEquals(EqualityKey.JUSTIFIED, key.getStatus());
    BeliefSet set = key.getBeliefSet();
    assertTrue(set instanceof DefeasibleBeliefSet);
    DefeasibleBeliefSet dfs = (DefeasibleBeliefSet) set;
    assertEquals(support, dfs.size());
    assertEquals(status, dfs.getStatus());
}
Also used : DefeasibleBeliefSet(org.drools.core.beliefsystem.defeasible.DefeasibleBeliefSet) BeliefSet(org.drools.core.beliefsystem.BeliefSet) DefeasibleBeliefSet(org.drools.core.beliefsystem.defeasible.DefeasibleBeliefSet)

Aggregations

DefeasibleBeliefSet (org.drools.core.beliefsystem.defeasible.DefeasibleBeliefSet)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 Test (org.junit.Test)2 KieSession (org.kie.api.runtime.KieSession)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ClassObjectFilter (org.drools.core.ClassObjectFilter)1 BeliefSet (org.drools.core.beliefsystem.BeliefSet)1 FactType (org.kie.api.definition.type.FactType)1