Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.HeuristicsCalculator.computeComparison
Source:HeuristicsCalculator.java
...72 return computeBetween((Between)exp, data);73 }74 if (exp instanceof ComparisonOperator) {75 // this deals with 6 subclasses:76 return computeComparisonOperator((ComparisonOperator) exp, data);77 }78 if(exp instanceof ExistsExpression){79 //TODO80 }81 if(exp instanceof ExpressionList){82 //TODO83 }84 if (exp instanceof InExpression) {85 return computeInExpression((InExpression) exp, data);86 }87 if (exp instanceof IsNullExpression) {88 return computeIsNull((IsNullExpression) exp, data);89 }90 if(exp instanceof JsonOperator){91 //TODO92 }93 if(exp instanceof LikeExpression){94 //TODO95 }96 if(exp instanceof Matches){97 //TODO98 }99 if(exp instanceof MultiExpressionList){100 //TODO101 }102 if(exp instanceof NamedExpressionList){103 //TODO104 }105 if(exp instanceof RegExpMatchOperator){106 //TODO107 }108 return cannotHandle(exp);109 }110 private double computeBetween(Between between, DataRow data) {111 Instant start = getAsInstant(getValue(between.getBetweenExpressionStart(), data));112 Instant end = getAsInstant(getValue(between.getBetweenExpressionEnd(), data));113 Instant x = getAsInstant(getValue(between.getLeftExpression(), data));114 double after = computeComparison(x, start, new GreaterThanEquals());115 double before = computeComparison(x, end, new MinorThanEquals());116 return addDistances(after, before);117 }118 private double computeInExpression(InExpression exp, DataRow data) {119 //TODO can left be a list???120 ItemsList itemsList = exp.getRightItemsList();121 if (itemsList instanceof ExpressionList) {122 ExpressionList list = (ExpressionList) itemsList;123 if (exp.isNot()) {124 double max = 0;125 for (Expression element : list.getExpressions()) {126 ComparisonOperator op = new NotEqualsTo();127 op.setLeftExpression(exp.getLeftExpression());128 op.setRightExpression(element);129 double dist = computeComparisonOperator(op, data);130 if (dist > max) {131 max = dist;132 break; // no need to look at others, as no gradient133 }134 }135 return max;136 } else {137 double min = Double.MAX_VALUE;138 for (Expression element : list.getExpressions()) {139 ComparisonOperator op = new EqualsTo();140 op.setLeftExpression(exp.getLeftExpression());141 op.setRightExpression(element);142 double dist = computeComparisonOperator(op, data);143 if (dist < min) {144 min = dist;145 }146 }147 return min;148 }149 } else {150 return cannotHandle(exp);151 }152 }153 private double computeIsNull(IsNullExpression exp, DataRow data) {154 Object x = getValue(exp.getLeftExpression(), data);155 if (x == null && !exp.isNot()) {156 return 0d;157 }158 if (x != null && exp.isNot()) {159 return 0d;160 }161 return 1;162 }163 private double cannotHandle(Expression exp) {164 SimpleLogger.uniqueWarn("WARNING, cannot handle SQL expression type '" + exp.getClass().getSimpleName() +165 "' with value: " + exp.toString());166 return Double.MAX_VALUE;167 }168 private double computeAnd(AndExpression exp, DataRow data) {169 double a = computeExpression(exp.getLeftExpression(), data);170 double b = computeExpression(exp.getRightExpression(), data);171 return addDistances(a, b);172 }173 private double addDistances(double a, double b) {174 double sum = a + b;175 if (sum < Math.max(a, b)) {176 //overflow177 return Double.MAX_VALUE;178 } else {179 return sum;180 }181 }182 private double computeOr(OrExpression exp, DataRow data) {183 double a = computeExpression(exp.getLeftExpression(), data);184 double b = computeExpression(exp.getRightExpression(), data);185 return Math.min(a, b);186 }187 private Instant getAsInstant(Object obj){188 if(obj == null){189 /*190 TODO this shouldn't really happen if we have full SQL support, like sub-selects191 */192 return null;193 }194 if(obj instanceof Timestamp){195 Timestamp timestamp = (Timestamp) obj;196 return timestamp.toInstant();197 }198 if(obj instanceof String){199 try {200 return ZonedDateTime.parse(obj.toString()).toInstant();201 } catch (DateTimeParseException e){202 /*203 maybe it is in some weird format like 28-Feb-17...204 this shouldn't really happen, but looks like Hibernate generate SQL from205 JPQL with Date handled like this :(206 */207 DateTimeFormatter df = new DateTimeFormatterBuilder()208 // case insensitive to parse JAN and FEB209 .parseCaseInsensitive()210 // add pattern211 .appendPattern("dd-MMM-yy")212 // create formatter (use English Locale to parse month names)213 .toFormatter(Locale.ENGLISH);214 return LocalDate.parse(obj.toString(), df)215 .atStartOfDay().toInstant(ZoneOffset.UTC);216 }217 }218 SimpleLogger.warn("Cannot handle time value for class: " + obj.getClass());219 return null;220 }221 private double computeComparisonOperator(ComparisonOperator exp, DataRow data) {222 Object left = getValue(exp.getLeftExpression(), data);223 Object right = getValue(exp.getRightExpression(), data);224 if(left instanceof Timestamp || right instanceof Timestamp){225 Instant a = getAsInstant(left);226 Instant b = getAsInstant(right);227 if(a==null || b==null){228 return cannotHandle(exp);229 }230 return computeComparison(a, b, exp);231 }232 if (left instanceof Number && right instanceof Number) {233 double x = ((Number) left).doubleValue();234 double y = ((Number) right).doubleValue();235 return computerComparison(x, y, exp);236 }237 if (left instanceof String && right instanceof String) {238 return computeComparison(left.toString(), right.toString(), exp);239 }240 if (left instanceof Boolean && right instanceof Boolean) {241 return computeBooleanComparison((Boolean) left, (Boolean) right, exp);242 }243 if (left == null || right == null) {244 return computeNullComparison(left, right, exp);245 }246 return cannotHandle(exp);247 }248 private double computeComparison(Instant a, Instant b, ComparisonOperator exp) {249 if(a==null || b==null){250 return Double.MAX_VALUE;251 }252 double dif = - Duration.between(a,b).toMillis();253 return computerComparison(dif, exp);254 }255 private double computeBooleanComparison(boolean x, boolean y, ComparisonOperator exp) {256 if (!checkEqualOrNotOperator(exp)) {257 return cannotHandle(exp);258 }259 if (exp instanceof EqualsTo && x == y) {260 return 0d;261 }262 if (exp instanceof NotEqualsTo && x != y) {263 return 0d;264 }265 return 1d;266 }267 private boolean checkEqualOrNotOperator(ComparisonOperator exp) {268 return (exp instanceof EqualsTo) || (exp instanceof NotEqualsTo);269 }270 private double computeNullComparison(Object x, Object y, ComparisonOperator exp) {271 assert x == null || y == null;272 if (!checkEqualOrNotOperator(exp)) {273 return cannotHandle(exp);274 }275 if (exp instanceof EqualsTo && x == y) {276 return 0d;277 }278 if (exp instanceof NotEqualsTo && x != y) {279 return 0d;280 }281 return Double.MAX_VALUE;282 }283 private double computerComparison(double dif, ComparisonOperator exp) {284 if (exp instanceof EqualsTo) {285 return Math.abs(dif);286 } else if (exp instanceof GreaterThanEquals) {287 return dif >= 0 ? 0d : -dif;288 } else if (exp instanceof GreaterThan) {289 return dif > 0 ? 0d : 1d - dif;290 } else if (exp instanceof MinorThanEquals) {291 return dif <=0 ? 0d : dif;292 } else if (exp instanceof MinorThan) {293 return dif < 0 ? 0d : 1d + dif;294 } else if (exp instanceof NotEqualsTo) {295 return dif != 0 ? 0d : 1d;296 } else {297 return cannotHandle(exp);298 }299 }300 private double computerComparison(double x, double y, ComparisonOperator exp) {301 double dif = x - y;302 return computerComparison(dif, exp);303 }304 private double computeComparison(String a, String b, ComparisonOperator exp) {305 if (exp instanceof EqualsTo) {306 return DistanceHelper.getLeftAlignmentDistance(a, b);307 } else if (exp instanceof NotEqualsTo) {308 if (a.equals(b)) {309 return Double.MAX_VALUE;310 } else {311 return 0d;312 }313 } else {314 return cannotHandle(exp);315 }316 }317 private Object getValue(Expression exp, DataRow data) {318 //TODO all cases...
computeComparison
Using AI Code Generation
1import org.evomaster.client.java.controller.internal.db.HeuristicsCalculator2import org.evomaster.client.java.controller.internal.db.HeuristicsCalculator3let similarity: double = HeuristicsCalculator.computeComparison(string1, string2)4if(result){5} else {6}7import the class that contains the method to use8import the class that contains the method to use
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!