1

I have a string conditions logic that retrieves values for a Map<String,Boolean> and checks if logic is true or false. For example my logic looks like:

((1A2)O3)

Being A => AND and O => OR.

And my map has this kind of structure:

new Map<String,Boolean>{'1'=>true,'2'=>false,'3'=>true};

Is it possible to replace characters in logic conditions with boolean values and get a proper result in Apex? Or even in Javascript?

0

1 Answer 1

4

Answer of your question:

No even if you replace complete String with actual expression there is no way you can parse it. For parsing the expression you would need to write the parser.

Write Parser:

You are actually evaluating expression and expressions are evaluated using Stack (generally).

Basically here you would need to build a stack where you will put each character in stack and then start popping those characters until you find a matching closing bracket, by that way you can calculate it with much less complexity.

Go through this algo for more detail.

It is already discussed in this thread, more easier solution could be using the executeAnonymous API although i have never implemented this myself. Refer Daniel Ballinger's this blog post for more details.

3
  • Can you give a more specific example? Cause I'm pretty lost with this... Thanks in advance!
    – molinet
    Commented May 14, 2019 at 15:41
  • @molinet I have added some links which provides more ways of doing it.
    – Mr.Frodo
    Commented May 14, 2019 at 15:55
  • Thanks a lot! Now I understand it better and managed to achieve the requirement.
    – molinet
    Commented May 16, 2019 at 8:31

Not the answer you're looking for? Browse other questions tagged .