use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class NeuralNetworkTest method testOverride.
@Test
public void testOverride() throws Exception {
setKSession(getModelSession(source3, VERBOSE));
setKbase(getKSession().getKieBase());
getKSession().fireAllRules();
getKSession().getEntryPoint("in_Feat1").insert(2.2);
getKSession().fireAllRules();
getKSession().getEntryPoint("in_Feat2").insert(5);
getKSession().fireAllRules();
// System.err.println(reportWMObjects(getKSession()));
FactType out1 = getKbase().getFactType("org.kie.pmml.pmml_4_2.test", "Out1");
FactType out2 = getKbase().getFactType("org.kie.pmml.pmml_4_2.test", "Out2");
FactType nump = getKbase().getFactType("org.kie.pmml.pmml_4_2.test", "Feat2");
assertEquals(1, getKSession().getObjects(new ClassObjectFilter(out1.getFactClass())).size());
assertEquals(1, getKSession().getObjects(new ClassObjectFilter(out2.getFactClass())).size());
assertEquals(2, getKSession().getObjects(new ClassObjectFilter(nump.getFactClass())).size());
getKSession().getEntryPoint("in_Feat1").insert(2.5);
getKSession().getEntryPoint("in_Feat2").insert(6);
getKSession().fireAllRules();
// System.err.println(reportWMObjects(getKSession()));
assertEquals(1, getKSession().getObjects(new ClassObjectFilter(out1.getFactClass())).size());
assertEquals(1, getKSession().getObjects(new ClassObjectFilter(out2.getFactClass())).size());
assertEquals(2, getKSession().getObjects(new ClassObjectFilter(nump.getFactClass())).size());
checkGeneratedRules();
}
use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class TraitTest method testPreserveAllSetBitMask.
@Test
public void testPreserveAllSetBitMask() {
// DROOLS-1699
String drl = "package t.x;\n" + "" + "import " + Entity.class.getName() + "; " + "" + "declare trait MyThing end\n" + "" + "declare trait RootThing extends MyThing " + " objProp : java.util.List = new java.util.ArrayList() " + "end " + "" + "declare trait F extends RootThing end\n" + "" + "declare trait D extends RootThing end\n" + "" + "declare trait E extends D end\n" + "" + "rule Init when\n" + "then " + " Entity e1 = new Entity( \"X\" ); " + " insert( e1 ); " + " Entity e2 = new Entity( \"Y\" ); " + " insert( e2 ); " + " " + " D d1 = don( e1, D.class, true ); " + " F f2 = don( e2, F.class, true ); " + " " + " modify ( d1 ) { getObjProp().add( f2.getCore() ); } " + " modify ( f2.getCore() ) {} " + "end " + "" + "rule Rec no-loop when\n" + " MyThing( $x_0 := core, this isA D.class, $p : this#RootThing.objProp ) " + " exists MyThing( $x_1 := core , core memberOf $p, this isA F.class ) " + "then " + " System.out.println( \"Recognized \" + $x_0 + \" as an instance of E by rule \" + drools.getRule().getName() ); " + " don( $x_0, E.class, true ); " + "end " + "" + "rule Shed_2 when\n" + " $s : String( this == \"go2\") " + " $x : E( $objs : objProp ) " + " $y : F( $z : core memberOf $objs ) " + "then " + " retract( $s ); " + " System.out.println( \"SUCCESS : E has been recognized, removing Y \" ); " + " modify ( $x ) { getObjProp().remove( $z ); } " + " modify ( $y ) {} " + "end ";
KieHelper helper = new KieHelper();
KieBase kieBase = helper.addContent(drl, ResourceType.DRL).getKieContainer().getKieBase();
KieSession kSession = kieBase.newKieSession();
kSession.fireAllRules();
kSession.insert("go2");
kSession.fireAllRules();
for (Object o : kSession.getObjects(new ClassObjectFilter(Entity.class))) {
Entity e = (Entity) o;
if (e.getId().equals("X")) {
assertTrue(e.hasTrait("t.x.D"));
assertFalse(e.hasTrait("t.x.E"));
assertFalse(e.hasTrait("t.x.F"));
assertEquals(0, ((List) e._getDynamicProperties().get("objProp")).size());
} else if (e.getId().equals("Y")) {
assertTrue(e.hasTrait("t.x.F"));
assertFalse(e.hasTrait("t.x.D"));
assertFalse(e.hasTrait("t.x.E"));
} else {
fail("Unrecognized entity in WM");
}
}
}
use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class ExtendsTest method testExtends.
@Test
public void testExtends() throws Exception {
// Test Base Fact Type
KieSession ksession = genSession("test_Extends.drl");
FactType person = ksession.getKieBase().getFactType("defaultpkg", "Person");
FactType eqPair = ksession.getKieBase().getFactType("defaultpkg", "EqualityPair");
Object p = person.newInstance();
assertNotNull(p);
ksession.insert("Populate");
ksession.fireAllRules();
// A Rule will generate 3 Persons, one with default constructor, two with field constructor
// and 3 Students, which extends Person. One with default, one with field constructor
assertEquals(6, ksession.getObjects(new ClassObjectFilter(person.getFactClass())).size());
assertEquals(1 + 4 + 1 + 4, ksession.getObjects(new ClassObjectFilter(eqPair.getFactClass())).size());
ksession.dispose();
}
use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class DroolsAbstractPMMLTest method getDoubleFieldValue.
public Double getDoubleFieldValue(FactType type) {
Class<?> klass = type.getFactClass();
Iterator iter = getKSession().getObjects(new ClassObjectFilter(klass)).iterator();
Object obj = iter.next();
return (Double) type.get(obj, "value");
}
use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class DroolsAbstractPMMLTest method checkFirstDataFieldOfTypeStatus.
protected void checkFirstDataFieldOfTypeStatus(FactType type, boolean valid, boolean missing, String ctx, Object... target) {
Class<?> klass = type.getFactClass();
Iterator iter = getKSession().getObjects(new ClassObjectFilter(klass)).iterator();
assertTrue(iter.hasNext());
Object obj = iter.next();
if (ctx == null) {
while (type.get(obj, "context") != null && iter.hasNext()) obj = iter.next();
} else {
while ((!ctx.equals(type.get(obj, "context"))) && iter.hasNext()) obj = iter.next();
}
Object tgt = type.get(obj, "value");
if (tgt instanceof Double) {
assert (target[0] instanceof Double);
assertEquals((Double) target[0], (Double) tgt, 1e-6);
} else {
assertEquals(target[0], tgt);
}
assertEquals(valid, type.get(obj, "valid"));
assertEquals(missing, type.get(obj, "missing"));
}
Aggregations