0

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.

what i want to do is parse this String in for without adding external tools/components.

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. Like removing this " " from String zapytanie and execute it.

I'm pretty sure i can not use eval, but still new thing to learn so i tried it :)

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.

Any idea how to fix that error, and can you tell me other methods to do the task without using javascript here?

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.

from zapytanie = ((a=3 and b=4) or (b=3 or c=7))

https://i.stack.imgur.com/IwGVT.png

And i do not know how to use this tree smart way

tryingHard
  • 1,794
  • 4
  • 35
  • 74
  • 2
    it looks like you haven't tried anything yet. show us your attempt – Philipp Sander Nov 26 '15 at 13:52
  • 1
    Your string looks like it could become quite complex so I'd probably use an external parser library or something like Javassist to add the code defined in the string directly to the method. Any reason why you don't want to use a library? – Thomas Nov 26 '15 at 13:53
  • Cause i get this task from somebody and i must not use it. Philip - any hint ? – tryingHard Nov 26 '15 at 13:54
  • Then please ask that somebody why you must not use that. If it is for learning purposes then please state so, if it is for other reasons then please state them. If the person who tasked you can't provide good reasons then reject the task. ;) – Thomas Nov 26 '15 at 13:55
  • I am studying and this is a task to finish my subject :) Thanks for any tips guys! Hope you understand what i'am trying to say :) – tryingHard Nov 26 '15 at 13:59
  • @PhilippSander i updated the first post, any hint now :) ? – tryingHard Nov 26 '15 at 14:28

2 Answers2

0

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.

If you cannot use eval then I suppose you need to educate yourself on parsers, specifically Operator-precedence parser I'd say.

Jiří Kantor
  • 744
  • 1
  • 5
  • 12
  • It is java not javascript. – talex Nov 26 '15 at 14:00
  • I'm pretty sure i can't use eval, but still i checked this option. I get error sasying "ReferenceError: "XYZ" is not defined." Do you know how can i make this reference? Thank you for your tip :) – tryingHard Nov 26 '15 at 14:11
  • @talex Uh I don't know why I thought it was JS, probably because I'm working with it a lot now :-) – Jiří Kantor Dec 01 '15 at 08:54
  • @yami I recommend looking at [this question](http://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java) – Jiří Kantor Dec 01 '15 at 08:55
0

I figured it out myself :)

  1. let's say query was ((a<=1 and b=4) or (b=2 and c=7)). We get the query as a String - from user input.

and a is XYZ[i][0], b is XYZ[i][1] from the database int[][] array and so on.

  1. First what i did is check if the query is ok - regarding number of left/right brackets. If not then stop here.

  2. 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

  3. RPN is in postorder order. I created a tree from it regarding this order and added elements to JTree. It looks like this: https://i.stack.imgur.com/hpjJn.png

  4. 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 ||.

  5. On the final i have root which have only Boolean value that tells use if this row was ok or not.

    1. if it was ok - then we write down that row.
tryingHard
  • 1,794
  • 4
  • 35
  • 74