use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class PatternBuilder method processConstraintsAndBinds.
/**
* Process all constraints and bindings on this pattern
*/
private void processConstraintsAndBinds(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern) {
DumperContext mvelCtx = new DumperContext().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;
}
result.setNegated(b.isNegated());
isPositional &= !(result.getDescrs().size() == 1 && result.getDescrs().get(0) instanceof BindingDescr);
if (isPositional) {
processPositional(context, patternDescr, xpathStartDeclaration, pattern, (ExprConstraintDescr) b);
} else {
// need to build the actual constraint
List<Constraint> constraints = build(context, patternDescr, xpathStartDeclaration, pattern, result, mvelCtx);
pattern.addConstraints(constraints);
}
}
TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
for (String field : lookAheadFieldsOfIdentifier(context.getRuleDescr(), patternDescr)) {
addFieldToPatternWatchlist(pattern, typeDeclaration, field);
}
}
}
use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class PatternBuilder method buildXPathDescr.
private Constraint buildXPathDescr(RuleBuildContext context, PatternDescr patternDescr, Declaration xpathStartDeclaration, Pattern pattern, ExpressionDescr descr, DumperContext mvelCtx) {
String expression = 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();
if (objectType.isTemplate()) {
throw new UnsupportedOperationException("xpath is not supported with fact templates");
}
Class<?> patternClass = ((ClassObjectType) objectType).getClassType();
List<Class<?>> backReferenceClasses = new ArrayList<>();
backReferenceClasses.add(patternClass);
XpathBackReference backRef = new XpathBackReference(pattern, backReferenceClasses);
pattern.setBackRefDeclarations(backRef);
ObjectType originalType = pattern.getObjectType();
ObjectType currentObjectType = originalType;
mvelCtx.setInXpath(true);
try {
// then the xpathoffset must be adjusted by -1, as pattern is not actually added to the rete network
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().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());
}
context.increaseXpathChuckNr();
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;
}
// preserving this, as the recursive build() resets it
int chunkNbr = context.getXpathChuckNr();
for (Constraint c : build(context, patternDescr, xpathStartDeclaration, pattern, result, mvelCtx)) {
xpathChunk.addConstraint(c);
}
context.setXpathChuckNr(chunkNbr);
}
}
xpathConstraint.setXpathStartDeclaration(xpathStartDeclaration);
if (descr instanceof BindingDescr) {
Declaration pathDeclr = pattern.addDeclaration(((BindingDescr) descr).getVariable());
pathDeclr.setxPathOffset(context.getXpathChuckNr());
xpathConstraint.setDeclaration(pathDeclr);
}
} finally {
mvelCtx.setInXpath(false);
pattern.setBackRefDeclarations(null);
pattern.setObjectType(originalType);
context.resetXpathChuckNr();
}
return xpathConstraint;
}
use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class DescrDumper method processConnectiveDescr.
protected void processConnectiveDescr(StringBuilder sbuilder, BaseDescr base, int parentPriority, boolean isInsideRelCons, DumperContext context) {
ConstraintConnectiveDescr ccd = (ConstraintConnectiveDescr) base;
boolean wrapParenthesis = parentPriority > ccd.getConnective().getPrecedence();
if (wrapParenthesis) {
sbuilder.append("( ");
}
boolean first = true;
List<BaseDescr> descrs = new ArrayList<>(ccd.getDescrs());
for (BaseDescr constr : descrs) {
if (!(constr instanceof BindingDescr)) {
if (first) {
first = false;
} else {
sbuilder.append(" ");
sbuilder.append(ccd.getConnective().toString());
sbuilder.append(" ");
}
}
context.incOpenCcd();
dump(sbuilder, constr, ccd, ccd.getDescrs().indexOf(constr), ccd.getConnective().getPrecedence(), isInsideRelCons, context);
context.decOpenCcd();
}
if (first) {
// means all children were actually only bindings, replace by just true
sbuilder.append("true");
}
if (wrapParenthesis) {
sbuilder.append(" )");
}
}
use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class DRL6Expressions method relationalExpression.
// $ANTLR start "relationalExpression"
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:368:1: relationalExpression returns [BaseDescr result] : left= shiftExpression ( ( operator | LEFT_PAREN )=>right= orRestriction )* ;
public final BaseDescr relationalExpression() throws RecognitionException {
relationalExpression_stack.push(new relationalExpression_scope());
BaseDescr result = null;
ParserRuleReturnScope left = null;
BaseDescr right = null;
relationalExpression_stack.peek().lsd = null;
try {
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:371:3: (left= shiftExpression ( ( operator | LEFT_PAREN )=>right= orRestriction )* )
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:371:5: left= shiftExpression ( ( operator | LEFT_PAREN )=>right= orRestriction )*
{
pushFollow(FOLLOW_shiftExpression_in_relationalExpression1832);
left = shiftExpression();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
if (buildDescr) {
if ((left != null ? ((DRL6Expressions.shiftExpression_return) left).result : null) == null) {
result = new AtomicExprDescr((left != null ? input.toString(left.start, left.stop) : null));
} else if ((left != null ? ((DRL6Expressions.shiftExpression_return) left).result : null) instanceof AtomicExprDescr) {
if ((left != null ? input.toString(left.start, left.stop) : null).equals(((AtomicExprDescr) (left != null ? ((DRL6Expressions.shiftExpression_return) left).result : null)).getExpression())) {
result = (left != null ? ((DRL6Expressions.shiftExpression_return) left).result : null);
} else {
result = new AtomicExprDescr((left != null ? input.toString(left.start, left.stop) : null));
}
} else if ((left != null ? ((DRL6Expressions.shiftExpression_return) left).result : null) instanceof BindingDescr) {
if ((left != null ? input.toString(left.start, left.stop) : null).equals(((BindingDescr) (left != null ? ((DRL6Expressions.shiftExpression_return) left).result : null)).getExpression())) {
result = (left != null ? ((DRL6Expressions.shiftExpression_return) left).result : null);
} else {
BindingDescr bind = (BindingDescr) (left != null ? ((DRL6Expressions.shiftExpression_return) left).result : null);
int offset = bind.isUnification() ? 2 : 1;
String fullExpression = (left != null ? input.toString(left.start, left.stop) : null).substring((left != null ? input.toString(left.start, left.stop) : null).indexOf(":") + offset).trim();
result = new BindingDescr(bind.getVariable(), bind.getExpression(), fullExpression, bind.isUnification());
}
} else {
result = (left != null ? ((DRL6Expressions.shiftExpression_return) left).result : null);
}
relationalExpression_stack.peek().lsd = result;
}
}
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:396:3: ( ( operator | LEFT_PAREN )=>right= orRestriction )*
loop37: while (true) {
int alt37 = 2;
int LA37_0 = input.LA(1);
if ((LA37_0 == ID)) {
int LA37_2 = input.LA(2);
if ((((((helper.validateIdentifierKey(DroolsSoftKeywords.NOT))) || ((helper.isPluggableEvaluator(false)))) && synpred9_DRL6Expressions()))) {
alt37 = 1;
}
} else if ((LA37_0 == EQUALS)) {
int LA37_3 = input.LA(2);
if ((synpred9_DRL6Expressions())) {
alt37 = 1;
}
} else if ((LA37_0 == NOT_EQUALS)) {
int LA37_4 = input.LA(2);
if ((synpred9_DRL6Expressions())) {
alt37 = 1;
}
} else if ((LA37_0 == LESS)) {
int LA37_20 = input.LA(2);
if ((synpred9_DRL6Expressions())) {
alt37 = 1;
}
} else if ((LA37_0 == GREATER)) {
int LA37_21 = input.LA(2);
if ((synpred9_DRL6Expressions())) {
alt37 = 1;
}
} else if ((LA37_0 == TILDE) && (synpred9_DRL6Expressions())) {
alt37 = 1;
} else if ((LA37_0 == LESS_EQUALS) && (synpred9_DRL6Expressions())) {
alt37 = 1;
} else if ((LA37_0 == GREATER_EQUALS) && (synpred9_DRL6Expressions())) {
alt37 = 1;
} else if ((LA37_0 == LEFT_PAREN) && (synpred9_DRL6Expressions())) {
alt37 = 1;
}
switch(alt37) {
case 1:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:396:5: ( operator | LEFT_PAREN )=>right= orRestriction
{
pushFollow(FOLLOW_orRestriction_in_relationalExpression1857);
right = orRestriction();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
if (buildDescr) {
result = right;
relationalExpression_stack.peek().lsd = result;
}
}
}
break;
default:
break loop37;
}
}
}
} catch (RecognitionException re) {
throw re;
} finally {
// do for sure before leaving
relationalExpression_stack.pop();
}
return result;
}
use of org.drools.drl.ast.descr.BindingDescr in project drools by kiegroup.
the class DRL6Expressions method inExpression.
// $ANTLR end "instanceOfExpression"
// $ANTLR start "inExpression"
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:325:1: inExpression returns [BaseDescr result] : left= relationalExpression ( ( not_key in_key )=> not_key in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN |in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN )? ;
public final BaseDescr inExpression() throws RecognitionException {
BaseDescr result = null;
BaseDescr left = null;
ParserRuleReturnScope e1 = null;
ParserRuleReturnScope e2 = null;
ConstraintConnectiveDescr descr = null;
BaseDescr leftDescr = null;
BindingDescr binding = null;
try {
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:328:3: (left= relationalExpression ( ( not_key in_key )=> not_key in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN |in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN )? )
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:328:5: left= relationalExpression ( ( not_key in_key )=> not_key in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN |in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN )?
{
pushFollow(FOLLOW_relationalExpression_in_inExpression1615);
left = relationalExpression();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
if (buildDescr) {
result = left;
}
if (left instanceof BindingDescr) {
binding = (BindingDescr) left;
leftDescr = new AtomicExprDescr(binding.getExpression());
} else {
leftDescr = left;
}
}
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:337:5: ( ( not_key in_key )=> not_key in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN |in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN )?
int alt36 = 3;
int LA36_0 = input.LA(1);
if ((LA36_0 == ID)) {
int LA36_1 = input.LA(2);
if ((LA36_1 == ID)) {
int LA36_3 = input.LA(3);
if ((LA36_3 == LEFT_PAREN) && ((((helper.validateIdentifierKey(DroolsSoftKeywords.NOT))) && synpred8_DRL6Expressions()))) {
alt36 = 1;
}
} else if ((LA36_1 == LEFT_PAREN) && (((helper.validateIdentifierKey(DroolsSoftKeywords.IN))))) {
alt36 = 2;
}
}
switch(alt36) {
case 1:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:337:6: ( not_key in_key )=> not_key in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN
{
pushFollow(FOLLOW_not_key_in_inExpression1635);
not_key();
state._fsp--;
if (state.failed)
return result;
pushFollow(FOLLOW_in_key_in_inExpression1639);
in_key();
state._fsp--;
if (state.failed)
return result;
match(input, LEFT_PAREN, FOLLOW_LEFT_PAREN_in_inExpression1641);
if (state.failed)
return result;
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
}
pushFollow(FOLLOW_expression_in_inExpression1663);
e1 = expression();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
descr = ConstraintConnectiveDescr.newAnd();
RelationalExprDescr rel = new RelationalExprDescr("!=", false, null, leftDescr, (e1 != null ? ((DRL6Expressions.expression_return) e1).result : null));
descr.addOrMerge(rel);
result = descr;
}
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:345:7: ( COMMA e2= expression )*
loop34: while (true) {
int alt34 = 2;
int LA34_0 = input.LA(1);
if ((LA34_0 == COMMA)) {
alt34 = 1;
}
switch(alt34) {
case 1:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:345:8: COMMA e2= expression
{
match(input, COMMA, FOLLOW_COMMA_in_inExpression1682);
if (state.failed)
return result;
pushFollow(FOLLOW_expression_in_inExpression1686);
e2 = expression();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
RelationalExprDescr rel = new RelationalExprDescr("!=", false, null, leftDescr, (e2 != null ? ((DRL6Expressions.expression_return) e2).result : null));
descr.addOrMerge(rel);
}
}
break;
default:
break loop34;
}
}
match(input, RIGHT_PAREN, FOLLOW_RIGHT_PAREN_in_inExpression1707);
if (state.failed)
return result;
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_END);
}
}
break;
case 2:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:351:7: in= in_key LEFT_PAREN e1= expression ( COMMA e2= expression )* RIGHT_PAREN
{
pushFollow(FOLLOW_in_key_in_inExpression1723);
in_key();
state._fsp--;
if (state.failed)
return result;
match(input, LEFT_PAREN, FOLLOW_LEFT_PAREN_in_inExpression1725);
if (state.failed)
return result;
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
}
pushFollow(FOLLOW_expression_in_inExpression1747);
e1 = expression();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
descr = ConstraintConnectiveDescr.newOr();
RelationalExprDescr rel = new RelationalExprDescr("==", false, null, leftDescr, (e1 != null ? ((DRL6Expressions.expression_return) e1).result : null));
descr.addOrMerge(rel);
result = descr;
}
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:359:7: ( COMMA e2= expression )*
loop35: while (true) {
int alt35 = 2;
int LA35_0 = input.LA(1);
if ((LA35_0 == COMMA)) {
alt35 = 1;
}
switch(alt35) {
case 1:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:359:8: COMMA e2= expression
{
match(input, COMMA, FOLLOW_COMMA_in_inExpression1766);
if (state.failed)
return result;
pushFollow(FOLLOW_expression_in_inExpression1770);
e2 = expression();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
RelationalExprDescr rel = new RelationalExprDescr("==", false, null, leftDescr, (e2 != null ? ((DRL6Expressions.expression_return) e2).result : null));
descr.addOrMerge(rel);
}
}
break;
default:
break loop35;
}
}
match(input, RIGHT_PAREN, FOLLOW_RIGHT_PAREN_in_inExpression1791);
if (state.failed)
return result;
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_END);
}
}
break;
}
}
if (state.backtracking == 0) {
if (binding != null && descr != null)
descr.addOrMerge(binding);
}
} catch (RecognitionException re) {
throw re;
} finally {
// do for sure before leaving
}
return result;
}
Aggregations