use of org.drools.model.Global in project drools by kiegroup.
the class FlowTest method testFromGlobal.
@Test
public void testFromGlobal() throws Exception {
// global java.util.List list
// rule R when
// $o : String(length > 3) from list
// then
// insert($o);
// end
final Global<List> var_list = globalOf(List.class, "defaultpkg", "list");
final Variable<String> var_$o = declarationOf(String.class, "$o", // cannot use Function.identity() here because target type is ?.
from(var_list, x -> x));
Rule rule = rule("R").build(expr("$expr$1$", var_$o, (_this) -> _this.length() > 3).indexedBy(int.class, org.drools.model.Index.ConstraintType.GREATER_THAN, 0, _this -> _this.length(), 3).reactOn("length"), on(var_$o).execute((drools, $o) -> {
drools.insert($o);
}));
Model model = new ModelImpl().addGlobal(var_list).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
List<String> messages = Arrays.asList("a", "Hello World!", "b");
ksession.setGlobal("list", messages);
ksession.fireAllRules();
List<String> results = getObjectsIntoList(ksession, String.class);
assertFalse(results.contains("a"));
assertTrue(results.contains("Hello World!"));
assertFalse(results.contains("b"));
}
use of org.drools.model.Global in project drools by kiegroup.
the class FlowTest method testGlobalInConsequence.
@Test
public void testGlobalInConsequence() {
Variable<Person> markV = declarationOf(Person.class);
Global<Result> resultG = globalOf(Result.class, "org.mypkg");
Rule rule = rule("org.mypkg", "global").build(expr("exprA", markV, p -> p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.EQUAL, 0, Person::getName, "Mark").reactOn("name"), on(markV, resultG).execute((p, r) -> r.setValue(p.getName() + " is " + p.getAge())));
Model model = new ModelImpl().addRule(rule).addGlobal(resultG);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Result result = new Result();
ksession.setGlobal(resultG.getName(), result);
Person mark = new Person("Mark", 37);
Person edson = new Person("Edson", 35);
Person mario = new Person("Mario", 40);
ksession.insert(mark);
ksession.insert(edson);
ksession.insert(mario);
ksession.fireAllRules();
assertEquals("Mark is 37", result.getValue());
}
use of org.drools.model.Global in project drools by kiegroup.
the class OOPathFlowTest method testOOPathCast.
@Test
public void testOOPathCast() {
Global<List> listG = globalOf(List.class, "defaultpkg", "list");
Variable<Man> manV = declarationOf(Man.class, "$man");
// Drools doc: In this way subsequent constraints can also safely access to properties declared only in that subclass
Variable<InternationalAddress> addressV = declarationOf(InternationalAddress.class, "$italy", reactiveFrom(manV, Man::getAddress));
Rule rule = rule("oopath").build(expr("exprA", addressV, c -> c.getState().equals("Italy")), on(manV, listG).execute((t, l) -> l.add(t.getName())));
Model model = new ModelImpl().addGlobal(listG).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Man bob = new Man("Bob", 40);
bob.setAddress(new InternationalAddress("Via Verdi", "Italy"));
ksession.insert(bob);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
use of org.drools.model.Global in project drools by kiegroup.
the class OOPathFlowTest method testOOPath.
@Test
public void testOOPath() {
Global<List> listG = globalOf(List.class, "defaultpkg", "list");
Variable<Man> manV = declarationOf(Man.class);
Variable<Woman> wifeV = declarationOf(Woman.class, reactiveFrom(manV, Man::getWife));
Variable<Child> childV = declarationOf(Child.class, reactiveFrom(wifeV, Woman::getChildren));
Rule rule = rule("oopath").build(expr("exprA", childV, c -> c.getAge() > 10), on(manV, listG).execute((t, l) -> l.add(t.getName())));
Model model = new ModelImpl().addGlobal(listG).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Woman alice = new Woman("Alice", 38);
final Man bob = new Man("Bob", 40);
final Man carl = new Man("Bob", 42);
bob.setWife(alice);
final Child charlie = new Child("Charles", 12);
final Child debbie = new Child("Debbie", 10);
alice.addChild(charlie);
alice.addChild(debbie);
ksession.insert(bob);
ksession.insert(carl);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
use of org.drools.model.Global in project drools by kiegroup.
the class RuleContext method getDeclaration.
Declaration getDeclaration(Variable variable) {
if (variable.isFact()) {
Declaration declaration = innerDeclaration.get(variable);
if (declaration == null) {
declaration = queryDeclaration.get(variable);
}
if (declaration == null) {
Pattern pattern = patterns.get(variable);
declaration = pattern != null ? pattern.getDeclaration() : null;
}
return declaration;
} else {
Global global = ((Global) variable);
ObjectType objectType = builder.getObjectType(global);
InternalReadAccessor globalExtractor = new GlobalExtractor(global.getName(), objectType);
return new Declaration(global.getName(), globalExtractor, new Pattern(0, objectType));
}
}
Aggregations