Search in sources :

Example 16 with PropagationContext

use of org.drools.core.spi.PropagationContext in project drools by kiegroup.

the class AlphaNodeTest method testReturnValueConstraintAssertObject.

/*
     *  This just test AlphaNode With a different Constraint type.
     */
@Test
public void testReturnValueConstraintAssertObject() throws Exception {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase);
    buildContext.setRule(new RuleImpl("test"));
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final RuleImpl rule = new RuleImpl("test-rule");
    PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
    final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
    final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
    final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
    final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, buildContext);
    final MockObjectSink sink = new MockObjectSink();
    alphaNode.addObjectSink(sink);
    final Cheese cheddar = new Cheese("cheddar", 5);
    final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
    assertLength(0, sink.getAsserted());
    // object should assert as it passes text
    alphaNode.assertObject(f0, context, ksession);
    assertLength(1, sink.getAsserted());
    final Object[] list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
    final Cheese stilton = new Cheese("stilton", 6);
    f0.setObject(stilton);
    sink.getAsserted().clear();
    // object should not assert as it does not pass text
    alphaNode.assertObject(f0, context, ksession);
    assertLength(0, sink.getAsserted());
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PropagationContext(org.drools.core.spi.PropagationContext) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.core.test.model.Cheese) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 17 with PropagationContext

use of org.drools.core.spi.PropagationContext in project drools by kiegroup.

the class AlphaNodeTest method testUpdateSinkWithoutMemory.

@Test
public void testUpdateSinkWithoutMemory() {
    // An AlphaNode should try and repropagate from its source
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext buildContext = new BuildContext(kBase);
    buildContext.setRule(new RuleImpl("test"));
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final RuleImpl rule = new RuleImpl("test-rule");
    PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
    final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
    final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
    final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
    final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, // no memory
    buildContext);
    alphaNode.attach(buildContext);
    final MockObjectSink sink1 = new MockObjectSink();
    alphaNode.addObjectSink(sink1);
    // Assert a single fact which should be in the AlphaNode memory and also
    // propagated to the
    // the tuple sink
    final Cheese cheese = new Cheese("cheddar", 0);
    final DefaultFactHandle handle1 = new DefaultFactHandle(1, cheese);
    // adding handle to the mock source
    source.addFact(handle1);
    alphaNode.assertObject(handle1, context, ksession);
    // Create a fact that should not be propagated, since the alpha node restriction will filter it out
    final Cheese stilton = new Cheese("stilton", 10);
    final DefaultFactHandle handle2 = new DefaultFactHandle(2, stilton);
    // adding handle to the mock source
    source.addFact(handle2);
    alphaNode.assertObject(handle2, context, ksession);
    assertLength(1, sink1.getAsserted());
    // Attach a new tuple sink
    final MockObjectSink sink2 = new MockObjectSink();
    // Tell the alphanode to update the new node. Make sure the first sink1
    // is not updated
    // likewise the source should not do anything
    alphaNode.updateSink(sink2, context, ksession);
    assertLength(1, sink1.getAsserted());
    assertLength(1, sink2.getAsserted());
    assertEquals(1, source.getUdated());
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PropagationContext(org.drools.core.spi.PropagationContext) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.core.test.model.Cheese) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 18 with PropagationContext

use of org.drools.core.spi.PropagationContext in project drools by kiegroup.

the class SlidingLengthWindow method assertFact.

/**
 * @inheritDoc
 */
public boolean assertFact(final Object context, final InternalFactHandle handle, final PropagationContext pctx, final InternalWorkingMemory workingMemory) {
    SlidingLengthWindowContext window = (SlidingLengthWindowContext) context;
    window.pos = (window.pos + 1) % window.handles.length;
    if (window.handles[window.pos] != null) {
        final EventFactHandle previous = window.handles[window.pos];
        // retract previous
        final PropagationContext expiresPctx = createPropagationContextForFact(workingMemory, previous, PropagationContext.Type.EXPIRATION);
        ObjectTypeNode.doRetractObject(previous, expiresPctx, workingMemory);
    }
    window.handles[window.pos] = (EventFactHandle) handle;
    return true;
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) EventFactHandle(org.drools.core.common.EventFactHandle)

Example 19 with PropagationContext

use of org.drools.core.spi.PropagationContext in project drools by kiegroup.

the class ReteObjectTypeNode method attach.

public void attach(BuildContext context) {
    super.attach(context);
    if (context == null) {
        return;
    }
    // to working memories
    for (InternalWorkingMemory workingMemory : context.getWorkingMemories()) {
        PropagationContextFactory pctxFactory = workingMemory.getKnowledgeBase().getConfiguration().getComponentFactory().getPropagationContextFactory();
        final PropagationContext propagationContext = pctxFactory.createPropagationContext(workingMemory.getNextPropagationIdCounter(), PropagationContext.Type.RULE_ADDITION, null, null, null);
        propagationContext.setEntryPoint(((EntryPointNode) this.source).getEntryPoint());
        this.source.updateSink(this, propagationContext, workingMemory);
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PropagationContext(org.drools.core.spi.PropagationContext)

Example 20 with PropagationContext

use of org.drools.core.spi.PropagationContext in project drools by kiegroup.

the class StatefulKnowledgeSessionImpl method openLiveQuery.

public LiveQuery openLiveQuery(final String query, final Object[] arguments, final ViewChangedEventListener listener) {
    try {
        startOperation();
        this.lock.lock();
        this.kBase.executeQueuedActions();
        agenda.executeFlush();
        DroolsQuery queryObject = new DroolsQuery(query, arguments, new OpenQueryViewChangedEventListenerAdapter(listener), true, null, null, null, null, null);
        InternalFactHandle handle = this.handleFactory.newFactHandle(queryObject, null, this, this);
        final PropagationContext pCtx = pctxFactory.createPropagationContext(getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, handle, getEntryPoint());
        evalQuery(queryObject.getName(), queryObject, handle, pCtx, false);
        return new LiveQueryImpl(this, handle);
    } finally {
        this.lock.unlock();
        endOperation();
    }
}
Also used : OpenQueryViewChangedEventListenerAdapter(org.drools.core.runtime.rule.impl.OpenQueryViewChangedEventListenerAdapter) PropagationContext(org.drools.core.spi.PropagationContext) InternalFactHandle(org.drools.core.common.InternalFactHandle) LiveQueryImpl(org.drools.core.runtime.rule.impl.LiveQueryImpl) DroolsQuery(org.drools.core.base.DroolsQuery)

Aggregations

PropagationContext (org.drools.core.spi.PropagationContext)35 PropagationContextFactory (org.drools.core.common.PropagationContextFactory)8 LeftTuple (org.drools.core.reteoo.LeftTuple)8 DroolsQuery (org.drools.core.base.DroolsQuery)6 InternalFactHandle (org.drools.core.common.InternalFactHandle)6 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)6 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)4 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)4 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)2 DefaultKnowledgeHelper (org.drools.core.base.DefaultKnowledgeHelper)2 BetaConstraints (org.drools.core.common.BetaConstraints)2 EventFactHandle (org.drools.core.common.EventFactHandle)2 EventSupport (org.drools.core.common.EventSupport)2