use of org.kie.api.runtime.KieSession in project drools by kiegroup.
the class FlowTest method testNotEmptyPredicate.
@Test
public void testNotEmptyPredicate() {
Rule rule = rule("R").build(not(input(declarationOf(Person.class))), execute((drools) -> drools.insert(new Result("ok"))));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Person mario = new Person("Mario", 40);
ksession.insert(mario);
ksession.fireAllRules();
assertTrue(ksession.getObjects(new ClassObjectFilter(Result.class)).isEmpty());
}
use of org.kie.api.runtime.KieSession 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.kie.api.runtime.KieSession 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.kie.api.runtime.KieSession 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.kie.api.runtime.KieSession 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());
}
Aggregations