use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class RuleParserTest method testAutofocus.
@Test
public void testAutofocus() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "autofocus.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertNotNull(rule);
assertEquals("rule1", rule.getName());
final AttributeDescr att = (AttributeDescr) rule.getAttributes().get("auto-focus");
assertEquals("true", att.getValue());
assertEquals("auto-focus", att.getName());
}
use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class RuleParserTest method testAttributes_alternateSyntax.
@Test
public void testAttributes_alternateSyntax() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "rule_attributes_alt.drl");
assertEquals("simple_rule", rule.getName());
assertEqualsIgnoreWhitespace("bar();", (String) rule.getConsequence());
final Map<String, AttributeDescr> attrs = rule.getAttributes();
assertEquals(6, attrs.size());
AttributeDescr at = (AttributeDescr) attrs.get("salience");
assertEquals("salience", at.getName());
assertEquals("42", at.getValue());
at = (AttributeDescr) attrs.get("agenda-group");
assertEquals("agenda-group", at.getName());
assertEquals("my_group", at.getValue());
at = (AttributeDescr) attrs.get("no-loop");
assertEquals("no-loop", at.getName());
assertEquals("true", at.getValue());
at = (AttributeDescr) attrs.get("lock-on-active");
assertEquals("lock-on-active", at.getName());
assertEquals("true", at.getValue());
at = (AttributeDescr) attrs.get("duration");
assertEquals("duration", at.getName());
assertEquals("42", at.getValue());
at = (AttributeDescr) attrs.get("activation-group");
assertEquals("activation-group", at.getName());
assertEquals("my_activation_group", at.getValue());
}
use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class CompositePackageDescr method internalAdd.
private void internalAdd(Resource resource, PackageDescr packageDescr) {
for (ImportDescr descr : packageDescr.getImports()) {
addImport(descr);
descr.setResource(resource);
}
for (FunctionImportDescr descr : packageDescr.getFunctionImports()) {
addFunctionImport(descr);
descr.setResource(resource);
}
for (AccumulateImportDescr descr : packageDescr.getAccumulateImports()) {
addAccumulateImport(descr);
descr.setResource(resource);
}
for (AttributeDescr descr : packageDescr.getAttributes()) {
addAttribute(descr);
descr.setResource(resource);
}
for (GlobalDescr descr : packageDescr.getGlobals()) {
addGlobal(descr);
descr.setResource(resource);
}
for (FunctionDescr descr : packageDescr.getFunctions()) {
addFunction(descr);
descr.setResource(resource);
}
for (RuleDescr descr : packageDescr.getRules()) {
addRule(descr);
descr.setResource(resource);
}
// Avoid adding the same type declaration twice, see
// TypeDeclarationTest.testDuplicatedTypeDeclarationInDifferentResources
// IncrementalCompilationTest.testIncrementalCompilationWithAmbiguousRedeclares
// RHDM-1738
Set<TypeDeclarationDescr> typeDeclarationDescrs = new HashSet<>(getTypeDeclarations());
for (TypeDeclarationDescr descr : packageDescr.getTypeDeclarations()) {
if (!typeDeclarationDescrs.contains(descr)) {
addTypeDeclaration(descr);
descr.setResource(resource);
}
}
for (EnumDeclarationDescr enumDescr : packageDescr.getEnumDeclarations()) {
addEnumDeclaration(enumDescr);
enumDescr.setResource(resource);
}
for (EntryPointDeclarationDescr descr : packageDescr.getEntryPointDeclarations()) {
addEntryPointDeclaration(descr);
descr.setResource(resource);
}
for (WindowDeclarationDescr descr : packageDescr.getWindowDeclarations()) {
addWindowDeclaration(descr);
descr.setResource(resource);
}
packageDescr.getPreferredPkgUUID().ifPresent(pkgUUID -> {
if (getPreferredPkgUUID().isPresent() && !pkgUUID.equals(getPreferredPkgUUID().get())) {
throw new RuntimeException(String.format("Trying to overwrite preferredPkgUUID %s with a different value %s", getPreferredPkgUUID().get(), pkgUUID));
}
setPreferredPkgUUID(pkgUUID);
});
}
use of org.drools.drl.ast.descr.AttributeDescr 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