use of org.mvel2.ParserConfiguration in project mvel by mikebrock.
the class TypesAndInferenceTests method testGenericMethods.
public void testGenericMethods() {
String str = "Integer.parseInt( a.getMap().get(\"x\") )";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("a", AGenericTestClass.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
AGenericTestClass a = new AGenericTestClass();
a.setMap(new HashMap<String, String>());
a.getMap().put("x", "10");
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("a", a);
Number result = (Number) MVEL.executeExpression(stmt, null, variables);
assertEquals(10, result.intValue());
}
use of org.mvel2.ParserConfiguration in project mvel by mikebrock.
the class TypesAndInferenceTests method testGetCorrectInputs.
public void testGetCorrectInputs() {
String str = "total = total + $cheese.price";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("total", int.class);
pctx.addInput("$cheese", Cheese.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
assertTrue("Should not contain" + pctx.getVariables(), pctx.getVariables().isEmpty());
}
use of org.mvel2.ParserConfiguration in project stargate-core by tuplejump.
the class AggregateFunction method init.
@Override
public void init(Options options) {
this.options = options;
int k = 0;
positions = new HashMap<>();
aggregateFields = new String[aggregates.length];
for (AggregateFactory aggregateFactory : aggregates) {
String field = aggregateFactory.getField();
if (field != null) {
aggregateFields[k] = field;
positions.put(field, k++);
}
}
ParserConfiguration parserConfig = getParserConfiguration();
if (groupBy != null) {
this.groupByExpressions = new ExecutableStatement[groupBy.length];
this.simpleExpressions = new boolean[groupBy.length];
groupByFields = new ArrayList<>();
for (int i = 0; i < groupBy.length; i++) {
String groupByField = groupBy[i];
String groupByCol = getGroupBy(groupByField);
boolean isSimpleExpression = options.types.containsKey(getColumnName(groupByCol));
ParserContext parserContext = new ParserContext(parserConfig);
groupByExpressions[i] = (ExecutableStatement) MVEL.compileExpression(groupByField, parserContext);
if (isSimpleExpression) {
simpleExpressions[i] = true;
int pos = k++;
positions.put(groupByCol, pos);
groupByFields.add(groupByCol);
} else {
simpleExpressions[i] = false;
Set<String> keys = parserContext.getInputs().keySet();
for (String key : keys) {
int pos = k++;
boolean canResolve = options.types.containsKey(getColumnName(key));
if (canResolve) {
String groupByColField = getGroupBy(key);
positions.put(key, pos);
groupByFields.add(groupByColField);
}
}
}
}
}
group = new Group(options, aggregates, groupBy, groupByExpressions);
}
use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class MVELConstraintTestUtil method getParserConfiguration.
@Override
protected ParserConfiguration getParserConfiguration(ReteEvaluator reteEvaluator) {
ParserConfiguration parserConfiguration = new ParserConfiguration();
parserConfiguration.addImport(Cheese.class);
return parserConfiguration;
}
use of org.mvel2.ParserConfiguration in project drools by kiegroup.
the class MVELExprAnalyzer method analyzeExpression.
// ------------------------------------------------------------
// Instance methods
// ------------------------------------------------------------
/**
* Analyze an expression.
*
* @param expr
* The expression to analyze.
* @param availableIdentifiers
* Total set of declarations available.
*
* @return The <code>Set</code> of declarations used by the expression.
* @throws RecognitionException
* If an error occurs in the parser.
*/
@SuppressWarnings("unchecked")
public static MVELAnalysisResult analyzeExpression(final PackageBuildContext context, final String expr, final BoundIdentifiers availableIdentifiers, final Map<String, Class<?>> localTypes, String contextIdentifier, Class kcontextClass) {
if (expr.trim().length() <= 0) {
MVELAnalysisResult result = analyze((Set<String>) Collections.EMPTY_SET, availableIdentifiers);
result.setMvelVariables(new HashMap<String, Class<?>>());
result.setTypesafe(true);
return result;
}
MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
MVEL.COMPILER_OPT_ALLOW_RESOLVE_INNERCLASSES_WITH_DOTNOTATION = true;
MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = true;
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
ParserConfiguration conf = getMVELDialectRuntimeData(context).getParserConfiguration();
conf.setClassLoader(context.getKnowledgeBuilder().getRootClassLoader());
// first compilation is for verification only
// @todo proper source file name
final ParserContext parserContext1 = new ParserContext(conf);
if (localTypes != null) {
for (Entry entry : localTypes.entrySet()) {
parserContext1.addInput((String) entry.getKey(), (Class) entry.getValue());
}
}
if (availableIdentifiers.getThisClass() != null) {
parserContext1.addInput("this", availableIdentifiers.getThisClass());
}
if (availableIdentifiers.getOperators() != null) {
for (Entry<String, EvaluatorWrapper> opEntry : availableIdentifiers.getOperators().entrySet()) {
parserContext1.addInput(opEntry.getKey(), opEntry.getValue().getClass());
}
}
parserContext1.setStrictTypeEnforcement(false);
parserContext1.setStrongTyping(false);
Class<?> returnType;
try {
returnType = MVEL.analyze(expr, parserContext1);
} catch (Exception e) {
BaseDescr base = (context instanceof RuleBuildContext) ? ((RuleBuildContext) context).getRuleDescr() : context.getParentDescr();
if (e instanceof CompileException && e.getCause() != null && e.getMessage().startsWith("[Error: null]")) {
// rewrite error message in cause original message is null
e = new CompileException(e.getCause().toString(), ((CompileException) e).getExpr(), ((CompileException) e).getCursor(), e.getCause());
}
AsmUtil.copyErrorLocation(e, context.getParentDescr());
context.addError(new DescrBuildError(base, context.getParentDescr(), null, "Unable to Analyse Expression " + expr + ":\n" + e.getMessage()));
return null;
}
Set<String> requiredInputs = new HashSet<String>();
requiredInputs.addAll(parserContext1.getInputs().keySet());
HashMap<String, Class<?>> variables = (HashMap<String, Class<?>>) ((Map) parserContext1.getVariables());
if (localTypes != null) {
for (String str : localTypes.keySet()) {
// we have to do this due to mvel regressions on detecting true local vars
variables.remove(str);
}
}
// MVEL includes direct fields of context object in non-strict mode. so we need to strip those
if (availableIdentifiers.getThisClass() != null) {
requiredInputs.removeIf(s -> PropertyTools.getFieldOrAccessor(availableIdentifiers.getThisClass(), s) != null);
}
// now, set the required input types and compile again
final ParserContext parserContext2 = new ParserContext(conf);
parserContext2.setStrictTypeEnforcement(true);
parserContext2.setStrongTyping(true);
for (String input : requiredInputs) {
if ("this".equals(input)) {
continue;
}
if (WM_ARGUMENT.equals(input)) {
parserContext2.addInput(input, InternalWorkingMemory.class);
continue;
}
Class<?> cls = availableIdentifiers.resolveType(input);
if (cls == null) {
if (input.equals(contextIdentifier) || input.equals("kcontext")) {
cls = kcontextClass;
} else if (input.equals("rule")) {
cls = Rule.class;
} else if (localTypes != null) {
cls = localTypes.get(input);
}
}
if (cls != null) {
parserContext2.addInput(input, cls);
}
}
if (availableIdentifiers.getThisClass() != null) {
parserContext2.addInput("this", availableIdentifiers.getThisClass());
}
boolean typesafe = context.isTypesafe();
try {
returnType = MVEL.analyze(expr, parserContext2);
typesafe = true;
} catch (Exception e) {
// is this an error, or can we fall back to non-typesafe mode?
if (typesafe) {
BaseDescr base = (context instanceof RuleBuildContext) ? ((RuleBuildContext) context).getRuleDescr() : context.getParentDescr();
AsmUtil.copyErrorLocation(e, context.getParentDescr());
context.addError(new DescrBuildError(base, context.getParentDescr(), null, "Unable to Analyse Expression " + expr + ":\n" + e.getMessage()));
return null;
}
}
if (typesafe) {
requiredInputs = new HashSet<String>();
requiredInputs.addAll(parserContext2.getInputs().keySet());
requiredInputs.addAll(variables.keySet());
variables = (HashMap<String, Class<?>>) ((Map) parserContext2.getVariables());
if (localTypes != null) {
for (String str : localTypes.keySet()) {
// we have to do this due to mvel regressions on detecting true local vars
variables.remove(str);
}
}
}
MVELAnalysisResult result = analyze(requiredInputs, availableIdentifiers);
result.setReturnType(returnType);
result.setMvelVariables(variables);
result.setTypesafe(typesafe);
return result;
}
Aggregations