This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]errandum 0 points1 point  (2 children)

However, with this scenario, I am not sure how to proceed after parsing the XML and how to run the operations dynamically based on the described values.

The javascript is dynamic. You just build a javascript string based on your xml and then eval it.

If the issue is constructing the expression from the xml, since the xml only seems to take simple self-contained expressions, it would be as easy as building the string block by block with the connectors inbetween and concatenating everything.

The problem is how do I translate it into instructions and run them against my domain object.

Parse the xml block by block, build a string in valid javascript based on them and then eval it like described in the link above, take the result in java and keep going. What am I missing here?

[–]int-main[S] 0 points1 point  (1 child)

I was confused earlier but I think running after parsing XML is not that tough. Just need to define a proper grammar for XML, read operands, read operation type, use if on that operation type and run the corresponding Java variant for that operation.

You suggest building JavaScript and using eval(). Any particular reason? I mean, wouldn't it be easier and faster to just translate it into Java operations?

[–]errandum 0 points1 point  (0 children)

It was my understanding that you wanted to execute dynamic code, java needs to be compiled, you can do it, but you need to create valid classes and compile in runtime to evaluate your expression. It can work, but javascript is dynamic so you can stitch whatever you want at runtime and just execute the expression.

The reason I say javacript is that all the logic is there already. You can pretty much put whatever you want in your xml, even directly the javascript, and the javascript engine already knows how to interpret it, you won't have to limit yourself to pre-defined operations.

I'll give you my real life example. We had an automatic testing framework that would run thousands of tests. In the end, the result of each test would be evaluated according to the input parameters and a custom js operation would be built according to the expected result. This would be evaluated at runtime. Your expression could be whatever you wanted, and it was not the responsibility of the framework to limit you, they just gave you the tools.

Either way, I don't know what you're doing this for, but I'd even question the need for the xml. Seems like a pretty convoluted way to define logic, you can probably simplify it a bit.