Search in sources :

Example 36 with Result

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

the class EvalTest method testFunction2.

@Test
public void testFunction2() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "function Boolean isFortyYearsOld(Person p, Boolean booleanParameter) {\n" + "    return p.getAge() == 40; \n" + "}" + "rule R when\n" + "  $p : Person()\n" + "  eval( isFortyYearsOld($p, true) )\n" + "then\n" + "  insert(new Result($p.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);
    assertEquals(1, results.size());
    assertEquals("Mario", 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 37 with Result

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

the class ExisistentialTest method testExists.

@Test
public void testExists() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + "  exists Person( name.length == 5 )\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 38 with Result

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

the class ExisistentialTest method testExistsEmptyPredicate.

@Test
public void testExistsEmptyPredicate() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + "  exists( Person() )\n" + "then\n" + "  insert(new Result(\"ok\"));\n" + "end";
    KieSession ksession = getKieSession(str);
    Person mark = new Person("Mark", 37);
    Person mario = new Person("Mario", 40);
    ksession.insert(mark);
    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 39 with Result

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

the class ExisistentialTest method testForall.

@Test
public void testForall() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Result.class.getCanonicalName() + ";" + "rule R when\n" + "  forall( $p : Person( name.length == 5 ) " + "       Person( this == $p, age > 40 ) )\n" + "then\n" + "  insert(new Result(\"ok\"));\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mario", 41));
    ksession.insert(new Person("Mark", 39));
    ksession.insert(new Person("Edson", 42));
    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 40 with Result

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

the class FactTemplateTest method testBeta.

@Test
public void testBeta() {
    Result result = new Result();
    Prototype personFact = prototype("org.drools", "Person", field("name", Integer.class), field("age", String.class));
    PrototypeVariable markV = declarationOf(personFact);
    PrototypeVariable olderV = declarationOf(personFact);
    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.get("name").equals("Mark"), alphaIndexedBy(String.class, Index.ConstraintType.NOT_EQUAL, 1, p -> (String) p.get("name"), "Mark"), reactOn("name")).expr("exprC", markV, (p1, p2) -> (int) p1.get("age") > (int) p2.get("age"), betaIndexedBy(int.class, Index.ConstraintType.GREATER_THAN, 0, p -> (int) p.get("age"), p -> (int) p.get("age")), reactOn("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.setFieldValue("name", "Mark");
    mark.setFieldValue("age", 37);
    Fact edson = createMapBasedFact(personFact);
    edson.setFieldValue("name", "Edson");
    edson.setFieldValue("age", 35);
    Fact mario = createMapBasedFact(personFact);
    mario.setFieldValue("name", "Mario");
    mario.setFieldValue("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.setFieldValue("age", 34);
    ksession.update(markFH, mark);
    ksession.fireAllRules();
    assertEquals("Edson is older than Mark", result.getValue());
}
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) 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