use of org.drools.modelcompiler.domain.Employee 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.Employee in project drools by kiegroup.
the class OOPathTest method testOrConstraint.
@Test
public void testOrConstraint() {
final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + " $emp: Employee( $address: /address[ street == 'Elm' || city == 'Big City' ] )\n" + " Employee( this != $emp, /address[ street == 'Elm' || city == 'Big City' ] )\n" + "then\n" + " list.add( $address.getCity() );\n" + "end\n";
KieSession kieSession = getKieSession(drl);
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.Employee in project drools by kiegroup.
the class OOPathTest method testOrConditionalElementNoBinding.
@Test
public void testOrConditionalElementNoBinding() {
final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + " $employee: (\n" + " Employee( /address[ city == 'Big City' ] )\n" + " or " + " Employee( /address[ city == 'Small City' ] )\n" + " )\n" + "then\n" + " list.add( $employee.getName() );\n" + "end\n";
KieSession kieSession = getKieSession(drl);
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("Bruno", "Alice");
}
use of org.drools.modelcompiler.domain.Employee in project drools by kiegroup.
the class OOPathTest method testOOPathMultipleConditions.
@Test
public void testOOPathMultipleConditions() {
final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + " Employee( $address: /address[ street == 'Elm', city == 'Big City' ] )\n" + "then\n" + " list.add( $address.getCity() );\n" + "end\n";
KieSession kieSession = getKieSession(drl);
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");
}
use of org.drools.modelcompiler.domain.Employee in project drools by kiegroup.
the class OOPathTest method testOOPathMultipleConditionsWithBinding.
@Test
public void testOOPathMultipleConditionsWithBinding() {
final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + " $employee: (\n" + " Employee( /address[ street == 'Elm', city == 'Big City' ] )\n" + " )\n" + "then\n" + " list.add( $employee.getName() );\n" + "end\n";
KieSession kieSession = getKieSession(drl);
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("Alice");
}
Aggregations