【janino】CompileException Closing single quote missing

在这里插入图片描述

1.背景

写了一个IExpressionEvaluator表达式求值,结果报错

@Test
    public void ddTest1() throws Exception {

        String s = "( e1_deviceCat == "/Application" " + "|| e1_deviceCat == "/IDS/Network" "
                + "|| e1_deviceCat == "/Firewall" ) " + "&& e1_catBehavior == "/Authentication/Add" "
                + "&& e1_severity >= 4 " + "&& e1_catTechnique == "/TrafficAnomaly/NetWorkLayer" "
                + "&& e1_catObject == "/Host/Application/Service" " + "&& e1_destAddress != nil "
                + "&& timeHourRange(e1_startTime,0,13)";



        // Create "ExpressionEvaluator" object.
        IExpressionEvaluator ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
        ee.setExpressionType(boolean.class);
        ee.setParameters(
                new String[]{"e1_deviceCat", "e1_catBehavior", "e1_severity",
                        "e1_catTechnique", "e1_catObject", "e1_destAddress", "e1_startTime"},
                new Class[]{String.class, String.class, int.class, String.class, String.class,
                        String.class, String.class}
                );
        ee.cook(java.lang.String.valueOf(s));

        int count = 10000*1000;
        long start = System.currentTimeMillis();
        for (int i=0;i<count;i++){
            Object[] arguments = {"/Application", "/Authentication/Add",Integer.valueOf(i),"/TrafficAnomaly/NetWorkLayer",
                    "/Host/Application/Service","1.1.1."+i,"2016-03-02 12:57:52"};
            Object res = ee.evaluate(arguments);
            System.out.println(res);
        }
        long end = System.currentTimeMillis();
        long interval = (end - start)/1000;
        long avg = count/interval;

        System.out.println("总耗时秒:"+interval);
        System.out.println("每秒处理条数:"+avg);

    }


原因是你的表达式有问题,缺少括号

  @Test
    public void ddTest11() throws Exception {

        String s = " e1_deviceCat.equals(\"aa\") ";

        // Create "ExpressionEvaluator" object.
        IExpressionEvaluator ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
        ee.setExpressionType(boolean.class);
        ee.setParameters(
                new String[]{"e1_deviceCat"},
                new Class[]{String.class}
        );
        ee.cook(s);

        Object[] arguments = {"/Application", "/Authentication/Add", Integer.valueOf("5"),
                "/TrafficAnomaly/NetWorkLayer",
                "/Host/Application/Service", "1.1.1.1", "2016-03-02 12:57:52"};
        Object res = ee.evaluate(arguments);
        System.out.println(res);

    }

这样就是对的了。
或者 是因为janino不支持 单引号

 String s = " e1_deviceCat.equals(’aa‘) ";

单引号 要换成双引号才可以。

原创文章 1444 获赞 480 访问量 175万+

猜你喜欢

转载自blog.csdn.net/qq_21383435/article/details/106090456