@@ -27,7 +27,7 @@ public static Map<String, Expression> resolve(Expression exp) {
2727 resolveRecursive (exp , map );
2828
2929 // remove variables that were not used in the expression
30- map .entrySet ().removeIf (entry -> !hasUsage (exp , entry .getKey (), entry .getValue ()));
30+ map .entrySet ().removeIf (entry -> !hasUsage (exp , entry .getKey (), entry .getValue (), true ));
3131
3232 // transitively resolve variables
3333 return resolveTransitive (map );
@@ -136,43 +136,35 @@ private static Expression lookup(Expression exp, Map<String, Expression> map, Se
136136 *
137137 * @param exp
138138 * @param name
139+ * @param value
140+ * @param topLevel
139141 *
140142 * @return true if used, false otherwise
141143 */
142- private static boolean hasUsage (Expression exp , String name , Expression value ) {
143- // exclude own definitions
144- if (exp instanceof BinaryExpression binary && "==" .equals (binary .getOperator ())) {
144+ private static boolean hasUsage (Expression exp , String name , Expression value , boolean topLevel ) {
145+ if (topLevel && exp instanceof BinaryExpression binary && "&&" .equals (binary .getOperator ())) {
146+ return hasUsage (binary .getFirstOperand (), name , value , true )
147+ || hasUsage (binary .getSecondOperand (), name , value , true );
148+ }
149+
150+ if (topLevel && exp instanceof BinaryExpression binary && "==" .equals (binary .getOperator ())) {
145151 Expression left = binary .getFirstOperand ();
146152 Expression right = binary .getSecondOperand ();
147- if (left instanceof Var v && v .getName ().equals (name ) && right .equals (value )
148- && (isConstant (right ) || (!(right instanceof Var ) && canSubstitute (v , right ))))
149- return false ;
150- if (left instanceof FunctionInvocation && left .toString ().equals (name ) && right .equals (value )
151- && (isConstant (right ) || (!(right instanceof Var ) && !containsExpression (right , left ))))
152- return false ;
153- if (right instanceof Var v && v .getName ().equals (name ) && left .equals (value ) && isConstant (left ))
154- return false ;
155- if (right instanceof FunctionInvocation && right .toString ().equals (name ) && left .equals (value )
156- && isConstant (left ))
153+ boolean leftDefinition = name .equals (substitutionKey (left )) && right .equals (value )
154+ && (isConstant (right )
155+ || (left instanceof Var v && !(right instanceof Var ) && canSubstitute (v , right ))
156+ || (left instanceof FunctionInvocation && !(right instanceof Var )
157+ && !containsExpression (right , left )));
158+ boolean rightDefinition = name .equals (substitutionKey (right )) && left .equals (value ) && isConstant (left );
159+ if (leftDefinition || rightDefinition )
157160 return false ;
158161 }
159162
160- // usage found
161- if (exp instanceof Var var && var .getName ().equals (name )) {
162- return true ;
163- }
164- if (exp instanceof FunctionInvocation && exp .toString ().equals (name )) {
163+ if (name .equals (substitutionKey (exp )))
165164 return true ;
166- }
167-
168- // recurse children
169- if (exp .hasChildren ()) {
170- for (Expression child : exp .getChildren ())
171- if (hasUsage (child , name , value ))
172- return true ;
173- }
174-
175- // usage not found
165+ for (Expression child : exp .getChildren ())
166+ if (hasUsage (child , name , value , false ))
167+ return true ;
176168 return false ;
177169 }
178170
0 commit comments