Search in sources :

Example 1 with Transition

use of dk.brics.automaton.Transition in project evosuite by EvoSuite.

the class RegexDistanceUtils method cacheRegex.

private static void cacheRegex(String regex) {
    String r = expandRegex(regex);
    Automaton automaton = new RegExp(r, RegExp.NONE).toAutomaton();
    automaton.expandSingleton();
    // We convert this to a graph without self-loops in order to determine the topological order
    DirectedGraph<State, DefaultEdge> regexGraph = new DefaultDirectedGraph<State, DefaultEdge>(DefaultEdge.class);
    Set<State> visitedStates = new HashSet<State>();
    Queue<State> states = new LinkedList<State>();
    State initialState = automaton.getInitialState();
    states.add(initialState);
    while (!states.isEmpty()) {
        State currentState = states.poll();
        if (visitedStates.contains(currentState))
            continue;
        if (!regexGraph.containsVertex(currentState))
            regexGraph.addVertex(currentState);
        for (Transition t : currentState.getTransitions()) {
            // Need to get rid of back edges, otherwise there is no topological order!
            if (!t.getDest().equals(currentState)) {
                regexGraph.addVertex(t.getDest());
                regexGraph.addEdge(currentState, t.getDest());
                states.add(t.getDest());
                CycleDetector<State, DefaultEdge> det = new CycleDetector<State, DefaultEdge>(regexGraph);
                if (det.detectCycles()) {
                    regexGraph.removeEdge(currentState, t.getDest());
                }
            }
        }
        visitedStates.add(currentState);
    }
    TopologicalOrderIterator<State, DefaultEdge> iterator = new TopologicalOrderIterator<State, DefaultEdge>(regexGraph);
    List<State> topologicalOrder = new ArrayList<State>();
    while (iterator.hasNext()) {
        topologicalOrder.add(iterator.next());
    }
    regexStateCache.put(regex, topologicalOrder);
    regexAutomatonCache.put(regex, automaton);
}
Also used : Automaton(dk.brics.automaton.Automaton) RegExp(dk.brics.automaton.RegExp) DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) ArrayList(java.util.ArrayList) DefaultEdge(org.jgrapht.graph.DefaultEdge) TopologicalOrderIterator(org.jgrapht.traverse.TopologicalOrderIterator) LinkedList(java.util.LinkedList) CycleDetector(org.jgrapht.alg.CycleDetector) State(dk.brics.automaton.State) Transition(dk.brics.automaton.Transition) HashSet(java.util.HashSet)

Aggregations

Automaton (dk.brics.automaton.Automaton)1 RegExp (dk.brics.automaton.RegExp)1 State (dk.brics.automaton.State)1 Transition (dk.brics.automaton.Transition)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 CycleDetector (org.jgrapht.alg.CycleDetector)1 DefaultDirectedGraph (org.jgrapht.graph.DefaultDirectedGraph)1 DefaultEdge (org.jgrapht.graph.DefaultEdge)1 TopologicalOrderIterator (org.jgrapht.traverse.TopologicalOrderIterator)1