/** Appends the specified character to the specified state, returning a new state. */ public static int appendChar(Automaton a, int state, int c) { int newState = a.createState(); a.addTransition(state, newState, c, c); return newState; }
/** Accept any single character starting from the specified state, returning the new state */ public static int appendAnyChar(Automaton a, int state) { int newState = a.createState(); a.addTransition(state, newState, Character.MIN_CODE_POINT, Character.MAX_CODE_POINT); return newState; }
/** * 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 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 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 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; }
/** 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; }
/** * 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 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; }
Transition[][] transitions2 = a2.getSortedTransitions(); Automaton c = new Automaton(); c.createState(); ArrayDeque<StatePair> worklist = new ArrayDeque<>(); HashMap<StatePair,StatePair> newstates = new HashMap<>(); StatePair r = newstates.get(q); if (r == null) { q.s = c.createState(); worklist.add(q); newstates.put(q, q);
int[] stateRep = new int[k]; result.createState(); newState = 0; } else { newState = result.createState();
if (prefix != null) { lastState = a.createState(); for (int i = 0, cp = 0; i < prefix.length(); i += Character.charCount(cp)) { int state = a.createState(); cp = prefix.codePointAt(i); a.addTransition(lastState, state, cp, cp); lastState = a.createState(); int state = a.createState(); a.setAccept(state, description.isAccept(i));
/** * 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; }
map[i] = result.createState(); result.setAccept(map[i], a.isAccept(i));
/** 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; }
result.createState(); result.createState();
int numStates = a.getNumStates(); for(int i=0;i<numStates;i++) { result.createState(); result.setAccept(i, a.isAccept(i)); int deadState = result.createState(); result.addTransition(deadState, deadState, Character.MIN_CODE_POINT, Character.MAX_CODE_POINT);
/** * 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); }
if (automaton.getNumStates() == 0) { automaton = new Automaton(); automaton.createState();