Skip to content

Commit

Permalink
Merge branch 'main' into remove-facts
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Izquierdo authored Feb 3, 2023
2 parents bb3fa51 + 4caabc1 commit 06dd3bd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ansible_rulebook/condition_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ def OperatorExpressionFactory(tokens):
condition = infix_notation(
all_terms,
[
("not", 1, OpAssoc.RIGHT, lambda toks: NegateExpression(*toks[0])),
(
one_of("* /"),
2,
Expand Down Expand Up @@ -208,6 +207,7 @@ def OperatorExpressionFactory(tokens):
OpAssoc.LEFT,
lambda toks: OperatorExpressionFactory(toks[0]),
),
("not", 1, OpAssoc.RIGHT, lambda toks: NegateExpression(*toks[0])),
(
one_of(["and", "or"]),
2,
Expand Down
4 changes: 2 additions & 2 deletions docs/conditions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ Negation Example
| In this example the boolean expression is evaluated first and then negated.
.. note::
``not`` operator can work without parenthesis when the value is a boolean, E.g ``condition: not event.disabled``
``not`` operator can work without parenthesis when the value is a single logical statement

In the rest of the cases the expression must be enclosed in parenthesis. E.g ``condition: not (event.i == 4)``
If there are multiple logical statements with **or** or **and** please use round brackets like shown above.


Adding time constraints for rules with multiple conditions
Expand Down
10 changes: 10 additions & 0 deletions tests/examples/50_negation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- b: false
- bt: true
- i: 10
- msg: Fred
- j: 9
rules:
- name: r1
condition: not event.b
Expand All @@ -21,3 +23,11 @@
condition: not (event.i > 50 or event.i < 10)
action:
print_event:
- name: r4
condition: not event.msg == "Barney"
action:
print_event:
- name: r5
condition: not event.j >= 10
action:
print_event:
10 changes: 9 additions & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,15 @@ async def test_50_negation():
assert event["action"] == "print_event", "5"
assert event["matching_events"] == {"m": {"i": 10}}, "5"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "7"
assert event["type"] == "Action", "6"
assert event["action"] == "print_event", "6"
assert event["matching_events"] == {"m": {"msg": "Fred"}}, "6"
event = event_log.get_nowait()
assert event["type"] == "Action", "7"
assert event["action"] == "print_event", "7"
assert event["matching_events"] == {"m": {"j": 9}}, "7"
event = event_log.get_nowait()
assert event["type"] == "Shutdown", "8"


@pytest.mark.asyncio
Expand Down

0 comments on commit 06dd3bd

Please sign in to comment.