Search in sources :

Example 1 with Prototype

use of org.drools.model.Prototype in project drools by kiegroup.

the class FactTemplateTest method testQuery.

@Test
public void testQuery() {
    Prototype customerFact = prototype("customer");
    Prototype addressFact = prototype("address");
    PrototypeVariable customerV = variable(customerFact, "c");
    PrototypeVariable addressV = variable(addressFact, "a");
    Query query = query("Q0").build(protoPattern(customerV), protoPattern(addressV).expr("customer_id", Index.ConstraintType.EQUAL, customerV, "id"));
    Model model = new ModelImpl().addQuery(query);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    MaterializedViewChangedEventListener listener = new MaterializedViewChangedEventListener();
    ksession.openLiveQuery("Q0", new Object[0], listener);
    Fact address1 = createMapBasedFact(addressFact);
    address1.set("id", "100001");
    address1.set("customer_id", "1001");
    address1.set("street", "42 Main Street");
    FactHandle fhA1 = ksession.insert(address1);
    ksession.fireAllRules();
    Fact customer1 = createMapBasedFact(customerFact);
    customer1.set("id", "1001");
    customer1.set("first_name", "Sally");
    ksession.insert(customer1);
    ksession.fireAllRules();
    assertEquals(1, listener.inserts);
    assertEquals(0, listener.updates);
    assertEquals(0, listener.deletes);
    Fact address2 = createMapBasedFact(addressFact);
    address2.set("id", "100002");
    address2.set("customer_id", "1001");
    address2.set("street", "11 Post Dr.");
    ksession.insert(address2);
    ksession.fireAllRules();
    assertEquals(2, listener.inserts);
    assertEquals(0, listener.updates);
    assertEquals(0, listener.deletes);
    ksession.delete(fhA1);
    ksession.fireAllRules();
    assertEquals(2, listener.inserts);
    assertEquals(0, listener.updates);
    assertEquals(1, listener.deletes);
}
Also used : Prototype(org.drools.model.Prototype) Query(org.drools.model.Query) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) PrototypeVariable(org.drools.model.PrototypeVariable) KieSession(org.kie.api.runtime.KieSession) ModelImpl(org.drools.model.impl.ModelImpl) FactFactory.createMapBasedFact(org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact) Fact(org.drools.core.facttemplates.Fact) Test(org.junit.Test)

Example 2 with Prototype

use of org.drools.model.Prototype in project drools by kiegroup.

the class FactTemplateTest method testBeta.

@Test
public void testBeta() {
    Result result = new Result();
    Prototype personFact = prototype("org.drools.Person", "name", "age");
    PrototypeVariable markV = variable(personFact);
    PrototypeVariable olderV = variable(personFact);
    Rule rule = rule("beta").build(protoPattern(markV).expr("name", Index.ConstraintType.EQUAL, "Mark"), protoPattern(olderV).expr("name", Index.ConstraintType.NOT_EQUAL, "Mark").expr("age", Index.ConstraintType.GREATER_THAN, markV, "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.set("name", "Mark");
    mark.set("age", 37);
    Fact edson = createMapBasedFact(personFact);
    edson.set("name", "Edson");
    edson.set("age", 35);
    Fact mario = createMapBasedFact(personFact);
    mario.set("name", "Mario");
    mario.set("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.set("age", 34);
    ksession.update(markFH, mark);
    ksession.fireAllRules();
    assertEquals("Edson is older than Mark", result.getValue());
}
Also used : PrototypeDSL.variable(org.drools.model.PrototypeDSL.variable) PrototypeDSL.protoPattern(org.drools.model.PrototypeDSL.protoPattern) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Prototype(org.drools.model.Prototype) PrototypeDSL.field(org.drools.model.PrototypeDSL.field) PrototypeVariable(org.drools.model.PrototypeVariable) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) FactFactory.createMapBasedFact(org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact) Result(org.drools.modelcompiler.domain.Result) DSL.accumulate(org.drools.model.DSL.accumulate) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) PatternDSL.query(org.drools.model.PatternDSL.query) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Model(org.drools.model.Model) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) PatternDSL.rule(org.drools.model.PatternDSL.rule) Fact(org.drools.core.facttemplates.Fact) Variable(org.drools.model.Variable) Collection(java.util.Collection) Row(org.kie.api.runtime.rule.Row) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PrototypeDSL.prototype(org.drools.model.PrototypeDSL.prototype) CompositeObjectSinkAdapter(org.drools.core.reteoo.CompositeObjectSinkAdapter) 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) Query(org.drools.model.Query) DSL.accFunction(org.drools.model.DSL.accFunction) Assert.assertNull(org.junit.Assert.assertNull) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) 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) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) PrototypeVariable(org.drools.model.PrototypeVariable) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) FactFactory.createMapBasedFact(org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact) Fact(org.drools.core.facttemplates.Fact) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 3 with Prototype

use of org.drools.model.Prototype 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 4 with Prototype

use of org.drools.model.Prototype in project drools by kiegroup.

the class FactTemplateTest method testAccumulate.

@Test
public void testAccumulate() {
    Prototype personFact = prototype("org.drools.Person", "name", "age");
    PrototypeVariable person = variable(personFact);
    Result result = new Result();
    Variable<Integer> resultSum = declarationOf(Integer.class);
    Variable<Double> resultAvg = declarationOf(Double.class);
    Variable<Integer> age = declarationOf(Integer.class);
    Rule rule = rule("accumulate").build(accumulate(pattern(person).expr(p -> ((String) p.get("name")).startsWith("M")).bind(age, p -> (int) p.get("age")), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction::new, age).as(resultSum), accFunction(org.drools.core.base.accumulators.AverageAccumulateFunction::new, age).as(resultAvg)), on(resultSum, resultAvg).execute((sum, avg) -> result.setValue("total = " + sum + "; average = " + avg)));
    Model model = new ModelImpl().addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    Fact mark = createMapBasedFact(personFact);
    mark.set("name", "Mark");
    mark.set("age", 37);
    ksession.insert(mark);
    Fact edson = createMapBasedFact(personFact);
    edson.set("name", "Edson");
    edson.set("age", 35);
    ksession.insert(edson);
    Fact mario = createMapBasedFact(personFact);
    mario.set("name", "Mario");
    mario.set("age", 40);
    ksession.insert(mario);
    ksession.fireAllRules();
    assertEquals("total = 77; average = 38.5", result.getValue());
}
Also used : PrototypeDSL.variable(org.drools.model.PrototypeDSL.variable) PrototypeDSL.protoPattern(org.drools.model.PrototypeDSL.protoPattern) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Prototype(org.drools.model.Prototype) PrototypeDSL.field(org.drools.model.PrototypeDSL.field) PrototypeVariable(org.drools.model.PrototypeVariable) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) FactFactory.createMapBasedFact(org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact) Result(org.drools.modelcompiler.domain.Result) DSL.accumulate(org.drools.model.DSL.accumulate) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) PatternDSL.query(org.drools.model.PatternDSL.query) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Model(org.drools.model.Model) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) PatternDSL.rule(org.drools.model.PatternDSL.rule) Fact(org.drools.core.facttemplates.Fact) Variable(org.drools.model.Variable) Collection(java.util.Collection) Row(org.kie.api.runtime.rule.Row) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PrototypeDSL.prototype(org.drools.model.PrototypeDSL.prototype) CompositeObjectSinkAdapter(org.drools.core.reteoo.CompositeObjectSinkAdapter) 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) Query(org.drools.model.Query) DSL.accFunction(org.drools.model.DSL.accFunction) Assert.assertNull(org.junit.Assert.assertNull) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) 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 5 with Prototype

use of org.drools.model.Prototype in project drools by kiegroup.

the class FactTemplateTest method testBetaMixingClassAndFact.

@Test
public void testBetaMixingClassAndFact() {
    Prototype personFact = prototype("org.drools.FactPerson", "name", "age");
    PrototypeVariable markV = variable(personFact);
    Variable<Person> olderV = declarationOf(Person.class);
    Rule rule = rule("beta").build(protoPattern(markV).expr("name", Index.ConstraintType.EQUAL, "Mark"), 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 -> 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.set("name", "Mark");
    mark.set("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 : PrototypeDSL.variable(org.drools.model.PrototypeDSL.variable) PrototypeDSL.protoPattern(org.drools.model.PrototypeDSL.protoPattern) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Prototype(org.drools.model.Prototype) PrototypeDSL.field(org.drools.model.PrototypeDSL.field) PrototypeVariable(org.drools.model.PrototypeVariable) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) FactFactory.createMapBasedFact(org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact) Result(org.drools.modelcompiler.domain.Result) DSL.accumulate(org.drools.model.DSL.accumulate) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) PatternDSL.query(org.drools.model.PatternDSL.query) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Model(org.drools.model.Model) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) PatternDSL.rule(org.drools.model.PatternDSL.rule) Fact(org.drools.core.facttemplates.Fact) Variable(org.drools.model.Variable) Collection(java.util.Collection) Row(org.kie.api.runtime.rule.Row) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PrototypeDSL.prototype(org.drools.model.PrototypeDSL.prototype) CompositeObjectSinkAdapter(org.drools.core.reteoo.CompositeObjectSinkAdapter) 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) Query(org.drools.model.Query) DSL.accFunction(org.drools.model.DSL.accFunction) Assert.assertNull(org.junit.Assert.assertNull) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) 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

Fact (org.drools.core.facttemplates.Fact)7 Model (org.drools.model.Model)7 Prototype (org.drools.model.Prototype)7 PrototypeVariable (org.drools.model.PrototypeVariable)7 ModelImpl (org.drools.model.impl.ModelImpl)7 FactFactory.createMapBasedFact (org.drools.modelcompiler.facttemplate.FactFactory.createMapBasedFact)7 Test (org.junit.Test)7 KieBase (org.kie.api.KieBase)7 KieSession (org.kie.api.runtime.KieSession)7 FactHandle (org.kie.api.runtime.rule.FactHandle)7 Collection (java.util.Collection)6 FactTemplateObjectType (org.drools.core.facttemplates.FactTemplateObjectType)6 EntryPointNode (org.drools.core.reteoo.EntryPointNode)6 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)6 Index (org.drools.model.Index)6 PatternDSL.alphaIndexedBy (org.drools.model.PatternDSL.alphaIndexedBy)6 PatternDSL.betaIndexedBy (org.drools.model.PatternDSL.betaIndexedBy)6 PatternDSL.declarationOf (org.drools.model.PatternDSL.declarationOf)6 PatternDSL.on (org.drools.model.PatternDSL.on)6 PatternDSL.pattern (org.drools.model.PatternDSL.pattern)6