# 场景描述
直接看代码,可能不知所云,我先铺垫一下
复杂业务场景下,经常会有复杂条件判断,比如多个and或者or的条件组合,可以采用如下这种方式
我这里是需要获取到所有不匹配的数据,如果不需要就把注释掉的跳出代码放开即可
public static void main(String[] args) throws IOException {
String rule = "{ " +
" \"condition\": \"and\", " +
" \"children\": [{ " +
" \"condition\": \">=\", " +
" \"name\": \"xueli\", " +
" \"value\": \"3\" " +
" }, { " +
" \"condition\": \"=\", " +
" \"name\": \"zhiji\", " +
" \"value\": \"6\" " +
" }, { " +
" \"condition\": \">=\", " +
" \"name\": \"gongzuonianxian\", " +
" \"value\": \"1\" " +
" }, { " +
" \"condition\": \"or\", " +
" \"children\": [{ " +
" \"condition\": \">=\", " +
" \"name\": \"zhicheng\", " +
" \"value\": \"2\" " +
" }, { " +
" \"condition\": \">=\", " +
" \"name\": \"jinengjianding\", " +
" \"value\": \"2\" " +
" }] " +
" }] " +
"}";
JSONObject rules = JSONObject.parseObject(rule);
Map<String, Object> data = new HashMap<>();
data.put("xueli","3");
data.put("zhiji","6");
data.put("gongzuonianxian","1");
data.put("zhicheng","2");
data.put("jinengjianding","2");
Boolean status = makeData(rules, data);
System.out.println("============>" + status);
}
// 1、示意方法
public static Boolean makeData(JSONObject rule, Map<String, Object> data) {
String condition = rule.getString("condition");
JSONArray children = rule.getJSONArray("children");
boolean result;
if ("and".equals(condition)) {
result = true;
for (int i = 0; i < children.size(); i++) {
result &= makeData(children.getJSONObject(i), data);
/*if (!result) {
break;
}*/
}
} else if ("or".equals(condition)) {
result = false;
for (int i = 0; i < children.size(); i++) {
result |= makeData(children.getJSONObject(i), data);
/*if (result) {
break;
}*/
}
} else {
String name = rule.getString("name");
String value = rule.getString("value");
int dataVal = Integer.parseInt(data.get(name).toString());
int ruleVal = Integer.parseInt(value);
if (">=".equals(condition)) {
result = dataVal >= ruleVal;
if(!result){
System.out.println(name);
}
} else if ("=".equals(condition)) {
result = dataVal == ruleVal;
if(!result){
System.out.println(name);
}
} else {
throw new IllegalArgumentException("Unsupported condition: " + condition);
}
}
return result;
}
// 2、更全一点的方法
public static Boolean makeData(JSONObject rule, Map<String, Object> data) {
String condition = rule.getString("condition");
JSONArray children = rule.getJSONArray("children");
boolean result;
if ("and".equals(condition)) {
result = true;
for (int i = 0; i < children.size(); i++) {
result &= evaluateConditions(children.getJSONObject(i), data);
if (!result) {
// break;
}
}
} else if ("or".equals(condition)) {
result = false;
for (int i = 0; i < children.size(); i++) {
result |= evaluateConditions(children.getJSONObject(i), data);
if (result) {
// break;
}
}
} else {
String name = rule.getString("name");
String value = rule.getString("value");
Object dataVal = data.get(name);
if (dataVal != null) {
if ("ge".equals(condition)) {
result = Integer.parseInt(dataVal.toString()) >= Integer.parseInt(value);
} else if ("eq".equals(condition)) {
result = Integer.parseInt(dataVal.toString()) == Integer.parseInt(value);
} else if ("contains".equals(condition)) {
result = value.toString().contains(String.valueOf(dataVal));
} else {
throw new IllegalArgumentException("Unsupported condition: " + condition);
}
if (!result) {
System.out.println(name + " expected: " + value + ", but was: " + dataVal);
}
} else {
System.out.println("Value for " + name + " not found in data");
result = false;
}
}
return result;
}
复杂业务场景下的规则与数据碰撞(可用于漏洞规则匹配/复杂条件判断)
于 2024-08-29 17:31:34 首次发布