Search in sources :

Example 21 with PropagationContext

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

the class StatefulKnowledgeSessionImpl method initInitialFact.

public InternalFactHandle initInitialFact(InternalKnowledgeBase kBase, InternalWorkingMemoryEntryPoint entryPoint, EntryPointId epId, MarshallerReaderContext context) {
    InitialFact initialFact = InitialFactImpl.getInstance();
    InternalFactHandle handle = new DefaultFactHandle(0, initialFact, 0, entryPoint);
    ClassObjectTypeConf otc = (ClassObjectTypeConf) entryPoint.getObjectTypeConfigurationRegistry().getObjectTypeConf(epId, initialFact);
    ObjectTypeNode otn = otc.getConcreteObjectTypeNode();
    if (otn != null) {
        PropagationContextFactory ctxFact = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
        PropagationContext pctx = ctxFact.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, handle, epId, context);
        otn.assertInitialFact(handle, pctx, this);
    }
    return handle;
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ClassObjectTypeConf(org.drools.core.reteoo.ClassObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext) InitialFact(org.drools.core.InitialFact) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 22 with PropagationContext

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

the class StatefulKnowledgeSessionImpl method internalGetQueryResult.

protected QueryResultsImpl internalGetQueryResult(boolean calledFromRHS, String queryName, Object... arguments) {
    try {
        startOperation();
        this.lock.lock();
        this.kBase.executeQueuedActions();
        // eventually enqueued by events that have been inserted when already expired
        if (calledFromRHS) {
            flushPropagations();
            flushPropagations();
        } else {
            agenda.executeFlush();
            agenda.executeFlush();
        }
        DroolsQuery queryObject = new DroolsQuery(queryName, arguments, getQueryListenerInstance(), false, 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());
        BaseNode[] tnodes = evalQuery(queryName, queryObject, handle, pCtx, calledFromRHS);
        List<Map<String, Declaration>> decls = new ArrayList<Map<String, Declaration>>();
        if (tnodes != null) {
            for (BaseNode node : tnodes) {
                decls.add(((QueryTerminalNode) node).getSubRule().getOuterDeclarations());
            }
        }
        this.handleFactory.destroyFactHandle(handle);
        return new QueryResultsImpl((List<QueryRowWithSubruleIndex>) queryObject.getQueryResultCollector().getResults(), decls.toArray(new Map[decls.size()]), this, (queryObject.getQuery() != null) ? queryObject.getQuery().getParameters() : new Declaration[0]);
    } finally {
        this.lock.unlock();
        endOperation();
    }
}
Also used : QueryRowWithSubruleIndex(org.drools.core.base.QueryRowWithSubruleIndex) QueryResultsImpl(org.drools.core.QueryResultsImpl) PropagationContext(org.drools.core.spi.PropagationContext) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) QueryTerminalNode(org.drools.core.reteoo.QueryTerminalNode) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DroolsQuery(org.drools.core.base.DroolsQuery)

Example 23 with PropagationContext

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

the class DynamicEntryPoint method propagateInsert.

private void propagateInsert(InternalFactHandle handle) {
    ObjectTypeConf typeConf = getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPointNode.getEntryPoint(), handle.getObject());
    PropagationContext pctx = pctxFactory.createPropagationContext(this.wm.getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, handle, this.entryPoint);
    entryPointNode.assertObject(handle, pctx, typeConf, wm);
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext)

Example 24 with PropagationContext

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

the class NamedEntryPoint method insert.

public void insert(InternalFactHandle handle, Object object, RuleImpl rule, TerminalNode terminalNode, ObjectTypeConf typeConf) {
    PropagationContext pctx = pctxFactory.createPropagationContext(this.wm.getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, rule, terminalNode, handle, entryPoint);
    insert(handle, object, rule, typeConf, pctx);
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext)

Example 25 with PropagationContext

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

the class NamedEntryPoint method update.

public InternalFactHandle update(InternalFactHandle handle, final Object object, final BitMask mask, final Class<?> modifiedClass, final Activation activation) {
    try {
        this.lock.lock();
        this.wm.startOperation();
        this.kBase.executeQueuedActions();
        // the handle might have been disconnected, so reconnect if it has
        if (handle.isDisconnected()) {
            handle = this.objectStore.reconnect(handle);
        }
        final Object originalObject = handle.getObject();
        if (handle.getEntryPoint() != this) {
            throw new IllegalArgumentException("Invalid Entry Point. You updated the FactHandle on entry point '" + handle.getEntryPoint().getEntryPointId() + "' instead of '" + getEntryPointId() + "'");
        }
        final ObjectTypeConf typeConf = getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPoint, object);
        if (handle.getId() == -1 || object == null || handle.isExpired()) {
            // the handle is invalid, most likely already retracted, so return and we cannot assert a null object
            return handle;
        }
        if (originalObject != object || !AssertBehaviour.IDENTITY.equals(this.kBase.getConfiguration().getAssertBehaviour())) {
            this.objectStore.updateHandle(handle, object);
        }
        this.handleFactory.increaseFactHandleRecency(handle);
        final PropagationContext propagationContext = pctxFactory.createPropagationContext(this.wm.getNextPropagationIdCounter(), PropagationContext.Type.MODIFICATION, activation == null ? null : activation.getRule(), activation == null ? null : activation.getTuple().getTupleSink(), handle, entryPoint, mask, modifiedClass, null);
        if (typeConf.isTMSEnabled()) {
            EqualityKey newKey = tms.get(object);
            EqualityKey oldKey = handle.getEqualityKey();
            if ((oldKey.getStatus() == EqualityKey.JUSTIFIED || oldKey.getBeliefSet() != null) && newKey != oldKey) {
                // Mixed stated and justified, we cannot have updates untill we figure out how to use this.
                throw new IllegalStateException("Currently we cannot modify something that has mixed stated and justified equal objects. " + "Rule " + activation.getRule().getName() + " attempted an illegal operation");
            }
            if (newKey == null) {
                oldKey.removeFactHandle(handle);
                newKey = new EqualityKey(handle, // updates are always stated
                EqualityKey.STATED);
                handle.setEqualityKey(newKey);
                getTruthMaintenanceSystem().put(newKey);
            } else if (newKey != oldKey) {
                oldKey.removeFactHandle(handle);
                handle.setEqualityKey(newKey);
                newKey.addFactHandle(handle);
            }
            // If the old equality key is now empty, and no justified entries, remove it
            if (oldKey.isEmpty() && oldKey.getLogicalFactHandle() == null) {
                getTruthMaintenanceSystem().remove(oldKey);
            }
        }
        if (handle.isTraitable() && object != originalObject && object instanceof TraitableBean && originalObject instanceof TraitableBean) {
            this.traitHelper.replaceCore(handle, object, originalObject, propagationContext.getModificationMask(), object.getClass(), activation);
        }
        update(handle, object, originalObject, typeConf, propagationContext);
    } finally {
        this.wm.endOperation();
        this.lock.unlock();
    }
    return handle;
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext) TraitableBean(org.drools.core.factmodel.traits.TraitableBean)

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