Java Apache JEXL布尔表达式问题
我正在用Java开发一个系统来检查文本中关键字组合的出现情况。 例如,我要检查以下表达式:Java Apache JEXL布尔表达式问题,java,jexl,Java,Jexl,我正在用Java开发一个系统来检查文本中关键字组合的出现情况。 例如,我要检查以下表达式:(黄色|红色)和&sofa。 我把工作分为两个步骤。第一个是检测文本中的单个单词。 第二个是使用结果检查布尔表达式。 经过简短的网络搜索后,我选择了ApacheJexl // My web app contains a set of preconfigured keywords inserted by administrator: List<String> system_occurence =
(黄色|红色)和&sofa
。
我把工作分为两个步骤。第一个是检测文本中的单个单词。
第二个是使用结果检查布尔表达式。
经过简短的网络搜索后,我选择了ApacheJexl
// My web app contains a set of preconfigured keywords inserted by administrator:
List<String> system_occurence =new ArrayList<String>() {{
add("yellow");
add("brown");
add("red");
add("kitchen");
add("sofa");
}};
// The method below check if some of system keywords are in the text
List<String> occurence = getOccurenceKeywordsInTheText();
for ( String word :occurrence){
jexlContext.set(word,true);
}
// Set to false system keywords not in the text
system_occurence.removeAll(occurence);
for ( String word :system_occurence){
jexlContext.set(word,false);
}
// Value the boolean expression
String jexlExp ="( yellow || red ) && sofa";
JexlExpression e = jexl.createExpression( jexlExp_ws_keyword_matching );
Boolean o = (Boolean) e.evaluate(jexlContext);
我怎样才能解决这个问题?这是我的路吗
对不起,我的英语不好:)尝试用其他字符(如下划线)替换空格
package-jexl;
导入org.apache.commons.jexl3.*;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
公共类Test2{
私有静态最终JexlEngine JEXL_ENGINE=new JexlBuilder().cache(512).strict(false).silent(false).create();
公共静态void main(字符串[]args){
//“我的web应用”包含一组由管理员插入的预配置关键字:
列表系统\u发生率=新建ArrayList(){{
添加(“黄色”);
添加(“棕色”);
添加(“红色”);
添加(“厨房”);
添加(“沙发”);
}};
JexlContext JexlContext=new MapContext();
//下面的方法检查文本中是否有一些系统关键字
List occurrence=Arrays.asList(“厨房”);
for(字符串字:出现){
jexlContext.set(word,true);
}
//设置为false系统关键字不在文本中
系统\发生。移除所有(发生);
for(字符串字:系统\u出现){
jexlContext.set(word,false);
}
//为布尔表达式赋值
String jexlExp=“(勒布朗•詹姆斯•红色)和沙发”;
JexlExpression e=JEXL_ENGINE.createExpression(jexlExp);
布尔o=(布尔)e.evaluate(jexlContext);
系统输出打印ln(o);
}
}
// The below example fails, JEXL launch Exception
String jexlExp ="( Lebron James || red ) && sofa";
// The below example fails, JEXL launch Exception
String jexlExp ="( òsdà || red ) && sofa";
package jexl;
import org.apache.commons.jexl3.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test2 {
private static final JexlEngine JEXL_ENGINE = new JexlBuilder().cache(512).strict(false).silent(false).create();
public static void main(String[] args) {
// My web app contains a set of preconfigured keywords inserted by administrator:
List<String> system_occurence =new ArrayList<String>() {{
add("yellow");
add("brown");
add("red");
add("kitchen");
add("sofa");
}};
JexlContext jexlContext = new MapContext();
// The method below check if some of system keywords are in the text
List<String> occurence = Arrays.asList("kitchen");
for ( String word :occurence){
jexlContext.set(word,true);
}
// Set to false system keywords not in the text
system_occurence.removeAll(occurence);
for ( String word :system_occurence){
jexlContext.set(word,false);
}
// Value the boolean expression
String jexlExp ="( Lebron_James || red ) && sofa";
JexlExpression e = JEXL_ENGINE.createExpression( jexlExp );
Boolean o = (Boolean) e.evaluate(jexlContext);
System.out.println(o);
}
}