use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class LogicalTraitTest method testShadowAliasClassOnTraitLogicalRetract.
@Test
public void testShadowAliasClassOnTraitLogicalRetract() {
String drl = "package org.drools.test; \n" + "import org.drools.core.factmodel.traits.*; \n" + "import org.drools.core.factmodel.traits.Trait; \n" + "" + "global java.util.List list; \n" + "" + "declare trait X \n" + " fld : K \n" + " fld2 : K \n" + "end \n" + "" + "declare trait W \n" + " fld : Q \n" + "end \n" + "declare trait V \n" + " fld2 : T \n" + "end \n" + "" + "declare Y \n" + "@Traitable( logical = true ) \n" + " fld : T \n" + " fld2 : Q \n" + "end \n" + "" + "declare trait T @Trait( logical=true ) id : int end \n" + "declare trait Q @Trait( logical=true ) id : int end \n" + "declare K @Traitable() id : int end \n" + "" + "rule Don \n" + "when \n" + " $s : String( this == \"go\" ) \n" + "then \n" + " K k1 = new K( 1 ); \n" + " K k2 = new K( 2 ); \n" + " T t = don( k1, T.class ); \n" + " Q q = don( k2, Q.class ); \n" + " Y y = new Y( t, q ); \n" + " insert( y ); \n" + "" + " don( y, X.class, true ); \n" + "end \n" + "" + "rule Check \n" + "when \n" + " String( this == \"go\" ) \n" + " $x : X( $f1 : fld, $f2 : fld2 ) \n" + "then \n" + " list.add( $f1.getId() );" + " list.add( $f2.getId() );" + "end \n" + "" + "rule Check2\n" + "when \n" + " not String( this == \"go\" ) \n" + " $x : Y( $f1 : fld, $f2 : fld2 ) \n" + "then \n" + " list.add( $f1.getId() );" + " list.add( $f2.getId() );" + "end \n" + "";
KnowledgeBuilder kbuilderImpl = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilderImpl.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilderImpl.hasErrors()) {
fail(kbuilderImpl.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilderImpl.getKnowledgePackages());
TraitFactory.setMode(mode, kbase);
KieSession ks = kbase.newKieSession();
ArrayList list = new ArrayList();
ks.setGlobal("list", list);
FactHandle handle = ks.insert("go");
ks.fireAllRules();
assertEquals(Arrays.asList(1, 2), list);
ks.retract(handle);
ks.fireAllRules();
for (Object o : ks.getObjects(new ClassObjectFilter(ks.getKieBase().getFactType("org.drools.test", "Y").getFactClass()))) {
assertTrue(o instanceof TraitableBean);
}
try {
ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
System.out.println(list);
assertEquals(Arrays.asList(1, 2, 1, 2), list);
}
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"));
}
use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class ConstrainedDataDictionaryTest method testProperties.
@Test
public void testProperties() throws Exception {
getKSession().getEntryPoint("in_Vallued").insert(1);
getKSession().getEntryPoint("in_Intervalled").insert(8.3);
getKSession().getEntryPoint("in_Cat").insert("aa");
getKSession().getEntryPoint("in_Sort").insert(1);
getKSession().fireAllRules();
Collection<EventFactHandle> fact1 = getKSession().getFactHandles(new ClassObjectFilter(getKbase().getFactType(packageName, "Vallued").getFactClass()));
assertEquals(1, fact1.size());
// assertEquals(true, getKbase().getFactType(packageName, "Vallued").get(fact1.iterator().next().getObject(), "continuous"));
Collection<EventFactHandle> fact2 = getKSession().getFactHandles(new ClassObjectFilter(getKbase().getFactType(packageName, "Intervalled").getFactClass()));
assertEquals(1, fact2.size());
// assertEquals(true, getKbase().getFactType(packageName,"Intervalled").get(fact2.iterator().next().getObject(),"continuous"));
Collection<EventFactHandle> fact3 = getKSession().getFactHandles(new ClassObjectFilter(getKbase().getFactType(packageName, "Cat").getFactClass()));
assertEquals(1, fact3.size());
// assertEquals(true, getKbase().getFactType(packageName,"Cat").get(fact3.iterator().next().getObject(),"categorical"));
Collection<EventFactHandle> fact4 = getKSession().getFactHandles(new ClassObjectFilter(getKbase().getFactType(packageName, "Sort").getFactClass()));
assertEquals(1, fact4.size());
// assertEquals(true, getKbase().getFactType(packageName,"Sort").get(fact4.iterator().next().getObject(),"ordinal"));
checkGeneratedRules();
}
use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class UserDefinedFunctionsTest method testComplexFunctionsNested2.
@Test
public void testComplexFunctionsNested2() throws Exception {
setKSession(getModelSession(source2, VERBOSE));
setKbase(getKSession().getKieBase());
FactType userAge1 = getKbase().getFactType(packageName, "UserAge");
FactType userAge2 = getKbase().getFactType(packageName, "UserAgeComplex");
FactHandle h = getKSession().getEntryPoint("in_Age").insert(10.0);
getKSession().fireAllRules();
checkFirstDataFieldOfTypeStatus(userAge1, true, false, null, 6270.0);
checkFirstDataFieldOfTypeStatus(userAge2, true, false, null, 44.1);
System.out.println("_________________________________________________________________");
FactType age = getKbase().getFactType(packageName, "Age");
Object aged = getKSession().getObjects(new ClassObjectFilter(age.getFactClass())).iterator().next();
getKSession().delete(getKSession().getFactHandle(aged));
getKSession().fireAllRules();
assertEquals(0, getKSession().getFactCount());
getKSession().getEntryPoint("in_Age").insert(20.0);
getKSession().fireAllRules();
checkFirstDataFieldOfTypeStatus(userAge1, true, false, null, 9570.0);
checkFirstDataFieldOfTypeStatus(userAge2, true, false, null, 115.2);
getKSession().getEntryPoint("in_Age").insert(30.0);
getKSession().fireAllRules();
checkFirstDataFieldOfTypeStatus(userAge1, true, false, null, 12870.0);
checkFirstDataFieldOfTypeStatus(userAge2, true, false, null, 306.3);
assertEquals(4, getKSession().getFactCount());
checkGeneratedRules();
}
use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class FlowTest method testQueryInRuleWithDeclaration.
@Test
public void testQueryInRuleWithDeclaration() {
Variable<Person> personV = declarationOf(Person.class);
Query2Def<Person, Integer> qdef = query("olderThan", Person.class, Integer.class);
Query query = qdef.build(expr("exprA", qdef.getArg1(), qdef.getArg2(), (p, a) -> p.getAge() > a));
Rule rule = rule("R").build(expr("exprB", personV, p -> p.getName().startsWith("M")), qdef.call(personV, valueOf(40)), on(personV).execute((drools, p) -> drools.insert(new Result(p.getName()))));
Model model = new ModelImpl().addQuery(query).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
ksession.insert(new Person("Edson", 41));
ksession.fireAllRules();
Collection<Result> results = (Collection<Result>) ksession.getObjects(new ClassObjectFilter(Result.class));
assertEquals(1, results.size());
assertEquals("Mario", results.iterator().next().getValue());
}
Aggregations