use of org.drools.compiler.Address in project drools by kiegroup.
the class NullSafeDereferencingTest method testMixedNullSafes.
@Test
public void testMixedNullSafes() {
String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + " $p : Person( " + " address!.street!.length > 0 && ( address!.street!.length < 15 || > 20 && < 30 ) " + " && address!.zipCode!.length > 0 && address.zipCode == \"12345\" " + " ) \n" + "then\n" + " System.out.println( $p ); \n" + "end";
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = kbase.newKieSession();
ksession.insert(new Person("Mario", 38));
Person mark = new Person("Mark", 37);
mark.setAddress(new Address("Main Street", null, "12345"));
ksession.insert(mark);
Person edson = new Person("Edson", 34);
edson.setAddress(new Address(null));
ksession.insert(edson);
Person alex = new Person("Alex", 34);
alex.setAddress(new Address("The Main Verrry Long Street"));
ksession.insert(alex);
Person frank = new Person("Frank", 24);
frank.setAddress(new Address("Long Street number 21", null, "12345"));
ksession.insert(frank);
assertEquals(2, ksession.fireAllRules());
ksession.dispose();
}
use of org.drools.compiler.Address in project drools by kiegroup.
the class PropertyReactivityTest method testModifyWithGetter.
@Test
public void testModifyWithGetter() {
final String rule1 = "package foo.bar\n" + "import " + Person.class.getName() + "\n" + "declare Person @propertyReactive end\n" + "rule x\n" + " when\n" + " $p : Person( address != null ) @watch(!address) \n" + " then\n" + " modify($p){getAddress().setStreet(\"foo\");}\n" + "end";
final KieHelper helper = new KieHelper();
helper.addContent(rule1, ResourceType.DRL);
final KieSession ksession = helper.build().newKieSession();
final Person p = new Person();
p.setAddress(new Address());
ksession.insert(p);
final int fired = ksession.fireAllRules(10);
assertEquals(1, fired);
assertEquals("foo", p.getAddress().getStreet());
}
use of org.drools.compiler.Address in project drools by kiegroup.
the class FirstOrderLogicTest method testFromWithOr.
@Test
public void testFromWithOr() throws Exception {
KieBase kbase = loadKnowledgeBase("test_FromWithOr.drl");
KieSession session = createKnowledgeSession(kbase);
final List<Address> results = new ArrayList<Address>();
session.setGlobal("results", results);
Address a1 = new Address();
a1.setZipCode("12345");
Address a2 = new Address();
a2.setZipCode("54321");
Address a3 = new Address();
a3.setZipCode("99999");
Person p = new Person();
p.addAddress(a1);
p.addAddress(a2);
p.addAddress(a3);
session.insert(p);
session.fireAllRules();
assertEquals(2, results.size());
assertTrue(results.contains(a1));
assertTrue(results.contains(a2));
}
use of org.drools.compiler.Address in project drools by kiegroup.
the class Misc2Test method testMergeMVELDialect.
@Test
public void testMergeMVELDialect() {
// DROOLS-1751
String drl1 = "package com.sample\n" + "import org.drools.compiler.*;\n" + "rule rule1 \n" + " when\n" + " (PersonHolder($addresses : person.addresses))\n" + " &&\n" + " (Address (street == \"AAA\") from $addresses)\n" + " then\n" + "end";
KnowledgeBuilder kbuilder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder1.add(ResourceFactory.newByteArrayResource(drl1.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages1 = kbuilder1.getKnowledgePackages();
String drl2 = "package com.sample\n" + "import org.drools.compiler.*;\n" + "rule rule2 \n" + " when\n" + " PersonHolder()\n" + " then\n" + "end";
KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder2.add(ResourceFactory.newByteArrayResource(drl2.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages2 = kbuilder2.getKnowledgePackages();
InternalKnowledgeBase kbase1 = KnowledgeBaseFactory.newKnowledgeBase();
Collection<KiePackage> combinedPackages1 = new ArrayList<KiePackage>();
combinedPackages1.addAll(knowledgePackages1);
combinedPackages1.addAll(knowledgePackages2);
// Add once to make inUse=true
kbase1.addPackages(combinedPackages1);
InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
Collection<KiePackage> combinedPackages2 = new ArrayList<KiePackage>();
combinedPackages2.addAll(knowledgePackages1);
combinedPackages2.addAll(knowledgePackages2);
// this will cause package deepClone
kbase2.addPackages(combinedPackages2);
KieSession ksession = kbase2.newKieSession();
PersonHolder personHolder = new PersonHolder();
Person person = new Person("John");
Address address = new Address("AAA", "BBB", "111");
person.addAddress(address);
personHolder.setPerson(person);
ksession.insert(personHolder);
int fired = ksession.fireAllRules();
assertEquals(2, fired);
}
use of org.drools.compiler.Address 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());
}
}
Aggregations