How to use computeExpression method of org.evomaster.client.java.controller.internal.db.HeuristicsCalculator class

Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.HeuristicsCalculator.computeExpression

Source:HeuristicsCalculator.java Github

copy

Full Screen

...36 SqlNameContext context = new SqlNameContext(stmt);37 HeuristicsCalculator calculator = new HeuristicsCalculator(context);38 double min = Double.MAX_VALUE;39 for (DataRow row : data.seeRows()) {40 double dist = calculator.computeExpression(where, row);41 if (dist == 0) {42 return 0;43 }44 if (dist < min) {45 min = dist;46 }47 }48 return min;49 }50 /**51 * Compute a "branch" distance heuristics.52 *53 * @param exp the WHERE clause which we want to resolve as true54 * @param data current data raw in the database, based on the columns/tables involved in the WHERE55 * @return a branch distance, where 0 means that the data would make the WHERE resolves to true56 */57 public double computeExpression(Expression exp, DataRow data) {58 //TODO all cases59 //------ net.sf.jsqlparser.expression.operators.* ---------60 if (exp instanceof Parenthesis) {61 return computeExpression(((Parenthesis) exp).getExpression(), data);62 }63 //------ net.sf.jsqlparser.expression.operators.conditional.* ---------64 if (exp instanceof AndExpression) {65 return computeAnd((AndExpression) exp, data);66 }67 if (exp instanceof OrExpression) {68 return computeOr((OrExpression) exp, data);69 }70 //------ net.sf.jsqlparser.expression.operators.relational.* ---------71 if(exp instanceof Between){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){...

Full Screen

Full Screen

computeExpression

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.internal.db.HeuristicsCalculator;2import org.evomaster.client.java.controller.api.dto.database.schema.DbSchemaDto;3import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto;4import org.evomaster.client.java.controller.api.dto.database.operations.InsertionDto;5import org.evomaster.client.java.controller.api.dto.database.operations.SqlScriptDto;6import org.evomaster.client.java.controller.api.dto.database.operations.UpdateDto;7import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;8import org.evomaster.client.java.controller.api.dto.database.schema.ColumnDto;9import org.evomaster.client.java.controller.api.dto.database.schema.ForeignKeyDto;10import org.evomaster.client.java.controller.api.dto.database.schema.ForeignKeyAction;11import org.evomaster.client.java.controller.api.dto.database.schema.DataType;12import java.util.List;13import java.util.ArrayList;14import java.util.Arrays;15public class ComputeExpressionExample {16 public static void main(String[] args){17 DbSchemaDto schema = new DbSchemaDto();18 TableDto table = new TableDto();19 table.setName("user");20 ColumnDto column = new ColumnDto();21 column.setName("id");22 column.setDataType(DataType.INTEGER);23 table.addColumn(column);24 ColumnDto column2 = new ColumnDto();25 column2.setName("name");26 column2.setDataType(DataType.VARCHAR);27 table.addColumn(column2);28 ColumnDto column3 = new ColumnDto();29 column3.setName("email");30 column3.setDataType(DataType.VARCHAR);31 table.addColumn(column3);32 ColumnDto column4 = new ColumnDto();33 column4.setName("age");34 column4.setDataType(DataType.INTEGER);35 table.addColumn(column4);36 ColumnDto column5 = new ColumnDto();37 column5.setName("country

Full Screen

Full Screen

computeExpression

Using AI Code Generation

copy

Full Screen

1List<String> sqlStatements = getSqlStatements();2for(String sql : sqlStatements){3 double computedValue = HeuristicsCalculator.computeExpression(sql);4 System.out.println(sql + " " + computedValue);5}6private List<String> getSqlStatements(){7 List<String> sqlStatements = new ArrayList<>();8 for(TestSuiteChromosome testSuite : bestTestSuites){9 for(TestCaseChromosome testCase : testSuite.getTestCases()){10 for(StatementChromosome statement : testCase.getTestCase().getStatements()){11 if(statement.getStatement() instanceof SqlStatement){12 SqlStatement sqlStatement = (SqlStatement) statement.getStatement();13 sqlStatements.add(sqlStatement.getSql());14 }15 }16 }17 }18 return sqlStatements;19}20List<String> sqlStatements = getSqlStatements();21try {22 File file = new File("SQL_Statements.txt");23 if (!file.exists()) {24 file.createNewFile();25 }26 FileWriter fw = new FileWriter(file.getAbsoluteFile());27 BufferedWriter bw = new BufferedWriter(fw);28 for(String sql : sqlStatements){29 double computedValue = HeuristicsCalculator.computeExpression(sql);30 bw.write(sql + " " + computedValue);31 bw.newLine();32 }33 bw.close();34} catch (IOException e) {35 e.printStackTrace();36}37List<String> sqlStatements = getSqlStatements();38for(String sql : sqlStatements){39 double computedValue = HeuristicsCalculator.computeExpression(sql);40 System.out.println(sql + " " + computedValue);41}42public class HeuristicsCalculator {

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful