fix issue #24 : Improve rule name/description default values

pull/27/head
Mahmoud Ben Hassine 9 years ago
parent 1a6f2994a4
commit 57f9c1e9bc

@ -9,6 +9,7 @@ import org.easyrules.util.Utils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
@ -40,33 +41,34 @@ class RuleProxy implements InvocationHandler {
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
if (method.getName().equals("getName")) {
return getRuleAnnotation().name();
String methodName = method.getName();
if (methodName.equals("getName")) {
return getRuleName();
}
if (method.getName().equals("getDescription")) {
return getRuleAnnotation().description();
if (methodName.equals("getDescription")) {
return getRuleDescription();
}
if (method.getName().equals("getPriority")) {
return getPriority();
if (methodName.equals("getPriority")) {
return getRulePriority();
}
if (method.getName().equals("evaluate")) {
if (methodName.equals("evaluate")) {
return getConditionMethod().invoke(target, args); // validated upfront
}
if (method.getName().equals("execute")) {
if (methodName.equals("execute")) {
for (ActionMethodOrderBean actionMethodBean : getActionMethodBeans()) {
actionMethodBean.getMethod().invoke(target);
}
}
if (method.getName().equals("equals")) {
if (methodName.equals("equals")) {
return target.equals(args[0]);
}
if (method.getName().equals("hashCode")) {
if (methodName.equals("hashCode")) {
return target.hashCode();
}
if (method.getName().equals("toString")) {
if (methodName.equals("toString")) {
return target.toString();
}
if (method.getName().equals("compareTo")) {
if (methodName.equals("compareTo")) {
Method compareToMethod = getCompareToMethod();
if (compareToMethod != null) {
return compareToMethod.invoke(target, args);
@ -81,8 +83,8 @@ class RuleProxy implements InvocationHandler {
private int compareTo(final org.easyrules.api.Rule otherRule) throws Exception {
String otherName = otherRule.getName();
int otherPriority = otherRule.getPriority();
String name = getRuleAnnotation().name();
int priority = getPriority();
String name = getRuleName();
int priority = getRulePriority();
if (priority < otherPriority) {
return -1;
@ -93,7 +95,7 @@ class RuleProxy implements InvocationHandler {
}
}
private int getPriority() throws Exception {
private int getRulePriority() throws Exception {
int priority = Utils.DEFAULT_RULE_PRIORITY;
Method[] methods = getMethods();
@ -118,7 +120,7 @@ class RuleProxy implements InvocationHandler {
private Set<ActionMethodOrderBean> getActionMethodBeans() {
Method[] methods = getMethods();
Set<ActionMethodOrderBean> actionMethodBeans = new TreeSet<ActionMethodOrderBean>();
Set<ActionMethodOrderBean> actionMethodBeans = new TreeSet<>();
for (Method method : methods) {
if (method.isAnnotationPresent(Action.class)) {
Action actionAnnotation = method.getAnnotation(Action.class);
@ -140,11 +142,49 @@ class RuleProxy implements InvocationHandler {
}
private Method[] getMethods() {
return target.getClass().getMethods();
return getTargetClass().getMethods();
}
private Rule getRuleAnnotation() {
return target.getClass().getAnnotation(Rule.class);
return getTargetClass().getAnnotation(Rule.class);
}
private String getRuleName() {
Rule rule = getRuleAnnotation();
return rule.name().equals(Utils.DEFAULT_RULE_NAME) ? getTargetClass().getSimpleName() : rule.name();
}
private String getRuleDescription() {
// Default description = "when " + conditionMethodName + " then " + comma separated actionMethodsNames
StringBuilder description = new StringBuilder();
appendConditionMethodName(description);
appendActionMethodsNames(description);
Rule rule = getRuleAnnotation();
return rule.description().equals(Utils.DEFAULT_RULE_DESCRIPTION) ? description.toString() : rule.description();
}
private void appendConditionMethodName(StringBuilder description) {
Method method = getConditionMethod();
if (method != null) {
description.append("when ");
description.append(method.getName());
description.append(" then ");
}
}
private void appendActionMethodsNames(StringBuilder description) {
Iterator<ActionMethodOrderBean> iterator = getActionMethodBeans().iterator();
while (iterator.hasNext()) {
description.append(iterator.next().getMethod().getName());
if (iterator.hasNext()) {
description.append(",");
}
}
}
private Class<?> getTargetClass() {
return target.getClass();
}
}

@ -97,6 +97,18 @@ public class DefaultRulesEngineTest {
assertThat(annotatedRule.isExecuted()).isTrue();
}
@Test
public void whenRuleNameIsNotSpecified_thenItShouldBeEqualToClassNameByDefault() throws Exception {
org.easyrules.api.Rule rule = RuleProxy.asRule(new DummyRule());
assertThat(rule.getName()).isEqualTo("DummyRule");
}
@Test
public void whenRuleDescriptionIsNotSpecified_thenItShouldBeEqualToConditionNameFollowedByActionsNames() throws Exception {
org.easyrules.api.Rule rule = RuleProxy.asRule(new DummyRule());
assertThat(rule.getDescription()).isEqualTo("when condition then action1,action2");
}
@After
public void clearRules() {
rulesEngine.clearRules();
@ -144,4 +156,24 @@ public class DefaultRulesEngineTest {
}
}
@Rule
public class DummyRule {
@Condition
public boolean condition() {
return true;
}
@Action(order = 1)
public void action1() throws Exception {
// no op
}
@Action(order = 2)
public void action2() throws Exception {
// no op
}
}
}

Loading…
Cancel
Save