-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DROOLS-7568] Assigned variable doesn't match when it's on the left s…
…ide of a constraint
- Loading branch information
Showing
3 changed files
with
350 additions
and
1 deletion.
There are no files selected for viewing
267 changes: 267 additions & 0 deletions
267
...gration-api/src/test/java/org/drools/ansible/rulebook/integration/api/AssignmentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Match> 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<Match> 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<Match> 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<Match> 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
drools-ansible-rulebook-integration-main/src/main/resources/saved_event_ast.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] |