use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method parseLhs.
private Map<String, String> parseLhs(final RuleModel m, final AndDescr lhs, final boolean isJavaDialect, final ExpandedDRLInfo expandedDRLInfo, final PackageDataModelOracle dmo) {
Map<String, String> boundParams = new HashMap<String, String>();
int lineCounter = -1;
for (BaseDescr descr : lhs.getDescrs()) {
lineCounter = parseNonDrlInLhs(m, expandedDRLInfo, lineCounter);
IPattern pattern = parseBaseDescr(m, descr, isJavaDialect, boundParams, dmo);
if (pattern != null) {
m.addLhsItem(pattern);
}
}
parseNonDrlInLhs(m, expandedDRLInfo, lineCounter);
return boundParams;
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class ForallBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final ForallDescr forallDescr = (ForallDescr) descr;
final PatternBuilder patternBuilder = (PatternBuilder) context.getDialect().getBuilder(PatternDescr.class);
final Pattern basePattern = (Pattern) patternBuilder.build(context, forallDescr.getBasePattern());
if (basePattern == null) {
return null;
}
final Forall forall = new Forall(basePattern);
// adding the newly created forall CE to the build stack
// this is necessary in case of local declaration usage
context.getDeclarationResolver().pushOnBuildStack(forall);
for (BaseDescr baseDescr : forallDescr.getRemainingPatterns()) {
final Pattern anotherPattern = (Pattern) patternBuilder.build(context, (PatternDescr) baseDescr);
forall.addRemainingPattern(anotherPattern);
}
if (forallDescr.getDescrs().size() == 1) {
// An optimization for unlinking, where we allow unlinking if the resulting 'not' node has no constraints
// we need to record this here, due to getRemainingPatterns injecting "this == " + BASE_IDENTIFIER $__forallBaseIdentifier
// which we wish to ignore
PatternDescr p = (PatternDescr) forallDescr.getDescrs().get(0);
if (p.getConstraint().getDescrs().isEmpty()) {
forall.setEmptyBetaConstraints(true);
}
}
// poping the forall
context.getDeclarationResolver().popBuildStack();
return forall;
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class QueryElementBuilder method build.
@SuppressWarnings("unchecked")
public RuleConditionElement build(RuleBuildContext context, BaseDescr descr, QueryImpl query) {
PatternDescr patternDescr = (PatternDescr) descr;
Declaration[] params = query.getParameters();
List<BaseDescr> args = (List<BaseDescr>) patternDescr.getDescrs();
List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
ObjectType argsObjectType = ClassObjectType.ObjectArray_ObjectType;
InternalReadAccessor arrayReader = new SelfReferenceClassFieldReader(Object[].class);
Pattern pattern = new Pattern(context.getNextPatternId(), 0, argsObjectType, null);
if (!StringUtils.isEmpty(patternDescr.getIdentifier())) {
if (query.isAbductive()) {
Declaration declr = context.getDeclarationResolver().getDeclaration(patternDescr.getIdentifier());
if (declr != null && !patternDescr.isUnification()) {
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Duplicate declaration " + patternDescr.getIdentifier() + ", unable to bind abducted value"));
}
} else {
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Query binding is not supported by non-abductive queries : " + patternDescr.getIdentifier()));
}
}
boolean addAbductiveReturnArgument = query.isAbductive() && !StringUtils.isEmpty(patternDescr.getIdentifier()) && args.size() < params.length;
if (addAbductiveReturnArgument) {
ExprConstraintDescr extraDescr = new ExprConstraintDescr(patternDescr.getIdentifier());
extraDescr.setPosition(patternDescr.getConstraint().getDescrs().size());
extraDescr.setType(ExprConstraintDescr.Type.POSITIONAL);
args.add(extraDescr);
}
QueryArgument[] arguments = new QueryArgument[params.length];
// Deal with the constraints, both positional and bindings
for (BaseDescr base : args) {
String expression = null;
boolean isPositional = false;
boolean isBinding = false;
BindingDescr bind = null;
ConstraintConnectiveDescr result = null;
if (base instanceof BindingDescr) {
bind = (BindingDescr) base;
expression = bind.getVariable() + (bind.isUnification() ? " := " : " : ") + bind.getExpression();
isBinding = true;
} else {
if (base instanceof ExprConstraintDescr) {
ExprConstraintDescr ecd = (ExprConstraintDescr) base;
expression = ecd.getExpression();
isPositional = ecd.getType() == ExprConstraintDescr.Type.POSITIONAL;
} else {
expression = base.getText();
}
result = parseExpression(context, patternDescr, expression);
if (result == null) {
// error, can't parse expression.
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to parse constraint: \n" + expression));
continue;
}
isBinding = result.getDescrs().size() == 1 && result.getDescrs().get(0) instanceof BindingDescr;
if (isBinding) {
bind = (BindingDescr) result.getDescrs().get(0);
}
}
if ((!isPositional) && (!isBinding)) {
// error, can't have non binding slots.
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Query's must use positional or bindings, not field constraints:\n" + expression));
} else if (isPositional && isBinding) {
// error, can't have positional binding slots.
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Query's can't use positional bindings:\n" + expression));
} else if (isPositional) {
processPositional(context, query, params, arguments, requiredDeclarations, arrayReader, pattern, base, expression, result);
} else {
// it is binding
processBinding(context, descr, params, arguments, requiredDeclarations, arrayReader, pattern, bind);
}
}
List<Integer> varIndexList = new ArrayList<Integer>();
for (int i = 0; i < arguments.length; i++) {
if (!(arguments[i] instanceof QueryArgument.Declr)) {
if (arguments[i] instanceof QueryArgument.Var) {
varIndexList.add(i);
}
continue;
}
Class actual = ((QueryArgument.Declr) arguments[i]).getArgumentClass();
Declaration formalArgument = query.getParameters()[i];
Class formal = formalArgument.getDeclarationClass();
// input argument require a broader type, while output types require a narrower type, so we check for both.
if (!ClassUtils.isTypeCompatibleWithArgumentType(actual, formal) && !ClassUtils.isTypeCompatibleWithArgumentType(formal, actual)) {
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Query is being invoked with known argument of type " + actual + " at position " + i + ", but the expected query argument is of type " + formal));
}
}
return new QueryElement(pattern, query.getName(), arguments, toIntArray(varIndexList), requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]), !patternDescr.isQuery(), query.isAbductive());
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class PatternBuilder method processConstraintsAndBinds.
/**
* Process all constraints and bindings on this pattern
*/
protected void processConstraintsAndBinds(final RuleBuildContext context, final PatternDescr patternDescr, final Pattern pattern) {
MVELDumper.MVELDumperContext mvelCtx = new MVELDumper.MVELDumperContext().setRuleContext(context);
for (BaseDescr b : patternDescr.getDescrs()) {
String expression;
boolean isPositional = false;
if (b instanceof BindingDescr) {
BindingDescr bind = (BindingDescr) b;
expression = bind.getVariable() + (bind.isUnification() ? " := " : " : ") + bind.getExpression();
} else if (b instanceof ExprConstraintDescr) {
ExprConstraintDescr descr = (ExprConstraintDescr) b;
expression = descr.getExpression();
isPositional = descr.getType() == ExprConstraintDescr.Type.POSITIONAL;
} else {
expression = b.getText();
}
ConstraintConnectiveDescr result = parseExpression(context, patternDescr, b, expression);
if (result == null) {
return;
}
isPositional &= !(result.getDescrs().size() == 1 && result.getDescrs().get(0) instanceof BindingDescr);
if (isPositional) {
processPositional(context, patternDescr, pattern, (ExprConstraintDescr) b);
} else {
// need to build the actual constraint
List<Constraint> constraints = build(context, patternDescr, pattern, result, mvelCtx);
pattern.addConstraints(constraints);
}
}
TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
for (String field : context.getRuleDescr().lookAheadFieldsOfIdentifier(patternDescr)) {
addFieldToPatternWatchlist(pattern, typeDeclaration, field);
}
}
combineConstraints(context, pattern, mvelCtx);
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class PatternBuilder method build.
protected List<Constraint> build(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, ConstraintConnectiveDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
List<Constraint> constraints = new ArrayList<Constraint>();
List<BaseDescr> initialDescrs = new ArrayList<BaseDescr>(descr.getDescrs());
for (BaseDescr d : initialDescrs) {
boolean isXPath = isXPathDescr(d);
if (isXPath && pattern.hasXPath()) {
registerDescrBuildError(context, patternDescr, "More than a single oopath constraint is not allowed in the same pattern");
return constraints;
}
Constraint constraint = isXPath ? buildXPathDescr(context, patternDescr, pattern, d, mvelCtx) : buildCcdDescr(context, patternDescr, pattern, d, descr, mvelCtx);
if (constraint != null) {
Declaration declCorrXpath = getDeclarationCorrespondingToXpath(pattern, isXPath, constraint);
if (declCorrXpath == null) {
constraints.add(constraint);
} else {
// A constraint is using a declration bound to an xpath in the same pattern
// Move the constraint inside the last chunk of the xpath defining this declaration, rewriting it as 'this'
Pattern modifiedPattern = pattern.clone();
modifiedPattern.setObjectType(new ClassObjectType(declCorrXpath.getDeclarationClass()));
constraint = buildCcdDescr(context, patternDescr, modifiedPattern, d.replaceVariable(declCorrXpath.getBindingName(), "this"), descr, mvelCtx);
if (constraint != null) {
pattern.getXpathConstraint().getChunks().getLast().addConstraint(constraint);
}
}
}
}
if (descr.getDescrs().size() > initialDescrs.size()) {
// The initial build process may have generated other constraint descrs.
// This happens when null-safe references or inline-casts are used
// These additional constraints must be built, and added as
List<BaseDescr> additionalDescrs = new ArrayList<BaseDescr>(descr.getDescrs());
additionalDescrs.removeAll(initialDescrs);
if (!additionalDescrs.isEmpty()) {
List<Constraint> additionalConstraints = new ArrayList<Constraint>();
for (BaseDescr d : additionalDescrs) {
Constraint constraint = buildCcdDescr(context, patternDescr, pattern, d, descr, mvelCtx);
if (constraint != null) {
additionalConstraints.add(constraint);
}
}
constraints.addAll(0, additionalConstraints);
}
}
return constraints;
}
Aggregations