Search in sources :

Example 1 with FunctionN

use of org.drools.model.functions.FunctionN in project drools by kiegroup.

the class LambdaConsequence method evaluate.

@Override
public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
    Tuple tuple = knowledgeHelper.getTuple();
    Declaration[] declarations = ((RuleTerminalNode) knowledgeHelper.getMatch().getTuple().getTupleSink()).getRequiredDeclarations();
    Variable[] vars = consequence.getVariables();
    Object[] facts;
    int factsOffset = 0;
    if (consequence.isUsingDrools()) {
        factsOffset++;
        facts = new Object[vars.length + 1];
        facts[0] = new DroolsImpl(knowledgeHelper, workingMemory);
    } else {
        facts = new Object[vars.length];
    }
    int declrCounter = 0;
    for (Variable var : vars) {
        if (var.isFact()) {
            Declaration declaration = declarations[declrCounter++];
            InternalFactHandle fh = tuple.get(declaration);
            if (consequence.isUsingDrools()) {
                ((DroolsImpl) facts[0]).registerFactHandle(fh);
            }
            facts[factsOffset++] = declaration.getValue((InternalWorkingMemory) workingMemory, fh.getObject());
        } else {
            facts[factsOffset++] = workingMemory.getGlobal(var.getName());
        }
    }
    consequence.getBlock().execute(facts);
    Object[] objs = knowledgeHelper.getTuple().toObjects();
    for (org.drools.model.Consequence.Update update : consequence.getUpdates()) {
        Object updatedFact = context.getBoundFact(update.getUpdatedVariable(), objs);
        // TODO the Update specs has the changed fields so use update(FactHandle newObject, long mask, Class<?> modifiedClass) instead
        knowledgeHelper.update(updatedFact);
    }
    for (FunctionN insert : consequence.getInserts()) {
        Object insertedFact = insert.apply(facts);
        knowledgeHelper.insert(insertedFact);
    }
    for (Variable delete : consequence.getDeletes()) {
        Object deletedFact = context.getBoundFact(delete, objs);
        knowledgeHelper.delete(deletedFact);
    }
}
Also used : Variable(org.drools.model.Variable) FunctionN(org.drools.model.functions.FunctionN) Consequence(org.drools.core.spi.Consequence) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) Tuple(org.drools.core.spi.Tuple) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Aggregations

InternalFactHandle (org.drools.core.common.InternalFactHandle)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)1 Declaration (org.drools.core.rule.Declaration)1 Consequence (org.drools.core.spi.Consequence)1 Tuple (org.drools.core.spi.Tuple)1 Variable (org.drools.model.Variable)1 FunctionN (org.drools.model.functions.FunctionN)1