use of org.drools.drl.ast.descr.RelationalExprDescr in project drools by kiegroup.
the class PatternBuilder method buildExpression.
private Constraint buildExpression(final RuleBuildContext context, final Pattern pattern, final BaseDescr d, final String expr, final Map<String, OperatorDescr> aliases, boolean negated) {
if ("_.neg".equals(expr)) {
pattern.setHasNegativeConstraint(true);
return new NegConstraint();
} else if ("!_.neg".equals(expr)) {
pattern.setHasNegativeConstraint(true);
return new NegConstraint(false);
}
RelationalExprDescr relDescr = d instanceof RelationalExprDescr ? (RelationalExprDescr) d : null;
boolean simple = isSimpleExpr(relDescr);
if (// simple means also relDescr is != null
simple && !ClassObjectType.Map_ObjectType.isAssignableFrom(pattern.getObjectType()) && !ClassObjectType.Match_ObjectType.isAssignableFrom(pattern.getObjectType())) {
String normalizedExpr = normalizeExpression(context, pattern, relDescr, expr);
if (negated) {
normalizedExpr = normalizeNegatedExpr(normalizedExpr, relDescr.getOperator());
relDescr.getOperatorDescr().setNegated(!relDescr.getOperatorDescr().isNegated());
}
return buildRelationalExpression(context, pattern, relDescr, normalizedExpr, aliases);
}
// Either it's a complex expression, so do as predicate
// Or it's a Map and we have to treat it as a special case
String rewrittenExpr = rewriteOrExpressions(context, pattern, d, expr);
if (simple) {
// simple means also relDescr is != null
rewrittenExpr = ConstraintUtil.inverseExpression(relDescr, expr, findLeftExpressionValue(relDescr), findRightExpressionValue(relDescr), relDescr.getOperator(), pattern);
}
if (negated) {
rewrittenExpr = "!(" + rewrittenExpr + ")";
}
return createAndBuildPredicate(context, pattern, d, rewrittenExpr, aliases);
}
use of org.drools.drl.ast.descr.RelationalExprDescr in project drools by kiegroup.
the class DescrDumper method processNullSafeDereferencing.
private String[] processNullSafeDereferencing(String expr, AtomicExprDescr atomicExpr, ConstraintConnectiveDescr ccd, int nullSafePos, int parentIdx, int childIdx) {
// convert "field1!.field2" in ["field1 != null && ", "field1.field2"]
String field1 = expr.substring(0, nullSafePos).trim();
expr = field1 + "." + expr.substring(nullSafePos + 2).trim();
RelationalExprDescr check = new RelationalExprDescr("!=", false, null, new AtomicExprDescr(getPreconditionsToAppend(field1)), new AtomicExprDescr("null"));
if (ccd.getConnective() == ConnectiveType.AND || ccd.getConnective() == ConnectiveType.INC_AND) {
ccd.getDescrs().add(childIdx, check);
} else {
BaseDescr desc = ccd.getDescrs().get(parentIdx);
if (desc instanceof ConstraintConnectiveDescr) {
((ConstraintConnectiveDescr) desc).getDescrs().add(childIdx, check);
} else {
ConstraintConnectiveDescr localAnd = new ConstraintConnectiveDescr(ConnectiveType.AND);
BaseDescr original = ccd.getDescrs().remove(parentIdx);
localAnd.getDescrs().add(check);
localAnd.getDescrs().add(original);
ccd.getDescrs().add(parentIdx, localAnd);
}
}
String innerCheck = check + " && ";
String[] nullCheckAndExpr = new String[] { innerCheck, expr };
atomicExpr.setRewrittenExpression(expr);
return nullCheckAndExpr;
}
use of org.drools.drl.ast.descr.RelationalExprDescr 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;
}
use of org.drools.drl.ast.descr.RelationalExprDescr in project drools by kiegroup.
the class DRL6Expressions method singleRestriction.
// $ANTLR end "andRestriction"
// $ANTLR start "singleRestriction"
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:435:1: singleRestriction returns [BaseDescr result] : (op= operator ( ( squareArguments shiftExpression )=>sa= squareArguments value= shiftExpression |value= shiftExpression ) | LEFT_PAREN or= orRestriction RIGHT_PAREN );
public final BaseDescr singleRestriction() throws RecognitionException {
BaseDescr result = null;
ParserRuleReturnScope op = null;
java.util.List<String> sa = null;
ParserRuleReturnScope value = null;
BaseDescr or = null;
try {
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:436:3: (op= operator ( ( squareArguments shiftExpression )=>sa= squareArguments value= shiftExpression |value= shiftExpression ) | LEFT_PAREN or= orRestriction RIGHT_PAREN )
int alt44 = 2;
int LA44_0 = input.LA(1);
if ((LA44_0 == EQUALS || (LA44_0 >= GREATER && LA44_0 <= GREATER_EQUALS) || (LA44_0 >= LESS && LA44_0 <= LESS_EQUALS) || LA44_0 == NOT_EQUALS || LA44_0 == TILDE)) {
alt44 = 1;
} else if ((LA44_0 == ID) && ((((helper.validateIdentifierKey(DroolsSoftKeywords.NOT))) || ((helper.isPluggableEvaluator(false)))))) {
alt44 = 1;
} else if ((LA44_0 == LEFT_PAREN)) {
alt44 = 2;
} else {
if (state.backtracking > 0) {
state.failed = true;
return result;
}
NoViableAltException nvae = new NoViableAltException("", 44, 0, input);
throw nvae;
}
switch(alt44) {
case 1:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:436:6: op= operator ( ( squareArguments shiftExpression )=>sa= squareArguments value= shiftExpression |value= shiftExpression )
{
pushFollow(FOLLOW_operator_in_singleRestriction2045);
op = operator();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
}
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:438:6: ( ( squareArguments shiftExpression )=>sa= squareArguments value= shiftExpression |value= shiftExpression )
int alt43 = 2;
int LA43_0 = input.LA(1);
if ((LA43_0 == LEFT_SQUARE)) {
int LA43_1 = input.LA(2);
if ((synpred12_DRL6Expressions())) {
alt43 = 1;
} else if ((true)) {
alt43 = 2;
}
} else if ((LA43_0 == BOOL || (LA43_0 >= DECIMAL && LA43_0 <= DIV) || LA43_0 == DOT || LA43_0 == FLOAT || LA43_0 == HEX || (LA43_0 >= ID && LA43_0 <= INCR) || LA43_0 == LEFT_PAREN || LA43_0 == LESS || LA43_0 == MINUS || LA43_0 == NEGATION || LA43_0 == NULL || LA43_0 == PLUS || LA43_0 == QUESTION_DIV || (LA43_0 >= STAR && LA43_0 <= TIME_INTERVAL))) {
alt43 = 2;
} else {
if (state.backtracking > 0) {
state.failed = true;
return result;
}
NoViableAltException nvae = new NoViableAltException("", 43, 0, input);
throw nvae;
}
switch(alt43) {
case 1:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:438:8: ( squareArguments shiftExpression )=>sa= squareArguments value= shiftExpression
{
pushFollow(FOLLOW_squareArguments_in_singleRestriction2074);
sa = squareArguments();
state._fsp--;
if (state.failed)
return result;
pushFollow(FOLLOW_shiftExpression_in_singleRestriction2078);
value = shiftExpression();
state._fsp--;
if (state.failed)
return result;
}
break;
case 2:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:439:10: value= shiftExpression
{
pushFollow(FOLLOW_shiftExpression_in_singleRestriction2091);
value = shiftExpression();
state._fsp--;
if (state.failed)
return result;
}
break;
}
if (state.backtracking == 0) {
if (buildDescr) {
BaseDescr descr = ((value != null ? ((DRL6Expressions.shiftExpression_return) value).result : null) != null && ((!((value != null ? ((DRL6Expressions.shiftExpression_return) value).result : null) instanceof AtomicExprDescr)) || ((value != null ? input.toString(value.start, value.stop) : null).equals(((AtomicExprDescr) (value != null ? ((DRL6Expressions.shiftExpression_return) value).result : null)).getExpression())))) ? (value != null ? ((DRL6Expressions.shiftExpression_return) value).result : null) : new AtomicExprDescr((value != null ? input.toString(value.start, value.stop) : null));
result = new RelationalExprDescr((op != null ? ((DRL6Expressions.operator_return) op).opr : null), (op != null ? ((DRL6Expressions.operator_return) op).negated : false), sa, relationalExpression_stack.peek().lsd, descr);
if (relationalExpression_stack.peek().lsd instanceof BindingDescr) {
relationalExpression_stack.peek().lsd = new AtomicExprDescr(((BindingDescr) relationalExpression_stack.peek().lsd).getExpression());
}
}
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_END);
}
}
break;
case 2:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:454:6: LEFT_PAREN or= orRestriction RIGHT_PAREN
{
match(input, LEFT_PAREN, FOLLOW_LEFT_PAREN_in_singleRestriction2116);
if (state.failed)
return result;
pushFollow(FOLLOW_orRestriction_in_singleRestriction2120);
or = orRestriction();
state._fsp--;
if (state.failed)
return result;
match(input, RIGHT_PAREN, FOLLOW_RIGHT_PAREN_in_singleRestriction2122);
if (state.failed)
return result;
if (state.backtracking == 0) {
result = or;
}
}
break;
}
} catch (RecognitionException re) {
throw re;
} finally {
// do for sure before leaving
}
return result;
}
use of org.drools.drl.ast.descr.RelationalExprDescr in project drools by kiegroup.
the class DRL6Expressions method equalityExpression.
// $ANTLR end "andExpression"
// $ANTLR start "equalityExpression"
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:299:1: equalityExpression returns [BaseDescr result] : left= instanceOfExpression ( (op= EQUALS |op= NOT_EQUALS ) right= instanceOfExpression )* ;
public final BaseDescr equalityExpression() throws RecognitionException {
BaseDescr result = null;
Token op = null;
BaseDescr left = null;
BaseDescr right = null;
try {
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:300:3: (left= instanceOfExpression ( (op= EQUALS |op= NOT_EQUALS ) right= instanceOfExpression )* )
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:300:5: left= instanceOfExpression ( (op= EQUALS |op= NOT_EQUALS ) right= instanceOfExpression )*
{
pushFollow(FOLLOW_instanceOfExpression_in_equalityExpression1477);
left = instanceOfExpression();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
if (buildDescr) {
result = left;
}
}
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:301:3: ( (op= EQUALS |op= NOT_EQUALS ) right= instanceOfExpression )*
loop32: while (true) {
int alt32 = 2;
int LA32_0 = input.LA(1);
if ((LA32_0 == EQUALS || LA32_0 == NOT_EQUALS)) {
alt32 = 1;
}
switch(alt32) {
case 1:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:301:5: (op= EQUALS |op= NOT_EQUALS ) right= instanceOfExpression
{
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:301:5: (op= EQUALS |op= NOT_EQUALS )
int alt31 = 2;
int LA31_0 = input.LA(1);
if ((LA31_0 == EQUALS)) {
alt31 = 1;
} else if ((LA31_0 == NOT_EQUALS)) {
alt31 = 2;
} else {
if (state.backtracking > 0) {
state.failed = true;
return result;
}
NoViableAltException nvae = new NoViableAltException("", 31, 0, input);
throw nvae;
}
switch(alt31) {
case 1:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:301:7: op= EQUALS
{
op = (Token) match(input, EQUALS, FOLLOW_EQUALS_in_equalityExpression1489);
if (state.failed)
return result;
}
break;
case 2:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:301:19: op= NOT_EQUALS
{
op = (Token) match(input, NOT_EQUALS, FOLLOW_NOT_EQUALS_in_equalityExpression1495);
if (state.failed)
return result;
}
break;
}
if (state.backtracking == 0) {
helper.setHasOperator(true);
if (input.LA(1) != DRL6Lexer.EOF)
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
}
pushFollow(FOLLOW_instanceOfExpression_in_equalityExpression1511);
right = instanceOfExpression();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
if (buildDescr) {
result = new RelationalExprDescr((op != null ? op.getText() : null), false, null, left, right);
}
}
}
break;
default:
break loop32;
}
}
}
} catch (RecognitionException re) {
throw re;
} finally {
// do for sure before leaving
}
return result;
}
Aggregations