mardi 13 octobre 2015

Java: Challenging Reflection - Dynamic Fields and Operators

I have data modeled in tables, which when queried could return a result set like:

+----------+-----------------+-----------+--------+-----------+----------+-------+
| subgroup | parent_subgroup | condition | field  | field_pos | operator | value |
+----------+-----------------+-----------+--------+-----------+----------+-------+
|        0 |            NULL | OR        | Field1 |         4 | equal    | A     |
|        1 |               0 | AND       | Field2 |         9 | equal    | B     |
|        1 |               0 | AND       | Field3 |        20 | equal    | C     |
+----------+-----------------+-----------+--------+-----------+----------+-------+

This would evaluate to the Boolean expression:

Field1 = A OR (Field2 = B AND Field3 = C)

Basically, I'd like to manufacture lines of code which execute during runtime. Which, having just said it, sounds insane. I'd much rather be doing this in a weakly typed language like PHP, but this has to happen in Java land.

The left two columns form an adjacency list, and the field_pos indicates the index in an array of strings where the value would be compared with.

So in Java this would need to turn into:

String[] fields = {"1", "2", "3", ... "n"};
if (fields[4].equals("A") || (fields[9].equals("B") && fields[20].equals("C"))) {
    return true;
}

I assume Java reflection would help, but don't know how this could be made elegant and not full of conditionals. Any ideas?





Aucun commentaire:

Enregistrer un commentaire