Java Apache JEXL boolean expressions issue -
i'm developing in java system check occurrence of combination of keywords in text. example, have following expression check: ( yellow || red ) && sofa
. divided job 2 steps. first 1 detect individual words in text. second 1 use result checking boolean expression. after short web search choose apache jexl.
// web app contains set of preconfigured keywords inserted administrator: list<string> system_occurence =new arraylist<string>() {{ add("yellow"); add("brown"); add("red"); add("kitchen"); add("sofa"); }}; // method below check if of system keywords in text list<string> occurence = getoccurencekeywordsinthetext(); ( string word :occurrence){ jexlcontext.set(word,true); } // set false system keywords not in text system_occurence.removeall(occurence); ( string word :system_occurence){ jexlcontext.set(word,false); } // value boolean expression string jexlexp ="( yellow || red ) && sofa"; jexlexpression e = jexl.createexpression( jexlexp_ws_keyword_matching ); boolean o = (boolean) e.evaluate(jexlcontext);
in above example, use simple words in boolean expression. ascii , non-composite words i've no problems. i've problems non-ascii , composite keywords inside boolean expression because can't use variables names.
// below example fails, jexl launch exception string jexlexp ="( lebron james || red ) && sofa"; // below example fails, jexl launch exception string jexlexp ="( òsdà || red ) && sofa";
how can solve? right way?
sorry bad english :)
try replace space other character underscore (_).
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) { // web app contains set of preconfigured keywords inserted administrator: list<string> system_occurence =new arraylist<string>() {{ add("yellow"); add("brown"); add("red"); add("kitchen"); add("sofa"); }}; jexlcontext jexlcontext = new mapcontext(); // method below check if of system keywords in text list<string> occurence = arrays.aslist("kitchen"); ( string word :occurence){ jexlcontext.set(word,true); } // set false system keywords not in text system_occurence.removeall(occurence); ( string word :system_occurence){ jexlcontext.set(word,false); } // value boolean expression string jexlexp ="( lebron_james || red ) && sofa"; jexlexpression e = jexl_engine.createexpression( jexlexp ); boolean o = (boolean) e.evaluate(jexlcontext); system.out.println(o); } }
Comments
Post a Comment