adobe / regola

A rule evaluator in Java
Apache License 2.0
0 stars 2 forks source link

Suggestion for new feature - Range Rule #2

Open jetfreakk opened 1 year ago

jetfreakk commented 1 year ago

Expected Behaviour

Range Validation: The RangeRule class will be specifically designed for validating whether a given fact falls within a specified numeric range with different inclusivity options. It's well-suited for scenarios where we have predefined expectations about the valid range of values for a particular fact.

Actual Behaviour

Currently Range Rule is not supported.

sic2 commented 1 year ago

@jetfreakk thanks for this suggestion. Could you expand on this concept? It would be good to provide some examples (as shown in the README) and list also the operations that would be supported (out of the existing ones and/or new ones).

Thanks

jetfreakk commented 1 year ago

Range Rule The "Range Rule" will be used to evaluate whether a fact within a specified numeric range meets certain inclusivity criteria. It checks if a given numeric fact falls within the defined range, considering whether the minimum and maximum values are included or excluded from the range. It will extend KeyBasedRule

Example:

{
  "type": "RANGE",
  "key": "age",
  "min": 18,
  "max": 30,
  "inclusiveMin": true,
  "inclusiveMax": false
}
Key Fact Min Max InclusiveMin InclusiveMax Result
"age" 25 18 30 true false VALID
"age" 30 18 30 true false INVALID
"age" 18 18 30 true false VALID
"age" 17 18 30 true false INVALID
"age" 31 18 30 true false INVALID

I have also cloned regola and implemented a first draft of the same if that helps.

sic2 commented 1 year ago

Some ideas...

Rather than have inclusideMin/inclusiveMax, might be good to define a set of operations to be consistent with the other rules.

In the future, we could potentially consider having other operations too. For example, one that compares ranges and determines if a range included, overlaps, etc...

A limitation of this proposal is that it works for integers only. Should it work for alphanumerical values too? What about doubles?

Feel free to share a fork of your implementation. I will be happy to have a quick look ahead of any PR.

jetfreakk commented 1 year ago

That sounds like a good idea too.

I am assuming by some mentioned operations you mean the following (Tried drafting some examples of the operations)

Key Fact Min Range Max Range Operation Other Range Key Result
range1 {min: 5, max: 25} 10 20 INCLUDES range2 VALID
range1 {min: 10, max: 20} 5 25 OVERLAPS range2 VALID
range1 {min: 5, max: 10} 15 20 DOES_NOT_OVERLAP range2 VALID

I have made use of "Numbers" so it should support Integers and Doubles. I don't see the use case of alphanumeric for this case. (Not sure how alphanumeric will come into picture here)

Sure,here is the fork.

https://github.com/jetfreakk/regola/blob/main/src/main/java/com/adobe/abp/regola/rules/RangeRule.java

jetfreakk commented 1 year ago

Additional Note :- The Above example of Operations is Range to Range comparision (Overlapping, sub-range, etc) . The one I was thinking about is more on the lines of a fact lying inside a rule and the fact being a Number (Example, 5 lies in [5,10].

sic2 commented 1 year ago

@jetfreakk can you open a PR from your fork so I can start reviewing your proposals? Thanks