use of org.apache.lucene.util.automaton.Transition in project lucene-solr by apache.
the class TermAutomatonQuery method toDot.
/** Returns the dot (graphviz) representation of this automaton.
* This is extremely useful for visualizing the automaton. */
public String toDot() {
// TODO: refactor & share with Automaton.toDot!
StringBuilder b = new StringBuilder();
b.append("digraph Automaton {\n");
b.append(" rankdir = LR\n");
final int numStates = det.getNumStates();
if (numStates > 0) {
b.append(" initial [shape=plaintext,label=\"0\"]\n");
b.append(" initial -> 0\n");
}
Transition t = new Transition();
for (int state = 0; state < numStates; state++) {
b.append(" ");
b.append(state);
if (det.isAccept(state)) {
b.append(" [shape=doublecircle,label=\"" + state + "\"]\n");
} else {
b.append(" [shape=circle,label=\"" + state + "\"]\n");
}
int numTransitions = det.initTransition(state, t);
for (int i = 0; i < numTransitions; i++) {
det.getNextTransition(t);
assert t.max >= t.min;
for (int j = t.min; j <= t.max; j++) {
b.append(" ");
b.append(state);
b.append(" -> ");
b.append(t.dest);
b.append(" [label=\"");
if (j == anyTermID) {
b.append('*');
} else {
b.append(idToTerm.get(j).utf8ToString());
}
b.append("\"]\n");
}
}
}
b.append('}');
return b.toString();
}
use of org.apache.lucene.util.automaton.Transition in project lucene-solr by apache.
the class GraphTokenStreamFiniteStrings method articulationPoints.
/**
* Returns the articulation points (or cut vertices) of the graph:
* https://en.wikipedia.org/wiki/Biconnected_component
*/
public int[] articulationPoints() {
if (det.getNumStates() == 0) {
return new int[0];
}
//
Automaton.Builder undirect = new Automaton.Builder();
undirect.copy(det);
for (int i = 0; i < det.getNumStates(); i++) {
int numT = det.initTransition(i, transition);
for (int j = 0; j < numT; j++) {
det.getNextTransition(transition);
undirect.addTransition(transition.dest, i, transition.min);
}
}
int numStates = det.getNumStates();
BitSet visited = new BitSet(numStates);
int[] depth = new int[det.getNumStates()];
int[] low = new int[det.getNumStates()];
int[] parent = new int[det.getNumStates()];
Arrays.fill(parent, -1);
List<Integer> points = new ArrayList<>();
articulationPointsRecurse(undirect.finish(), 0, 0, depth, low, parent, visited, points);
Collections.reverse(points);
return points.stream().mapToInt(p -> p).toArray();
}
use of org.apache.lucene.util.automaton.Transition in project lucene-solr by apache.
the class GraphTokenStreamFiniteStrings method articulationPointsRecurse.
private static void articulationPointsRecurse(Automaton a, int state, int d, int[] depth, int[] low, int[] parent, BitSet visited, List<Integer> points) {
visited.set(state);
depth[state] = d;
low[state] = d;
int childCount = 0;
boolean isArticulation = false;
Transition t = new Transition();
int numT = a.initTransition(state, t);
for (int i = 0; i < numT; i++) {
a.getNextTransition(t);
if (visited.get(t.dest) == false) {
parent[t.dest] = state;
articulationPointsRecurse(a, t.dest, d + 1, depth, low, parent, visited, points);
childCount++;
if (low[t.dest] >= depth[state]) {
isArticulation = true;
}
low[state] = Math.min(low[state], low[t.dest]);
} else if (t.dest != parent[state]) {
low[state] = Math.min(low[state], depth[t.dest]);
}
}
if ((parent[state] != -1 && isArticulation) || (parent[state] == -1 && childCount > 1)) {
points.add(state);
}
}
Aggregations