use of org.drools.model.Prototype in project drools by kiegroup.
the class FactTemplateTest method testQuery.
@Test
public void testQuery() {
Prototype customerFact = prototype("customer");
Prototype addressFact = prototype("address");
PrototypeVariable customerV = variable(customerFact, "c");
PrototypeVariable addressV = variable(addressFact, "a");
Query query = query("Q0").build(protoPattern(customerV), protoPattern(addressV).expr("customer_id", Index.ConstraintType.EQUAL, customerV, "id"));
Model model = new ModelImpl().addQuery(query);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
MaterializedViewChangedEventListener listener = new MaterializedViewChangedEventListener();
ksession.openLiveQuery("Q0", new Object[0], listener);
Fact address1 = createMapBasedFact(addressFact);
address1.set("id", "100001");
address1.set("customer_id", "1001");
address1.set("street", "42 Main Street");
FactHandle fhA1 = ksession.insert(address1);
ksession.fireAllRules();
Fact customer1 = createMapBasedFact(customerFact);
customer1.set("id", "1001");
customer1.set("first_name", "Sally");
ksession.insert(customer1);
ksession.fireAllRules();
assertEquals(1, listener.inserts);
assertEquals(0, listener.updates);
assertEquals(0, listener.deletes);
Fact address2 = createMapBasedFact(addressFact);
address2.set("id", "100002");
address2.set("customer_id", "1001");
address2.set("street", "11 Post Dr.");
ksession.insert(address2);
ksession.fireAllRules();
assertEquals(2, listener.inserts);
assertEquals(0, listener.updates);
assertEquals(0, listener.deletes);
ksession.delete(fhA1);
ksession.fireAllRules();
assertEquals(2, listener.inserts);
assertEquals(0, listener.updates);
assertEquals(1, listener.deletes);
}
use of org.drools.model.Prototype in project drools by kiegroup.
the class FactTemplateTest method testBeta.
@Test
public void testBeta() {
Result result = new Result();
Prototype personFact = prototype("org.drools.Person", "name", "age");
PrototypeVariable markV = variable(personFact);
PrototypeVariable olderV = variable(personFact);
Rule rule = rule("beta").build(protoPattern(markV).expr("name", Index.ConstraintType.EQUAL, "Mark"), protoPattern(olderV).expr("name", Index.ConstraintType.NOT_EQUAL, "Mark").expr("age", Index.ConstraintType.GREATER_THAN, markV, "age"), on(olderV, markV).execute((p1, p2) -> result.setValue(p1.get("name") + " is older than " + p2.get("name"))));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
assertTrue(hasFactTemplateObjectType(ksession, "Person"));
Fact mark = createMapBasedFact(personFact);
mark.set("name", "Mark");
mark.set("age", 37);
Fact edson = createMapBasedFact(personFact);
edson.set("name", "Edson");
edson.set("age", 35);
Fact mario = createMapBasedFact(personFact);
mario.set("name", "Mario");
mario.set("age", 40);
FactHandle markFH = ksession.insert(mark);
FactHandle edsonFH = ksession.insert(edson);
FactHandle marioFH = ksession.insert(mario);
ksession.fireAllRules();
assertEquals("Mario is older than Mark", result.getValue());
result.setValue(null);
ksession.delete(marioFH);
ksession.fireAllRules();
assertNull(result.getValue());
mark.set("age", 34);
ksession.update(markFH, mark);
ksession.fireAllRules();
assertEquals("Edson is older than Mark", result.getValue());
}
use of org.drools.model.Prototype in project drools by kiegroup.
the class FactTemplateTest method testAlpha.
@Test
public void testAlpha() {
Prototype personFact = prototype("org.drools", "Person", field("name", Integer.class), field("age", String.class));
PrototypeVariable markV = declarationOf(personFact);
Rule rule = rule("alpha").build(pattern(markV).expr("exprA", p -> p.get("name").equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 1, p -> (String) p.get("name"), "Mark"), reactOn("name", "age")), on(markV).execute((drools, p) -> drools.insert(new Result("Found a " + p.get("age") + " years old Mark"))));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
assertTrue(hasFactTemplateObjectType(ksession, "Person"));
Fact mark = createMapBasedFact(personFact);
mark.setFieldValue("name", "Mark");
mark.setFieldValue("age", 40);
ksession.insert(mark);
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertThat(results, hasItem(new Result("Found a 40 years old Mark")));
}
use of org.drools.model.Prototype in project drools by kiegroup.
the class FactTemplateTest method testAccumulate.
@Test
public void testAccumulate() {
Prototype personFact = prototype("org.drools.Person", "name", "age");
PrototypeVariable person = variable(personFact);
Result result = new Result();
Variable<Integer> resultSum = declarationOf(Integer.class);
Variable<Double> resultAvg = declarationOf(Double.class);
Variable<Integer> age = declarationOf(Integer.class);
Rule rule = rule("accumulate").build(accumulate(pattern(person).expr(p -> ((String) p.get("name")).startsWith("M")).bind(age, p -> (int) p.get("age")), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction::new, age).as(resultSum), accFunction(org.drools.core.base.accumulators.AverageAccumulateFunction::new, age).as(resultAvg)), on(resultSum, resultAvg).execute((sum, avg) -> result.setValue("total = " + sum + "; average = " + avg)));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Fact mark = createMapBasedFact(personFact);
mark.set("name", "Mark");
mark.set("age", 37);
ksession.insert(mark);
Fact edson = createMapBasedFact(personFact);
edson.set("name", "Edson");
edson.set("age", 35);
ksession.insert(edson);
Fact mario = createMapBasedFact(personFact);
mario.set("name", "Mario");
mario.set("age", 40);
ksession.insert(mario);
ksession.fireAllRules();
assertEquals("total = 77; average = 38.5", result.getValue());
}
use of org.drools.model.Prototype in project drools by kiegroup.
the class FactTemplateTest method testBetaMixingClassAndFact.
@Test
public void testBetaMixingClassAndFact() {
Prototype personFact = prototype("org.drools.FactPerson", "name", "age");
PrototypeVariable markV = variable(personFact);
Variable<Person> olderV = declarationOf(Person.class);
Rule rule = rule("beta").build(protoPattern(markV).expr("name", Index.ConstraintType.EQUAL, "Mark"), 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() > (int) p2.get("age"), betaIndexedBy(int.class, Index.ConstraintType.GREATER_THAN, 0, p -> p.getAge(), p -> p.get("age")), reactOn("age")), on(olderV, markV).execute((drools, p1, p2) -> drools.insert(new Result(p1.getName() + " is older than " + p2.get("name")))));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
assertTrue(hasFactTemplateObjectType(ksession, "FactPerson"));
Fact mark = createMapBasedFact(personFact);
mark.set("name", "Mark");
mark.set("age", 37);
ksession.insert(mark);
FactHandle edsonFH = ksession.insert(new Person("Edson", 35));
FactHandle marioFH = ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
Collection<Result> results = getObjectsIntoList(ksession, Result.class);
assertThat(results, hasItem(new Result("Mario is older than Mark")));
}
Aggregations