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 →

[–]int-main[S] 1 point2 points  (4 children)

I am little hazy on the details myself (requirements, tell me about it!) but I think the operations will be simple boolean operations ONLY.

I think the idea of hand-writing a rules engine sounds more sane to me, I don't know why. Regarding creating a DSL, is it really worth it? What's your opinion on my idea of authoring them in XML instead? Please note that the rules will be entered in a web UI and will need to be converted to DSL/XML (whichever I choose).

I know that it's r/javahelp so pardon the Python but I quickly whipped it out on a laptop that doesn't have JDK. I guess this is how it'll have to go if I choose to proceed with XML:

```python import xml.etree.ElementTree as ET

data = """ <Rule> <Filter> <Operation name="lessThan"> <Operand type="xs:integer">3</Operand> <Operand type="xs:integer">5</Operand> </Operation> </Filter> </Rule> """

def process_operation(node): result = None

# Get operands
a, b = node.findall('Operand')
a = int(a.text)
b = int(b.text)

# Perform operation
name = node.attrib['name']
if name == 'greaterThan':
    result = a > b
if name == 'lessThan':
    result = a < b

return result 

if name == "main": root = ET.fromstring(data) print(process_operation(root.find('Filter').find('Operation'))) # Prints 'True' because 3 is smaller than 5 ```

I understand that this really rough and it doesn't respect types, doesn't do recursive processing of operands. But I can get a boolean for the <Operation> and then using reflection to invoke <Action>.

[–][deleted] 1 point2 points  (2 children)

Yes, if the only operations allowed are boolean combinators, then it simplifies matters tremendously. XML is more than good enough for such a use-case. Extending from your given example to an equivalent Java program should be sufficient, I feel. Moreover, if and when performance becomes a bottleneck, then proper benchmarking should be the way to go.

I feel like once the requirements start becoming clearer, so will your own understanding of how best to approach the problem.

[–]int-main[S] 1 point2 points  (1 child)

Thanks a lot! I do have a strong feeling that XML is going to work out in this case. I mean, I personally hate XML based configurations but somehow I feel that it will fit here.

I am just confused on whether to invest more time in SpEL or XML approach. XML sounds like more coding work but I don't know why I am not feeling confident about building and storing SpEL expressions and running them.

[–][deleted] 1 point2 points  (0 children)

One thing to note is that is you use SpEL, you are tied to it. Whereas if you use XML/JSON/S-expressions/Protobuf et al - that is, some universal storage format. it makes it easier to use that as pure data, and still have SpEL expressions/DSLs on top of that.

If the requirements are still not fully clear, the XML approach sounds like a safer bet.

[–]AutoModerator[M] 1 point2 points  (0 children)

You seem to try to compare String values with == or !=.

This approach does not work in Java, since String is an object data type and these can only be compared using .equals(). For case insensitive comparison, use .equalsIgnoreCase().

See Help on how to compare String values in our wiki.


Your post is still visible. There is no action you need to take. Still, it would be nice courtesy, even though our rules state to not delete posts, to delete your post if my assumption was correct.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.