co.com.rule.application.util.RuleEvaluateUtil Maven / Gradle / Ivy
package co.com.rule.application.util;
import java.util.List;
import java.util.Map;
/**
*
* @author juanlocl
*
*/
public class RuleEvaluateUtil {
private static final String CONDITIONS = "conditions";
private static final String ALL = "all";
private static final String ANY = "any";
private static final String OPERATOR = "operator";
private static final String VALUE = "value";
private static final String FACT = "fact";
RuleEvaluateUtil() {
throw new IllegalAccessError("Utility class");
}
@SuppressWarnings("rawtypes")
public static boolean evaluate(String object, String rules) {
boolean result = false;
if (isValidObject(object) && isValidObject(rules)) {
Map objectLocal = JsonUtil.jsonToMap(object);
Map rulesLocal = JsonUtil.jsonToMap(rules);
if (isValidRules(rulesLocal)) {
result = evaluateRules(objectLocal, (Map) rulesLocal.get(CONDITIONS));
}
}
return result;
}
@SuppressWarnings("rawtypes")
public static String execute(String object, String rules) {
String result = "";
if (evaluate(object, rules)) {
Map objectLocal = JsonUtil.jsonToMap(object);
Map rulesLocal = (Map) JsonUtil.jsonToMap(rules).get("execute");
objectLocal = executeOperation(objectLocal, rulesLocal);
result = JsonUtil.toJsonString(objectLocal);
}
return result;
}
@SuppressWarnings("rawtypes")
private static Map executeOperation(Map object, Map executeRule) {
String operations = (String) (executeRule.keySet().toArray())[0];
Map result = null;
if (!operations.isEmpty()) {
switch (operations) {
case "replace":
result = replaceValues(object, (Map) executeRule.get(operations));
break;
default:
}
}
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Map replaceValues(Map object, Map values) {
values.forEach((key, value) -> object.put(key, value));
return object;
}
@SuppressWarnings("rawtypes")
private static boolean evaluateRules(Map object, Map rules) {
boolean result = false;
if (rules.containsKey(ALL)) {
if (rules.get(ALL) instanceof List) {
result = evaluateAllRules(object, (List) rules.get(ALL));
}
} else if (rules.containsKey(ANY)) {
if (rules.get(ANY) instanceof List) {
result = evaluateAnyRules(object, (List) rules.get(ANY));
}
} else if (isValidRule(rules)) {
result = evaluateRule(object, rules);
}
return result;
}
@SuppressWarnings("rawtypes")
private static boolean evaluateAllRules(Map object, List rules) {
for (Object rule : rules) {
if (!evaluateRule(object, rule)) {
return false;
}
}
return true;
}
@SuppressWarnings("rawtypes")
private static boolean evaluateAnyRules(Map object, List rules) {
for (Object rule : rules) {
if (evaluateRule(object, rule)) {
return true;
}
}
return false;
}
@SuppressWarnings("rawtypes")
private static boolean evaluateRule(Map object, Object rule) {
boolean result = false;
Map ruleTmp;
if (rule instanceof Map) {
ruleTmp = (Map) rule;
if (isValidRule(ruleTmp) && object.containsKey(ruleTmp.get(FACT))) {
result = executeOperation(ruleTmp.get(OPERATOR).toString(), object.get(ruleTmp.get(FACT)),
ruleTmp.get(VALUE));
} else {
result = evaluateRules(object, ruleTmp);
}
}
return result;
}
private static boolean executeOperation(String operator, Object value, Object valueExpected) {
boolean result;
switch (operator) {
case "equal":
result = equalValue(value, valueExpected);
break;
case "noEqual":
result = noEqualValue(value, valueExpected);
break;
case "empty":
result = emptyValue(value);
break;
case "noEmpty":
result = noEmptyValue(value);
break;
default:
result = false;
}
return result;
}
private static boolean emptyValue(Object value) {
return String.valueOf(value).isEmpty();
}
private static boolean noEmptyValue(Object value) {
return !String.valueOf(value).isEmpty();
}
private static boolean equalValue(Object value, Object valueExpected) {
return value.equals(valueExpected);
}
private static boolean noEqualValue(Object value, Object valueExpected) {
return !value.equals(valueExpected);
}
private static boolean isValidObject(String object) {
return object != null && !object.isEmpty();
}
@SuppressWarnings("rawtypes")
private static boolean isValidRules(Map rules) {
return rules.containsKey(CONDITIONS);
}
@SuppressWarnings("rawtypes")
private static boolean isValidRule(Map rule) {
return rule.containsKey(FACT) && rule.containsKey(OPERATOR) && rule.containsKey(VALUE);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy