Search in sources :

Example 16 with Result

use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.

the class CompilerTest method testSimpleInsert.

@Test
public void testSimpleInsert() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person( name.length == 4 )\n" + "then\n" + "  Result r = new Result($p.getName());" + "  insert(r);\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mark", 37));
    ksession.insert(new Person("Mario", 40));
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertEquals(1, results.size());
    assertEquals("Mark", results.iterator().next().getValue());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 17 with Result

use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.

the class CompilerTest method testBindWith2Arguments.

@Test
public void testBindWith2Arguments() {
    String str = "import " + Adult.class.getCanonicalName() + ";\n" + "import " + Child.class.getCanonicalName() + ";\n" + "import " + Result.class.getCanonicalName() + ";\n" + "rule R when\n" + "  $y : Adult( $sum : (name.length + age) )\n" + "then\n" + "  insert(new Result($sum));\n" + "end";
    KieSession ksession = getKieSession(str);
    Adult a = new Adult("Mario", 43);
    ksession.insert(a);
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertEquals(((Number) results.iterator().next().getValue()).intValue(), 48);
}
Also used : Adult(org.drools.modelcompiler.domain.Adult) KieSession(org.kie.api.runtime.KieSession) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 18 with Result

use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.

the class CompilerTest method testAgeWithSumUsing2DeclarationInBeta.

@Test
public void testAgeWithSumUsing2DeclarationInBeta() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $p : Person( personAge : age )\n" + "  $plusTwo : Person(age == personAge + 2 + $p.age - $p.age )\n" + "then\n" + "  insert(new Result($plusTwo.getName()));\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mario", 40));
    ksession.insert(new Person("Mark", 37));
    ksession.insert(new Person("Edson", 35));
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    System.out.println(results);
    assertEquals(1, results.size());
    assertEquals("Mark", results.iterator().next().getValue());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 19 with Result

use of org.drools.modelcompiler.domain.Result in project drools by kiegroup.

the class CompilerTest method testBetaWithDeclaration.

@Test
public void testBetaWithDeclaration() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $r : Result()\n" + "  $p1 : Person(name == \"Mark\", $markAge : age)\n" + "  $p2 : Person(name != \"Mark\", age > $markAge)\n" + "then\n" + "  $r.setValue($p2.getName() + \" is older than \" + $p1.getName());\n" + "end";
    KieSession ksession = getKieSession(str);
    Result result = new Result();
    ksession.insert(result);
    Person mark = new Person("Mark", 37);
    Person edson = new Person("Edson", 35);
    Person mario = new Person("Mario", 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.setAge(34);
    ksession.update(markFH, mark, "age");
    ksession.fireAllRules();
    assertEquals("Edson is older than Mark", result.getValue());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 20 with Result

use of org.drools.modelcompiler.domain.Result 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());
}
Also used : CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) DSL(org.drools.model.DSL) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Assert.assertThat(org.junit.Assert.assertThat) Query2Def(org.drools.model.Query2Def) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) QueryImpl(org.drools.core.rule.QueryImpl) PatternDSL.rule(org.drools.model.PatternDSL.rule) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) Index(org.drools.model.Index) PatternDSL.on(org.drools.model.PatternDSL.on) PatternDSL.pattern(org.drools.model.PatternDSL.pattern) List(java.util.List) Query(org.drools.model.Query) PatternDSL.or(org.drools.model.PatternDSL.or) PatternDSL.globalOf(org.drools.model.PatternDSL.globalOf) Person(org.drools.modelcompiler.domain.Person) PatternDSL.valueOf(org.drools.model.PatternDSL.valueOf) ModelImpl(org.drools.model.impl.ModelImpl) BitMask(org.drools.model.BitMask) PatternDSL.execute(org.drools.model.PatternDSL.execute) StockTick(org.drools.modelcompiler.domain.StockTick) PatternDSL.after(org.drools.model.PatternDSL.after) ClockType(org.drools.core.ClockType) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Adult(org.drools.modelcompiler.domain.Adult) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) PatternDSL.query(org.drools.model.PatternDSL.query) PatternDSL.reactiveFrom(org.drools.model.PatternDSL.reactiveFrom) KieServices(org.kie.api.KieServices) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) PatternDSL.and(org.drools.model.PatternDSL.and) Variable(org.drools.model.Variable) Test(org.junit.Test) PatternDSL.accumulate(org.drools.model.PatternDSL.accumulate) PatternDSL.not(org.drools.model.PatternDSL.not) PatternDSL.when(org.drools.model.PatternDSL.when) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) PatternDSL.accFunction(org.drools.model.PatternDSL.accFunction) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.drools.model.Rule) PatternDSL.betaIndexedBy(org.drools.model.PatternDSL.betaIndexedBy) Assert.assertEquals(org.junit.Assert.assertEquals) Query(org.drools.model.Query) Result(org.drools.modelcompiler.domain.Result) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Aggregations

Result (org.drools.modelcompiler.domain.Result)91 KieSession (org.kie.api.runtime.KieSession)91 Test (org.junit.Test)90 Person (org.drools.modelcompiler.domain.Person)84 FactHandle (org.kie.api.runtime.rule.FactHandle)36 Collection (java.util.Collection)35 Adult (org.drools.modelcompiler.domain.Adult)35 Child (org.drools.modelcompiler.domain.Child)35 Model (org.drools.model.Model)32 Rule (org.drools.model.Rule)32 Variable (org.drools.model.Variable)32 ModelImpl (org.drools.model.impl.ModelImpl)32 BaseModelTest.getObjectsIntoList (org.drools.modelcompiler.BaseModelTest.getObjectsIntoList)32 KieBaseBuilder (org.drools.modelcompiler.builder.KieBaseBuilder)32 CoreMatchers.hasItem (org.hamcrest.CoreMatchers.hasItem)32 Assert.assertEquals (org.junit.Assert.assertEquals)32 Assert.assertNull (org.junit.Assert.assertNull)32 Assert.assertThat (org.junit.Assert.assertThat)32 KieBase (org.kie.api.KieBase)32 ArrayList (java.util.ArrayList)30