use of org.drools.modelcompiler.domain.Address in project drools by kiegroup.
the class CompilerTest method testSimpleInsertWithProperties.
@Test
public void testSimpleInsertWithProperties() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $p : Person( address.city.startsWith(\"M\"))\n" + "then\n" + " Result r = new Result($p.getName());" + " insert(r);\n" + "end";
KieSession ksession = getKieSession(str);
ksession.insert(new Person("Mark", 37, new Address("London")));
ksession.insert(new Person("Luca", 32, new Address("Milan")));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("Luca", results.iterator().next().getValue());
}
use of org.drools.modelcompiler.domain.Address in project drools by kiegroup.
the class CompilerTest method testNullSafeDereferncing2.
@Test
@Ignore("the codegen does not support it yet")
public void testNullSafeDereferncing2() {
String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $p : Person( address!.city.startsWith(\"M\") )\n" + "then\n" + " Result r = new Result($p.getName());" + " insert(r);\n" + "end";
KieSession ksession = getKieSession(str);
ksession.insert(new Person("John1", 41, null));
ksession.insert(new Person("John2", 42, new Address("Milan")));
ksession.fireAllRules();
List<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals("John2", results.get(0).getValue());
}
use of org.drools.modelcompiler.domain.Address in project drools by kiegroup.
the class CompilerTest method testChainOfMethodCallInConstraintSub.
@Test
public void testChainOfMethodCallInConstraintSub() {
String str = "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + // DRL feature "Grouped accessors for nested objects" is addressed by the RuleDescr directly.
" $p : Person( address.( city.length() == 5 && city.startsWith(\"I\") ) )\n" + "then\n" + " insert(\"matched\");\n" + "end";
KieSession ksession = getKieSession(str);
Person john = new Person("John", 47);
Address a = new Address("Italy");
john.setAddress(a);
ksession.insert(john);
ksession.fireAllRules();
Collection<String> results = getObjectsIntoList(ksession, String.class);
assertEquals(1, results.size());
}
use of org.drools.modelcompiler.domain.Address in project drools by kiegroup.
the class FlowTest method testOrConditional.
@Test
public void testOrConditional() {
final org.drools.model.Global<java.util.List> var_list = globalOf(java.util.List.class, "defaultpkg", "list");
final Variable<Employee> var_$pattern_Employee$1$ = declarationOf(Employee.class, "$pattern_Employee$1$");
final Variable<Address> var_$address = declarationOf(Address.class, "$address", reactiveFrom(var_$pattern_Employee$1$, (_this) -> _this.getAddress()));
org.drools.model.Rule rule = rule("R").build(or(and(input(var_$pattern_Employee$1$), expr("$expr$2$", var_$address, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCity(), "Big City")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCity(), "Big City").reactOn("city")), and(input(var_$pattern_Employee$1$), expr("$expr$4$", var_$address, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCity(), "Small City")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCity(), "Small City").reactOn("city"))), on(var_$address, var_list).execute(($address, list) -> {
list.add($address.getCity());
}));
Model model = new ModelImpl().addRule(rule).addGlobal(var_list);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession kieSession = kieBase.newKieSession();
List<String> results = new ArrayList<>();
kieSession.setGlobal("list", results);
final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
kieSession.insert(bruno);
final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
kieSession.insert(alice);
kieSession.fireAllRules();
Assertions.assertThat(results).containsExactlyInAnyOrder("Big City", "Small City");
}
use of org.drools.modelcompiler.domain.Address in project drools by kiegroup.
the class InTest method testInWithJoin.
@Test
public void testInWithJoin() {
String str = "import org.drools.modelcompiler.domain.Address; \n" + "rule R when \n" + " $a1: Address($street: street, city in (\"Brno\", \"Milan\", \"Bratislava\")) \n" + " $a2: Address(city in (\"Krakow\", \"Paris\", $a1.city)) \n" + "then \n" + "end\n";
KieSession ksession = getKieSession(str);
ksession.insert(new Address("Brno"));
ksession.insert(new Address("Milan"));
assertEquals(2, ksession.fireAllRules());
}
Aggregations