Best EvoMaster code snippet using org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.CostMatrix.getSubPathCost
Source:CostMatrix.java
...26 if (col == otherCol) {27 continue;28 }29 double otherCost = matrix[FIRST_ROW][otherCol];30 min = Math.min(min, getSubPathCost(otherCost, Math.ceil(t.cost)));31 }32 matrix[FIRST_ROW][col] = min;33 }34 //then look at the other rows35 for (int i = 1; i < ROWS; i++) {36 for (int col = 0; col < COLUMNS; col++) {37 matrix[i][col] = Double.MAX_VALUE;38 for (GraphTransition t : graph.getIncomingTransitions(i, col)) {39 int otherCol = graph.getColumn(t.fromState);40 int otherRow = t.fromRow;41 if (!t.type.equals(GraphTransition.TransitionType.PHANTOM)) {42 matrix[i][col] = Math.min(matrix[i][col], getSubPathCost(matrix[otherRow][otherCol], Math.ceil(t.cost)));43 } else {44 /*45 * artificial transition to final/sink state, so just take same values as previous state46 */47 matrix[i][col] = Math.min(matrix[i][col], matrix[otherRow][otherCol]);48 }49 }50 }51 }52 double min = matrix[ROWS - 1][COLUMNS - 1];53 return (int) Math.round(min);54 }55 /**56 * Note: this is different from normal matching algorithms, as we enforce an order57 * among the operators: delete, replace and then insert.58 * @param graph59 * @return60 */61 public static double calculateCostForStringAVM(RegexGraph graph) {62 final int ROWS = graph.getNumberOfRows();63 final int COLUMNS = graph.getNumberOfColumns();64 /*65 * we create a matrix based on each row and each column in the graph.66 * Each cell has 3 values, each representing the cost of thre different types of path:67 *68 * 0) only deletion69 * 1) deletions followed by replacement70 * 2) as above, and then followed by insertions71 */72 final double[][][] matrix = new double[ROWS][COLUMNS][3];73 calculateInsertionCostOnFirstRow(graph, matrix);74 for (int i = 1; i < ROWS; i++) {75 for (int col = 0; col < COLUMNS; col++) {76 /*77 * unless a path is explicitly updated, it will have maximum distance by default78 */79 matrix[i][col][DEL] = Double.MAX_VALUE;80 matrix[i][col][REP] = Double.MAX_VALUE;81 matrix[i][col][INS] = Double.MAX_VALUE;82 for (GraphTransition t : graph.getIncomingTransitions(i, col)) {83 int otherCol = graph.getColumn(t.fromState);84 int otherRow = t.fromRow;85 if (t.type.equals(GraphTransition.TransitionType.INSERTION)) {86 assert otherRow == i;87 /*88 * if we have an insertion, only the insertion path can be continued.89 * that's the reason why on the left side we only update for [INS].90 * An insertion can continue any type of path (and so all types are present on the right side).91 */92 matrix[i][col][INS] = Math.min(matrix[i][col][INS], getSubPathCost(matrix[otherRow][otherCol][DEL], t.cost));93 matrix[i][col][INS] = Math.min(matrix[i][col][INS], getSubPathCost(matrix[otherRow][otherCol][REP], t.cost));94 matrix[i][col][INS] = Math.min(matrix[i][col][INS], getSubPathCost(matrix[otherRow][otherCol][INS], t.cost));95 } else if (t.type.equals(GraphTransition.TransitionType.REPLACEMENT)) {96 /*97 * if we have a replacement, then we cannot continue a delete path.98 * So, no [DEL] on the left side.99 * A replacement can continue a delete or replace path, but not an insertion one (and so [DEL] and100 * [REP] on right side)101 */102 matrix[i][col][REP] = Math.min(matrix[i][col][REP], getSubPathCost(matrix[otherRow][otherCol][DEL], t.cost));103 matrix[i][col][REP] = Math.min(matrix[i][col][REP], getSubPathCost(matrix[otherRow][otherCol][REP], t.cost));104 /*105 * from this state on, an insertion path can be followed, with same cost (ie right side) as replacement path106 */107 matrix[i][col][INS] = Math.min(matrix[i][col][INS], getSubPathCost(matrix[otherRow][otherCol][DEL], t.cost));108 matrix[i][col][INS] = Math.min(matrix[i][col][INS], getSubPathCost(matrix[otherRow][otherCol][REP], t.cost));109 } else if (t.type.equals(GraphTransition.TransitionType.DELETION)) {110 /*111 * deletion can only follow a deletion path (so only [DEL] or right side).112 * but, from this state on, any new path can be followed (so all on left side)113 */114 matrix[i][col][DEL] = Math.min(matrix[i][col][DEL], getSubPathCost(matrix[otherRow][otherCol][DEL], t.cost));115 matrix[i][col][REP] = Math.min(matrix[i][col][REP], getSubPathCost(matrix[otherRow][otherCol][DEL], t.cost));116 matrix[i][col][INS] = Math.min(matrix[i][col][INS], getSubPathCost(matrix[otherRow][otherCol][DEL], t.cost));117 } else if (t.type.equals(GraphTransition.TransitionType.PHANTOM)) {118 assert t.cost == 0;119 /*120 * artificial transition to final/sink state, so just take same values as previous state121 */122 matrix[i][col][DEL] = Math.min(matrix[i][col][DEL], matrix[otherRow][otherCol][DEL]);123 matrix[i][col][REP] = Math.min(matrix[i][col][REP], matrix[otherRow][otherCol][REP]);124 matrix[i][col][INS] = Math.min(matrix[i][col][INS], matrix[otherRow][otherCol][INS]);125 }126 }127 }128 /*129 * TODO: The algorithm of Myers's paper, at page 12, makes a distinction between D and E transitions.130 * Insertions of type E are done last. Not fully clear if it has an effect here: ie, recall that131 * here we do minimization (calculate distance) and not maximization (similarity)132 */133 }134 /*135 * get the minimum among the 3 different paths in the sink state136 */137 double min = Double.MAX_VALUE;138 for (double value : matrix[ROWS - 1][COLUMNS - 1]) {139 if (value < min) {140 min = value;141 }142 }143 return min;144 }145 /**146 * We cannot just do previousStateCost + transitionCost, as there might be computational overflows147 *148 * @param previousStateCost149 * @param transitionCost150 * @return151 * @throws IllegalArgumentException152 */153 private static double getSubPathCost(double previousStateCost, double transitionCost) throws IllegalArgumentException {154 if (previousStateCost < 0) {155 throw new IllegalArgumentException("previousStateCost cannot be negative: " + previousStateCost);156 }157 if (transitionCost < 0) {158 throw new IllegalArgumentException("transitionCost cannot be negative: " + transitionCost);159 }160 if (previousStateCost == Double.MAX_VALUE || transitionCost == Double.MAX_VALUE) {161 return Double.MAX_VALUE;162 }163 double sum = previousStateCost + transitionCost;164 if (sum < previousStateCost || sum < transitionCost) {165 /*166 * likely overflow167 */168 return Double.MAX_VALUE;169 }170 return sum;171 }172 /**173 * First row is special, ie very different from the others174 *175 * @param graph176 * @param matrix177 */178 private static void calculateInsertionCostOnFirstRow(RegexGraph graph, final double[][][] matrix) {179 // First row is cost of matching empty sequence on regex180 final int FIRST_ROW = 0;181 /*182 * init first starting state with 0 costs183 */184 matrix[FIRST_ROW][0][0] = 0;185 matrix[FIRST_ROW][0][1] = 0;186 matrix[FIRST_ROW][0][2] = 0;187 for (int col = 1; col < graph.getNumberOfColumns(); col++) {188 double min = Double.MAX_VALUE;189 for (GraphTransition t : graph.getIncomingTransitions(FIRST_ROW, col)) {190 /*191 * on first row, there can be only insertions coming from the same row,192 * apart from last node that can have a phantom transition to sink state193 */194 assert t.type.equals(GraphTransition.TransitionType.INSERTION) ||195 t.type.equals(GraphTransition.TransitionType.PHANTOM);196 assert t.fromRow == 0;197 int otherCol = graph.getColumn(t.fromState);198 //self transition199 if (col == otherCol) {200 continue;201 }202 double otherCost = matrix[FIRST_ROW][otherCol][2];203 min = Math.min(min, getSubPathCost(otherCost, t.cost));204 }205 /*206 * as there can be only insertions, the delete and replace paths cannot be followed, and207 * so maximum distance208 */209 matrix[FIRST_ROW][col][0] = Double.MAX_VALUE;210 matrix[FIRST_ROW][col][1] = Double.MAX_VALUE;211 matrix[FIRST_ROW][col][2] = min;212 }213 }214}...
getSubPathCost
Using AI Code Generation
1package org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex;2import org.evomaster.client.java.instrumentation.coverage.methodreplacement.DistanceHelper;3import org.evomaster.client.java.instrumentation.coverage.methodreplacement.DistanceHelperKt;4import org.evomaster.client.java.instrumentation.shared.Replacement;5import java.util.Arrays;6import java.util.List;7public class CostMatrixReplacement implements Replacement {8 public List<String> getTargetClassNames() {9 return Arrays.asList("org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.CostMatrix");10 }11 public String getTargetMethodDescription() {12 return "int getSubPathCost(int, int, int, int)";13 }14 public Object replaceCall(String className, String methodName, Object[] parameters) {15 int row = (int) parameters[0];16 int col = (int) parameters[1];17 int rowSize = (int) parameters[2];18 int colSize = (int) parameters[3];19 if (row < 0 || col < 0 || row >= rowSize || col >= colSize) {20 throw new IllegalArgumentException("Invalid row or column");21 }22 int cost = DistanceHelperKt.getCostMatrix()[row][col];23 return cost;24 }25}26package org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex;27import org.evomaster.client.java.instrumentation.coverage.methodreplacement.DistanceHelper;28import org.evomaster.client.java.instrumentation.coverage.methodreplacement.DistanceHelperKt;29import org.evomaster.client.java.instrumentation.shared.Replacement;30import java.util.Arrays;31import java.util.List;32public class CostMatrixReplacement implements Replacement {33 public List<String> getTargetClassNames() {34 return Arrays.asList("org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.CostMatrix");35 }36 public String getTargetMethodDescription() {37 return "int getSubPathCost(int, int, int, int)";38 }39 public Object replaceCall(String className, String methodName, Object[] parameters) {40 int row = (int) parameters[0];41 int col = (int) parameters[1];
getSubPathCost
Using AI Code Generation
1import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.CostMatrix;2import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.RegexCoverageInfo;3import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.RegexHandler;4import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.RegexSpecialization;5public class ExampleRegex {6 public static void main(String[] args) {7 String regex = "a{2,3}b{2,3}";8 String input = "aaabbb";9 String input2 = "aaabbbb";10 RegexSpecialization specialization = new RegexSpecialization(regex);11 RegexHandler handler = new RegexHandler(specialization);12 RegexCoverageInfo regexCoverageInfo = new RegexCoverageInfo();13 CostMatrix costMatrix = new CostMatrix();14 int cost = costMatrix.getSubPathCost(input, handler, regexCoverageInfo);15 int cost2 = costMatrix.getSubPathCost(input2, handler, regexCoverageInfo);16 System.out.println("Cost: " + cost);17 System.out.println("Cost2: " + cost2);18 }19}
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!!