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);
}
}
Aggregations