use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class KiePackagesBuilder method populateLHS.
private void populateLHS(RuleContext ctx, View view) {
GroupElement lhs = ctx.getRule().getLhs();
addSubConditions(ctx, lhs, view.getSubConditions());
if (requiresLeftActivation(lhs)) {
lhs.addChild(0, new Pattern(ctx.getNextPatternIndex(), ClassObjectType.InitialFact_ObjectType));
}
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class PatternDSLTest method testAfterWithAnd.
@Test
public void testAfterWithAnd() throws Exception {
Variable<StockTick> var_$a = declarationOf(StockTick.class, "$a");
Variable<StockTick> var_$b = declarationOf(StockTick.class, "$b");
Rule rule = rule("R").build(pattern(var_$a).expr("$expr$3$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "DROO"), alphaIndexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "DROO"), reactOn("company")), pattern(var_$b).and().expr("$expr$5$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "ACME"), alphaIndexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "ACME"), reactOn("company")).expr("$expr$6$", var_$a, after(5L, java.util.concurrent.TimeUnit.SECONDS, 8L, java.util.concurrent.TimeUnit.SECONDS)).endAnd(), execute(() -> {
System.out.println("fired");
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model, EventProcessingOption.STREAM);
KieSessionConfiguration conf = KieServices.get().newKieSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = kieBase.newKieSession(conf, null);
SessionPseudoClock clock = ksession.getSessionClock();
ksession.insert(new StockTick("DROO"));
clock.advanceTime(6, TimeUnit.SECONDS);
ksession.insert(new StockTick("ACME"));
assertEquals(1, ksession.fireAllRules());
clock.advanceTime(4, TimeUnit.SECONDS);
ksession.insert(new StockTick("ACME"));
assertEquals(0, ksession.fireAllRules());
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class PatternDSLTest method testQueryInRule.
@Test
public void testQueryInRule() {
Variable<Person> personV = DSL.declarationOf(Person.class);
Query2Def<Person, Integer> qdef = query("olderThan", Person.class, Integer.class);
Query query = qdef.build(pattern(qdef.getArg1()).expr("exprA", qdef.getArg2(), (p, a) -> p.getAge() > a));
Variable<Person> personVRule = DSL.declarationOf(Person.class);
Rule rule = rule("R").build(qdef.call(personVRule, valueOf(40)), on(personVRule).execute((drools, p) -> drools.insert(new Result(p.getName()))));
Model model = new ModelImpl().addQuery(query).addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
ksession.fireAllRules();
Collection<Result> results = (Collection<Result>) ksession.getObjects(new ClassObjectFilter(Result.class));
assertEquals(1, results.size());
assertEquals("Mario", results.iterator().next().getValue());
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class PatternDSLTest method testAccumulateConstrainingValue.
@Test
public void testAccumulateConstrainingValue() {
Variable<org.drools.modelcompiler.domain.Person> var_$p = declarationOf(org.drools.modelcompiler.domain.Person.class, "$p");
Variable<Integer> var_$expr$5$ = declarationOf(Integer.class, "$expr$5$");
Variable<java.lang.Integer> var_$sum = declarationOf(java.lang.Integer.class, "$sum");
Rule rule = rule("X").build(accumulate(pattern(var_$p).expr("$expr$4$", (_this) -> _this.getName().startsWith("M")).bind(var_$expr$5$, (_this) -> _this.getAge()), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, var_$expr$5$).as(var_$sum)), pattern(var_$sum).expr("$expr$3$", (_this) -> _this > 50), on(var_$sum).execute((drools, $sum) -> {
drools.insert(new Result($sum));
}));
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();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertEquals(1, results.size());
assertEquals(77, results.iterator().next().getValue());
}
use of org.drools.core.rule.Pattern in project drools by kiegroup.
the class PatternDSLTest method testBreakingNamedConsequence.
@Test
public void testBreakingNamedConsequence() {
Variable<Result> resultV = declarationOf(Result.class);
Variable<Person> markV = declarationOf(Person.class);
Variable<Person> olderV = declarationOf(Person.class);
Rule rule = rule("beta").build(pattern(resultV), pattern(markV).expr("exprA", p -> p.getName().equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 1, p -> p.getName(), "Mark"), reactOn("name", "age")), when("cond1", markV, p -> p.getAge() < 30).then(on(markV, resultV).breaking().execute((p, r) -> r.addValue("Found young " + p.getName()))).elseWhen("cond2", markV, p -> p.getAge() > 50).then(on(markV, resultV).breaking().execute((p, r) -> r.addValue("Found old " + p.getName()))).elseWhen().then(on(markV, resultV).breaking().execute((p, r) -> r.addValue("Found " + p.getName()))), pattern(olderV).expr("exprB", p -> !p.getName().equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.NOT_EQUAL, 1, p -> p.getName(), "Mark"), reactOn("name")).expr("exprC", markV, (p1, p2) -> p1.getAge() > p2.getAge(), betaIndexedBy(int.class, Index.ConstraintType.GREATER_THAN, 0, p -> p.getAge(), p -> p.getAge()), reactOn("age")), on(olderV, markV, resultV).execute((p1, p2, r) -> r.addValue(p1.getName() + " is older than " + p2.getName())));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Result result = new Result();
ksession.insert(result);
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
Collection<String> results = (Collection<String>) result.getValue();
assertEquals(1, results.size());
assertEquals("Found Mark", results.iterator().next());
}
Aggregations