Skip to content

Commit

Permalink
[DROOLS-7568] Assigned variable doesn't match when it's on the left s…
Browse files Browse the repository at this point in the history
…ide of a constraint
  • Loading branch information
tkobayas committed Sep 27, 2023
1 parent 7e87676 commit eb3cf59
Show file tree
Hide file tree
Showing 3 changed files with 350 additions and 1 deletion.
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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
}
}
]
}
}
]

0 comments on commit eb3cf59

Please sign in to comment.