use of org.drools.core.base.extractors.MVELObjectClassFieldReader in project drools by kiegroup.
the class MVELTest method testMapAccessorWithStaticFieldAccess.
@Test
public void testMapAccessorWithStaticFieldAccess() {
final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + TestEnum.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Person( namedAddresses[TestEnum.ONE] == new Address('s1'), namedAddresses[TestEnum.ONE].street == new Address('s1').street ) \n" + "then \n" + " list.add('r1'); \n" + "end \n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession ksession = createKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Person p = new Person("yoda");
p.getNamedAddresses().put(TestEnum.ONE, 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);
assertEquals(new Address("s1"), ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
}
alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
constraint = alphanode.getConstraint();
if (constraint instanceof MvelConstraint) {
assertTrue(((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
assertEquals(new Address("s1").getStreet(), ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
}
}
use of org.drools.core.base.extractors.MVELObjectClassFieldReader in project drools by kiegroup.
the class MVELTest method testArrayAccessorWithStaticFieldAccess.
@Test
public void testArrayAccessorWithStaticFieldAccess() {
final String str = "" + "package org.drools.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";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession ksession = createKnowledgeSession(kbase);
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);
assertEquals(new Address("s1"), ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
}
alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
constraint = alphanode.getConstraint();
if (constraint instanceof MvelConstraint) {
assertTrue(((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
assertEquals(new Address("s1").getStreet(), ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
}
}
use of org.drools.core.base.extractors.MVELObjectClassFieldReader in project drools by kiegroup.
the class MVELTest method testArrayAccessorWithGenerics.
@Test
public void testArrayAccessorWithGenerics() {
final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Person( addresses[0] == new Address('s1'), addresses[0].street == new Address('s1').street ) \n" + "then \n" + " list.add('r1'); \n" + "end \n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession ksession = createKnowledgeSession(kbase);
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) constraint).getFieldExtractor() instanceof MVELObjectClassFieldReader);
assertEquals(new Address("s1"), ((MvelConstraint) constraint).getField().getValue());
}
alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
constraint = alphanode.getConstraint();
if (constraint instanceof MvelConstraint) {
assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof MVELObjectClassFieldReader);
assertEquals(new Address("s1").getStreet(), ((MvelConstraint) constraint).getField().getValue());
}
}
Aggregations