Java Code Examples for org.mvel2.ParserContext#addImport()

The following examples show how to use org.mvel2.ParserContext#addImport() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MVELDataTransformer.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Override
public Object compile(String expression, Map<String, Object> parameters) {
	logger.debug("About to compile mvel expression {}", expression);
	ClassLoader classLoader = (ClassLoader) parameters.get("classloader");
	if (classLoader == null) {
		classLoader = this.getClass().getClassLoader();
	}
	ParserConfiguration config = new ParserConfiguration();
       config.setClassLoader(classLoader);
	ParserContext context = new ParserContext(config);
	if (parameters != null) {
		@SuppressWarnings("unchecked")
		Set<String> imports = (Set<String>)parameters.get("imports");
		if (imports != null) {
			for(String clazz : imports) {
				try {
					Class<?> cl = Class.forName(clazz, true, classLoader);
					context.addImport(cl.getSimpleName(), cl);
				} catch (ClassNotFoundException e) {
					logger.warn("Unable to load class {} due to {}", clazz, e.getException());
				};
			}
		}
	}
	return MVEL.compileExpression(expression, context);
}
 
Example 2
Source File: MVELConsequenceBuilderTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testDebugSymbolCount() {
    String expr = "System.out.println( \"a1\" );\n" + "System.out.println( \"a2\" );\n" + "System.out.println( \"a3\" );\n" + "System.out.println( \"a4\" );\n";

    ParserContext context = new ParserContext();
    context.setDebugSymbols( true );
    context.addImport( "System",
                       System.class );
    context.setStrictTypeEnforcement( true );
    //context.setDebugSymbols( true );
    context.setSourceFile( "mysource" );

    ExpressionCompiler compiler = new ExpressionCompiler( expr, context );
    Serializable compiledExpression = compiler.compile();

    String s = DebugTools.decompile( compiledExpression );

    System.out.println( "s " + s );

    int fromIndex = 0;
    int count = 0;
    while ( (fromIndex = s.indexOf( "DEBUG_SYMBOL",
                                    fromIndex + 1 )) > -1 ) {
        count++;
    }
    assertEquals( 4,
                  count );

}
 
Example 3
Source File: MVELInitializer.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
private MVELInitializer() {
	MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true; // forcing to use PropertyHandler
       MathProcessor.COMPARE_WITH_PRECISION = true;
       OptimizerFactory.setDefaultOptimizer(OptimizerFactory.SAFE_REFLECTIVE); //Workaround for MVEL-291 "Custom property handler optimization fails with RuntimeException unable to compileShared"
	PropertyHandlerFactory.registerPropertyHandler(IMessage.class, new IMessagePropertyHandler());
	PropertyHandlerFactory.registerPropertyHandler(Map.class, new MapPropertyHandler());

	ctx = new ParserContext();
	ctx.addImport(LocalDateTime.class);
	ctx.addImport(LocalDate.class);
	ctx.addImport(LocalTime.class);
	ctx.addImport(BigDecimal.class);
	ctx.addImport(SailfishURI.class);
	ctx.addImport(Objects.class);
}
 
Example 4
Source File: MvelExecutionContextBuilder.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public static MvelExecutionContext build(Decision decision, Map<String, Object> inputVariables,
    Map<String, Method> customExpressionFunctions, Map<Class<?>, PropertyHandler> propertyHandlers) {

  MvelExecutionContext executionContext = new MvelExecutionContext();

  // initialize audit trail
  executionContext.setAuditContainer(DecisionExecutionAuditUtil.initializeRuleExecutionAudit(decision, inputVariables));

  ParserContext parserContext = new ParserContext();

  // add custom functions to context
  if (customExpressionFunctions != null && !customExpressionFunctions.isEmpty()) {
    for (Map.Entry<String, Method> config : customExpressionFunctions.entrySet()) {
      parserContext.addImport(config.getKey(), config.getValue());
    }
  }

  executionContext.setParserContext(parserContext);

  if (propertyHandlers != null) {
    for (Class<?> variableClass : propertyHandlers.keySet()) {
      executionContext.addPropertyHandler(variableClass, propertyHandlers.get(variableClass));
    }
  }
  
  DecisionTable decisionTable = (DecisionTable) decision.getExpression();
  
  preProcessInputVariables(decisionTable, inputVariables);

  executionContext.setStackVariables(inputVariables);

  logger.debug("Execution Context created");

  return executionContext;
}
 
Example 5
Source File: PayloadExpressionEvaluator.java    From chancery with Apache License 2.0 5 votes vote down vote up
PayloadExpressionEvaluator(@Nonnull String template) {
    if (template == null) {
        System.err.println("wuuut");
    }
    final ParserContext parserContext = new ParserContext();
    parserContext.addImport("iso", ISODateTimeFormat.class);
    parserContext.addImport("dtf", DateTimeFormat.class);
    compiledTemplate = TemplateCompiler.compileTemplate(template, parserContext);
}