Search code examples
hexdrools

How to compare two hex values in drools


I need to compare two hex values in drools.

for eg: compare 0xbadf00d with 0xbadf00e

This should result in false as d doesn't match with e. So my question is, can hex be treated as string value and same comparisons can be made, or there is some other way.

I tried googling but no use.


Solution

  • When using ASCII, the natural order of the digits and letters of an HEX is ascending. This makes the comparison of these values as Strings trivial (assuming they are left-padded with 0s and using the same case).

    As an example, if you have an Input class with a hex attribute of type String you can write something like this:

    rule "Test"
    when
        $i1: Input()
        $i2: Input(hex > $i1.hex)
    then
        //Do whatever you need here
    end
    

    Hope it helps,