Best EvoMaster code snippet using org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.RegexGraph.cacheRegex
Source:RegexGraph.java
...67 if (!regexAutomatonCache.containsKey(regex)) {68 /*69 * Create an automaton representing the regex70 */71 cacheRegex(regex);72 }73 Automaton automaton = regexAutomatonCache.get(regex);74 return automaton;75 }76 private Map<Integer, Map<State, Set<GraphTransition>>> createGraph(String arg, String regex) {77 /*78 * Create a graph to calculate the distance. The algorithm is based on what discussed in:79 *80 * Mohammad Alshraideh and Leonardo Bottaci81 * Search-based software test data generation for string data using program-specific search operators82 * http://neo.lcc.uma.es/mase/attachments/085_TestDataGenerationForStringData.pdf83 *84 * and85 *86 * EUGENE W. MYERS and WEBB MILLER87 * APPROXIMATE MATCHING OF REGULAR EXPRESSIONS88 * http://www.cs.mun.ca/~harold/Courses/Old/Ling6800.W06/Diary/reg.aprox.pdf89 */90 Automaton automaton = getAndCacheAutomaton(regex);91 final int NUM_CHARS = arg.length();92 List<State> topologicalOrder = regexStateCache.get(regex);93 Map<Integer, Map<State, Set<GraphTransition>>> transitions = new HashMap<>();94 intToStateMap = new HashMap<>();95 stateToIntMap = new HashMap<>();96 int numState = 0;97 for (State currentState : topologicalOrder) {98 /*99 * Init data structure to quickly map/access state/index100 */101 stateToIntMap.put(currentState, numState);102 intToStateMap.put(numState, currentState);103 numState++;104 for (Transition t : currentState.getTransitions()) {105 State destination = t.getDest();106 ensureState(transitions, destination, NUM_CHARS);107 for (int row = 0; row <= NUM_CHARS; row++) {108 /*109 * add an insertion edge from currentState in row to target state in same row110 */111 transitions.get(row).get(destination)112 .add(new GraphTransition(1.0, row, currentState, GraphTransition.TransitionType.INSERTION));113 }114 for (int row = 0; row < NUM_CHARS; row++) {115 /*116 * Add a replacement edge from currentState in row to t.getDest in row+1117 * if charAt row+1 == the parameter of this transition, this is a zero-cost edge118 */119 double cost = 0.0;120 if (arg.charAt(row) < t.getMin() || arg.charAt(row) > t.getMax()) {121 int distMin = Math.abs(arg.charAt(row) - t.getMin());122 int distMax = Math.abs(arg.charAt(row) - t.getMax());123 cost = normalize(Math.min(distMin, distMax));124 }125 /*126 * Important: even if the cost is 0 (eg match on the arg/regex in which we replace char X with X), we CANNOT127 * use a PHANTOM transition. Even if we do not replace anything, we still need to consider it as a replacement128 * transition. Consider the case129 *130 * "ac".matches("abc")131 *132 * If we used a phantom transition to represent the alignment c/c, then it would be possible to insert 'b' in the133 * middle of "abc". On the other hand, if we use a replacement c/c, then inserting 'b' would not be allowed, as an134 * insertion cannot be followed by a replacement.135 */136 transitions.get(row + 1).get(destination)137 .add(new GraphTransition(cost, row, currentState, GraphTransition.TransitionType.REPLACEMENT));138 }139 }140 ensureState(transitions, currentState, NUM_CHARS);141 for (int row = 0; row < NUM_CHARS; row++) {142 /*143 * add a deletion edge with cost 1 from currentState to currentState in next row144 */145 transitions.get(row + 1).get(currentState)146 .add(new GraphTransition(1.0, row, currentState, GraphTransition.TransitionType.DELETION));147 }148 }149 // Add zero-cost transitions from accepting states to final state150 State finalState = new State();151 ensureState(transitions, finalState, NUM_CHARS);152 for (State s : automaton.getStates()) {153 if (s.isAccept()) {154 transitions.get(NUM_CHARS).get(finalState)155 .add(new GraphTransition(0, NUM_CHARS, s, GraphTransition.TransitionType.PHANTOM));156 }157 }158 intToStateMap.put(numState, finalState);159 stateToIntMap.put(finalState, numState);160 return transitions;161 }162 /**163 * Ensure that each row has the full data structures containing the target state164 *165 */166 private static void ensureState(167 Map<Integer, Map<State, Set<GraphTransition>>> transitions,168 State state,169 int numRows) {170 for (int row = 0; row <= numRows; row++) {171 if (!transitions.containsKey(row)) {172 transitions.put(row, new HashMap<>());173 }174 if (!transitions.get(row).containsKey(state)) {175 transitions.get(row).put(state, new HashSet<>());176 }177 }178 }179 private static void cacheRegex(String regex) {180 String r = RegexUtils.expandRegex(regex);181 Automaton automaton = new RegExp(r, RegExp.NONE).toAutomaton();182 automaton.expandSingleton();183 // We convert this to a graph without self-loops in order to determine the topological order184 DirectedGraph<State, DefaultEdge> regexGraph = new DefaultDirectedGraph<>(DefaultEdge.class);185 Set<State> visitedStates = new HashSet<>();186 Queue<State> states = new LinkedList<>();187 State initialState = automaton.getInitialState();188 states.add(initialState);189 while (!states.isEmpty()) {190 State currentState = states.poll();191 if (visitedStates.contains(currentState)) {192 continue;193 }...
cacheRegex
Using AI Code Generation
1import org.evomaster.client.java.instrumentation.coverage.methodreplacement.regex.RegexGraph;2import java.util.List;3import java.util.Map;4import java.util.Set;5import java.util.ArrayList;6import java.util.HashMap;7import java.util.HashSet;8import java.util.LinkedHashMap;9import java.util.LinkedHashSet;10import java.util.stream.Collectors;11import java.util.stream.Stream;12import java.util.stream.IntStream;13import java.util.regex.Pattern;14import java.util.regex.Matcher;15import java.util.regex.MatchResult;
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!!