Search in sources :

Example 1 with PrototypeVariable

use of org.drools.model.PrototypeVariable 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)

Example 2 with PrototypeVariable

use of org.drools.model.PrototypeVariable 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 3 with PrototypeVariable

use of org.drools.model.PrototypeVariable 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)

Aggregations

Collection (java.util.Collection)3 Fact (org.drools.core.facttemplates.Fact)3 FactTemplateObjectType (org.drools.core.facttemplates.FactTemplateObjectType)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 EntryPointNode (org.drools.core.reteoo.EntryPointNode)3 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)3 DSL.field (org.drools.model.DSL.field)3 DSL.prototype (org.drools.model.DSL.prototype)3 Index (org.drools.model.Index)3 Model (org.drools.model.Model)3 PatternDSL.alphaIndexedBy (org.drools.model.PatternDSL.alphaIndexedBy)3 PatternDSL.betaIndexedBy (org.drools.model.PatternDSL.betaIndexedBy)3 PatternDSL.declarationOf (org.drools.model.PatternDSL.declarationOf)3 PatternDSL.on (org.drools.model.PatternDSL.on)3 PatternDSL.pattern (org.drools.model.PatternDSL.pattern)3 PatternDSL.reactOn (org.drools.model.PatternDSL.reactOn)3 PatternDSL.rule (org.drools.model.PatternDSL.rule)3 Prototype (org.drools.model.Prototype)3 PrototypeVariable (org.drools.model.PrototypeVariable)3 Rule (org.drools.model.Rule)3