Search in sources :

Example 6 with MockTupleSource

use of org.drools.core.reteoo.MockTupleSource in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method setUp.

public void setUp(int... type) {
    kBase = KnowledgeBaseFactory.newKnowledgeBase();
    buildContext = new BuildContext(kBase, Collections.emptyList());
    PropagationContextFactory pctxFactory = new PhreakPropagationContextFactory();
    context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
    MockTupleSource mockTupleSource = new MockTupleSource(9, buildContext);
    rule1 = new RuleImpl("rule1");
    rule2 = new RuleImpl("rule2");
    rule3 = new RuleImpl("rule3");
    ObjectTypeNode otn = new ObjectTypeNode(3, null, new ClassObjectType(String.class), buildContext);
    liaNode = new LeftInputAdapterNode(4, otn, buildContext);
    // 3, 4, 5, 6 are in same shared segment
    n1 = createBetaNode(10, type[0], liaNode);
    n2 = createBetaNode(11, type[1], n1);
    RuleTerminalNode rtn1 = new RuleTerminalNode(18, n2, rule1, rule1.getLhs(), 0, buildContext);
    rtn1.attach(buildContext);
    n3 = createBetaNode(12, type[2], n1);
    n4 = createBetaNode(13, type[3], n3);
    n5 = createBetaNode(14, type[4], n4);
    n6 = createBetaNode(15, type[5], n5);
    RuleTerminalNode rtn2 = new RuleTerminalNode(19, n6, rule2, rule2.getLhs(), 0, buildContext);
    rtn2.attach(buildContext);
    n7 = createBetaNode(16, type[6], n6);
    n8 = createBetaNode(17, type[7], n7);
    RuleTerminalNode rtn3 = new RuleTerminalNode(20, n8, rule3, rule3.getLhs(), 0, buildContext);
    rtn3.attach(buildContext);
    // n1 -> n2 -> r1
    // \
    // n3 -> n4 -> n5 -> n6 -> r2
    // \
    // n7 -> n8 -> r3
    n1.addAssociation(rule1);
    n1.addAssociation(rule2);
    n1.addAssociation(rule3);
    n2.addAssociation(rule1);
    n2.addAssociation(rule2);
    n2.addAssociation(rule3);
    n3.addAssociation(rule2);
    n3.addAssociation(rule3);
    n4.addAssociation(rule2);
    n4.addAssociation(rule3);
    n5.addAssociation(rule2);
    n5.addAssociation(rule3);
    n6.addAssociation(rule2);
    n6.addAssociation(rule3);
    n7.addAssociation(rule3);
    n8.addAssociation(rule3);
}
Also used : PhreakPropagationContextFactory(org.drools.core.common.PhreakPropagationContextFactory) PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PhreakPropagationContextFactory(org.drools.core.common.PhreakPropagationContextFactory) MockTupleSource(org.drools.core.reteoo.MockTupleSource) BuildContext(org.drools.core.reteoo.builder.BuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Example 7 with MockTupleSource

use of org.drools.core.reteoo.MockTupleSource in project drools by kiegroup.

the class MVELConsequenceBuilderTest method testSimpleExpression.

@Test
public void testSimpleExpression() throws Exception {
    PackageDescr pkgDescr = new PackageDescr("pkg1");
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
    pkgBuilder.addPackage(pkgDescr);
    InternalKnowledgePackage pkg = pkgBuilder.getPackageRegistry("pkg1").getPackage();
    final RuleDescr ruleDescr = new RuleDescr("rule 1");
    ruleDescr.setNamespace("pkg1");
    ruleDescr.setConsequence("modify (cheese) {price = 5 };\nretract (cheese)");
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
    final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
    final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
    final Pattern pattern = new Pattern(0, cheeseObjeectType, "cheese");
    final GroupElement subrule = new GroupElement(GroupElement.AND);
    subrule.addChild(pattern);
    final Map<String, Declaration> map = new HashMap<String, Declaration>();
    map.put("cheese", pattern.getDeclaration());
    declarationResolver.setDeclarations(map);
    context.setDeclarationResolver(declarationResolver);
    final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
    builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    PropagationContextFactory pctxFactory = RuntimeComponentFactory.get().getPropagationContextFactory();
    kBase.addPackage(pkg);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
    MockTupleSource source = new MockTupleSource(1, buildContext);
    source.setObjectCount(1);
    RuleTerminalNode rtn = new RuleTerminalNode(0, source, context.getRule(), subrule, 0, buildContext);
    final Cheese cheddar = new Cheese("cheddar", 10);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
    final LeftTupleImpl tuple = new LeftTupleImpl(f0, rtn, true);
    f0.removeLeftTuple(tuple);
    final AgendaItem item = new AgendaItemImpl(0, tuple, 10, pctxFactory.createPropagationContext(1, PropagationContext.Type.DELETION, null, null, null), rtn, null);
    final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
    kbHelper.setActivation(item);
    ((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
    context.getRule().getConsequence().evaluate(kbHelper, ksession);
    assertEquals(5, cheddar.getPrice());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) HashMap(java.util.HashMap) GroupElement(org.drools.core.rule.GroupElement) Cheese(org.drools.mvel.compiler.Cheese) MVELConsequence(org.drools.mvel.expr.MVELConsequence) AgendaItem(org.drools.core.common.AgendaItem) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) MockTupleSource(org.drools.core.reteoo.MockTupleSource) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) AgendaItemImpl(org.drools.core.common.AgendaItemImpl) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Pattern(org.drools.core.rule.Pattern) PropagationContextFactory(org.drools.core.common.PropagationContextFactory) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) DefaultKnowledgeHelper(org.drools.kiesession.consequence.DefaultKnowledgeHelper) MVELDialect(org.drools.mvel.builder.MVELDialect) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) MVELConsequenceBuilder(org.drools.mvel.builder.MVELConsequenceBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 8 with MockTupleSource

use of org.drools.core.reteoo.MockTupleSource in project drools by kiegroup.

the class MVELEvalBuilderTest method testSimpleExpression.

@Test
public void testSimpleExpression() {
    InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
    final RuleDescr ruleDescr = new RuleDescr("rule 1");
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
    final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
    final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "price");
    final Pattern pattern = new Pattern(0, new ClassObjectType(int.class));
    final Declaration declaration = new Declaration("a", extractor, pattern);
    final Map map = new HashMap();
    map.put("a", declaration);
    declarationResolver.setDeclarations(map);
    context.setDeclarationResolver(declarationResolver);
    final EvalDescr evalDescr = new EvalDescr();
    evalDescr.setContent("a == 10");
    final MVELEvalBuilder builder = new MVELEvalBuilder();
    final EvalCondition eval = (EvalCondition) builder.build(context, evalDescr);
    ((MVELEvalExpression) eval.getEvalExpression()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
    org.drools.core.reteoo.MockLeftTupleSink sink = new MockLeftTupleSink(buildContext);
    MockTupleSource source = new MockTupleSource(1, buildContext);
    source.setObjectCount(1);
    sink.setLeftTupleSource(source);
    final Cheese cheddar = new Cheese("cheddar", 10);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
    final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
    f0.removeLeftTuple(tuple);
    Object evalContext = eval.createContext();
    assertTrue(eval.isAllowed(tuple, ksession, evalContext));
    cheddar.setPrice(9);
    ksession.update(f0, cheddar);
    assertFalse(eval.isAllowed(tuple, ksession, evalContext));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) HashMap(java.util.HashMap) MockLeftTupleSink(org.drools.core.reteoo.MockLeftTupleSink) Cheese(org.drools.mvel.compiler.Cheese) EvalCondition(org.drools.core.rule.EvalCondition) MockTupleSource(org.drools.core.reteoo.MockTupleSource) EvalDescr(org.drools.drl.ast.descr.EvalDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) MVELEvalBuilder(org.drools.mvel.builder.MVELEvalBuilder) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Pattern(org.drools.core.rule.Pattern) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) MVELDialect(org.drools.mvel.builder.MVELDialect) MVELEvalExpression(org.drools.mvel.expr.MVELEvalExpression) MockLeftTupleSink(org.drools.core.reteoo.MockLeftTupleSink) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) HashMap(java.util.HashMap) Map(java.util.Map) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 9 with MockTupleSource

use of org.drools.core.reteoo.MockTupleSource in project drools by kiegroup.

the class MVELSalienceBuilderTest method testSimpleExpression.

@Test
public void testSimpleExpression() {
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final Person p = new Person("mark", "", 31);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(p);
    MockLeftTupleSink sink = new MockLeftTupleSink(buildContext);
    MockTupleSource source = new MockTupleSource(1, buildContext);
    source.setObjectCount(1);
    sink.setLeftTupleSource(source);
    final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
    RuleTerminalNode rtn = new RuleTerminalNode();
    rtn.setSalienceDeclarations(context.getDeclarationResolver().getDeclarations(context.getRule()).values().toArray(new Declaration[1]));
    AgendaItem item = new AgendaItemImpl(0, tuple, 0, null, rtn, null);
    assertEquals(25, context.getRule().getSalience().getValue(item, context.getRule(), ksession));
}
Also used : MockTupleSource(org.drools.core.reteoo.MockTupleSource) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) AgendaItemImpl(org.drools.core.common.AgendaItemImpl) MockLeftTupleSink(org.drools.core.reteoo.MockLeftTupleSink) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.mvel.compiler.Person) AgendaItem(org.drools.core.common.AgendaItem) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

MockTupleSource (org.drools.core.reteoo.MockTupleSource)9 Test (org.junit.Test)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)7 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)5 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)5 ClassObjectType (org.drools.core.base.ClassObjectType)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)4 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)4 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)4 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)3 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)3 Declaration (org.drools.core.rule.Declaration)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 SessionConfiguration (org.drools.core.SessionConfiguration)2