use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class DefeasibilityTest method testDefeaterNeutrality.
@Test(timeout = 10000)
public void testDefeaterNeutrality() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeaterOnly.drl");
ArrayList list = new ArrayList();
kSession.setGlobal("list", list);
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
ObjectHashMap keys = tms.getEqualityKeyMap();
Iterator iter = keys.iterator();
ObjectHashMap.ObjectEntry entry;
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Xtype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFEATEDLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
for (Object o : kSession.getObjects()) {
System.out.println(o);
}
assertEquals(0, list.size());
assertEquals(1, kSession.getFactCount());
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class DefeasibilityTest method testRemoveDefeasibleEntailmentMultiActivationWithDefeat.
@Test(timeout = 10000)
public void testRemoveDefeasibleEntailmentMultiActivationWithDefeat() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeatDefeaterRetract.drl");
ArrayList list = new ArrayList();
kSession.setGlobal("list", list);
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
ObjectHashMap keys = tms.getEqualityKeyMap();
Iterator iter = keys.iterator();
ObjectHashMap.ObjectEntry entry;
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Xtype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFEATEDLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(1, list.size());
assertTrue(list.contains("Stage1"));
assertEquals(3, kSession.getObjects().size());
for (Object o : kSession.getObjects()) {
System.out.println(o);
}
FactHandle h = kSession.insert("go");
kSession.fireAllRules();
keys = tms.getEqualityKeyMap();
iter = keys.iterator();
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Xtype.getFactClass()) {
checkStatus(key, 3, DefeasibilityStatus.DEFEASIBLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
for (Object o : kSession.getObjects()) {
System.out.println(o);
}
// A, A, B, X, GO
assertEquals(5, kSession.getObjects().size());
assertEquals(3, list.size());
assertTrue(list.contains("Stage1"));
assertTrue(list.contains("Stage2"));
kSession.retract(h);
kSession.fireAllRules();
for (Object o : kSession.getObjects()) {
System.out.println(o);
}
// A, A, B, X, GO
assertEquals(3, kSession.getObjects().size());
keys = tms.getEqualityKeyMap();
iter = keys.iterator();
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Xtype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFEATEDLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class DefeasibilityTest method testDefeaterPositiveVsNegative.
@Test(timeout = 10000)
public void testDefeaterPositiveVsNegative() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeatersPosNeg.drl");
ArrayList list = new ArrayList();
kSession.setGlobal("list", list);
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
ObjectHashMap keys = tms.getEqualityKeyMap();
Iterator iter = keys.iterator();
ObjectHashMap.ObjectEntry entry;
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Object fact = key.getFactHandle().getObject();
Class factClass = fact.getClass();
if (factClass == Xtype.getFactClass()) {
Integer val = (Integer) Xtype.get(fact, "id");
switch(val) {
case -1:
checkStatus(key, 2, DefeasibilityStatus.UNDECIDABLY);
break;
case 3:
checkStatus(key, 1, DefeasibilityStatus.DEFEATEDLY);
break;
case -35:
checkStatus(key, 3, DefeasibilityStatus.UNDECIDABLY);
break;
case 44:
checkStatus(key, 2, DefeasibilityStatus.DEFEASIBLY);
break;
default:
fail("Unrecognized fact");
}
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(1, list.size());
assertTrue(list.contains(-44));
assertTrue(!list.contains(-35));
assertEquals(2, kSession.getFactCount());
assertEquals(1, getNegativeObjects(kSession).size());
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class TraitTest method testTraitProxy.
@Test(timeout = 10000)
public void testTraitProxy() {
String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
Resource res = ResourceFactory.newClassPathResource(source);
assertNotNull(res);
kbuilder.add(res, ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
kb.addPackages(kbuilder.getKnowledgePackages());
TraitFactory.setMode(mode, kb);
TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
impClass.set(imp, "name", "aaa");
Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
Class trait2 = kb.getFactType("org.drools.compiler.trait.test", "Role").getFactClass();
assertNotNull(trait);
TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
proxy.getFields().put("field", "xyz");
// proxy.getFields().put("name", "aaa");
assertNotNull(proxy);
TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(imp, trait);
assertSame(proxy2, proxy);
TraitProxy proxy3 = (TraitProxy) tFactory.getProxy(imp, trait2);
assertNotNull(proxy3);
assertEquals("xyz", proxy3.getFields().get("field"));
assertEquals("aaa", proxy3.getFields().get("name"));
TraitableBean imp2 = (TraitableBean) impClass.newInstance();
impClass.set(imp2, "name", "aaa");
TraitProxy proxy4 = (TraitProxy) tFactory.getProxy(imp2, trait);
// proxy4.getFields().put("name", "aaa");
proxy4.getFields().put("field", "xyz");
assertEquals(proxy2, proxy4);
} catch (InstantiationException e) {
e.printStackTrace();
fail(e.getMessage());
} catch (IllegalAccessException e) {
e.printStackTrace();
fail(e.getMessage());
} catch (LogicalTypeInconsistencyException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class TraitTest method testHasTypes.
@Test(timeout = 10000)
public void testHasTypes() {
String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
Resource res = ResourceFactory.newClassPathResource(source);
assertNotNull(res);
kbuilder.add(res, ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
kb.addPackages(kbuilder.getKnowledgePackages());
TraitFactory traitBuilder = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
TraitFactory.setMode(mode, kb);
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
impClass.set(imp, "name", "aaabcd");
Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
Class trait2 = kb.getFactType("org.drools.compiler.trait.test", "Role").getFactClass();
assertNotNull(trait);
TraitProxy proxy = (TraitProxy) traitBuilder.getProxy(imp, trait);
Thing thing = traitBuilder.getProxy(imp, Thing.class);
TraitableBean core = (TraitableBean) proxy.getObject();
TraitProxy proxy2 = (TraitProxy) traitBuilder.getProxy(imp, trait);
Thing thing2 = traitBuilder.getProxy(imp, Thing.class);
assertSame(proxy, proxy2);
assertSame(thing, thing2);
assertEquals(2, core.getTraits().size());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations