use of org.drools.compiler.compiler.RuleBuildError in project drools by kiegroup.
the class ConditionalBranchBuilder method build.
public ConditionalBranch build(RuleBuildContext context, BaseDescr descr, Pattern prefixPattern) {
ConditionalBranchDescr conditionalBranch = (ConditionalBranchDescr) descr;
String consequenceName = conditionalBranch.getConsequence().getName();
if (!context.getRuleDescr().getNamedConsequences().containsKey(consequenceName)) {
DroolsError err = new RuleBuildError(context.getRule(), context.getParentDescr(), null, "Unknown consequence name: " + consequenceName);
context.addError(err);
return null;
}
RuleConditionBuilder evalBuilder = (RuleConditionBuilder) context.getDialect().getBuilder(EvalDescr.class);
EvalCondition condition = (EvalCondition) evalBuilder.build(context, conditionalBranch.getCondition(), getLastPattern(context));
NamedConsequenceBuilder namedConsequenceBuilder = (NamedConsequenceBuilder) context.getDialect().getBuilder(NamedConsequenceDescr.class);
NamedConsequence consequence = namedConsequenceBuilder.build(context, conditionalBranch.getConsequence());
ConditionalBranchDescr elseBranchDescr = conditionalBranch.getElseBranch();
return new ConditionalBranch(condition, consequence, elseBranchDescr != null ? build(context, elseBranchDescr, prefixPattern) : null);
}
use of org.drools.compiler.compiler.RuleBuildError 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.compiler.compiler.RuleBuildError in project drools by kiegroup.
the class RuleBuilder method parseAnnotation.
private static void parseAnnotation(RuleBuildContext context, RuleImpl rule, RuleDescr ruleDescr, boolean enforceEager) {
try {
ActivationListener activationListener = getTypedAnnotation(ruleDescr, ActivationListener.class);
if (activationListener != null) {
rule.setActivationListener(CoreComponentsBuilder.get().getMVELExecutor().evalToString(activationListener.value()));
}
if (enforceEager) {
rule.setEager(true);
} else {
Propagation propagation = getTypedAnnotation(ruleDescr, Propagation.class);
if (propagation != null) {
if (propagation.value() == Propagation.Type.IMMEDIATE) {
rule.setDataDriven(true);
} else if (propagation.value() == Propagation.Type.EAGER) {
rule.setEager(true);
}
}
}
Direct direct = getTypedAnnotation(ruleDescr, Direct.class);
if (direct != null && direct.value()) {
rule.setActivationListener("direct");
}
rule.setAllMatches(ruleDescr.hasAnnotation(All.class));
} catch (Exception e) {
DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, e.getMessage());
context.addError(err);
}
}
use of org.drools.compiler.compiler.RuleBuildError in project drools by kiegroup.
the class RuleBuilder method buildCalendars.
private static void buildCalendars(RuleImpl rule, String calendarsString, RuleBuildContext context) {
Object val = null;
try {
val = CoreComponentsBuilder.get().getMVELExecutor().eval(calendarsString);
String[] calNames = null;
if (val instanceof List) {
calNames = (String[]) ((List) val).toArray(new String[((List) val).size()]);
} else if (val instanceof String) {
calNames = new String[] { (String) val };
} else {
DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Calendars attribute did not return a String or String[] '" + val + "'");
context.addError(err);
}
if (calNames != null) {
rule.setCalendars(calNames);
}
} catch (Exception e) {
DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Unable to build Calendars attribute '" + val + "'" + e.getMessage());
context.addError(err);
}
}
use of org.drools.compiler.compiler.RuleBuildError in project drools by kiegroup.
the class RuleBuildContext method initRuleUnitClassName.
private void initRuleUnitClassName() {
String ruleUnitClassName = rule.getRuleUnitClassName();
boolean nameInferredFromResource = false;
if (ruleUnitClassName == null && rule.getResource() != null && rule.getResource().getSourcePath() != null) {
// We cannot depend on splitting based on File.separator, because e.g. MemoryFileSystem is "/" based
// also on Windows => We need to parse the classname based on Java classname allowed characters.
ruleUnitClassName = extractClassNameFromSourcePath();
nameInferredFromResource = true;
}
if (RuleUnitComponentFactory.get() != null && ruleUnitClassName != null) {
TypeResolver typeResolver = getPkg().getTypeResolver();
boolean unitFound = false;
Class<?> ruleUnitClass = ClassUtils.safeLoadClass(typeResolver.getClassLoader(), ruleUnitClassName);
if (ruleUnitClass != null) {
unitFound = RuleUnitComponentFactory.get().isRuleUnitClass(ruleUnitClass);
if (unitFound && nameInferredFromResource) {
rule.setRuleUnitClassName(ruleUnitClassName);
}
}
if (!unitFound && !nameInferredFromResource) {
addError(new RuleBuildError(rule, getParentDescr(), null, ruleUnitClassName + " is not a valid RuleUnit class name"));
}
}
}
Aggregations