use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testMetadataBasics.
@Test
public void testMetadataBasics() {
final String PACKAGE_NAME = "org.asd";
final String RULE_NAME = "hello world";
final String RULE_KEY = "output";
final String RULE_VALUE = "Hello world!";
org.drools.model.Rule rule = rule(PACKAGE_NAME, RULE_NAME).metadata(RULE_KEY, // equivalent of DRL form: @output("\"Hello world!\"")
"\"" + RULE_VALUE + "\"").build(execute(() -> {
System.out.println("Hello world!");
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
final Map<String, Object> metadata = ksession.getKieBase().getRule(PACKAGE_NAME, RULE_NAME).getMetaData();
Assertions.assertThat(metadata.containsKey(RULE_KEY)).isTrue();
// testing of the DRL form: @output("\"Hello world!\"")
Assertions.assertThat(metadata.get(RULE_KEY)).isEqualTo("\"" + RULE_VALUE + "\"");
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testMVELmodify.
@Test
public void testMVELmodify() {
final Variable<Person> var_$p = declarationOf(Person.class, "$p");
final org.drools.model.BitMask mask_$p = org.drools.model.BitMask.getPatternMask(Person.class, "age");
org.drools.model.Rule rule = rule("R").build(input(var_$p), on(var_$p).executeScript("mvel", "System.out.println($p); modify($p) { setAge(1); } System.out.println($p);"));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Matteo", 47));
ksession.fireAllRules();
List<Person> results = getObjectsIntoList(ksession, Person.class);
assertEquals(1, results.get(0).getAge());
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testQueryWithNamedArg.
@Test
public void testQueryWithNamedArg() {
Variable<Person> personV = declarationOf(Person.class, "$p");
Query1Def<Integer> qdef = query("olderThan", Integer.class, "ageArg");
Query query = qdef.build(expr("exprA", personV, qdef.getArg("ageArg", Integer.class), (p, a) -> p.getAge() > a));
Model model = new ModelImpl().addQuery(query);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert(new Person("Mark", 39));
ksession.insert(new Person("Mario", 41));
QueryResults results = ksession.getQueryResults("olderThan", 40);
assertEquals(1, results.size());
Person p = (Person) results.iterator().next().get("$p");
assertEquals("Mario", p.getName());
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method test3Patterns.
@Test
public void test3Patterns() {
Result result = new Result();
Variable<Person> personV = declarationOf(Person.class);
Variable<Person> markV = declarationOf(Person.class);
Variable<String> nameV = declarationOf(String.class);
Rule rule = rule("myrule").build(expr("exprA", markV, p -> p.getName().equals("Mark")), expr("exprB", personV, markV, (p1, p2) -> p1.getAge() > p2.getAge()), expr("exprC", nameV, personV, (s, p) -> s.equals(p.getName())), on(nameV).execute(result::setValue));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
ksession.insert("Mario");
ksession.insert(new Person("Mark", 37));
ksession.insert(new Person("Edson", 35));
ksession.insert(new Person("Mario", 40));
ksession.fireAllRules();
assertEquals("Mario", result.getValue());
}
use of org.drools.model.impl.ModelImpl in project drools by kiegroup.
the class FlowTest method testNamedConsequence.
@Test
public void testNamedConsequence() {
Variable<Result> resultV = declarationOf(Result.class);
Variable<Person> markV = declarationOf(Person.class);
Variable<Person> olderV = declarationOf(Person.class);
Rule rule = rule("beta").build(expr("exprA", markV, p -> p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.EQUAL, 1, Person::getName, "Mark").reactOn("name", // also react on age, see RuleDescr.lookAheadFieldsOfIdentifier
"age"), on(markV, resultV).execute((p, r) -> r.addValue("Found " + p.getName())), expr("exprB", olderV, p -> !p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.NOT_EQUAL, 1, Person::getName, "Mark").reactOn("name"), expr("exprC", olderV, markV, (p1, p2) -> p1.getAge() > p2.getAge()).indexedBy(int.class, ConstraintType.GREATER_THAN, 0, Person::getAge, Person::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(2, results.size());
assertTrue(results.containsAll(asList("Found Mark", "Mario is older than Mark")));
}
Aggregations