use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class FieldAccessTest method testAccessFieldsFromSubClass.
@Test
public // unless typesafe=false is used
void testAccessFieldsFromSubClass() throws Exception {
String rule = "";
rule += "package org.drools.compiler;\n";
rule += "import org.drools.mvel.compiler.Person;\n";
rule += "import org.drools.mvel.compiler.Pet;\n";
rule += "import org.drools.mvel.compiler.Cat;\n";
rule += "declare Person @typesafe(false) end\n";
rule += "rule \"Test Rule\"\n";
rule += "when\n";
rule += " Person(\n";
rule += " pet.breed == \"Siamise\"\n";
rule += " )\n";
rule += "then\n";
rule += " System.out.println(\"hello person\");\n";
rule += "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, rule);
KieSession session = kbase.newKieSession();
final Person person = new Person();
person.setPet(new Cat("Mittens"));
session.insert(person);
session.fireAllRules();
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class TypeCoercionTest method testPrimitiveToBoxedCoercionInMethodArgument.
@Test
public void testPrimitiveToBoxedCoercionInMethodArgument() throws Exception {
final String str = "package org.drools.mvel.compiler.test;\n" + "import " + TypeCoercionTest.class.getName() + "\n" + "import org.drools.mvel.compiler.*\n" + "rule R1 when\n" + " Person( $ag1 : age )" + " $p2 : Person( name == TypeCoercionTest.integer2String($ag1) )" + "then\n" + "end\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
final KieSession ksession = kbase.newKieSession();
final Person p = new Person("42", 42);
ksession.insert(p);
assertEquals(1, ksession.fireAllRules());
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class ReteEvaluatorTest method testPropertyReactivity.
@Test
public void testPropertyReactivity() {
String str = "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $s : String()\n" + " $p : Person(name == $s)\n" + "then\n" + " modify($p) { setAge($p.getAge()+1) }\n" + "end";
ReteEvaluator reteEvaluator = new StatefulKnowledgeSessionImpl(1L, getKBase(str));
Person me = new Person("Mario", 40);
reteEvaluator.insert("Mario");
reteEvaluator.insert(me);
assertEquals(1, reteEvaluator.fireAllRules());
assertEquals(41, me.getAge());
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class MetricLogUtilsTest method testEval.
@Test
public void testEval() {
String str = "import " + Address.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule R1\n" + "dialect \"mvel\"\n" + "when\n" + " $p1 : Person()\n" + " eval($p1.age > 6)" + "then\n" + // " System.out.println(\"$p1.name = \" + $p1.getName());\n" +
"end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
List<Person> personList = IntStream.range(0, 10).mapToObj(i -> new Person("John" + i, i)).collect(Collectors.toList());
KieSession ksession = kbase.newKieSession();
personList.stream().forEach(ksession::insert);
int fired = ksession.fireAllRules();
ksession.dispose();
assertEquals(3, fired);
// 1 node expected
Collection<Timer> timers = Search.in(registry).name("org.drools.metric.elapsed.time.per.evaluation").timers();
assertThat(timers).hasSize(1);
Collection<Timer> timers2 = Search.in(registry).name("org.drools.metric.elapsed.time").timers();
assertThat(timers2).hasSize(1);
Collection<Counter> counters = Search.in(registry).name("org.drools.metric.evaluation.count").counters();
assertThat(counters).hasSize(1);
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class MetricLogUtilsTest method testJoin.
@Test
public void testJoin() {
String str = "import " + Address.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule R1\n" + "when\n" + " $p1 : Person(age > 5)\n" + " $p2 : Person(age > $p1.age)\n" + "then\n" + "end\n" + "rule R2\n" + "when\n" + " $p1 : Person(age > 5)\n" + " $p2 : Person(age < $p1.age)\n" + "then\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
List<Person> personList = IntStream.range(0, 10).mapToObj(i -> new Person("John" + i, i)).collect(Collectors.toList());
KieSession ksession = kbase.newKieSession();
personList.stream().forEach(ksession::insert);
int fired = ksession.fireAllRules();
ksession.dispose();
assertEquals(36, fired);
// 2 nodes expected
Collection<Timer> timers = Search.in(registry).name("org.drools.metric.elapsed.time.per.evaluation").timers();
assertThat(timers).hasSize(2);
Collection<Timer> timers2 = Search.in(registry).name("org.drools.metric.elapsed.time").timers();
assertThat(timers2).hasSize(2);
Collection<Counter> counters = Search.in(registry).name("org.drools.metric.evaluation.count").counters();
assertThat(counters).hasSize(2);
}
Aggregations