use of org.drools.mvel.MVELDialectRuntimeData in project drools by kiegroup.
the class MVELObjectExpression method getValue.
@Override
public Object getValue(final Tuple leftTuple, final Declaration[] declrs, final ReteEvaluator reteEvaluator) {
VariableResolverFactory factory = unit.getFactory(null, declrs, null, null, leftTuple, null, reteEvaluator, reteEvaluator.getGlobalResolver());
// do we have any functions for this namespace?
InternalKnowledgePackage pkg = reteEvaluator.getKnowledgeBase().getPackage("MAIN");
if (pkg != null) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
factory.setNextFactory(data.getFunctionFactory());
}
return evaluator.evaluate(factory);
}
use of org.drools.mvel.MVELDialectRuntimeData in project drools by kiegroup.
the class MVELAccumulateBuilder method build.
@SuppressWarnings("unchecked")
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
boolean typesafe = context.isTypesafe();
try {
final AccumulateDescr accumDescr = (AccumulateDescr) descr;
if (!accumDescr.hasValidInput()) {
return null;
}
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(accumDescr.getInput().getClass());
// create source CE
final RuleConditionElement source = builder.build(context, accumDescr.getInput());
if (source == null) {
return null;
}
MVELDialect dialect = (MVELDialect) context.getDialect();
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
Map<String, Declaration> sourceOuterDeclr = source.getOuterDeclarations();
Map<String, Class<?>> declarationClasses = DeclarationScopeResolver.getDeclarationClasses(decls);
declarationClasses.putAll(DeclarationScopeResolver.getDeclarationClasses(sourceOuterDeclr));
BoundIdentifiers boundIds = new BoundIdentifiers(declarationClasses, context);
final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
Accumulator[] accumulators;
if (accumDescr.isExternalFunction()) {
// uses accumulate functions
accumulators = buildExternalFunctions(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple, source, declarationClasses);
} else {
// it is a custom accumulate
accumulators = buildCustomAccumulate(context, accumDescr, dialect, decls, sourceOuterDeclr, boundIds, readLocalsFromTuple);
}
List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
for (Accumulator acc : accumulators) {
MvelAccumulator mvelAcc = (MvelAccumulator) acc;
Collections.addAll(requiredDeclarations, mvelAcc.getRequiredDeclarations());
}
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
Accumulate accumulate;
if (accumDescr.isMultiFunction()) {
accumulate = new MultiAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators, accumulators.length);
int index = 0;
for (Accumulator accumulator : accumulators) {
data.addCompileable(((MultiAccumulate) accumulate).new Wirer(index++), (MVELCompileable) accumulator);
((MVELCompileable) accumulator).compile(data, context.getRule());
}
} else {
accumulate = new SingleAccumulate(source, requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), accumulators[0]);
data.addCompileable(((SingleAccumulate) accumulate).new Wirer(), (MVELCompileable) accumulators[0]);
((MVELCompileable) accumulators[0]).compile(data, context.getRule());
}
return accumulate;
} catch (Exception e) {
AsmUtil.copyErrorLocation(e, descr);
context.addError(new DescrBuildError(context.getParentDescr(), descr, e, "Unable to build expression for 'accumulate' : " + e.getMessage()));
return null;
} finally {
context.setTypesafe(typesafe);
}
}
use of org.drools.mvel.MVELDialectRuntimeData in project drools by kiegroup.
the class MVELSalienceExpression method getValue.
public int getValue(final Activation activation, final Rule rule, final ReteEvaluator reteEvaluator) {
VariableResolverFactory factory = unit.getFactory(reteEvaluator, reteEvaluator != null ? ((AgendaItem) activation).getTerminalNode().getSalienceDeclarations() : null, rule, null, reteEvaluator != null ? activation.getTuple() : null, null, reteEvaluator, reteEvaluator.getGlobalResolver());
// do we have any functions for this namespace?
InternalKnowledgePackage pkg = reteEvaluator.getKnowledgeBase().getPackage("MAIN");
if (pkg != null) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
factory.setNextFactory(data.getFunctionFactory());
}
Object value = evaluator.evaluate(factory);
if (value instanceof String) {
value = TimeUtils.parseTimeString((String) value);
}
return ((Number) value).intValue();
}
use of org.drools.mvel.MVELDialectRuntimeData in project drools by kiegroup.
the class MVELEvalExpression method evaluate.
public boolean evaluate(final Tuple tuple, final Declaration[] requiredDeclarations, final ReteEvaluator reteEvaluator, final Object context) throws Exception {
VariableResolverFactory factory = (VariableResolverFactory) context;
unit.updateFactory(null, tuple, null, reteEvaluator, reteEvaluator.getGlobalResolver(), factory);
// do we have any functions for this namespace?
InternalKnowledgePackage pkg = reteEvaluator.getKnowledgeBase().getPackage("MAIN");
if (pkg != null) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
factory.setNextFactory(data.getFunctionFactory());
}
return evaluator.evaluate(factory);
}
use of org.drools.mvel.MVELDialectRuntimeData in project drools by kiegroup.
the class MVELReturnValueExpression method evaluate.
public FieldValue evaluate(final InternalFactHandle handle, final Tuple tuple, final Declaration[] previousDeclarations, final Declaration[] requiredDeclarations, final ReteEvaluator reteEvaluator, final Object ctx) throws Exception {
VariableResolverFactory factory = (VariableResolverFactory) ctx;
unit.updateFactory(handle, tuple, null, reteEvaluator, reteEvaluator.getGlobalResolver(), factory);
// do we have any functions for this namespace?
InternalKnowledgePackage pkg = reteEvaluator.getKnowledgeBase().getPackage("MAIN");
if (pkg != null) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
factory.setNextFactory(data.getFunctionFactory());
}
Object value = evaluator.evaluate(handle, factory);
return FieldFactory.getInstance().getFieldValue(value);
}
Aggregations