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