use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class MetricLogUtilsTest method testAccumulate.
@Test
public void testAccumulate() {
String str = "import " + Address.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule R1\n" + "when\n" + " $p1 : Person()\n" + " accumulate ( $p2: Person ( getName().startsWith(\"J\"), this != $p1);\n" + " $average : average($p2.getAge());\n" + " $average > $p1.age, $average > 3\n" + " )\n" + "then\n" + // " System.out.println(\"$p1.name = \" + $p1.getName() + \", other's $average = \" + $average);\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(5, 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 testNot.
@Test
public void testNot() {
String str = "import " + Address.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule R1\n" + "when\n" + " $p1 : Person()\n" + " $p2 : Person(this != $p1)\n" + " not Person(this != $p1, this != $p2, (age == $p1.age || age == $p2.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(90, 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);
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class ConstraintsTest method testQuadroupleBetaConstraints.
@Test
public void testQuadroupleBetaConstraints() {
String str = "import " + Person.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "rule R when\n" + " Person( $age : age, $name : name, $happy : happy, $alive : alive )\n" + " Person( name == $name, age == $age + 1, happy == $happy, alive == $alive )\n" + "then\n" + " list.add($age);\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = kbase.newKieSession();
List<Integer> list = new ArrayList<>();
ksession.setGlobal("list", list);
Person p1 = new Person("AAA", 31, true);
p1.setAlive(true);
Person p2 = new Person("AAA", 34, true);
p2.setAlive(true);
Person p3 = new Person("AAA", 33, true);
p3.setAlive(true);
ksession.insert(p1);
ksession.insert(p2);
ksession.insert(p3);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals(33, (int) list.get(0));
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class JTMSTest method testChangeInPositivePrime.
@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testChangeInPositivePrime() {
String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import org.kie.internal.event.rule.ActivationUnMatchListener;\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "import org.drools.mvel.compiler.Person;" + "global java.util.List list;\n" + "\n" + "rule \"go1\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " Person p = new Person( 'darth' ); \n" + " p.setNotInEqualTestObject(1); \n" + " insertLogical( p );\n" + "end\n" + "rule \"go2\"\n" + "when\n" + " String( this == 'go2' )\n" + "then\n" + " Person p = new Person( 'darth' ); \n" + " p.setNotInEqualTestObject(2); \n" + " insertLogical( p );\n" + "end\n" + "rule \"go3\"\n" + "when\n" + " String( this == 'go3' )\n" + "then\n" + " Person p = new Person( 'darth' ); \n" + " p.setNotInEqualTestObject(3); \n" + " insertLogical( p );\n" + "end\n" + "\n";
KieSession kSession = getSessionFromString(s);
List list = new ArrayList();
kSession.setGlobal("list", list);
// We want to make sure go1 is prime, and then that it switches to go2
FactHandle fhGo1 = kSession.insert("go1");
kSession.fireAllRules();
FactHandle fhGo2 = kSession.insert("go2");
kSession.fireAllRules();
FactHandle fhGo3 = kSession.insert("go3");
kSession.fireAllRules();
NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
// just go1, go2, go3, Person(darth)
assertEquals(4, ep.getObjects().size());
int count = 0;
for (Object object : ep.getObjects()) {
if (object instanceof Person) {
assertEquals(new Integer(1), ((Person) object).getNotInEqualTestObject());
count++;
}
}
assertEquals(1, count);
ObjectHashMap equalityMap = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep).getEqualityKeyMap();
// Only Person type is logical
assertEquals(1, equalityMap.size());
org.drools.core.util.Iterator it = equalityMap.iterator();
TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(3, key.getBeliefSet().size());
assertEquals(new Integer(1), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
kSession.retract(fhGo1);
kSession.fireAllRules();
it = equalityMap.iterator();
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(2, key.getBeliefSet().size());
assertEquals(new Integer(3), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
kSession.retract(fhGo3);
kSession.fireAllRules();
it = equalityMap.iterator();
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(1, key.getBeliefSet().size());
assertEquals(new Integer(2), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class JTMSTest method testChangeInNegativePrime.
@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testChangeInNegativePrime() {
String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import org.kie.internal.event.rule.ActivationUnMatchListener;\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "import org.drools.mvel.compiler.Person;" + "global java.util.List list;\n" + "\n" + "declare entry-point 'neg' end \n" + "" + "rule \"go1\"\n" + "when\n" + " String( this == 'go1' )\n" + "then\n" + " Person p = new Person( 'darth' ); \n" + " p.setNotInEqualTestObject(1); \n" + " insertLogical( p, 'neg' );\n" + "end\n" + "rule \"go2\"\n" + "when\n" + " String( this == 'go2' )\n" + "then\n" + " Person p = new Person( 'darth' ); \n" + " p.setNotInEqualTestObject(2); \n" + " insertLogical( p, 'neg' );\n" + "end\n" + "rule \"go3\"\n" + "when\n" + " String( this == 'go3' )\n" + "then\n" + " Person p = new Person( 'darth' ); \n" + " p.setNotInEqualTestObject(3); \n" + " insertLogical( p, 'neg' );\n" + "end\n" + "\n";
KieSession kSession = getSessionFromString(s);
List list = new ArrayList();
kSession.setGlobal("list", list);
// We want to make sure go1 is prime, and then that it switches to go2
FactHandle fhGo1 = kSession.insert("go1");
kSession.fireAllRules();
FactHandle fhGo2 = kSession.insert("go2");
kSession.fireAllRules();
FactHandle fhGo3 = kSession.insert("go3");
kSession.fireAllRules();
NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
// just go1, go2, go3
assertEquals(3, ep.getObjects().size());
// Person(darth)
assertEquals(1, getNegativeObjects(kSession).size());
int count = 0;
for (Object object : getNegativeObjects(kSession)) {
if (object instanceof Person) {
assertEquals(new Integer(1), ((Person) object).getNotInEqualTestObject());
count++;
}
}
assertEquals(1, count);
ObjectHashMap equalityMap = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep).getEqualityKeyMap();
// Only Person type is logical
assertEquals(1, equalityMap.size());
org.drools.core.util.Iterator it = equalityMap.iterator();
TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(3, key.getBeliefSet().size());
assertEquals(new Integer(1), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
kSession.retract(fhGo1);
kSession.fireAllRules();
it = equalityMap.iterator();
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(2, key.getBeliefSet().size());
assertEquals(new Integer(3), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
kSession.retract(fhGo3);
kSession.fireAllRules();
it = equalityMap.iterator();
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
}
assertEquals(1, key.getBeliefSet().size());
assertEquals(new Integer(2), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
}
Aggregations