public void testRandomRanges() throws Exception {
    final Random r = random;
    int ITERS = 10 * RANDOM_MULTIPLIER;
    int ITERS_PER_DFA = 100 * RANDOM_MULTIPLIER;
    for (int iter = 0; iter < ITERS; iter++) {
      int x1 = getCodeStart(r);
      int x2 = getCodeStart(r);
      final int startCode, endCode;

      if (x1 < x2) {
        startCode = x1;
        endCode = x2;
      } else {
        startCode = x2;
        endCode = x1;
      }

      if (isSurrogate(startCode) && isSurrogate(endCode)) {
        iter--;
        continue;
      }

      final Automaton a = new Automaton();
      final State end = new State();
      end.setAccept(true);
      a.getInitialState().addTransition(new Transition(startCode, endCode, end));
      a.setDeterministic(true);

      testOne(r, new ByteRunAutomaton(a), startCode, endCode, ITERS_PER_DFA);
    }
  }
Exemple #2
0
 /**
  * Constructs sub-automaton corresponding to decimal numbers of value at most x.substring(n) and
  * length x.substring(n).length().
  */
 private static State atMost(String x, int n) {
   State s = new State();
   if (x.length() == n) s.setAccept(true);
   else {
     char c = x.charAt(n);
     s.addTransition(new Transition(c, atMost(x, (char) n + 1)));
     if (c > '0') s.addTransition(new Transition('0', (char) (c - 1), anyOfRightLength(x, n + 1)));
   }
   return s;
 }
Exemple #3
0
 /**
  * Constructs sub-automaton corresponding to decimal numbers of value at least x.substring(n) and
  * length x.substring(n).length().
  */
 private static State atLeast(String x, int n, Collection<State> initials, boolean zeros) {
   State s = new State();
   if (x.length() == n) s.setAccept(true);
   else {
     if (zeros) initials.add(s);
     char c = x.charAt(n);
     s.addTransition(new Transition(c, atLeast(x, n + 1, initials, zeros && c == '0')));
     if (c < '9') s.addTransition(new Transition((char) (c + 1), '9', anyOfRightLength(x, n + 1)));
   }
   return s;
 }
  public static void main(String[] args) {
    final int startCode = Integer.parseInt(args[0]);
    final int endCode = Integer.parseInt(args[1]);

    Automaton a = new Automaton();
    State start = a.getInitialState();
    State end = new State();
    end.setAccept(true);

    UTF32ToUTF8 converter = new UTF32ToUTF8();
    converter.convertOneEdge(start, end, startCode, endCode);
  }
  /**
   * Converts an incoming utf32 automaton to an equivalent utf8 one. The incoming automaton need not
   * be deterministic. Note that the returned automaton will not in general be deterministic, so you
   * must determinize it if that's needed.
   */
  public Automaton convert(Automaton utf32) {
    if (utf32.isSingleton()) {
      utf32 = utf32.cloneExpanded();
    }

    State[] map = new State[utf32.getNumberedStates().length];
    List<State> pending = new ArrayList<State>();
    State utf32State = utf32.getInitialState();
    pending.add(utf32State);
    Automaton utf8 = new Automaton();
    utf8.setDeterministic(false);

    State utf8State = utf8.getInitialState();

    utf8States = new State[5];
    utf8StateCount = 0;
    utf8State.number = utf8StateCount;
    utf8States[utf8StateCount] = utf8State;
    utf8StateCount++;

    utf8State.setAccept(utf32State.isAccept());

    map[utf32State.number] = utf8State;

    while (pending.size() != 0) {
      utf32State = pending.remove(pending.size() - 1);
      utf8State = map[utf32State.number];
      for (int i = 0; i < utf32State.numTransitions; i++) {
        final Transition t = utf32State.transitionsArray[i];
        final State destUTF32 = t.to;
        State destUTF8 = map[destUTF32.number];
        if (destUTF8 == null) {
          destUTF8 = newUTF8State();
          destUTF8.accept = destUTF32.accept;
          map[destUTF32.number] = destUTF8;
          pending.add(destUTF32);
        }
        convertOneEdge(utf8State, destUTF8, t.min, t.max);
      }
    }

    utf8.setNumberedStates(utf8States, utf8StateCount);

    return utf8;
  }
Exemple #6
0
 /**
  * Constructs sub-automaton corresponding to decimal numbers of value between x.substring(n) and
  * y.substring(n) and of length x.substring(n).length() (which must be equal to
  * y.substring(n).length()).
  */
 private static State between(
     String x, String y, int n, Collection<State> initials, boolean zeros) {
   State s = new State();
   if (x.length() == n) s.setAccept(true);
   else {
     if (zeros) initials.add(s);
     char cx = x.charAt(n);
     char cy = y.charAt(n);
     if (cx == cy)
       s.addTransition(new Transition(cx, between(x, y, n + 1, initials, zeros && cx == '0')));
     else { // cx<cy
       s.addTransition(new Transition(cx, atLeast(x, n + 1, initials, zeros && cx == '0')));
       s.addTransition(new Transition(cy, atMost(y, n + 1)));
       if (cx + 1 < cy)
         s.addTransition(
             new Transition((char) (cx + 1), (char) (cy - 1), anyOfRightLength(x, n + 1)));
     }
   }
   return s;
 }
  /**
   * uy.edu.fing.mina.omega.tffst.test 5, is the very example of the policy's paper. it works well
   * but A <-> B and B <-> A must be unified
   *
   * @param args
   */
  public static void main(String[] args) {

    Tffst.setMinimizeAlways(false);

    Tffst tffst1 = new Tffst();

    State s0 = new State();
    tffst1.setInitialState(s0);

    State s4 = new State();
    s4.setAccept(true);

    SimpleTf tf1 = new SimpleTf();
    tf1.setSLabel("A");

    SimpleTf tf2 = new SimpleTf();
    tf2.setSLabel("C");

    SimpleTf tf7 = new SimpleTf();
    tf7.setSLabel("I");

    SimpleTf tf8 = new SimpleTf();
    tf8.setSLabel("J");

    Transition trans1 = new Transition(tf1, tf2, s4);
    Transition trans5 = new Transition(tf7, tf8, s4);

    s0.addTransition(trans1);
    s4.addTransition(trans5);

    Utils.showDot(tffst1.toDot(""));

    tffst1.setDeterministic(false);
    tffst1.determinize();

    Utils.showDot(tffst1.toDot(""));

    Utils.showDot(tffst1.toSimpleTransitions().toDot(""));
  }
Exemple #8
0
 /**
  * Constructs sub-automaton corresponding to decimal numbers of length x.substring(n).length().
  */
 private static State anyOfRightLength(String x, int n) {
   State s = new State();
   if (x.length() == n) s.setAccept(true);
   else s.addTransition(new Transition('0', '9', anyOfRightLength(x, n + 1)));
   return s;
 }
  /**
   * uy.edu.fing.mina.omega.tffst.test 5, is the very example of the Policy 2004's paper.
   *
   * @param args
   */
  public static void main(String[] args) {

    Tffst.setMinimizeAlways(false);

    Tffst tffst1 = new Tffst();

    State s0 = new State();
    tffst1.setInitialState(s0);

    State s1 = new State();

    State s2 = new State();

    State s3 = new State();

    State s4 = new State();
    s4.setAccept(true);

    SimpleTf tf1 = new SimpleTf();
    tf1.setName("A");

    SimpleTf tf2 = new SimpleTf();
    tf2.setName("C");

    SimpleTf tf3 = new SimpleTf();
    tf3.setName("D");

    SimpleTf tf4 = new SimpleTf();
    tf4.setName("E");

    SimpleTf tf5 = new SimpleTf();
    tf5.setName("F");

    SimpleTf tf6 = new SimpleTf();
    tf6.setName("G");

    SimpleTf tf7 = new SimpleTf();
    tf7.setName("I");

    SimpleTf tf8 = new SimpleTf();
    tf8.setName("J");

    Transition trans1 = new Transition(tf1, tf2, s1);
    Transition trans2 = new Transition(tf1, tf3, s2);
    Transition trans3 = new Transition(tf4, tf5, s3);
    Transition trans4 = new Transition(tf6, tf5, s3);
    Transition trans5 = new Transition(tf7, tf8, s4);
    Transition trans6 = new Transition(tf7, tf8, s4);

    s0.addOutTran(trans1);
    s0.addOutTran(trans2);
    s1.addOutTran(trans3);
    s2.addOutTran(trans4);
    s3.addOutTran(trans5);
    s4.addOutTran(trans6);

    Utils.showDot(tffst1.toDot("START"));

    tffst1.setDeterministic(false);
    tffst1.determinize();

    Utils.showDot(tffst1.toDot("FIN"));
  }