title: Evaluating a string holding a logic operation as a Boolean in java
body: <p>I am trying to evaluate <code>&quot;in1 &amp;&amp; in2&quot;</code> to a Boolean as a test, but i hope to be able to evaluate all booleans as stings for my actual project. in1 and in2 are the names of nodes that have a boolean state, i get the actual expression like so,</p>
  <pre><code>logic = logic.replaceAll(curName, (nodes.get(ins.get(j)).getState() ? &quot;true&quot; : &quot;false&quot;));
  </code></pre>
  <p>logic is the string contacting the logic i want evaluated, curname is the current node name being replaced with its boolean(&quot;in1&quot; for example) its in a loop so all node names are replaced before the string is evaluated, nodes is an array list of nodes, ins are the indexes of the input nodes in the node array, <code>getState()</code> returns the nodes boolean value this works fine, setting the new value of the logic string to &quot;true &amp;&amp; true&quot;.</p>
  <p>The hard part is evaluating the string as a Boolean. I found that i could use javax.script to help me <a href="https://stackoverflow.com/questions/19383953/is-it-possible-to-evaluate-a-boolean-expression-for-string-comparions">here</a>. So I implemented it as so</p>
  <pre><code>ScriptEngineManager sem = new ScriptEngineManager();
  ScriptEngine se = sem.getEngineByName(&quot;JavaScript&quot;);
  nodes.get(outs.get(i)).setState((boolean)se.eval(logic)); 
  </code></pre>
  <p>The problem is that it evaluates to false every time, When i try and cast the object returned by eval as a Boolean and try to display it like so,</p>
  <pre><code>System.out.printLine(String.valueOf((boolean)se.eval(logic)));
  </code></pre>
  <p>it only returns false.</p>
  <p>On <a href="http://docs.oracle.com/javase/6/docs/api/javax/script/ScriptEngine.html" rel="nofollow noreferrer">oracle's page on eval</a>, I see that there are some other parameters that can be passed to eval, am i missing one of them or is it something else entirely?</p>
  <p>*Side note, it is not a problem in any of the code i haven't shown here, ive already tested the evaluation with raw booleans rather then a string.</p>