use of org.drools.model.Variable in project drools by kiegroup.
the class PatternDSLTest method testWatch.
@Test
public void testWatch() {
Variable<Person> var_$p = declarationOf(Person.class, "$p");
BitMask mask_$p = BitMask.getPatternMask(org.drools.modelcompiler.domain.Person.class, "age");
Rule rule = rule("R").build(pattern(var_$p).expr("$expr$1$", (_this) -> _this.getAge() < 50, alphaIndexedBy(int.class, Index.ConstraintType.LESS_THAN, 0, _this -> _this.getAge(), 50), reactOn("age")).watch("!age"), on(var_$p).execute((drools, $p) -> {
$p.setAge($p.getAge() + 1);
drools.update($p, mask_$p);
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Person p = new Person("Mario", 40);
ksession.insert(p);
ksession.fireAllRules();
assertEquals(41, p.getAge());
}
use of org.drools.model.Variable in project drools by kiegroup.
the class PatternDSLTest method testNot.
@Test
public void testNot() {
Result result = new Result();
Variable<Person> oldestV = DSL.declarationOf(Person.class);
Variable<Person> otherV = DSL.declarationOf(Person.class);
Rule rule = rule("not").build(pattern(oldestV), not(pattern(otherV).expr("exprA", oldestV, (p1, p2) -> p1.getAge() > p2.getAge())), on(oldestV).execute(p -> result.setValue("Oldest person is " + p.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
assertEquals("Oldest person is Mario", result.getValue());
}
use of org.drools.model.Variable in project drools by kiegroup.
the class PatternDSLTest method testQueryInvokingQuery.
@Test
public void testQueryInvokingQuery() {
Variable<Relationship> relV = DSL.declarationOf(Relationship.class);
Query2Def<String, String> query1Def = query("isRelatedTo1", String.class, String.class);
Query2Def<String, String> query2Def = query("isRelatedTo2", String.class, String.class);
Query query2 = query2Def.build(pattern(relV).expr("exprA", query2Def.getArg1(), (r, s) -> r.getStart().equals(s)).expr("exprB", query2Def.getArg2(), (r, e) -> r.getEnd().equals(e)));
Query query1 = query1Def.build(query2Def.call(query1Def.getArg1(), query1Def.getArg2()));
Model model = new ModelImpl().addQuery(query2).addQuery(query1);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Relationship("A", "B"));
ksession.insert(new Relationship("B", "C"));
QueryResults results = ksession.getQueryResults("isRelatedTo1", "A", "B");
assertEquals(1, results.size());
assertEquals("B", results.iterator().next().get(query1Def.getArg2().getName()));
}
use of org.drools.model.Variable in project drools by kiegroup.
the class AccumulatePatternImpl method findPatternImplSource.
private Pattern findPatternImplSource() {
if (condition instanceof Pattern) {
return (Pattern) condition;
}
final Variable source = getAccumulateFunctions()[0].getSource();
for (Condition subCondition : condition.getSubConditions()) {
if (subCondition instanceof PatternImpl) {
PatternImpl patternImpl = (PatternImpl) subCondition;
boolean isSource = patternImpl.getBindings().stream().anyMatch(b -> (b instanceof Binding) && ((Binding) b).getBoundVariable().equals(source));
if (isSource) {
return patternImpl;
}
}
}
return null;
}
use of org.drools.model.Variable in project drools by kiegroup.
the class KiePackagesBuilder method processConsequence.
private void processConsequence(RuleContext ctx, Consequence consequence, String name) {
if (name.equals(RuleImpl.DEFAULT_CONSEQUENCE_NAME)) {
if ("java".equals(consequence.getLanguage())) {
ctx.getRule().setConsequence(new LambdaConsequence(consequence, ctx));
} else if ("mvel".equals(consequence.getLanguage())) {
ctx.getRule().setConsequence(new MVELConsequence(consequence, ctx));
} else {
throw new UnsupportedOperationException("Unknown script language for consequence: " + consequence.getLanguage());
}
} else {
ctx.getRule().addNamedConsequence(name, new LambdaConsequence(consequence, ctx));
}
Variable[] consequenceVars = consequence.getDeclarations();
String[] requiredDeclarations = new String[consequenceVars.length];
for (int i = 0; i < consequenceVars.length; i++) {
requiredDeclarations[i] = consequenceVars[i].getName();
}
ctx.getRule().setRequiredDeclarationsForConsequence(name, requiredDeclarations);
}
Aggregations