use of org.drools.compiler.lang.descr.BindingDescr in project drools by kiegroup.
the class PatternBuilder method buildXPathDescr.
private Constraint buildXPathDescr(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, BaseDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
String expression = ((ExpressionDescr) descr).getExpression();
XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expression);
if (xpathAnalysis.hasError()) {
registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': " + xpathAnalysis.getError());
return null;
}
XpathConstraint xpathConstraint = new XpathConstraint();
ObjectType objectType = pattern.getObjectType();
Class<?> patternClass = objectType.getClassType();
List<Class<?>> backReferenceClasses = new ArrayList<Class<?>>();
backReferenceClasses.add(patternClass);
XpathBackReference backRef = new XpathBackReference(pattern, backReferenceClasses);
pattern.setBackRefDeclarations(backRef);
ObjectType originalType = pattern.getObjectType();
ObjectType currentObjectType = originalType;
mvelCtx.setInXpath(true);
try {
for (XpathAnalysis.XpathPart part : xpathAnalysis) {
XpathConstraint.XpathChunk xpathChunk = xpathConstraint.addChunck(patternClass, part.getField(), part.getIndex(), part.isIterate(), part.isLazy());
// make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
context.getPkg().getClassFieldAccessorStore().wireObjectType(currentObjectType, xpathChunk);
if (xpathChunk == null) {
registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': cannot access " + part.getField() + " on " + patternClass);
pattern.setObjectType(originalType);
return null;
}
if (part.getInlineCast() != null) {
try {
patternClass = context.getDialect().getTypeResolver().resolveType(part.getInlineCast());
} catch (ClassNotFoundException e) {
registerDescrBuildError(context, patternDescr, "Unknown class " + part.getInlineCast() + " in xpath expression '" + expression + "'");
return null;
}
part.addInlineCastConstraint(patternClass);
currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
xpathChunk.setReturnedType(currentObjectType);
} else {
patternClass = xpathChunk.getReturnedClass();
currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
}
pattern.setObjectType(currentObjectType);
backReferenceClasses.add(0, patternClass);
backRef.reset();
for (String constraint : part.getConstraints()) {
ConstraintConnectiveDescr result = parseExpression(context, patternDescr, new ExprConstraintDescr(constraint), constraint);
if (result == null) {
continue;
}
for (Constraint c : build(context, patternDescr, pattern, result, mvelCtx)) {
xpathChunk.addConstraint(c);
}
}
}
} finally {
mvelCtx.setInXpath(false);
pattern.setBackRefDeclarations(null);
pattern.setObjectType(originalType);
}
xpathConstraint.setXpathStartDeclaration(patternDescr.getXpathStartDeclaration());
if (descr instanceof BindingDescr) {
xpathConstraint.setDeclaration(pattern.addDeclaration(((BindingDescr) descr).getVariable()));
}
return xpathConstraint;
}
use of org.drools.compiler.lang.descr.BindingDescr in project drools by kiegroup.
the class PatternBuilder method buildCcdDescr.
protected Constraint buildCcdDescr(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, BaseDescr d, ConstraintConnectiveDescr ccd, MVELDumper.MVELDumperContext mvelCtx) {
d.copyLocation(patternDescr);
mvelCtx.clear();
String expr = context.getCompilerFactory().getExpressionProcessor().dump(d, ccd, mvelCtx);
Map<String, OperatorDescr> aliases = mvelCtx.getAliases();
// create bindings
TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
for (BindingDescr bind : mvelCtx.getBindings()) {
buildRuleBindings(context, patternDescr, pattern, bind, typeDeclaration);
}
if (expr.length() == 0) {
return null;
}
// check if it is an atomic expression
Constraint constraint = processAtomicExpression(context, pattern, d, expr, aliases);
// otherwise check if it is a simple expression
return constraint != null ? constraint : buildExpression(context, pattern, d, expr, aliases);
}
use of org.drools.compiler.lang.descr.BindingDescr in project drools by kiegroup.
the class PatternBuilder method createDeclarationObject.
protected static Declaration createDeclarationObject(final RuleBuildContext context, final String identifier, final String expr, final Pattern pattern) {
final BindingDescr implicitBinding = new BindingDescr(identifier, expr);
final Declaration declaration = new Declaration(identifier, null, pattern, true);
InternalReadAccessor extractor = getFieldReadAccessor(context, implicitBinding, pattern, implicitBinding.getExpression(), declaration, false);
if (extractor == null) {
return null;
}
declaration.setReadAccessor(extractor);
return declaration;
}
use of org.drools.compiler.lang.descr.BindingDescr 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.BindingDescr in project drools by kiegroup.
the class PatternBuilder method processPositional.
protected void processPositional(final RuleBuildContext context, final PatternDescr patternDescr, final Pattern pattern, final ExprConstraintDescr descr) {
if (descr.getType() == ExprConstraintDescr.Type.POSITIONAL && pattern.getObjectType() instanceof ClassObjectType) {
Class<?> klazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
TypeDeclaration tDecl = context.getKnowledgeBuilder().getTypeDeclaration(klazz);
if (tDecl == null) {
registerDescrBuildError(context, patternDescr, "Unable to find @positional definitions for :" + klazz + "\n");
return;
}
ClassDefinition clsDef = tDecl.getTypeClassDef();
if (clsDef == null) {
registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
return;
}
FieldDefinition field = clsDef.getField(descr.getPosition());
if (field == null) {
registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
return;
}
String expr = descr.getExpression();
boolean isSimpleIdentifier = isIdentifier(expr);
if (isSimpleIdentifier) {
// create a binding
BindingDescr binder = new BindingDescr();
binder.setUnification(true);
binder.setExpression(field.getName());
binder.setVariable(descr.getExpression());
buildRuleBindings(context, patternDescr, pattern, binder);
} else {
// create a constraint
build(context, patternDescr, pattern, descr, field.getName() + " == " + descr.getExpression());
}
}
}
Aggregations