Search in sources :

Example 6 with Prototype

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

the class FactTemplateTest method testIndexedAlpha.

@Test
public void testIndexedAlpha() {
    Prototype personFact = prototype("org.drools.Person", "name", "age");
    PrototypeVariable personV = variable(personFact);
    Rule rule1 = rule("alpha1").build(protoPattern(personV).expr("name", Index.ConstraintType.EQUAL, "Mark"), on(personV).execute((drools, p) -> drools.insert(new Result("Found a " + p.get("age") + " years old Mark"))));
    Rule rule2 = rule("alpha2").build(protoPattern(personV).expr("name", Index.ConstraintType.EQUAL, "Mario"), on(personV).execute((drools, p) -> drools.insert(new Result("Found a " + p.get("age") + " years old Mario"))));
    Rule rule3 = rule("alpha3").build(protoPattern(personV).expr("name", Index.ConstraintType.EQUAL, "Edson"), on(personV).execute((drools, p) -> drools.insert(new Result("Found a " + p.get("age") + " years old Edson"))));
    Model model = new ModelImpl().addRule(rule1).addRule(rule2).addRule(rule3);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    ObjectTypeNode otn = getFactTemplateObjectTypeNode(ksession, "Person");
    assertEquals(3, ((CompositeObjectSinkAdapter) otn.getObjectSinkPropagator()).getHashedSinkMap().size());
    Fact mark = createMapBasedFact(personFact);
    mark.set("name", "Mark");
    mark.set("age", 40);
    ksession.insert(mark);
    ksession.fireAllRules();
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertEquals(1, results.size());
    assertThat(results, hasItem(new Result("Found a 40 years old 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) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) PrototypeVariable(org.drools.model.PrototypeVariable) CompositeObjectSinkAdapter(org.drools.core.reteoo.CompositeObjectSinkAdapter) 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 7 with Prototype

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

the class FactTemplateTest method testAlpha.

private void testAlpha(Prototype personFact, boolean hasFields) {
    PrototypeVariable markV = variable(personFact);
    Rule rule = rule("alpha").build(protoPattern(markV).expr("name", Index.ConstraintType.EQUAL, "Mark"), 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.set("name", "Mark");
    mark.set("age", 40);
    FactHandle fh = ksession.insert(mark);
    assertEquals(1, ksession.fireAllRules());
    Collection<Result> results = getObjectsIntoList(ksession, Result.class);
    assertThat(results, hasItem(new Result("Found a 40 years old Mark")));
    mark.set("age", 41);
    ksession.update(fh, mark, "age");
    // property reactivity should prevent this firing
    assertEquals(hasFields ? 0 : 1, ksession.fireAllRules());
    ksession.update(fh, mark, "age", "name");
    // saying to the engine that also name is changed to check if this time the update is processed as it should
    assertEquals(1, ksession.fireAllRules());
}
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) 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)

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