Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we do math in conditions? #25

Closed
develop-cs opened this issue Jun 18, 2024 · 1 comment
Closed

Can we do math in conditions? #25

develop-cs opened this issue Jun 18, 2024 · 1 comment
Labels
how to Explain how to do something question Further information is requested

Comments

@develop-cs
Copy link
Collaborator

develop-cs commented Jun 18, 2024

Answer is definitely "yes"

How?

By using a standard condition.

Using standard conditions is the most versatile way of using Arta.

Example

rules.yaml:

---
rules:
  default_rule_set:
    example_rule:
      OK:
        condition: A_BY_B_GT_THRESHOLD
        action: do_something
        action_parameters:
          value: OK
      KO:
        condition: not A_BY_B_GT_THRESHOLD
        action: do_something
        action_parameters:
          value: KO

conditions:
  A_BY_B_GT_THRESHOLD:
    description: "A*B > threshold?"
    validation_function: is_mul_greater_than
    condition_parameters:
      a: input.value_a
      b: input.value_b
      threshold: 0.87

conditions_source_modules:
  - conditions
actions_source_modules:
  - actions

conditions.py:

def is_mul_greater_than(a: float, b: float, threshold: float) -> bool:
    return a * b > threshold

actions.py:

def do_something(value: str, **kwargs) -> str:
    return value

main.py:

from typing import Any

from arta import RulesEngine

eng = RulesEngine(config_path=".")

data: dict[str, float] = {
    "value_a": 1.3,
    "value_b": 0.7,
}

result: dict[str, Any] = eng.apply_rules(input_data=data)

print(result)

It should print:
{'example_rule': 'OK'}

1.3*0.7=0.91, greater than 0.87.

Warning: For now you can't use a simple condition for math but we will work on that. See next comment.

Enhancement: The following enhancement proposal could be useful with math in conditions: #7

@develop-cs develop-cs added question Further information is requested how to Explain how to do something labels Jun 18, 2024
@develop-cs
Copy link
Collaborator Author

Since final release 0.7.0, math expressions are implemented for simple conditions (only basic operators like: +, -, * and /).

Example:

---
rules:
  default_rule_set:
    math:
      OPS:
        simple_condition: input.x+input.y>input.threshold and input.x*input.y<123.4
        action: do_nothing
        action_parameters: null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
how to Explain how to do something question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant