use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class FlowTest method testNotEmptyPredicate.
@Test
public void testNotEmptyPredicate() {
Rule rule = rule("R").build(not(input(declarationOf(Person.class))), execute((drools) -> drools.insert(new Result("ok"))));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Person mario = new Person("Mario", 40);
ksession.insert(mario);
ksession.fireAllRules();
assertTrue(ksession.getObjects(new ClassObjectFilter(Result.class)).isEmpty());
}
use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class FlowTest method testQueryInRule.
@Test
public void testQueryInRule() {
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));
Variable<Person> personVRule = declarationOf(Person.class);
Rule rule = rule("R").build(qdef.call(personVRule, valueOf(40)), on(personVRule).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.fireAllRules();
Collection<Result> results = (Collection<Result>) ksession.getObjects(new ClassObjectFilter(Result.class));
assertEquals(1, results.size());
assertEquals("Mario", results.iterator().next().getValue());
}
use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndUpdate.
@Test
public void testActivateOnMatchAndUpdate() throws Exception {
String str = "";
str += "package org.drools.compiler.integrationtests \n";
str += "import " + Alarm.class.getCanonicalName() + " \n";
str += "import " + Sensor.class.getCanonicalName() + " \n";
str += "rule StringRule @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
str += " when \n";
str += " $a : Alarm() \n";
str += " $s : Sensor() \n";
str += " then \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void matchCreated(MatchCreatedEvent event) {
Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
for (FactHandle alarm : alarms) {
event.getKieRuntime().update(alarm, new Alarm());
}
}
});
// go !
Alarm alarm = new Alarm();
alarm.setMessage("test");
alarm.setNumber(123);
ksession.insert(alarm);
Sensor sensor = new Sensor();
sensor.setPressure(1);
sensor.setTemperature(25);
ksession.insert(sensor);
}
use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndDelete.
@Test
public void testActivateOnMatchAndDelete() throws Exception {
String str = "";
str += "package org.drools.compiler.integrationtests \n";
str += "import " + Alarm.class.getCanonicalName() + " \n";
str += "import " + Sensor.class.getCanonicalName() + " \n";
str += "rule StringRule @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
str += " when \n";
str += " $a : Alarm() \n";
str += " $s : Sensor() \n";
str += " then \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void matchCreated(MatchCreatedEvent event) {
Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
for (FactHandle alarm : alarms) {
event.getKieRuntime().delete(alarm);
}
}
});
// go !
Alarm alarm = new Alarm();
alarm.setMessage("test");
alarm.setNumber(123);
ksession.insert(alarm);
Sensor sensor = new Sensor();
sensor.setPressure(1);
sensor.setTemperature(25);
ksession.insert(sensor);
ksession.fireAllRules();
}
use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.
the class LogicalTraitTest method testHardGetSetOnLogicallyTraitedField.
@Category(ReviseTraitTestWithPRAlwaysCategory.class)
@Test
public void testHardGetSetOnLogicallyTraitedField() {
String drl = "package org.drools.test; " + "import " + Qty.class.getCanonicalName() + "; " + "" + "global java.util.List list; " + "declare Obs @Traitable( logical = true ) value : Qty end " + "declare trait TObs @Trait( logical = true ) value : TQty end " + "declare trait TQty @Trait( logical = true ) num : Integer end " + "rule Init " + "when " + "then " + " Obs o = new Obs( new Qty( 42 ) ); " + " don( o, TObs.class ); " + "end " + "rule Log " + "when " + " $o : TObs( $val : value.num ) " + "then " + " list.add( $val ); " + "end " + "rule Change " + "when " + " $s : String() " + " $o : TObs() " + "then " + " delete( $s ); " + " modify( $o ) { getValue().setNum( 99 ); } " + "end ";
KieBase knowledgeBase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build();
TraitFactory.setMode(mode, knowledgeBase);
KieSession knowledgeSession = knowledgeBase.newKieSession();
ArrayList list = new ArrayList();
knowledgeSession.setGlobal("list", list);
knowledgeSession.fireAllRules();
knowledgeSession.insert("x");
knowledgeSession.fireAllRules();
boolean found = false;
for (Object o : knowledgeSession.getObjects(new ClassObjectFilter(Qty.class))) {
assertEquals((Integer) 99, ((Qty) o).getNum());
assertEquals(99, ((CoreWrapper) o)._getFieldTMS().get("num", Integer.class));
found = true;
}
assertTrue(found);
assertEquals(Arrays.asList(42, 99), list);
knowledgeSession.dispose();
}
Aggregations