Skip to content

Commit

Permalink
[BZ-1276311] improve PatternBuilder performances
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Oct 29, 2015
1 parent 2292d17 commit 0b93503
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 118 deletions.
Expand Up @@ -22,37 +22,36 @@


public interface ExpressionRewriter { public interface ExpressionRewriter {


public String dump( BaseDescr base ); String dump( BaseDescr base );


public String dump( BaseDescr base, String dump( BaseDescr base,
MVELDumper.MVELDumperContext context );

String dump( BaseDescr base,
ConstraintConnectiveDescr parent,
MVELDumper.MVELDumperContext context );

String dump( BaseDescr base,
int parentPrecedence );

StringBuilder dump( StringBuilder sbuilder,
BaseDescr base,
int parentPriority,
boolean isInsideRelCons,
MVELDumper.MVELDumperContext context ); MVELDumper.MVELDumperContext context );


public String dump( BaseDescr base, StringBuilder dump( StringBuilder sbuilder,
BaseDescr base,
ConstraintConnectiveDescr parent, ConstraintConnectiveDescr parent,
int parentIndex,
int parentPriority,
boolean isInsideRelCons,
MVELDumper.MVELDumperContext context ); MVELDumper.MVELDumperContext context );


public String dump( BaseDescr base, String processRestriction( MVELDumper.MVELDumperContext context,
int parentPrecedence ); String left,

OperatorDescr operator,
public StringBuilder dump( StringBuilder sbuilder, String right );
BaseDescr base,
int parentPriority,
boolean isInsideRelCons,
MVELDumper.MVELDumperContext context );

public StringBuilder dump( StringBuilder sbuilder,
BaseDescr base,
ConstraintConnectiveDescr parent,
int parentIndex,
int parentPriority,
boolean isInsideRelCons,
MVELDumper.MVELDumperContext context );

public void processRestriction( MVELDumper.MVELDumperContext context,
StringBuilder sbuilder,
String left,
OperatorDescr operator,
String right );


public Class<?> getEvaluatorWrapperClass(); public Class<?> getEvaluatorWrapperClass();
} }
Expand Up @@ -151,7 +151,11 @@ private String[] processAtomicExpression( StringBuilder sbuilder, MVELDumperCont
String[] constrAndExpr = processImplicitConstraints( expr, atomicExpr, parent, parentIdx, context ); String[] constrAndExpr = processImplicitConstraints( expr, atomicExpr, parent, parentIdx, context );
// top-level, implicit constraints will be processed in different nodes. // top-level, implicit constraints will be processed in different nodes.
// Nested CCDs require all constraints to be evaluated locally, as a complex constraints // Nested CCDs require all constraints to be evaluated locally, as a complex constraints
sbuilder.append( context.isCcdNested() ? constrAndExpr[ 0 ] + constrAndExpr[ 1 ] : constrAndExpr[ 1 ] ); expr = context.isCcdNested() ? constrAndExpr[ 0 ] + constrAndExpr[ 1 ] : constrAndExpr[ 1 ];
if (!atomicExpr.hasRewrittenExpression()) {
atomicExpr.setRewrittenExpression( expr );
}
sbuilder.append( expr );
return constrAndExpr; return constrAndExpr;
} }


Expand Down Expand Up @@ -182,11 +186,12 @@ private void processRelationalExpression(StringBuilder sbuilder, RelationalExprD
processRightAtomicExpr(left, (AtomicExprDescr)red.getRight(), parent, idx, context) : processRightAtomicExpr(left, (AtomicExprDescr)red.getRight(), parent, idx, context) :
dump( new StringBuilder(), red.getRight(), parent, idx, Integer.MAX_VALUE, true, context).toString(); dump( new StringBuilder(), red.getRight(), parent, idx, Integer.MAX_VALUE, true, context).toString();


processRestriction( context, String expr = processRestriction( context,
sbuilder, left.toString(),
left.toString(), red.getOperatorDescr(),
red.getOperatorDescr(), right );// maximum precedence, so wrap any child connective in parenthesis
right );// maximum precedence, so wrap any child connective in parenthesis red.setExpression( expr );
sbuilder.append( expr );
} }


private String processRightAtomicExpr( StringBuilder left, AtomicExprDescr atomicExpr, ConstraintConnectiveDescr parent, int parentIdx, MVELDumperContext context ) { private String processRightAtomicExpr( StringBuilder left, AtomicExprDescr atomicExpr, ConstraintConnectiveDescr parent, int parentIdx, MVELDumperContext context ) {
Expand Down Expand Up @@ -445,11 +450,11 @@ protected void processConnectiveDescr( StringBuilder sbuilder,


} }


public void processRestriction( MVELDumperContext context, public String processRestriction( MVELDumperContext context,
StringBuilder sbuilder, String left,
String left, OperatorDescr operator,
OperatorDescr operator, String right ) {
String right ) { StringBuilder sbuilder = new StringBuilder();
Operator op = Operator.determineOperator( operator.getOperator(), Operator op = Operator.determineOperator( operator.getOperator(),
operator.isNegated() ); operator.isNegated() );
if ( op == Operator.determineOperator( "memberOf", if ( op == Operator.determineOperator( "memberOf",
Expand Down Expand Up @@ -494,6 +499,7 @@ public void processRestriction( MVELDumperContext context,
// rewrite operator as a function call // rewrite operator as a function call
rewriteOperator( context, sbuilder, left, operator, right ); rewriteOperator( context, sbuilder, left, operator, right );
} }
return sbuilder.toString();
} }


protected void rewriteBasicOperator( StringBuilder sbuilder, protected void rewriteBasicOperator( StringBuilder sbuilder,
Expand Down
Expand Up @@ -59,6 +59,10 @@ public String getRewrittenExpression() {
return rewrittenExpression != null ? rewrittenExpression : expression; return rewrittenExpression != null ? rewrittenExpression : expression;
} }


public boolean hasRewrittenExpression() {
return rewrittenExpression != null;
}

public void setRewrittenExpression(String rewrittenExpression) { public void setRewrittenExpression(String rewrittenExpression) {
this.rewrittenExpression = rewrittenExpression; this.rewrittenExpression = rewrittenExpression;
} }
Expand Down
Expand Up @@ -28,6 +28,8 @@ public class RelationalExprDescr extends BaseDescr {
private BaseDescr right; private BaseDescr right;
private OperatorDescr operator; private OperatorDescr operator;


private String expression;

public RelationalExprDescr() { } public RelationalExprDescr() { }


public RelationalExprDescr(String operator, public RelationalExprDescr(String operator,
Expand Down Expand Up @@ -58,6 +60,14 @@ public void setRight( BaseDescr right ) {
this.right = right; this.right = right;
} }


public String getExpression() {
return expression;
}

public void setExpression( String expression ) {
this.expression = expression;
}

public String getOperator() { public String getOperator() {
return operator != null ? operator.getOperator() : null; return operator != null ? operator.getOperator() : null;
} }
Expand Down
Expand Up @@ -203,23 +203,32 @@ protected static String normalizeMVELLiteralExpression(ValueType vtype,
String rightValue, String rightValue,
LiteralRestrictionDescr restrictionDescr) { LiteralRestrictionDescr restrictionDescr) {
if (vtype == ValueType.DATE_TYPE) { if (vtype == ValueType.DATE_TYPE) {
Date date = (Date)field.getValue(); return normalizeDate( field, leftValue, operator );
return leftValue + " " + operator + (date != null ? " new java.util.Date(" + date.getTime() + ")" : " null");
} }
if (operator.equals("str")) { if (operator.equals("str")) {
String method = restrictionDescr.getParameterText(); return normalizeStringOperator( leftValue, rightValue, restrictionDescr );
if (method.equals("length")) {
return leftValue + ".length()" + (restrictionDescr.isNegated() ? " != " : " == ") + rightValue;
}
return (restrictionDescr.isNegated() ? "!" : "") + leftValue + "." + method + "(" + rightValue + ")";
} }

// resolve ambiguity between mvel's "empty" keyword and constraints like: List(empty == ...) // resolve ambiguity between mvel's "empty" keyword and constraints like: List(empty == ...)
if (expr.startsWith("empty") && (operator.equals("==") || operator.equals("!=")) && !Character.isJavaIdentifierPart(expr.charAt(5))) { return normalizeEmptyKeyword( expr, operator );
expr = "isEmpty()" + expr.substring(5); }

protected static String normalizeDate( FieldValue field, String leftValue, String operator ) {
Date date = (Date)field.getValue();
return leftValue + " " + operator + (date != null ? " new java.util.Date(" + date.getTime() + ")" : " null");
}

protected static String normalizeStringOperator( String leftValue, String rightValue, LiteralRestrictionDescr restrictionDescr ) {
String method = restrictionDescr.getParameterText();
if (method.equals("length")) {
return leftValue + ".length()" + (restrictionDescr.isNegated() ? " != " : " == ") + rightValue;
} }
return (restrictionDescr.isNegated() ? "!" : "") + leftValue + "." + method + "(" + rightValue + ")";
}


return expr; protected static String normalizeEmptyKeyword( String expr, String operator ) {
return expr.startsWith("empty") && (operator.equals("==") || operator.equals("!=")) && !Character.isJavaIdentifierPart(expr.charAt(5)) ?
"isEmpty()" + expr.substring(5) :
expr;
} }


protected static String normalizeMVELVariableExpression(String expr, protected static String normalizeMVELVariableExpression(String expr,
Expand Down

0 comments on commit 0b93503

Please sign in to comment.