use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class MVELTest method testArrayAccessorWithStaticFieldAccess.
@Test
public void testArrayAccessorWithStaticFieldAccess() {
final String str = "" + "package org.drools.mvel.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + Triangle.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Person( addresses[Triangle.ZERO] == new Address('s1'), addresses[Triangle.ZERO].street == new Address('s1').street ) \n" + "then \n" + " list.add('r1'); \n" + "end \n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Person p = new Person("yoda");
p.addAddress(new Address("s1"));
ksession.insert(p);
ksession.fireAllRules();
assertEquals("r1", list.get(0));
// Check it was built with MVELReturnValueExpression constraint
final List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (final ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
node = n;
break;
}
}
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
if (constraint instanceof MVELConstraint) {
assertTrue(((MVELConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
}
alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
constraint = alphanode.getConstraint();
if (constraint instanceof MVELConstraint) {
assertTrue(((MVELConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
}
}
use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class UpdateTest method testModifyBlockWithFrom.
@Test
public void testModifyBlockWithFrom() throws Exception {
KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_ModifyBlockWithFrom.drl");
KieSession ksession = kbase.newKieSession();
final List results = new ArrayList();
ksession.setGlobal("results", results);
final Person bob = new Person("Bob");
final Address addr = new Address("abc");
bob.addAddress(addr);
ksession.insert(bob);
ksession.insert(addr);
ksession.fireAllRules();
// modify worked
assertEquals("12345", addr.getZipCode());
// chaining worked
assertEquals(1, results.size());
assertEquals(addr, results.get(0));
}
use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class MetricLogUtilsTest method testFrom.
@Test
public void testFrom() {
String str = "import " + Address.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule R1\n" + "when\n" + " $p : Person()\n" + " $a1 : Address() from $p.addresses\n" + " $a2 : Address(suburb != \"XYZ\", zipCode == $a1.zipCode, this != $a1) from $p.addresses\n" + "then\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
List<Person> personList = IntStream.range(0, 10).mapToObj(i -> new Person("John" + i, i)).map(p -> {
p.addAddress(new Address("StreetX" + p.getAge(), "ABC", "111"));
p.addAddress(new Address("StreetY" + p.getAge(), "ABC", "111"));
p.addAddress(new Address("StreetZ" + p.getAge(), "ABC", "999"));
return p;
}).collect(Collectors.toList());
KieSession ksession = kbase.newKieSession();
personList.stream().forEach(ksession::insert);
int fired = ksession.fireAllRules();
ksession.dispose();
assertEquals(20, 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.Address in project drools by kiegroup.
the class StatelessStressTest method testLotsOfStateless.
@Ignore
@Test
public void testLotsOfStateless() throws Exception {
KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "thread_class_test.drl");
int numThreads = 100;
Thread[] ts = new Thread[numThreads];
for (int i = 0; i < numThreads; i++) {
Runnable run = () -> {
long start = 0;
long previous = 0;
while (true) {
start = System.currentTimeMillis();
StatelessKieSession sess = kbase.newStatelessKieSession();
// StatelessKieSession is not serializable
// try {
// sess = SerializationHelper.serializeObject(sess);
// } catch (Exception ex) {
// throw new RuntimeException(ex);
// }
Person p = new Person();
p.setName("Michael");
Address add1 = new Address();
add1.setStreet("High");
Address add2 = new Address();
add2.setStreet("Low");
List l = new ArrayList();
l.add(add1);
l.add(add2);
p.setAddresses(l);
sess.execute(p);
long current = System.currentTimeMillis() - start;
if (previous != 0) {
float f = current / previous;
if (f > 1) {
System.err.println("SLOWDOWN");
}
}
previous = current;
}
};
Thread t = new Thread(run);
t.start();
ts[i] = t;
}
for (int i = 0; i < ts.length; i++) {
ts[i].join();
}
}
Aggregations