I use angular2-query-builder in my project. Besides, the back-end project using Java, Spring boot and Rulebook as a rule engine in order to fire rules which are generated by angular2-query-builder. So I need to make a logical expression from the output of angular2-query-builder.
This is a type-script object generated by query-builder.
query = {
condition: 'and',
rules: [
{field: 'age', operator: '<=', value: '16'},
{field: 'gender', operator: '=', value: 'm'}
]
};
The mapped model class which I've made in the back-end :
RuleSet {
String field;
String value;
String operator;
String entity;
String condition;
RuleSet[] rules;
//...getters and setters}
I need to get (age <= 16) && (gender = 'm')
from the object.
making a recursive method in order to traverse the rules
private void decode(RuleSet[] rules, List<String> expressions, String condition) {
for (int i = 0; i < rules.length; i++) {
RuleSet item = rules[i];
if (item.rules == null) {
expressions.add(merge("(", item.field, item.operator, item.value, ")"));
if (i != rules.length - 1) {
expressions.add(condition);
}
} else {
condition = item.getCondition();
expressions.add("(");
decode(item.rules, expressions, condition);
expressions.add(")");
}
}
}
public String merge(String... strings) {
StringBuilder result = new StringBuilder();
for (String item : strings) {
if (item != null) {
result.append(item);
}
}
return result.toString();
}
then call it like this:
List<String> expressions = new ArrayList<>();
decode(querySchema.ruleSet.rules, expressions, querySchema.ruleSet.condition);
by adding all items of expressions into a string object. The logical expression will be generated.