Fix inconsistency in SpEL template expression usage

Issue #257
pull/284/head
Mahmoud Ben Hassine 5 years ago
parent dc2777d6b7
commit 8864d0e41f
No known key found for this signature in database
GPG Key ID: 79FCFB0A184E0036

@ -50,8 +50,8 @@ public class SpELAction implements Action {
private final ExpressionParser parser = new SpelExpressionParser();
private String expression;
private Expression compiledExpression;
private final String expression;
private final Expression compiledExpression;
private BeanResolver beanResolver;
/**
@ -60,8 +60,7 @@ public class SpELAction implements Action {
* @param expression the action written in expression language
*/
public SpELAction(String expression) {
this.expression = expression;
compiledExpression = parser.parseExpression(expression);
this(expression, ParserContext.TEMPLATE_EXPRESSION);
}
/**
@ -71,9 +70,7 @@ public class SpELAction implements Action {
* @param beanResolver the bean resolver used to resolve bean references
*/
public SpELAction(String expression, BeanResolver beanResolver) {
this.expression = expression;
this.beanResolver = beanResolver;
compiledExpression = parser.parseExpression(expression);
this(expression, ParserContext.TEMPLATE_EXPRESSION, beanResolver);
}
/**

@ -46,7 +46,7 @@ public class SpELCondition implements Condition {
private final ExpressionParser parser = new SpelExpressionParser();
private Expression compiledExpression;
private final Expression compiledExpression;
private BeanResolver beanResolver;
/**
@ -55,7 +55,7 @@ public class SpELCondition implements Condition {
* @param expression the condition written in expression language
*/
public SpELCondition(String expression) {
compiledExpression = parser.parseExpression(expression);
this(expression, ParserContext.TEMPLATE_EXPRESSION);
}
/**
@ -65,8 +65,7 @@ public class SpELCondition implements Condition {
* @param beanResolver the bean resolver used to resolve bean references
*/
public SpELCondition(String expression, BeanResolver beanResolver) {
this.beanResolver = beanResolver;
compiledExpression = parser.parseExpression(expression);
this(expression, ParserContext.TEMPLATE_EXPRESSION, beanResolver);
}
/**

@ -75,7 +75,7 @@ public class SpELRuleFactory extends AbstractRuleFactory<ParserContext> {
* @return a new rule
*/
public Rule createRule(Reader ruleDescriptor) throws Exception {
return createRule(ruleDescriptor, new TemplateParserContext());
return createRule(ruleDescriptor, ParserContext.TEMPLATE_EXPRESSION);
}
/**
@ -104,7 +104,7 @@ public class SpELRuleFactory extends AbstractRuleFactory<ParserContext> {
* @return a set of rules
*/
public Rules createRules(Reader rulesDescriptor) throws Exception {
return createRules(rulesDescriptor, new TemplateParserContext());
return createRules(rulesDescriptor, ParserContext.TEMPLATE_EXPRESSION);
}
/**

@ -45,7 +45,7 @@ public class SpELActionTest {
@Test
public void testSpELActionExecution() throws Exception {
// given
Action markAsAdult = new SpELAction("#person.setAdult(true)");
Action markAsAdult = new SpELAction("#{ ['person'].setAdult(true) }");
Facts facts = new Facts();
Person foo = new Person("foo", 20);
facts.put("person", foo);
@ -60,7 +60,7 @@ public class SpELActionTest {
@Test
public void testSpELFunctionExecution() throws Exception {
// given
Action printAction = new SpELAction("T(org.jeasy.rules.spel.Person).sayHello()");
Action printAction = new SpELAction("#{ T(org.jeasy.rules.spel.Person).sayHello() }");
Facts facts = new Facts();
// when
@ -75,7 +75,7 @@ public class SpELActionTest {
// given
expectedException.expect(Exception.class);
expectedException.expectMessage("EL1004E: Method call: Method sayHi() cannot be found on type org.jeasy.rules.spel.Person");
Action action = new SpELAction("T(org.jeasy.rules.spel.Person).sayHi()");
Action action = new SpELAction("#{ T(org.jeasy.rules.spel.Person).sayHi() }");
Facts facts = new Facts();
Person foo = new Person("foo", 20);
facts.put("person", foo);
@ -90,8 +90,8 @@ public class SpELActionTest {
@Test
public void testSpELActionWithExpressionAndParserContext() throws Exception {
// given
ParserContext context = new TemplateParserContext();
Action printAction = new SpELAction("#{ T(org.jeasy.rules.spel.Person).sayHello() }", context);
ParserContext context = new TemplateParserContext("%{", "}");
Action printAction = new SpELAction("%{ T(org.jeasy.rules.spel.Person).sayHello() }", context);
Facts facts = new Facts();
// when

@ -26,6 +26,7 @@ package org.jeasy.rules.spel;
import org.assertj.core.api.Assertions;
import org.jeasy.rules.api.Condition;
import org.jeasy.rules.api.Facts;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.springframework.context.ApplicationContext;
@ -41,7 +42,7 @@ public class SpELConditionTest {
@Test
public void testSpELExpressionEvaluation() {
// given
Condition isAdult = new SpELCondition("#person.age > 18");
Condition isAdult = new SpELCondition("#{ ['person'].age > 18 }");
Facts facts = new Facts();
facts.put("person", new Person("foo", 20));
// when
@ -55,7 +56,7 @@ public class SpELConditionTest {
@Test
public void whenDeclaredFactIsNotPresent_thenShouldReturnFalse() {
// given
Condition isHot = new SpELCondition("#temperature > 30");
Condition isHot = new SpELCondition("#{ ['temperature'] > 30 }");
Facts facts = new Facts();
// when
@ -79,7 +80,7 @@ public class SpELConditionTest {
assertThat(evaluationResult).isTrue();
}
@org.junit.Rule
@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
@Test
@ -90,9 +91,9 @@ public class SpELConditionTest {
SpELRule spELRule = new SpELRule();
// setting an condition to be evaluated
spELRule.when("#person.age >= 18");
spELRule.when("#{ ['person'].age >= 18 }");
// provided an bean resolver that can resolve "myGreeter"
spELRule.then("@myGreeter.greeting(#person.name)", beanResolver);
spELRule.then("#{ @myGreeter.greeting(#person.name) }", beanResolver);
// given
Facts facts = new Facts();

@ -38,8 +38,8 @@ public class SpELRuleTest {
public void setUp() {
facts = new Facts();
spelRule = new SpELRule().name("spel rule").description("rule using SpEL").priority(1)
.when("#person.age > 18")
.then("#person.setAdult(true)");
.when("#{ ['person'].age > 18 }")
.then("#{ ['person'].setAdult(true) }");
}
@Test
@ -74,8 +74,8 @@ public class SpELRuleTest {
Person foo = new Person("foo", 20);
facts.put("person", foo);
spelRule = new SpELRule().name("rn").description("rd").priority(1)
.when("#root['person'].age > 18")
.then("#root['person'].setAdult(true)");
.when("#{ #root['person'].age > 18 }")
.then("#{ #root['person'].setAdult(true) }");
// when
spelRule.execute(facts);

@ -8,14 +8,14 @@
"name": "rule1",
"condition": "true",
"actions": [
"System.out.println();"
"#{ T(System).out.println() }"
]
},
{
"name": "rule2",
"condition": "false",
"actions": [
"System.out.println();"
"#{ T(System).out.println() }"
]
}
]

@ -5,8 +5,8 @@ composingRules:
- name: rule1
condition: "true"
actions:
- "System.out.println();"
- "#{ T(System).out.println() }"
- name: rule2
condition: "false"
actions:
- "System.out.println();"
- "#{ T(System).out.println() }"

@ -8,18 +8,18 @@
"name": "Time is evening",
"description": "If it's later than 7pm",
"priority": 1,
"condition": "day.hour > 19",
"condition": "#{ ['day'].hour > 19 }",
"actions": [
"person.shouldProvideId(true);"
"#{ ['person'].shouldProvideId(true) }"
]
},
{
"name": "Movie is rated R",
"description": "If the movie is rated R",
"priority": 1,
"condition": "movie.rating == R",
"condition": "#{ ['movie'].rating == R }",
"actions": [
"person.shouldProvideId(true);"
"#{ ['person'].shouldProvideId(true) }"
]
}
]
@ -28,9 +28,9 @@
"name": "weather rule",
"description": "when it rains, then take an umbrella",
"priority": 1,
"condition": "rain == True",
"condition": "#{ ['rain'] == true }",
"actions": [
"System.out.println(\"It rains, take an umbrella!\");"
"#{ T(System).out.println(\"It rains, take an umbrella!\") }"
]
}
]

@ -5,19 +5,19 @@ composingRules:
- name: Time is evening
description: If it's later than 7pm
priority: 1
condition: "day.hour > 19"
condition: "#{ ['day'].hour > 19 }"
actions:
- "person.shouldProvideId(true);"
- "#{ ['person'].shouldProvideId(true) }"
- name: Movie is rated R
description: If the movie is rated R
priority: 1
condition: "movie.rating == R"
condition: "#{ ['movie'].rating == R }"
actions:
- "person.shouldProvideId(true);"
- "#{ ['person'].shouldProvideId(true) }"
---
name: weather rule
description: when it rains, then take an umbrella
priority: 1
condition: "rain == True"
condition: "#{ ['rain'] == true }"
actions:
- "System.out.println(\"It rains, take an umbrella!\");"
- "#{ T(System).out.println(\"It rains, take an umbrella!\") }"

@ -4,25 +4,25 @@
"priority": 1,
"condition": "true",
"actions": [
"System.out.println();"
"#{ T(System).out.println() }"
],
"composingRules": [
{
"name": "Time is evening",
"description": "If it's later than 7pm",
"priority": 1,
"condition": "day.hour > 19",
"condition": "#{ ['day'].hour > 19 }",
"actions": [
"person.shouldProvideId(true);"
"#{ ['person'].shouldProvideId(true); }"
]
},
{
"name": "Movie is rated R",
"description": "If the movie is rated R",
"priority": 1,
"condition": "movie.rating == R",
"condition": "#{ ['movie'].rating == R }",
"actions": [
"person.shouldProvideId(true);"
"#{ ['person'].shouldProvideId(true); }"
]
}
]

@ -2,17 +2,17 @@ name: Movie id rule
priority: 1
condition: "true"
actions:
- "System.out.println();"
- "#{ T(System).out.println() }"
composingRules:
- name: Time is evening
description: If it's later than 7pm
priority: 1
condition: "day.hour > 19"
condition: "#{ ['day'].hour > 19 }"
actions:
- "person.shouldProvideId(true);"
- "#{ ['person'].shouldProvideId(true); }"
- name: Movie is rated R
description: If the movie is rated R
priority: 1
condition: "movie.rating == R"
condition: "#{ ['movie'].rating == R }"
actions:
- "person.shouldProvideId(true);"
- "#{ ['person'].shouldProvideId(true); }"

@ -12,9 +12,9 @@
"name": "weather rule",
"description": "when it rains, then take an umbrella",
"priority": 2,
"condition": "rain == true",
"condition": "#{ ['rain'] == true }",
"actions": [
"System.out.println(\"It rains, take an umbrella!\");"
"#{ T(System).out.println(\"It rains, take an umbrella!\") }"
]
}
]

@ -9,6 +9,6 @@ actions:
name: weather rule
description: when it rains, then take an umbrella
priority: 2
condition: "rain == true"
condition: "#{ ['rain'] == true }"
actions:
- "System.out.println(\"It rains, take an umbrella!\");"
- "#{ T(System).out.println(\"It rains, take an umbrella!\") }"
Loading…
Cancel
Save