/** * Returns a new (deterministic) automaton that accepts only the empty string. */ public static Automaton makeEmptyString() { Automaton a = new Automaton(); a.createState(); a.setAccept(0, true); return a; }
/** * Returns a new (deterministic) automaton that accepts all strings. */ public static Automaton makeAnyString() { Automaton a = new Automaton(); int s = a.createState(); a.setAccept(s, true); a.addTransition(s, s, Character.MIN_CODE_POINT, Character.MAX_CODE_POINT); a.finishState(); return a; }
/** * Returns a new (deterministic) automaton that accepts all binary terms. */ public static Automaton makeAnyBinary() { Automaton a = new Automaton(); int s = a.createState(); a.setAccept(s, true); a.addTransition(s, s, 0, 255); a.finishState(); return a; }
/** * Returns a new (deterministic) automaton that accepts the single given * string. */ public static Automaton makeString(String s) { Automaton a = new Automaton(); int lastState = a.createState(); for (int i = 0, cp = 0; i < s.length(); i += Character.charCount(cp)) { int state = a.createState(); cp = s.codePointAt(i); a.addTransition(lastState, state, cp); lastState = state; } a.setAccept(lastState, true); a.finishState(); assert a.isDeterministic(); assert Operations.hasDeadStates(a) == false; return a; }
int state = 0; while (state < otherNumStates && (state = otherAcceptStates.nextSetBit(state)) != -1) { setAccept(stateOffset + state, true); state++;
/** Add a [virtual] epsilon transition between source and dest. * Dest state must already have all transitions added because this * method simply copies those same transitions over to source. */ public void addEpsilon(int source, int dest) { Transition t = new Transition(); int count = initTransition(dest, t); for(int i=0;i<count;i++) { getNextTransition(t); addTransition(source, t.dest, t.min, t.max); } if (isAccept(dest)) { setAccept(source, true); } }
/** * Returns a new (deterministic) automaton that accepts the single given * string from the specified unicode code points. */ public static Automaton makeString(int[] word, int offset, int length) { Automaton a = new Automaton(); a.createState(); int s = 0; for (int i = offset; i < offset+length; i++) { int s2 = a.createState(); a.addTransition(s, s2, word[i]); s = s2; } a.setAccept(s, true); a.finishState(); return a; }
/** * Returns a (deterministic) automaton that accepts the complement of the * language of the given automaton. * <p> * Complexity: linear in number of states if already deterministic and * exponential otherwise. * @param maxDeterminizedStates maximum number of states determinizing the * automaton can result in. Set higher to allow more complex queries and * lower to prevent memory exhaustion. */ static public Automaton complement(Automaton a, int maxDeterminizedStates) { a = totalize(determinize(a, maxDeterminizedStates)); int numStates = a.getNumStates(); for (int p=0;p<numStates;p++) { a.setAccept(p, !a.isAccept(p)); } return removeDeadStates(a); }
stateMap[q] = newState; result.setAccept(newState, a.isAccept(q));
/** * Returns a new (deterministic) automaton that accepts a single codepoint whose * value is in the given interval (including both end points). */ public static Automaton makeCharRange(int min, int max) { if (min > max) { return makeEmpty(); } Automaton a = new Automaton(); int s1 = a.createState(); int s2 = a.createState(); a.setAccept(s2, true); a.addTransition(s1, s2, min, max); a.finishState(); return a; }
while (worklist.size() > 0) { p = worklist.removeFirst(); c.setAccept(p.s, a1.isAccept(p.s1) && a2.isAccept(p.s2)); Transition[] t1 = transitions1[p.s1]; Transition[] t2 = transitions2[p.s2];
/** * Returns an automaton that accepts the union of the empty string and the * language of the given automaton. This may create a dead state. * <p> * Complexity: linear in number of states. */ static public Automaton optional(Automaton a) { Automaton result = new Automaton(); result.createState(); result.setAccept(0, true); if (a.getNumStates() > 0) { result.copy(a); result.addEpsilon(0, 1); } result.finishState(); return result; }
if (liveSet.get(i)) { map[i] = result.createState(); result.setAccept(map[i], a.isAccept(i));
/** Build an automaton accepting all terms with the specified prefix. */ public static Automaton toAutomaton(BytesRef prefix) { final int numStatesAndTransitions = prefix.length+1; final Automaton automaton = new Automaton(numStatesAndTransitions, numStatesAndTransitions); int lastState = automaton.createState(); for(int i=0;i<prefix.length;i++) { int state = automaton.createState(); automaton.addTransition(lastState, state, prefix.bytes[prefix.offset+i]&0xff); lastState = state; } automaton.setAccept(lastState, true); automaton.addTransition(lastState, lastState, 0, 255); automaton.finishState(); assert automaton.isDeterministic(); return automaton; }
/** Compiles all added states and transitions into a new {@code Automaton} * and returns it. */ public Automaton finish() { // Create automaton with the correct size. int numStates = nextState; int numTransitions = nextTransition / 4; Automaton a = new Automaton(numStates, numTransitions); // Create all states. for (int state = 0; state < numStates; state++) { a.createState(); a.setAccept(state, isAccept(state)); } // Create all transitions sorter.sort(0, numTransitions); for (int upto = 0; upto < nextTransition; upto += 4) { a.addTransition(transitions[upto], transitions[upto+1], transitions[upto+2], transitions[upto+3]); } a.finishState(); return a; }
/** * Returns a new (deterministic) automaton that accepts the single given * binary term. */ public static Automaton makeBinary(BytesRef term) { Automaton a = new Automaton(); int lastState = a.createState(); for (int i=0;i<term.length;i++) { int state = a.createState(); int label = term.bytes[term.offset+i] & 0xff; a.addTransition(lastState, state, label); lastState = state; } a.setAccept(lastState, true); a.finishState(); assert a.isDeterministic(); assert Operations.hasDeadStates(a) == false; return a; }
result.setAccept(stateOffset + s, true); break;
a.setAccept(lastState, true); int state = a.createState(); a.addTransition(lastState, state, 0); a.setAccept(state, true); lastState = state; a.setAccept(sinkState, true); firstMaxState = a.createState(); if (i < max.length-1) { a.setAccept(firstMaxState, true); a.setAccept(lastState, true); int nextState = a.createState(); if (i < max.length-1) { a.setAccept(nextState, true); a.setAccept(lastState, true);
for(int i=0;i<numStates;i++) { result.createState(); result.setAccept(i, a.isAccept(i));