use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class NullSafeDereferencingTest method testNullSafeNullComparison.
@Test
public void testNullSafeNullComparison() {
String str = "import org.drools.mvel.compiler.*;\n" + "rule R1 when\n" + " Person( address!.street == null ) \n" + "then\n" + "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
ksession.insert(new Person("Mario", 38));
Person mark = new Person("Mark", 37);
mark.setAddress(new Address("Main Street"));
ksession.insert(mark);
Person edson = new Person("Edson", 34);
edson.setAddress(new Address(null));
ksession.insert(edson);
assertEquals(1, ksession.fireAllRules());
ksession.dispose();
}
use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class NullSafeDereferencingTest method testNullSafeNestedAccessors.
@Test
public void testNullSafeNestedAccessors() {
String str = "package org.drools.test; " + "import " + Person.class.getName() + "; " + "global java.util.List list; " + "rule R1 when " + " $street : String() " + " Person( address!.( street == $street, $zip : zipCode ) ) " + "then " + " list.add( $zip ); " + "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new Person("Mario", 38));
Person mark = new Person("Mark", 37);
mark.setAddress(new Address("Main Street", "", "123456"));
ksession.insert(mark);
ksession.insert("Main Street");
assertEquals(1, ksession.fireAllRules());
ksession.dispose();
assertEquals(Arrays.asList("123456"), list);
}
use of org.drools.mvel.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.mvel.compiler.*;\n" + "rule rule1 \n" + " when\n" + " (PersonHolder($addresses : person.addresses))\n" + " &&\n" + " (Address (street == \"AAA\") from $addresses)\n" + " then\n" + "end";
Collection<KiePackage> knowledgePackages1 = KieBaseUtil.getKieBaseFromKieModuleFromDrl("tmp", kieBaseTestConfiguration, drl1).getKiePackages();
String drl2 = "package com.sample\n" + "import org.drools.mvel.compiler.*;\n" + "rule rule2 \n" + " when\n" + " PersonHolder()\n" + " then\n" + "end";
Collection<KiePackage> knowledgePackages2 = KieBaseUtil.getKieBaseFromKieModuleFromDrl("tmp", kieBaseTestConfiguration, drl2).getKiePackages();
InternalKnowledgeBase kbase1 = KnowledgeBaseFactory.newKnowledgeBase();
Collection<KiePackage> combinedPackages1 = new ArrayList<>();
combinedPackages1.addAll(knowledgePackages1);
combinedPackages1.addAll(knowledgePackages2);
// Add once to make inUse=true
kbase1.addPackages(combinedPackages1);
InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
Collection<KiePackage> combinedPackages2 = new ArrayList<>();
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.mvel.compiler.Address in project drools by kiegroup.
the class Misc2Test method testUpdateWithNonEffectiveActivations.
@Test
public void testUpdateWithNonEffectiveActivations() throws Exception {
// JBRULES-3604
String str = "package inheritance\n" + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "\n" + "rule \"Parent\"\n" + " enabled false\n" + " when \n" + " $a : Address(suburb == \"xyz\")\n" + " then \n" + " System.out.println( $a ); \n" + "end \n" + "rule \"Child\" extends \"Parent\" \n" + " when \n" + " $b : Address( this == $a, street == \"123\")\n" + " then \n" + " System.out.println( $b ); \n" + "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
Address address = new Address();
address.setSuburb("xyz");
FactHandle addressHandle = ksession.insert(address);
int rulesFired = ksession.fireAllRules();
assertEquals(0, rulesFired);
address.setStreet("123");
ksession.update(addressHandle, address);
rulesFired = ksession.fireAllRules();
System.out.println(rulesFired);
assertEquals(1, rulesFired);
ksession.dispose();
}
use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class MVELTest method testArrayAccessorWithoutGenerics.
@Test
public void testArrayAccessorWithoutGenerics() {
final String str = "" + "package org.drools.mvel.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( addressesNoGenerics[0].street == new Address('s1').street) \n" + "then \n" + " list.add('r1'); \n" + "end \n";
KieBuilder kieBuilder = KieUtil.getKieBuilderFromDrls(kieBaseTestConfiguration, false, str);
List<org.kie.api.builder.Message> errors = kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR);
// This should fail as there are no generics for the List
assertFalse("Should have an error", errors.isEmpty());
}
Aggregations