Search in sources :

Example 66 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class BaseMannersTest method getMakePath.

/**
 * <pre>
 *    rule makePath() {
 *        Context context;
 *        int seatingId, seatingPid, pathSeat;
 *        String pathGuestName;
 *
 *        when {
 *            Context( state == Context.MAKE_PATH )
 *            Seating( seatingId:id, seatingPid:pid, pathDone == false )
 *            Path( id == seatingPid, pathGuestName:guest, pathSeat:seat )
 *            (not Path( id == seatingId, guestName == pathGuestName )
 *        } else {
 *            drools.assert( new Path( seatingId, pathSeat, pathGuestName ) );
 *
 *        }
 *    }
 * </pre>
 *
 * @return
 * @throws InvalidRuleException
 */
private RuleImpl getMakePath() throws InvalidRuleException {
    final RuleImpl rule = new RuleImpl("makePath");
    // -----------
    // context : Context( state == Context.MAKE_PATH )
    // -----------
    final Pattern contextPattern = new Pattern(0, this.contextType);
    contextPattern.addConstraint(getLiteralConstraint(contextPattern, "state", Context.MAKE_PATH));
    rule.addPattern(contextPattern);
    // ---------------
    // Seating( seatingId:id, seatingPid:pid, pathDone == false )
    // ---------------
    final Pattern seatingPattern = new Pattern(1, this.seatingType);
    setFieldDeclaration(seatingPattern, "id", "seatingId");
    setFieldDeclaration(seatingPattern, "pid", "seatingPid");
    seatingPattern.addConstraint(getLiteralConstraint(seatingPattern, "pathDone", false));
    rule.addPattern(seatingPattern);
    final Declaration seatingIdDeclaration = rule.getDeclaration("seatingId");
    final Declaration seatingPidDeclaration = rule.getDeclaration("seatingPid");
    // -----------
    // Path( id == seatingPid, pathGuestName:guestName, pathSeat:seat )
    // -----------
    final Pattern pathPattern = new Pattern(2, this.pathType);
    pathPattern.addConstraint(getBoundVariableConstraint(pathPattern, "id", seatingPidDeclaration, "=="));
    setFieldDeclaration(pathPattern, "guestName", "pathGuestName");
    setFieldDeclaration(pathPattern, "seat", "pathSeat");
    rule.addPattern(pathPattern);
    final Declaration pathGuestNameDeclaration = rule.getDeclaration("pathGuestName");
    final Declaration pathSeatDeclaration = rule.getDeclaration("pathSeat");
    // -------------
    // (not Path( id == seatingId, guestName == pathGuestName )
    // -------------
    final Pattern notPathPattern = new Pattern(3, this.pathType);
    notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "id", seatingIdDeclaration, "=="));
    notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "guestName", pathGuestNameDeclaration, "=="));
    final GroupElement not = GroupElementFactory.newNotInstance();
    not.addChild(notPathPattern);
    rule.addPattern(not);
    // ------------
    // drools.assert( new Path( id, pathName, pathSeat ) );
    // ------------
    final Consequence consequence = new Consequence() {

        public void evaluate(KnowledgeHelper drools, WorkingMemory workingMemory) throws ConsequenceException {
            try {
                RuleImpl rule = drools.getRule();
                LeftTuple tuple = (LeftTuple) drools.getTuple();
                int id = seatingIdDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(seatingIdDeclaration).getObject());
                int seat = pathSeatDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(pathSeatDeclaration).getObject());
                String guestName = (String) drools.get(pathGuestNameDeclaration);
                Path path = new Path(id, seat, guestName);
                drools.insert(path);
            // System.err.println( "make path : " + path );
            } catch (Exception e) {
                e.printStackTrace();
                throw new ConsequenceException(e);
            }
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    };
    rule.setConsequence(consequence);
    return rule;
}
Also used : Pattern(org.drools.core.rule.Pattern) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) GroupElement(org.drools.core.rule.GroupElement) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) LeftTuple(org.drools.core.reteoo.LeftTuple) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) InvalidRuleException(org.drools.core.rule.InvalidRuleException) IOException(java.io.IOException) ConsequenceException(org.drools.core.spi.ConsequenceException) IntrospectionException(java.beans.IntrospectionException) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) ObjectInput(java.io.ObjectInput) ConsequenceException(org.drools.core.spi.ConsequenceException)

Example 67 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory 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 68 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class ReteooBuilder method removeRules.

public synchronized void removeRules(Collection<RuleImpl> rulesToBeRemoved) {
    // reset working memories for potential propagation
    InternalWorkingMemory[] workingMemories = this.kBase.getWorkingMemories();
    for (RuleImpl rule : rulesToBeRemoved) {
        if (rule.hasChildren() && !rulesToBeRemoved.containsAll(rule.getChildren())) {
            throw new RuntimeException("Cannot remove parent rule " + rule + " without having removed all its chikdren");
        }
        final RuleRemovalContext context = new RuleRemovalContext(rule);
        context.setKnowledgeBase(kBase);
        BaseNode[] rulesTerminalNodes = rules.remove(rule.getFullyQualifiedName());
        if (rulesTerminalNodes == null) {
            // there couldn't be any rule to be removed if it comes from a broken drl
            continue;
        }
        for (BaseNode node : rulesTerminalNodes) {
            removeTerminalNode(context, (TerminalNode) node, workingMemories);
        }
        if (rule.isQuery()) {
            this.queries.remove(rule.getName());
        }
        if (rule.getParent() != null && !rulesToBeRemoved.contains(rule.getParent())) {
            rule.getParent().removeChild(rule);
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) BaseNode(org.drools.core.common.BaseNode) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl)

Example 69 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class ReteooBuilder method removeLeftTupleNode.

private boolean removeLeftTupleNode(InternalWorkingMemory[] wms, RuleRemovalContext context, Map<Integer, BaseNode> stillInUse, BaseNode node) {
    boolean removed;
    removed = node.remove(context, this, wms);
    if (removed) {
        stillInUse.remove(node.getId());
        // phreak must clear node memories, although this should ideally be pushed into AddRemoveRule
        for (InternalWorkingMemory workingMemory : wms) {
            workingMemory.clearNodeMemory((MemoryFactory) node);
        }
    } else {
        stillInUse.put(node.getId(), node);
    }
    return removed;
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory)

Example 70 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project jbpm by kiegroup.

the class MVELAction method execute.

public void execute(ProcessContext context) throws Exception {
    int length = unit.getOtherIdentifiers().length;
    Object[] vars = new Object[length];
    if (unit.getOtherIdentifiers() != null) {
        for (int i = 0; i < length; i++) {
            vars[i] = context.getVariable(unit.getOtherIdentifiers()[i]);
        }
    }
    InternalWorkingMemory internalWorkingMemory = null;
    if (context.getKieRuntime() instanceof StatefulKnowledgeSessionImpl) {
        internalWorkingMemory = ((StatefulKnowledgeSessionImpl) context.getKieRuntime()).getInternalWorkingMemory();
    } else if (context.getKieRuntime() instanceof StatelessKnowledgeSessionImpl) {
        StatefulKnowledgeSession statefulKnowledgeSession = ((StatelessKnowledgeSessionImpl) context.getKieRuntime()).newWorkingMemory();
        internalWorkingMemory = ((StatefulKnowledgeSessionImpl) statefulKnowledgeSession).getInternalWorkingMemory();
    }
    VariableResolverFactory factory = unit.getFactory(context, // No previous declarations
    null, // No rule
    null, // No "right object"
    null, // No (left) tuples
    null, vars, internalWorkingMemory, (GlobalResolver) context.getKieRuntime().getGlobals());
    // KnowledgePackage pkg = context.getKnowledgeRuntime().getKnowledgeBase().getKnowledgePackage( "MAIN" );
    // if ( pkg != null && pkg instanceof KnowledgePackageImp) {
    // MVELDialectRuntimeData data = ( MVELDialectRuntimeData ) ((KnowledgePackageImp) pkg).pkg.getDialectRuntimeRegistry().getDialectData( id );
    // factory.setNextFactory( data.getFunctionFactory() );
    // }
    // 
    MVELSafeHelper.getEvaluator().executeExpression(this.expr, null, factory);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) StatelessKnowledgeSessionImpl(org.drools.core.impl.StatelessKnowledgeSessionImpl) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession)

Aggregations

InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)285 Test (org.junit.Test)217 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)140 KieBase (org.kie.api.KieBase)129 ArrayList (java.util.ArrayList)96 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)91 List (java.util.List)76 SegmentMemory (org.drools.core.reteoo.SegmentMemory)74 KieSession (org.kie.api.runtime.KieSession)71 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)69 JoinNode (org.drools.core.reteoo.JoinNode)62 BetaMemory (org.drools.core.reteoo.BetaMemory)57 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)48 PathMemory (org.drools.core.reteoo.PathMemory)48 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)45 BetaNode (org.drools.core.reteoo.BetaNode)44 FactHandle (org.kie.api.runtime.rule.FactHandle)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 AlphaNode (org.drools.core.reteoo.AlphaNode)35 ClassObjectType (org.drools.core.base.ClassObjectType)30