use of org.drools.modelcompiler.builder.PackageModel.DATE_TIME_FORMATTER_FIELD in project drools by kiegroup.
the class ModelGenerator method ruleAttributes.
/**
* Build a list of method calls, representing each needed {@link org.drools.model.impl.RuleBuilder#attribute(org.drools.model.Rule.Attribute, Object)}
* starting from a drools-compiler {@link RuleDescr}.
* The tuple represent the Rule Attribute expressed in JavParser form, and the attribute value expressed in JavaParser form.
*/
private static List<MethodCallExpr> ruleAttributes(RuleContext context, RuleDescr ruleDescr) {
final List<MethodCallExpr> ruleAttributes = new ArrayList<>();
final Set<Entry<String, AttributeDescr>> excludingDialect = ruleDescr.getAttributes().entrySet().stream().filter(r -> !r.getKey().equals("dialect")).collect(Collectors.toSet());
for (Entry<String, AttributeDescr> as : excludingDialect) {
MethodCallExpr attributeCall = new MethodCallExpr(null, ATTRIBUTE_CALL);
attributeCall.addArgument(attributesMap.get(as.getKey()));
String value = as.getValue().getValue().trim();
switch(as.getKey()) {
case "salience":
try {
Integer.parseInt(value);
attributeCall.addArgument(value);
} catch (NumberFormatException nfe) {
addDynamicAttributeArgument(context, attributeCall, value, int.class);
}
break;
case "enabled":
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
attributeCall.addArgument(value.toLowerCase());
} else {
addDynamicAttributeArgument(context, attributeCall, value, boolean.class);
}
break;
case "no-loop":
case "auto-focus":
case "lock-on-active":
attributeCall.addArgument(value);
break;
case "agenda-group":
case "activation-group":
case "ruleflow-group":
case "duration":
attributeCall.addArgument(toStringLiteral(value));
break;
case "timer":
if (validateTimer(value)) {
attributeCall.addArgument(toStringLiteral(value));
} else {
context.addCompilationError(new InvalidExpressionErrorResult(value));
}
break;
case "calendars":
if (value.startsWith("[")) {
value = value.substring(1, value.length() - 1).trim();
}
Expression arrayExpr = parseExpression("new String[] { " + value + " }");
attributeCall.addArgument(arrayExpr);
break;
case "date-effective":
case "date-expires":
attributeCall.addArgument(parseExpression(String.format("java.util.GregorianCalendar.from(java.time.LocalDate.parse(\"%s\", " + DATE_TIME_FORMATTER_FIELD + ").atStartOfDay(java.time.ZoneId.systemDefault()))", as.getValue().getValue())));
break;
default:
throw new UnsupportedOperationException("Unhandled case for rule attribute: " + as.getKey());
}
ruleAttributes.add(attributeCall);
}
return ruleAttributes;
}
Aggregations