[英]Java parse String into Query
we have two-dimensional array of ints: 我们有一个整数的二维数组:
int[][] XYZ = new int[][]{
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 2, 3, 4, 5, 6, 7, 8 },
{ 8, 7, 6, 5, 4, 3, 2, 1 },
{ 2, 2, 2, 2, 2, 2, 2, 2 },
{ 1, 2, 3, 4, 5, 6, 7, 8 }
};
and a String: 和一个字符串:
String zapytanie = "((XYZ[i][0]==3 && XYZ[i][1]==4) || (XYZ[i][1]==3 || XYZ[i][2]==7))"
Query is generated dynamiccly and passed to String. 查询是动态生成的,并传递给String。
what i want to do is parse this String in for without adding external tools/components. 我想做的是在不添加外部工具/组件的情况下解析此String。
for(int i=0;i<XYZ.length;i++)
{
//todo parse here this expression
}
we want to find all rows that match query in the String and return them. 我们想在字符串中找到所有与查询匹配的行并返回它们。
My problem is i do not know how to parse String into Query. 我的问题是我不知道如何将String解析为Query。 Like removing this " " from String zapytanie and execute it.
就像从String zapytanie中删除此“”并执行它一样。
I'm pretty sure i can not use eval, but still new thing to learn so i tried it :) 我很确定我不能使用eval,但是仍然要学习新东西,所以我尝试了:)
first imported 首次进口
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
Second 第二
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
for(int i=0;i<XYZ.length;i++)
{
engine.eval(zapytanie);
}
error: ReferenceError: "XYZ" is not defined.
错误:
ReferenceError: "XYZ" is not defined.
Any idea how to fix that error, and can you tell me other methods to do the task without using javascript here? 任何想法如何解决该错误,您能告诉我其他方法来执行此任务而不使用此处的javascript吗?
I made a post-order tree from my query, but i was wondering is there a faster way, then using this tree: 我从查询中创建了一个后序树,但我想知道是否有更快的方法,然后使用此树:
a is XYZ[i][0], b is XYZ[i][1] and so on. a是XYZ [i] [0],b是XYZ [i] [1],依此类推。
from zapytanie = ((a=3 and b=4) or (b=3 or c=7))
来自
zapytanie = ((a=3 and b=4) or (b=3 or c=7))
http://i.imgur.com/T6gjT5W.png http://i.imgur.com/T6gjT5W.png
And i do not know how to use this tree smart way 而且我不知道如何使用这棵树的聪明方法
Well one option is to use eval , though I wouldn't recommend using it for anything in real life as it is a huge security hole. 一种选择是使用eval ,尽管我不建议在实际生活中将它用于任何事物,因为它是一个巨大的安全漏洞。
If you cannot use eval then I suppose you need to educate yourself on parsers, specifically Operator-precedence parser I'd say. 如果您不能使用eval,那么我想您需要对解析器进行教育,特别是我要说的运算符优先级解析器 。
I figured it out myself :) 我自己弄清楚了:)
((a<=1 and b=4) or (b=2 and c=7))
. ((a<=1 and b=4) or (b=2 and c=7))
。 We get the query
as a String
- from user input. String
获取query
。 and a is XYZ[i][0]
, b is XYZ[i][1]
from the database int[][] array
and so on. 并且
a is XYZ[i][0]
, b is XYZ[i][1]
来自数据库int[][] array
,依此类推。
First what i did is check if the query is ok - regarding number of left/right brackets. 首先,我所做的是检查查询是否正常-关于左/右括号的数量。 If not then stop here.
如果没有,则在这里停止。
i change query from String
that equals ((a<=1 and b=4) or (b=2 and c=7))
to Reverse Polish notation so now my String
equals a<=1 b=4 and b=2 c=7 and or
我改变查询从
String
,等于((a<=1 and b=4) or (b=2 and c=7))
从而现在逆波兰表示法我的String
等于a<=1 b=4 and b=2 c=7 and or
RPN is in postorder order. RPN处于后期订购状态。 I created a tree from it regarding this order and added elements to JTree.
我根据此顺序从中创建了一棵树,并向JTree添加了元素。 It looks like this: http://i.imgur.com/AK9hw6M.png
看起来像这样: http : //i.imgur.com/AK9hw6M.png
I travel my tree in postorder way and parse each of the items. 我以订购后的方式旅行我的树并解析每个项目。
If i find operator like and
or or
i take 2 childs of them and use one of the logic operators regarding operator above, so: &&
or ||
如果我发现像
and
或or
运算符and
或者我带了两个子运算符,并使用上述运算符的逻辑运算符之一,则: &&
或||
. 。
On the final i have root which have only Boolean
value that tells use if this row was ok or not. 在最后,我有root,该root仅具有
Boolean
值,该Boolean
值告诉使用此行是否正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.