Search in sources :

Example 21 with KnowledgeHelper

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

the class ModifyInterceptor method doAfter.

public int doAfter(Object value, ASTNode node, VariableResolverFactory factory) {
    while (factory != null && !(factory instanceof DroolsVarFactory)) {
        factory = factory.getNextFactory();
    }
    if (factory == null) {
        throw new RuntimeException("Unable to find DroolsMVELIndexedFactory");
    }
    KnowledgeHelper knowledgeHelper = ((DroolsVarFactory) factory).getKnowledgeHelper();
    if (modificationMask.isSet(PropertySpecificUtil.TRAITABLE_BIT)) {
        calculateModificationMask(knowledgeHelper, (WithNode) node);
    }
    knowledgeHelper.update(value, modificationMask, value.getClass());
    return 0;
}
Also used : DroolsVarFactory(org.drools.core.base.mvel.MVELCompilationUnit.DroolsVarFactory) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper)

Example 22 with KnowledgeHelper

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

the class MVELCompilationUnit method updateFactory.

private void updateFactory(Object knowledgeHelper, Declaration[] prevDecl, Rule rule, InternalFactHandle rightHandle, Object rightObject, Tuple tuple, Object[] otherVars, InternalWorkingMemory workingMemory, GlobalResolver globals, VariableResolverFactory factory) {
    int varLength = inputIdentifiers.length;
    int i = 0;
    if ("this".equals(inputIdentifiers[0])) {
        factory.getIndexedVariableResolver(i++).setValue(rightObject);
    }
    factory.getIndexedVariableResolver(i++).setValue(knowledgeHelper);
    factory.getIndexedVariableResolver(i++).setValue(knowledgeHelper);
    if (inputIdentifiers.length > i && "rule".equals(inputIdentifiers[i])) {
        factory.getIndexedVariableResolver(i++).setValue(rule);
    }
    if (globalIdentifiers != null) {
        for (String globalIdentifier : globalIdentifiers) {
            factory.getIndexedVariableResolver(i++).setValue(globals.resolveGlobal(globalIdentifier));
        }
    }
    InternalFactHandle[] handles = tuple instanceof LeftTuple ? ((LeftTuple) tuple).toFactHandles() : null;
    if (operators.length > 0) {
        for (EvaluatorWrapper operator : operators) {
            // TODO: need to have one operator per working memory
            factory.getIndexedVariableResolver(i++).setValue(operator);
            operator.loadHandles(workingMemory, handles, rightHandle);
        }
    }
    Object[] objs = null;
    if (tuple != null) {
        if (handles == null) {
            objs = tuple.toObjects();
        }
        if (this.previousDeclarations != null && this.previousDeclarations.length > 0) {
            // Consequences with 'or's will have different declaration offsets, so use the one's from the RTN's subrule.
            if (prevDecl == null) {
                // allows the caller to override the member var
                // used for rules, salience and timers so they work with 'or' CEs
                prevDecl = this.previousDeclarations;
            }
            for (Declaration decl : prevDecl) {
                int offset = decl.getPattern().getOffset();
                Object o = decl.getValue(workingMemory, objs != null ? objs[offset] : handles[offset].getObject());
                factory.getIndexedVariableResolver(i++).setValue(o);
            }
        }
    }
    if (this.localDeclarations != null && this.localDeclarations.length > 0) {
        for (Declaration decl : this.localDeclarations) {
            Object value;
            if (readLocalsFromTuple && tuple != null) {
                int offset = decl.getPattern().getOffset();
                value = decl.getValue(workingMemory, objs != null ? objs[offset] : handles[offset].getObject());
            } else {
                value = decl.getValue(workingMemory, rightObject);
            }
            factory.getIndexedVariableResolver(i++).setValue(value);
        }
    }
    int otherVarsPos = 0;
    if (otherVars != null) {
        otherVarsPos = i;
        for (Object o : otherVars) {
            factory.getIndexedVariableResolver(i++).setValue(o);
        }
    }
    int otherVarsLength = i - otherVarsPos;
    for (i = varLength; i < this.allVarsLength; i++) {
        // null all local vars
        factory.getIndexedVariableResolver(i).setValue(null);
    }
    DroolsVarFactory df = (DroolsVarFactory) factory.getNextFactory();
    df.setOtherVarsPos(otherVarsPos);
    df.setOtherVarsLength(otherVarsLength);
    if (knowledgeHelper instanceof KnowledgeHelper) {
        KnowledgeHelper kh = (KnowledgeHelper) knowledgeHelper;
        df.setKnowledgeHelper(kh);
    }
}
Also used : EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Aggregations

KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)22 WorkingMemory (org.drools.core.WorkingMemory)19 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)17 Consequence (org.drools.core.spi.Consequence)16 IOException (java.io.IOException)12 ObjectInput (java.io.ObjectInput)12 ObjectOutput (java.io.ObjectOutput)12 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)12 Declaration (org.drools.core.rule.Declaration)12 Pattern (org.drools.core.rule.Pattern)12 LeftTuple (org.drools.core.reteoo.LeftTuple)8 ConsequenceException (org.drools.core.spi.ConsequenceException)8 IntrospectionException (java.beans.IntrospectionException)7 InternalFactHandle (org.drools.core.common.InternalFactHandle)7 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)7 InvalidRuleException (org.drools.core.rule.InvalidRuleException)7 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)5 ClassObjectType (org.drools.core.base.ClassObjectType)5 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)5 ArrayList (java.util.ArrayList)4