use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class RuleParserTest method testEnabledExpression.
@Test
public void testEnabledExpression() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "rule_enabled_expression.drl");
assertEquals("simple_rule", rule.getName());
assertEqualsIgnoreWhitespace("bar();", (String) rule.getConsequence());
final Map<String, AttributeDescr> attrs = rule.getAttributes();
assertEquals(3, attrs.size());
AttributeDescr at = (AttributeDescr) attrs.get("enabled");
assertEquals("enabled", at.getName());
assertEquals("( 1 + 1 == 2 )", at.getValue());
at = (AttributeDescr) attrs.get("salience");
assertEquals("salience", at.getName());
assertEquals("( 1+2 )", at.getValue());
at = (AttributeDescr) attrs.get("lock-on-active");
assertEquals("lock-on-active", at.getName());
assertEquals("true", at.getValue());
}
use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class RuleParserTest method testDurationExpression.
@Test
public void testDurationExpression() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "rule_duration_expression.drl");
assertEquals("simple_rule", rule.getName());
assertEqualsIgnoreWhitespace("bar();", (String) rule.getConsequence());
final Map<String, AttributeDescr> attrs = rule.getAttributes();
assertEquals(2, attrs.size());
AttributeDescr at = (AttributeDescr) attrs.get("duration");
assertEquals("duration", at.getName());
assertEquals("1h30m", at.getValue());
at = (AttributeDescr) attrs.get("lock-on-active");
assertEquals("lock-on-active", at.getName());
assertEquals("true", at.getValue());
}
use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class RuleDescrVisitor method visitRuleDescr.
public void visitRuleDescr(RuleDescr descr) throws UnknownDescriptionException {
rule = new VerifierRule(descr, rulePackage, descr.getNamedConsequences());
rule.setName(descr.getName());
for (AttributeDescr attribute : descr.getAttributes().values()) {
rule.getAttributes().put(attribute.getName(), attribute.getValue());
}
Consequence consequence = visitConsequence(rule, descr.getConsequence());
for (String name : descr.getNamedConsequences().keySet()) {
visitNamedConsequence(name, descr.getNamedConsequences().get(name));
}
// TODO: NEEDS TO BE FIXED
// rule.getMetadata().putAll( descr.get );
rule.setConsequencePath(consequence.getPath());
rule.setConsequenceType(consequence.getConsequenceType());
rule.setLineNumber(descr.getLine());
data.add(rule);
rulePackage.getRules().add(rule);
solvers.startRuleSolver(rule);
visitAndDescr(descr.getLhs());
solvers.endRuleSolver();
addSubItems();
}
use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class RuleBuilder method buildAttributes.
public static void buildAttributes(final RuleBuildContext context) {
final RuleImpl rule = context.getRule();
final RuleDescr ruleDescr = context.getRuleDescr();
boolean enforceEager = false;
for (final AttributeDescr attributeDescr : ruleDescr.getAttributes().values()) {
final String name = attributeDescr.getName();
switch(name) {
case "no-loop":
rule.setNoLoop(getBooleanValue(attributeDescr, true));
enforceEager = true;
break;
case "auto-focus":
rule.setAutoFocus(getBooleanValue(attributeDescr, true));
break;
case "agenda-group":
if (StringUtils.isEmpty(rule.getRuleFlowGroup())) {
// don't override if RFG has already set this
rule.setAgendaGroup(attributeDescr.getValue());
} else {
if (rule.getRuleFlowGroup().equals(attributeDescr.getValue())) {
DroolsWarning warn = new RuleBuildWarning(rule, context.getParentDescr(), null, "Both an agenda-group ( " + attributeDescr.getValue() + " ) and a ruleflow-group ( " + rule.getRuleFlowGroup() + " ) are defined for rule " + rule.getName() + ". Since version 6.x the " + "two concepts have been unified, the ruleflow-group name will override the agenda-group. ");
context.addWarning(warn);
}
}
break;
case "activation-group":
rule.setActivationGroup(attributeDescr.getValue());
break;
case "ruleflow-group":
rule.setRuleFlowGroup(attributeDescr.getValue());
if (!rule.getAgendaGroup().equals(AgendaGroup.MAIN) && !rule.getAgendaGroup().equals(attributeDescr.getValue())) {
DroolsWarning warn = new RuleBuildWarning(rule, context.getParentDescr(), null, "Both an agenda-group ( " + attributeDescr.getValue() + " ) and a ruleflow-group ( " + rule.getRuleFlowGroup() + " ) are defined for rule " + rule.getName() + ". Since version 6.x the " + "two concepts have been unified, the ruleflow-group name will override the agenda-group. ");
context.addWarning(warn);
}
// assign AG to the same name as RFG, as they are aliased to AGs anyway
rule.setAgendaGroup(attributeDescr.getValue());
break;
case "lock-on-active":
boolean lockOnActive = getBooleanValue(attributeDescr, true);
rule.setLockOnActive(lockOnActive);
enforceEager |= lockOnActive;
break;
case DroolsSoftKeywords.DURATION:
case DroolsSoftKeywords.TIMER:
String duration = attributeDescr.getValue();
rule.setTimer(buildTimer(rule, duration, context));
break;
case "calendars":
buildCalendars(rule, attributeDescr.getValue(), context);
break;
case "date-effective":
try {
Date date = DateUtils.parseDate(attributeDescr.getValue());
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
rule.setDateEffective(cal);
} catch (Exception e) {
DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Wrong date-effective value: " + e.getMessage());
context.addError(err);
}
break;
case "date-expires":
try {
Date date = DateUtils.parseDate(attributeDescr.getValue());
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
rule.setDateExpires(cal);
} catch (Exception e) {
DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Wrong date-expires value: " + e.getMessage());
context.addError(err);
}
break;
}
}
buildSalience(context);
buildEnabled(context);
parseAnnotation(context, rule, ruleDescr, enforceEager);
}
use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class MVELConsequenceBuilderTest method setupTest.
private void setupTest(String consequence, Map<String, Object> namedConsequences) {
builder = new MVELConsequenceBuilder();
InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools.mvel.compiler.test");
pkg.addImport(new ImportDeclaration(Cheese.class.getCanonicalName()));
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, conf);
ruleDescr = new RuleDescr("test consequence builder");
ruleDescr.setConsequence(consequence);
ruleDescr.addAttribute(new AttributeDescr("dialect", "mvel"));
for (Entry<String, Object> entry : namedConsequences.entrySet()) {
ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
}
RuleImpl rule = new RuleImpl(ruleDescr.getName());
rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
rule.addPattern(new Pattern(0, new ClassObjectType(Map.class), "$map"));
PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry reg = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
context = new RuleBuildContext(pkgBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
for (String name : namedConsequences.keySet()) {
context.getDialect().getConsequenceBuilder().build(context, name);
}
context.getDialect().addRule(context);
pkgRegistry.getPackage().addRule(context.getRule());
pkgBuilder.compileAll();
pkgBuilder.reloadAll();
if (pkgBuilder.hasErrors()) {
fail(pkgBuilder.getErrors().toString());
}
}
Aggregations