/** * Returns a new (deterministic) automaton with the empty language. */ public static Automaton makeEmpty() { Automaton a = new Automaton(); a.finishState(); return a; }
/** * 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; }
/** * 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 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; }
Automaton c = new Automaton(); c.createState(); ArrayDeque<StatePair> worklist = new ArrayDeque<>();
/** * 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; }
Automaton result = new Automaton();
/** 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; }
/** * Returns an automaton that accepts the union of the languages of the given * automata. * <p> * Complexity: linear in number of states. */ public static Automaton union(Collection<Automaton> l) { Automaton result = new Automaton(); // Create initial state: result.createState(); // Copy over all automata for(Automaton a : l) { result.copy(a); } // Add epsilon transition from new initial state int stateOffset = 1; for(Automaton a : l) { if (a.getNumStates() == 0) { continue; } result.addEpsilon(0, stateOffset); stateOffset += a.getNumStates(); } result.finishState(); return removeDeadStates(result); }
Automaton result = new Automaton();
b = Automata.makeEmptyString(); } else if (min == 1) { b = new Automaton(); b.copy(a); } else {
final int prefixStates = prefix != null ? prefix.codePointCount(0, prefix.length()) : 0; final Automaton a = new Automaton(numStates + prefixStates, numTransitions); int lastState; if (prefix != null) {
return new Automaton();
Automaton result = new Automaton(); int numStates = a.getNumStates(); for(int i=0;i<numStates;i++) {
int maxDeterminizedStates, boolean isBinary) { if (automaton.getNumStates() == 0) { automaton = new Automaton(); automaton.createState();