0

In my enterprise application I have business rules like :

  1. ((AMOUNT < 20000.00) || ((AMOUNT >= 20000.00) && (RISKEXPOSURE == 'N')))
  2. (ind = A1 || ind = A2 || ind = A3 || ind = S1 || ind = S2 || ind = S9)

The rule, as you can see, is made of business expressions ex: (AMOUNT < 20000.00). The rules could have any number of business conditions joined by boolean operators && and ||. The identifiers AMOUNT, RISKEXPOSURE and ind are the business variables (which could vary from 1 to n based on the business domain).

My requirement is to find those expressions when true make the entire rule true. For ex: for business rule #1 - the whole rule will be true if: (AMOUNT < 20000.00) is true or ((AMOUNT >= 20000.00) && (RISKEXPOSURE == 'N')) is true hence my output should be:

Solution #1: (AMOUNT < 20000.00)

Solution #2: ((AMOUNT >= 20000.00) && (RISKEXPOSURE == 'N'))

Similarly for Business rule #2:

Solution #1:ind = A1

Solution #2:ind = A2

Solution #3:ind = A3

Solution #4:ind = S1

Solution #5:ind = S2

Solution #6:ind = S9


What I tried:

Since each business expression can be either true or false, I changed my business rule #1 as (A || ( B && C ) ). I used the propositional logic library from Tweetyproject. This gives me results but I cannot enforce a constraint such as an XOR. In my example A and B are mutually exclusive but my output possible results. Representing the rule in truth table form, I get all the following true conditions

011 ,100 ,101 ,110 ,111

However my result should be only 100, 011

This lead me to look for alternatives and the answer here suggested a constraint solver. I have been reading about the Choco Solver. But I am not sure how my business rule can be represented in a model acceptable by the solver. Any help is greatly appreciated.

Community
  • 1
  • 1
ssdimmanuel
  • 448
  • 10
  • 29
  • Do you have anything more complex than a simple list of ORs? –  Dec 17 '16 at 13:38
  • yes rules can be complex with even 50 or 60 expressions and OR and AND conditions. it can be simple ORs or simple ANDs or the combination of both – ssdimmanuel Dec 17 '16 at 13:52
  • Why can you represent XOR by translating it to more primitive operations? A XOR B == ( ~A && B ) || (A && ~B ) If you claim this is an extra condition, then simply glue this formula to #1 with an AND forcing it to be true. – Ira Baxter Dec 17 '16 at 15:52
  • "how my business rule can be represented"... usually the constraint solver takes in a set of equations with an implied conjunction over the set. Representing Rule#1 as you have suggested seems fairly typical; the XOR condition you could add as a second equation rather than just ANDing to Rule#1. What specific thing can you not write? – Ira Baxter Dec 17 '16 at 15:57
  • By seeing we can say that it's an XOR , but how can I determine that programattically ? Is there a generic way to identify constraint and then feed to the constraint solver library? – ssdimmanuel Dec 17 '16 at 16:27
  • I don't understand your problem. If you know two terms X and Y are exclusive, then X XOR Y is true. You can write that convertedt to AND OR and NOT, and just feed that to the constraint solver along with you basic business rules. Why do you see this as difficult? Show us an example that you have tried to convert and tell us why you can't do it. – Ira Baxter Dec 17 '16 at 20:02
  • Ok I get your point. How can I represent business rule #2 which has 6 OR conditions? – ssdimmanuel Dec 17 '16 at 20:08

2 Answers2

0

Simple solution can be to implement algorithm which find all combinations of boolean values of a given size. Then for each expression calculate the entire expression by the values in combination and the opposite value of your expression. If in every combination the whole expression calculation with the values in the combination row and with the opposite value for your expressions are different, the this expression is this which you need. I will try to explain it easily.

You have expression (A || ( B && C ) )= BIG Then you find combinations of possible values: e.g

[[001],[010],[011]...[111]]

For each of them replace the values in to your BIG expression and compare the result the expression calculated by the opposite value of expression you want - [101] with [001] | [110] with [010] and for each combinations the result is difference you find the expression. Then switch to next index and do this again

Aleydin Karaimin
  • 944
  • 1
  • 6
  • 16
  • I *really* don't follow what you are suggesting. At best what I understand is you are enumerating the space of values of the variables and substituting the combinations; that the classic brute force answer (which I provide to OP in the other question he asked). It can work for ~~30 variables; it cannot complete in reasonable time for 60 variables as OP has. The rest of your answer "compute the opposite value of the expression you want" huh? taken literally, if the formula being solved is BIG, the opposite expression ins ~BIG. Maybe you can see my confusion. Please write an algorithm. – Ira Baxter Dec 17 '16 at 16:03
  • Yeah, you are right! This is slow for complex rules, but by now I can't provide him with better solution, I hope someone will do this. – Aleydin Karaimin Dec 17 '16 at 18:22
0

I would recommend you to look at SMT solving. It naturally allows solving the stated problem. You introduce any reasonable amount of constraints (both integer and Boolean) and variables. Basically, it tries to satisfy an equation given some constraints. It will find first satisfying assignment to your task.

Links to explore:

  1. Z3
  2. CVC4
  3. yices2

Be aware that runtime and performance are heavily dependant on the problem itself: constraints, constants, exact values.

CaptainTrunky
  • 1,562
  • 2
  • 15
  • 23