Search in sources :

Example 66 with Result

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

the class ExisistentialTest method testNotEmptyPredicate.

@Test
public void testNotEmptyPredicate() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + "  not( Person( ) )\n" + "then\n" + "  insert(new Result(\"ok\"));\n" + "end";
    KieSession ksession = getKieSession(str);
    Person mario = new Person("Mario", 40);
    ksession.insert(mario);
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertEquals(0, results.size());
}
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 67 with Result

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

the class ExisistentialTest method testNot.

@Test
public void testNot() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + "  not( Person( name.length == 4 ) )\n" + "then\n" + "  insert(new Result(\"ok\"));\n" + "end";
    KieSession ksession = getKieSession(str);
    Person mario = new Person("Mario", 40);
    ksession.insert(mario);
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertEquals(1, results.size());
    assertEquals("ok", 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 68 with Result

use of org.drools.modelcompiler.domain.Result 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")));
}
Also used : CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Prototype(org.drools.model.Prototype) PrototypeVariable(org.drools.model.PrototypeVariable) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) DSL.prototype(org.drools.model.DSL.prototype) FactFactory.createMapBasedFact(org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact) Result(org.drools.modelcompiler.domain.Result) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Assert.assertThat(org.junit.Assert.assertThat) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Model(org.drools.model.Model) PatternDSL.rule(org.drools.model.PatternDSL.rule) Fact(org.drools.core.facttemplates.Fact) Variable(org.drools.model.Variable) Collection(java.util.Collection) DSL.field(org.drools.model.DSL.field) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Index(org.drools.model.Index) PatternDSL.on(org.drools.model.PatternDSL.on) PatternDSL.pattern(org.drools.model.PatternDSL.pattern) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Assert.assertNull(org.junit.Assert.assertNull) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Rule(org.drools.model.Rule) Person(org.drools.modelcompiler.domain.Person) PatternDSL.betaIndexedBy(org.drools.model.PatternDSL.betaIndexedBy) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Assert.assertEquals(org.junit.Assert.assertEquals) ModelImpl(org.drools.model.impl.ModelImpl) Prototype(org.drools.model.Prototype) PrototypeVariable(org.drools.model.PrototypeVariable) FactFactory.createMapBasedFact(org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact) Fact(org.drools.core.facttemplates.Fact) Result(org.drools.modelcompiler.domain.Result) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Test(org.junit.Test)

Example 69 with Result

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

the class FactTemplateTest method testBetaMixingClassAndFact.

@Test
public void testBetaMixingClassAndFact() {
    Prototype personFact = prototype("org.drools", "FactPerson", field("name", Integer.class), field("age", String.class));
    PrototypeVariable markV = declarationOf(personFact);
    Variable<Person> olderV = declarationOf(Person.class);
    Rule rule = rule("beta").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")), 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 -> (int) 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.setFieldValue("name", "Mark");
    mark.setFieldValue("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")));
}
Also used : CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Prototype(org.drools.model.Prototype) PrototypeVariable(org.drools.model.PrototypeVariable) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) DSL.prototype(org.drools.model.DSL.prototype) FactFactory.createMapBasedFact(org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact) Result(org.drools.modelcompiler.domain.Result) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Assert.assertThat(org.junit.Assert.assertThat) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Model(org.drools.model.Model) PatternDSL.rule(org.drools.model.PatternDSL.rule) Fact(org.drools.core.facttemplates.Fact) Variable(org.drools.model.Variable) Collection(java.util.Collection) DSL.field(org.drools.model.DSL.field) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Index(org.drools.model.Index) PatternDSL.on(org.drools.model.PatternDSL.on) PatternDSL.pattern(org.drools.model.PatternDSL.pattern) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) Assert.assertNull(org.junit.Assert.assertNull) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Rule(org.drools.model.Rule) Person(org.drools.modelcompiler.domain.Person) PatternDSL.betaIndexedBy(org.drools.model.PatternDSL.betaIndexedBy) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Assert.assertEquals(org.junit.Assert.assertEquals) ModelImpl(org.drools.model.impl.ModelImpl) Prototype(org.drools.model.Prototype) FactHandle(org.kie.api.runtime.rule.FactHandle) PrototypeVariable(org.drools.model.PrototypeVariable) FactFactory.createMapBasedFact(org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact) Fact(org.drools.core.facttemplates.Fact) Result(org.drools.modelcompiler.domain.Result) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) 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)

Example 70 with Result

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

the class FlowTest method testFromAccumulate.

@Test
public void testFromAccumulate() {
    final Variable<Number> var_$sum = declarationOf(Number.class, "$sum");
    final Variable<Person> var_$p = declarationOf(Person.class, "$p");
    final Variable<Integer> var_$expr$3$ = declarationOf(Integer.class, "$expr$3$");
    org.drools.model.Rule rule = rule("X").build(bind(var_$expr$3$).as(var_$p, (_this) -> _this.getAge()), expr("$expr$1$", var_$sum, (_this) -> _this.intValue() > 0).reactOn("intValue"), accumulate(expr("$expr$2$", var_$p, (_this) -> _this.getName().startsWith("M")), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, var_$expr$3$).as(var_$sum)), 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());
}
Also used : Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) FlowDSL.eval(org.drools.model.FlowDSL.eval) ObjectOutput(java.io.ObjectOutput) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) FlowDSL.not(org.drools.model.FlowDSL.not) Toy(org.drools.modelcompiler.domain.Toy) FlowDSL.and(org.drools.model.FlowDSL.and) FlowDSL.accFunction(org.drools.model.FlowDSL.accFunction) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Assert.assertThat(org.junit.Assert.assertThat) Query2Def(org.drools.model.Query2Def) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) FlowDSL.from(org.drools.model.FlowDSL.from) QueryResults(org.kie.api.runtime.rule.QueryResults) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) FlowDSL.reactiveFrom(org.drools.model.FlowDSL.reactiveFrom) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) TargetPolicy(org.drools.modelcompiler.domain.TargetPolicy) FlowDSL.or(org.drools.model.FlowDSL.or) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) FlowDSL.on(org.drools.model.FlowDSL.on) Customer(org.drools.modelcompiler.domain.Customer) Serializable(java.io.Serializable) ConstraintType(org.drools.model.Index.ConstraintType) List(java.util.List) Query(org.drools.model.Query) FlowDSL.executeScript(org.drools.model.FlowDSL.executeScript) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) Assert.assertFalse(org.junit.Assert.assertFalse) KnowledgeBaseFactory(org.drools.core.impl.KnowledgeBaseFactory) FlowDSL.bind(org.drools.model.FlowDSL.bind) FlowDSL.valueOf(org.drools.model.FlowDSL.valueOf) Person(org.drools.modelcompiler.domain.Person) ObjectInput(java.io.ObjectInput) InOperator(org.drools.model.operators.InOperator) FlowDSL.when(org.drools.model.FlowDSL.when) ModelImpl(org.drools.model.impl.ModelImpl) FlowDSL.globalOf(org.drools.model.FlowDSL.globalOf) StockTick(org.drools.modelcompiler.domain.StockTick) Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) ClockType(org.drools.core.ClockType) FlowDSL.accumulate(org.drools.model.FlowDSL.accumulate) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Adult(org.drools.modelcompiler.domain.Adult) Employee(org.drools.modelcompiler.domain.Employee) FlowDSL.execute(org.drools.model.FlowDSL.execute) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) FlowDSL.rule(org.drools.model.FlowDSL.rule) FlowDSL.expr(org.drools.model.FlowDSL.expr) Variable(org.drools.model.Variable) FlowDSL.query(org.drools.model.FlowDSL.query) Address(org.drools.modelcompiler.domain.Address) FlowDSL.window(org.drools.model.FlowDSL.window) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) FlowDSL.input(org.drools.model.FlowDSL.input) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) FlowDSL.forall(org.drools.model.FlowDSL.forall) Rule(org.drools.model.Rule) Query1Def(org.drools.model.Query1Def) Assert.assertEquals(org.junit.Assert.assertEquals) Result(org.drools.modelcompiler.domain.Result) KieBase(org.kie.api.KieBase) Rule(org.drools.model.Rule) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) 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