From eb3cf59deb4b905f9703e3c20db5cab8fdfbfca6 Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Wed, 27 Sep 2023 18:03:50 +0900 Subject: [PATCH] [DROOLS-7568] Assigned variable doesn't match when it's on the left side of a constraint --- .../integration/api/AssignmentTest.java | 267 ++++++++++++++++++ .../rulebook/integration/main/Main.java | 2 +- .../src/main/resources/saved_event_ast.json | 82 ++++++ 3 files changed, 350 insertions(+), 1 deletion(-) create mode 100644 drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/AssignmentTest.java create mode 100644 drools-ansible-rulebook-integration-main/src/main/resources/saved_event_ast.json diff --git a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/AssignmentTest.java b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/AssignmentTest.java new file mode 100644 index 00000000..b5e38da8 --- /dev/null +++ b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/AssignmentTest.java @@ -0,0 +1,267 @@ +package org.drools.ansible.rulebook.integration.api; + +import java.util.List; + +import org.junit.Test; +import org.kie.api.runtime.rule.Match; + +import static org.junit.Assert.assertEquals; + +public class AssignmentTest { + + @Test + public void testAssignmentReferenceEqualsExpressionRHS() { + String JSON = + """ + { + "rules":[ + { + "Rule":{ + "action":{ + "Action":{ + "action":"debug", + "action_args":{ + \s + } + } + }, + "condition":{ + "AllCondition":[ + { + "AssignmentExpression":{ + "lhs":{ + "Events":"varname" + }, + "rhs":{ + "EqualsExpression":{ + "lhs":{ + "Event":"i" + }, + "rhs":{ + "Integer":0 + } + } + } + } + }, + { + "EqualsExpression":{ + "lhs":{ + "Event":"j" + }, + "rhs":{ + "Events":"varname.i" + } + } + } + ] + } + } + } + ] + } + """; + + RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(JSON); + + List matchedRules = rulesExecutor.processEvents("{ \"i\": 0 }").join(); + matchedRules = rulesExecutor.processFacts("{ \"j\": 0 }").join(); + assertEquals(1, matchedRules.size()); + assertEquals("r_0", matchedRules.get(0).getRule().getName()); + assertEquals("varname", matchedRules.get(0).getDeclarationIds().get(0)); + + rulesExecutor.dispose(); + } + + @Test + public void testAssignmentReferenceEqualsExpressionLHS() { + String JSON = + """ + { + "rules":[ + { + "Rule":{ + "action":{ + "Action":{ + "action":"debug", + "action_args":{ + \s + } + } + }, + "condition":{ + "AllCondition":[ + { + "AssignmentExpression":{ + "lhs":{ + "Events":"varname" + }, + "rhs":{ + "EqualsExpression":{ + "lhs":{ + "Event":"i" + }, + "rhs":{ + "Integer":0 + } + } + } + } + }, + { + "EqualsExpression":{ + "lhs":{ + "Events":"varname.i" + }, + "rhs":{ + "Event":"j" + } + } + } + ] + } + } + } + ] + } + """; + + RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(JSON); + + List matchedRules = rulesExecutor.processEvents("{ \"i\": 0 }").join(); + matchedRules = rulesExecutor.processFacts("{ \"j\": 0 }").join(); + assertEquals(1, matchedRules.size()); + assertEquals("r_0", matchedRules.get(0).getRule().getName()); + assertEquals("varname", matchedRules.get(0).getDeclarationIds().get(0)); + + rulesExecutor.dispose(); + } + + @Test + public void testAssignmentReferenceItemInListExpressionLHS() { + String JSON = + """ + { + "rules":[ + { + "Rule":{ + "action":{ + "Action":{ + "action":"debug", + "action_args":{ + \s + } + } + }, + "condition":{ + "AllCondition":[ + { + "AssignmentExpression": { + "lhs": { + "Events": "varname" + }, + "rhs": { + "EqualsExpression": { + "lhs": { + "Event": "xyz" + }, + "rhs": { + "Integer": 300 + } + } + } + } + }, + { + "ItemInListExpression": { + "lhs": { + "Events": "varname.pr" + }, + "rhs": { + "Event": "prs_list" + } + } + } + ] + } + } + } + ] + } + """; + + RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(JSON); + + List matchedRules = rulesExecutor.processEvents("{ \"xyz\": 300, \"pr\": 456 }").join(); + matchedRules = rulesExecutor.processEvents("{ \"prs_list\": [ 456, 457 ] }").join(); + assertEquals(1, matchedRules.size()); + assertEquals("r_0", matchedRules.get(0).getRule().getName()); + assertEquals("varname", matchedRules.get(0).getDeclarationIds().get(0)); + + rulesExecutor.dispose(); + } + + @Test + public void testAssignmentReferenceListContainsItemExpressionRHS() { + String JSON = + """ + { + "rules":[ + { + "Rule":{ + "action":{ + "Action":{ + "action":"debug", + "action_args":{ + \s + } + } + }, + "condition":{ + "AllCondition":[ + { + "AssignmentExpression": { + "lhs": { + "Events": "varname" + }, + "rhs": { + "EqualsExpression": { + "lhs": { + "Event": "xyz" + }, + "rhs": { + "Integer": 300 + } + } + } + } + }, + { + "ListContainsItemExpression": { + "lhs": { + "Event": "prs_list" + }, + "rhs": { + "Events": "varname.pr" + } + } + } + ] + } + } + } + ] + } + """; + + RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(JSON); + + List matchedRules = rulesExecutor.processEvents("{ \"xyz\": 300, \"pr\": 456 }").join(); + matchedRules = rulesExecutor.processEvents("{ \"prs_list\": [ 456, 457 ] }").join(); + assertEquals(1, matchedRules.size()); + assertEquals("r_0", matchedRules.get(0).getRule().getName()); + assertEquals("varname", matchedRules.get(0).getDeclarationIds().get(0)); + + rulesExecutor.dispose(); + } +} \ No newline at end of file diff --git a/drools-ansible-rulebook-integration-main/src/main/java/org/drools/ansible/rulebook/integration/main/Main.java b/drools-ansible-rulebook-integration-main/src/main/java/org/drools/ansible/rulebook/integration/main/Main.java index 928bc512..65411a76 100644 --- a/drools-ansible-rulebook-integration-main/src/main/java/org/drools/ansible/rulebook/integration/main/Main.java +++ b/drools-ansible-rulebook-integration-main/src/main/java/org/drools/ansible/rulebook/integration/main/Main.java @@ -31,7 +31,7 @@ public class Main { private static final boolean EXECUTE_PAYLOAD_ASYNC = true; - private static final String DEFAULT_JSON = "56_once_after.json"; + private static final String DEFAULT_JSON = "saved_event_ast.json"; private static final int THREADS_NR = 1; // run with 1 thread by default diff --git a/drools-ansible-rulebook-integration-main/src/main/resources/saved_event_ast.json b/drools-ansible-rulebook-integration-main/src/main/resources/saved_event_ast.json new file mode 100644 index 00000000..111e10d0 --- /dev/null +++ b/drools-ansible-rulebook-integration-main/src/main/resources/saved_event_ast.json @@ -0,0 +1,82 @@ +[ + { + "RuleSet": { + "name": "demo", + "hosts": [ + "localhost" + ], + "sources": [ + { + "EventSource": { + "name": "generic", + "source_name": "generic", + "source_args": { + "payload": [ + { + "xyz": 300, + "pr": 456 + }, + { + "prs_list": [ + 456, + 457 + ] + } + ] + }, + "source_filters": [] + } + } + ], + "rules": [ + { + "Rule": { + "name": "r41", + "condition": { + "AllCondition": [ + { + "AssignmentExpression": { + "lhs": { + "Events": "varname" + }, + "rhs": { + "EqualsExpression": { + "lhs": { + "Event": "xyz" + }, + "rhs": { + "Integer": 300 + } + } + } + } + }, + { + "ItemInListExpression": { + "lhs": { + "Events": "varname.pr" + }, + "rhs": { + "Event": "prs_list" + } + } + } + ] + }, + "actions": [ + { + "Action": { + "action": "debug", + "action_args": { + "msg": "Assignment works" + } + } + } + ], + "enabled": true + } + } + ] + } + } +] \ No newline at end of file