[rules-users] [MVEL]how to intercept all ASTNode without annotation?

kapokfly ivan.jiang.ww at foxmail.com
Sun Dec 4 08:34:59 EST 2011


To be able to intercept against each node, I have to manually update the
expression String to register some interceptors like below, is there any
convenient API can make this enabled without adding such interceptors? 

  public void testMyInterceptors() {

    // Avoid MVEL OOM issue: http://jira.codehaus.org/browse/MVEL-252
    // This also disables JIT compilre
    // At this time, MVEL does not support per instance configuration of the
JIT due to performance constraints.
    OptimizerFactory.setDefaultOptimizer(OptimizerFactory.SAFE_REFLECTIVE);

    Interceptor testLeftInterceptor = new Interceptor() {
      public int doBefore(ASTNode node,
                          VariableResolverFactory factory) {
        System.out.println("BEFORE left Node: " + node.getName());
        System.out.println("BEFORE left Node: node.getClass(): " +
node.getClass());
        //node.setAsLiteral();
        //node.setLiteralValue("Ivan");
        System.out.println("BEFORE left Node: " + node.getLiteralValue());
        //node.setAccessor(new MetaDataAccessor());
        return 0;
      }

      public int doAfter(Object val,
                         ASTNode node,
                         VariableResolverFactory factory) {
        System.out.println("AFTER left Node: " + node.getName());
        System.out.println("AFTER left Node: node.getLiteralValue(): " +
node.getLiteralValue());
        return 0;
      }
    };

    Interceptor testRightInterceptor = new Interceptor() {

      public int doBefore(ASTNode node,
                          VariableResolverFactory factory) {
        System.out.println("BEFORE right Node: " + node.getName());
        System.out.println("BEFORE right Node: node.getClass(): " +
node.getClass());
        return 0;
      }

      public int doAfter(Object val,
                         ASTNode node,
                         VariableResolverFactory factory) {
        System.out.println("AFTER right Node: " + node.getName());
        return 0;
      }
    };

    Map<String, Interceptor> interceptors = new HashMap<String,
Interceptor>();
    interceptors.put("testLeft", testLeftInterceptor);
    interceptors.put("testRight", testRightInterceptor);

    String expression = "(@testLeft emailMessage.from == @testRight 'Mark 1'
|| @testLeft myBean.var == 1)";
    //compileExpression(expression, null, interceptors);
    Map<String, Object> contextObjects = new HashMap<String, Object>();
    EmailMessage emailMessage = new EmailMessage();
    emailMessage.setFrom("Mark");
    contextObjects.put("emailMessage", emailMessage);

    MyBean myBean = new MyBean();
    myBean.setVar(1);
    contextObjects.put("myBean", myBean);

    boolean evaluationResult =
executeExpression(compileExpression(expression, null, interceptors),
contextObjects, Boolean.class);
    System.out.println(evaluationResult);
  }

-----
Ivan, your Panda, forever
--
View this message in context: http://drools.46999.n3.nabble.com/MVEL-how-to-intercept-all-ASTNode-without-annotation-tp3559064p3559064.html
Sent from the Drools: User forum mailing list archive at Nabble.com.



More information about the rules-users mailing list