protected State createNewState(State parent, char letter) {

    String newStateName = ((parent.getName().equals("'root'")) ? "" : parent.getName()) + letter;
    State newState;
    try {
      newState = addNewState(newStateName);
    } catch (StateException e) {
      return getTransition(parent, letter);
    }
    addTransition(parent, letter, newState);
    if (parent.equals(root)) {
      fallbacks.put(newState, root);
    } else {
      State state = parent, stateForLetter = null;
      do {
        state = fallbacks.get(state);
        stateForLetter = getTransition(state, letter);
      } while (stateForLetter == null && !root.equals(state));
      if (stateForLetter != null) {
        fallbacks.put(newState, stateForLetter);
        if (isAccepting(stateForLetter)) {
          setAccepting(newState);
        }
      } else {
        fallbacks.put(newState, root);
      }
    }
    return newState;
  }
    /**
     * Add a new state to the state machine. Bottom up addition of states is allowed but the same
     * state may only exist in one hierarchy.
     *
     * @param state the state to add
     * @param parent the parent of state
     * @return stateInfo for this state
     */
    private final StateInfo addState(State state, State parent) {
      if (mDbg) {
        Log.d(
            TAG,
            "addStateInternal: E state="
                + state.getName()
                + ",parent="
                + ((parent == null) ? "" : parent.getName()));
      }
      StateInfo parentStateInfo = null;
      if (parent != null) {
        parentStateInfo = mStateInfo.get(parent);
        if (parentStateInfo == null) {
          // Recursively add our parent as it's not been added yet.
          parentStateInfo = addState(parent, null);
        }
      }
      StateInfo stateInfo = mStateInfo.get(state);
      if (stateInfo == null) {
        stateInfo = new StateInfo();
        mStateInfo.put(state, stateInfo);
      }

      // Validate that we aren't adding the same state in two different hierarchies.
      if ((stateInfo.parentStateInfo != null) && (stateInfo.parentStateInfo != parentStateInfo)) {
        throw new RuntimeException("state already added");
      }
      stateInfo.state = state;
      stateInfo.parentStateInfo = parentStateInfo;
      stateInfo.active = false;
      if (mDbg) Log.d(TAG, "addStateInternal: X stateInfo: " + stateInfo);
      return stateInfo;
    }
 public static ArrayList<State> findValidTargetState(State state, String name, boolean fuzzy) {
   ArrayList<State> result = new ArrayList<State>();
   for (State t : allValidTargetStates(state)) {
     if (t.getName().startsWith(name)) {
       if (fuzzy) result.add(t);
       else if (t.getName().equals(name)) result.add(t);
     }
   }
   return result;
 }
 static {
   for (State state : State.values()) {
     mapping.put(state.getNodeName(), state);
     mapping2.put(state.getName(), state);
     mapping3.put(state.getId(), state);
   }
 }
Beispiel #5
0
 /**
  * Adds a state to the FSM. The name of the state is taken from the state object. The first state
  * added automatically becomes the initial state that the FSM starts in.
  *
  * @param state state behavior.
  */
 public void add(State state) {
   if (states.containsKey(state.name)) throw new FjageError("Duplicate state name: " + state.name);
   states.put(state.getName(), state);
   state.fsm = this;
   state.log = log;
   if (initial == FINAL) initial = state;
 }
Beispiel #6
0
 /**
  * Returns the state of the device with the given name.
  *
  * @param name The name of the state requested.
  * @return The State object requested or null if there's no state with the given name.
  */
 public State getState(String name) {
   for (State s : getAllStates()) {
     if (s.getName().equals(name)) {
       return s;
     }
   }
   return null;
 }
Beispiel #7
0
 public void incrementTransition(Transducer.TransitionIterator ti, double count) {
   int inputFtr = (Integer) ti.getInput();
   State src = (HMM.State) ((TransitionIterator) ti).getSourceState();
   State dest = (HMM.State) ((TransitionIterator) ti).getDestinationState();
   int index = ti.getIndex();
   emissionEstimator[index].increment(inputFtr, weight * count);
   transitionEstimator[src.getIndex()].increment(dest.getName(), weight * count);
 }
 /**
  * Call the exit method for each state from the top of stack up to the common ancestor state.
  */
 private final void invokeExitMethods(StateInfo commonStateInfo) {
   while ((mStateStackTopIndex >= 0) && (mStateStack[mStateStackTopIndex] != commonStateInfo)) {
     State curState = mStateStack[mStateStackTopIndex].state;
     if (mDbg) Log.d(TAG, "invokeExitMethods: " + curState.getName());
     curState.exit();
     mStateStack[mStateStackTopIndex].active = false;
     mStateStackTopIndex -= 1;
   }
 }
 /** Convert StateInfo to string */
 @Override
 public String toString() {
   return "state="
       + state.getName()
       + ",active="
       + active
       + ",parent="
       + ((parentStateInfo == null) ? "null" : parentStateInfo.state.getName());
 }
Beispiel #10
0
  // Calculates epsilon transition states
  private DFAState closure(DFAState s) {
    HashSet<State> ret = new HashSet<State>();
    HashSet<State> toAdd;
    boolean isAccept = false;
    String name = "";
    DFAState output;

    // Add all input states to output
    for (State i : s.getStates()) {
      ret.add(i);
    }

    while (true) {
      toAdd = new HashSet<State>();

      // Iterate over all states currently in ret
      for (State r : ret) {
        if (r.getTransitionTable().containsKey("")) {
          for (State t : r.getTransitionTable().get("")) {
            if (!ret.contains(t)) {
              toAdd.add(t);
            }
          }
        }
      }
      if (toAdd.isEmpty()) {
        break;
      }
      for (State a : toAdd) {
        ret.add(a);
      }
    }
    // Determine if new state is accept
    for (State t : ret) {
      if (t.isAccept()) {
        isAccept = true;
        name = t.getName();
        break;
      }
    }
    // Create new DFAState
    output = new DFAState(name, isAccept, ret, new HashMap<String, DFAState>());
    for (DFAState sta : states) {
      if (output.equals(sta)) {
        output = sta;
        break;
      }
    }
    //        if(states.contains(output)){
    //        	System.out.println("NOOO");
    //        }
    states.add(output);
    if (output.isAccept()) {
      accept.add(output);
    }
    return output;
  }
 /*
  * refined is a list of format: a,b,c,d
  */
 protected void add2matrix(String refined, String source) {
   String[] alist = refined.split(",");
   for (int i = 0; i < alist.length; i++) {
     for (int j = i + 1; j < alist.length; j++) {
       int score = 1; // absent or erect
       State s1 = statematrix.getStateByName(alist[i]);
       State s2 = statematrix.getStateByName(alist[j]);
       s1 = s1 == null ? new State(alist[i]) : s1;
       s2 = s2 == null ? new State(alist[j]) : s2;
       boolean add = true;
       if (this.filtered) {
         add = notInGlossary(s1.getName(), s2.getName());
       }
       if (add && !s1.getName().matches("\\d+") && !s2.getName().matches("\\d+")) {
         statematrix.addPair(s1, s2, score, source);
       }
     }
   }
 }
  private void setSessionState(State sessionState) {

    if (this.sessionState.deepequals(sessionState)) {
      return;
    }
    if (this.sessionState.deepequals(new StateDiconnected())
        && !(sessionState.deepequals(new StateDiconnected()))) {
      logger.error(
          "status transition from "
              + this.sessionState.getName()
              + " to "
              + sessionState.getName()
              + " in not allowed");
      return;
    }
    if (logger.isTraceEnabled()) {
      logger.trace(
          "Changing status from " + this.sessionState.getName() + " to " + sessionState.getName());
    }
    this.sessionState = sessionState;
  }
Beispiel #13
0
 public void estimate() {
   Alphabet transitionAlphabet = getTransitionAlphabet();
   initialMultinomial = initialEstimator.estimate();
   initialEstimator = new Multinomial.LaplaceEstimator(transitionAlphabet);
   for (int i = 0; i < numStates(); i++) {
     State s = (State) getState(i);
     emissionMultinomial[i] = emissionEstimator[i].estimate();
     transitionMultinomial[i] = transitionEstimator[i].estimate();
     s.setInitialWeight(initialMultinomial.logProbability(s.getName()));
     // reset estimators
     emissionEstimator[i] = new Multinomial.LaplaceEstimator(inputAlphabet);
     transitionEstimator[i] = new Multinomial.LaplaceEstimator(transitionAlphabet);
   }
 }
    /** Initialize StateStack to mInitialState. */
    private final void setupInitialStateStack() {
      if (mDbg) {
        Log.d(TAG, "setupInitialStateStack: E mInitialState=" + mInitialState.getName());
      }

      StateInfo curStateInfo = mStateInfo.get(mInitialState);
      for (mTempStateStackCount = 0; curStateInfo != null; mTempStateStackCount++) {
        mTempStateStack[mTempStateStackCount] = curStateInfo;
        curStateInfo = curStateInfo.parentStateInfo;
      }

      // Empty the StateStack
      mStateStackTopIndex = -1;

      moveTempStateStackToStateStack();
    }
Beispiel #15
0
  public Object store(String id, State voobj) {
    State obj;

    if (id.equals("0")) {
      obj = new State();

    } else {
      obj = State.DAO().findById(id);
    }

    boolean isNewStateN65961 = obj.getId() == null || obj.getId().equals(0L);

    obj.setName(voobj.getName());
    obj.setCountry(Country.DAO().findBy(voobj.getCountry()));

    return obj.store();
  }
Beispiel #16
0
 /**
  * Separate initialization of initial/transitions and emissions. All probabilities are
  * proportional to (1+Uniform[0,1])^noise.
  *
  * @author kedarb
  * @param random Random object (if null use uniform distribution)
  * @param noise Noise exponent to use. If zero, then uniform distribution.
  */
 public void initTransitions(Random random, double noise) {
   Alphabet transitionAlphabet = getTransitionAlphabet();
   initialMultinomial =
       new Multinomial(
           getRandomArray(transitionAlphabet.size(), random, noise), transitionAlphabet);
   initialEstimator = new Multinomial.LaplaceEstimator(transitionAlphabet);
   transitionMultinomial = new Multinomial[numStates()];
   transitionEstimator = new Multinomial.LaplaceEstimator[numStates()];
   for (int i = 0; i < numStates(); i++) {
     transitionMultinomial[i] =
         new Multinomial(
             getRandomArray(transitionAlphabet.size(), random, noise), transitionAlphabet);
     transitionEstimator[i] = new Multinomial.LaplaceEstimator(transitionAlphabet);
     // set state's initial weight
     State s = (State) getState(i);
     s.setInitialWeight(initialMultinomial.logProbability(s.getName()));
   }
 }
Beispiel #17
0
    public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder;
      View row = convertView;
      if (row == null) {

        row = mInflater.inflate(R.layout.activity_main, parent, false);
        holder = new ViewHolder();
        holder.imageView = (ImageView) row.findViewById(R.id.flag);
        holder.nameView = (TextView) row.findViewById(R.id.name);
        holder.capitalView = (TextView) row.findViewById(R.id.capital);
        row.setTag(holder);
      } else {

        holder = (ViewHolder) row.getTag();
      }

      State state = getModel(position);

      holder.imageView.setImageResource((state.getFlagResource()));
      holder.nameView.setText(state.getName());
      holder.capitalView.setText(state.getCapital());

      return row;
    }
Beispiel #18
0
  public FlatModel(MarkovModel model) throws IllegalSymbolException, IllegalAlphabetException {
    this.source = model;
    this.delegate =
        new SimpleMarkovModel(source.advance().length, source.emissionAlphabet(), "flat");

    // add all the states
    // System.out.println("Adding states");
    Map toM = new HashMap();
    Map inModel = new HashMap();
    Map misStart = new HashMap();
    Map misEnd = new HashMap();
    Map modelStart = new HashMap();
    Map modelEnd = new HashMap();

    for (Iterator i = model.stateAlphabet().iterator(); i.hasNext(); ) {
      State s = (State) i.next();
      if (s instanceof DotState) { // simple dot state in model
        DotStateWrapper dsw = new DotStateWrapper(s);
        addAState(dsw);
        inModel.put(s, model);
        toM.put(s, dsw);
        // System.out.println("Added dot state " + dsw.getName());
      } else if (s instanceof EmissionState) { // simple emission state in model
        if (s instanceof MagicalState) {
          modelStart.put(model, model.magicalState());
          modelEnd.put(model, model.magicalState());
        } else {
          EmissionWrapper esw = new EmissionWrapper((EmissionState) s);
          addAState(esw);
          inModel.put(s, model);
          toM.put(s, esw);
          // System.out.println("Added emission state " + esw.getName());
        }
      } else if (s instanceof ModelInState) { // complex model inside state
        // System.out.println("Adding a model-in-state");
        ModelInState mis = (ModelInState) s;
        MarkovModel flatM = DP.flatView(mis.getModel());

        DotStateWrapper start = new DotStateWrapper(mis, "start");
        DotStateWrapper end = new DotStateWrapper(mis, "end");
        addAState(start);
        addAState(end);
        inModel.put(mis, model);
        modelStart.put(flatM, start);
        modelEnd.put(flatM, end);
        misStart.put(mis, start);
        misEnd.put(mis, end);
        // System.out.println("Added " + start.getName() + " and " + end.getName());

        for (Iterator j = flatM.stateAlphabet().iterator(); j.hasNext(); ) {
          State t = (State) j.next();
          if (t instanceof DotState) {
            DotStateWrapper dsw = new DotStateWrapper(t);
            addAState(dsw);
            inModel.put(t, flatM);
            toM.put(t, dsw);
            toM.put(((Wrapper) t).getWrapped(), dsw);
            // System.out.println("Added wrapped dot state " + dsw.getName());
          } else if (t instanceof EmissionState) {
            if (t instanceof MagicalState) {
              continue;
            }
            EmissionWrapper esw = new EmissionWrapper((EmissionState) t);
            addAState(esw);
            inModel.put(t, flatM);
            toM.put(t, esw);
            // toM.put(((Wrapper) t).getUnprojectedFeatures(), esw);
            // System.out.println("Added wrapped emission state " + esw.getName());
          } else { // unknown eventuality
            throw new IllegalSymbolException(s, "Don't know how to handle state: " + s.getName());
          }
        }
      } else { // unknown eventuality
        throw new IllegalSymbolException(s, "Don't know how to handle state: " + s.getName());
      }
    }

    // wire
    for (Iterator i = delegate.stateAlphabet().iterator(); i.hasNext(); ) {
      State s = (State) i.next();

      State sOrig;
      MarkovModel sModel;

      // System.out.println("Processing transitions from " + s.getName());

      // find underlying state and model for s
      if (s instanceof MagicalState) { // from magic
        sOrig = s;
        sModel = model;
      } else { // from not Magic
        Wrapper swrapper = (Wrapper) s;
        State swrapped = swrapper.getWrapped();
        MarkovModel subModel = (MarkovModel) inModel.get(swrapped);

        if (subModel != model) { // subModel -> *
          sOrig = swrapped;
          sModel = subModel;
        } else if (swrapper instanceof ModelInState) { // mis -> ?
          if (swrapper == modelStart.get(subModel)) { // mis -> subModel
            sModel = ((ModelInState) swrapped).getModel();
            sOrig = sModel.magicalState();
          } else { // mis -> model
            sModel = model;
            sOrig = swrapped;
          }
        } else { // normal
          sModel = model;
          sOrig = s;
        }
      }

      //
      // FIXME -- Matthew broked this...
      //

      TranslatedDistribution dist = null;
      // TranslatedDistribution dist = TranslatedDistribution.getDistribution(
      //  delegate.transitionsFrom(s),
      //  sModel.getWeights(sOrig)
      // );
      SimpleReversibleTranslationTable table = (SimpleReversibleTranslationTable) dist.getTable();
      try {
        delegate.setWeights(s, dist);
      } catch (ChangeVetoException cve) {
        throw new BioError("Couldn't edit delegate model", cve);
      }
      table.setTranslation(s, sOrig);

      // find all reachable states from s
      for (Iterator j = sModel.transitionsFrom(sOrig).iterator(); j.hasNext(); ) {
        State tOrig = (State) j.next();
        State t;
        if (tOrig instanceof MagicalState) { // * -> magic
          if (sModel == model) { // outer -> magic
            t = tOrig;
          } else { // subModel -> magic
            t = (State) modelEnd.get(sModel);
          }
        } else { // * -> normal
          t = (State) toM.get(sOrig);
        }
        table.setTranslation(t, tOrig);
      }
    }
  }
    public void StateCap() {

      State state =
          new State(
              4822023,
              50744,
              "Montgomery",
              "Alabama"); // Data Tranfer Object
      states.put(state.getName(), state); // DTO>>>>Template

      state = new State(731449, 571951, "Juneau", "Alaska");
      states.put(state.getName(), state);

      state = new State(6553255, 13635, "Phoenix", "Arizona");
      states.put(state.getName(), state);

      HashMap<String, String> sc = new HashMap<>(); // not valid
      HashMap<Integer, Integer> scI = new HashMap<>(); // not valid
      //
      //            sc.put("Montgomery", "Alabama");
      //            scI.put(4822023, 50744);
      //
      //            sc.put("Juneau", "Alaska");
      //            scI.put(731449, 571951);
      //
      //            sc.put("Phoenix", "Arizona");
      //            scI.put(6553255, 13635);

      sc.put("Little Rock", "Arkansas");
      scI.put(2949131, 52068);

      sc.put("Sacramento", "California");
      scI.put(38041430, 155959);

      sc.put("Denver", "Colorado");
      scI.put(5187582, 103718);

      sc.put("Hartford", "Connecticut");
      scI.put(3590347, 4845);

      sc.put("Dover", "Delaware");
      scI.put(917092, 1954);

      sc.put("Tallahassee", "Florida");
      scI.put(19317568, 53927);

      sc.put("Atlanta", "Georgia ");
      scI.put(9919945, 57906);

      sc.put("Honolulu", "Hawaii");
      scI.put(1392313, 6423);

      sc.put("Boise", "Idaho");
      scI.put(1595728, 82747);

      sc.put("Springfield", "Illinois");
      scI.put(12875255, 55584);

      sc.put("Indianapolis", "Indiana");
      scI.put(6537334, 35867);

      sc.put("Des Moines", "Iowa ");
      scI.put(3074186, 55869);

      sc.put("Topeka", "Kansas ");
      scI.put(2885905, 81815);

      sc.put("Frankfort", "Kentucky");
      scI.put(4380415, 39728);

      sc.put("Baton Rouge", "Louisiana");
      scI.put(4601893, 43562);

      sc.put("Augusta", "Maine");
      scI.put(1329192, 30862);

      sc.put("Annapolis", "Maryland");
      scI.put(5884563, 9774);

      sc.put("Boston", "Massachusetts ");
      scI.put(6646144, 7840);

      sc.put("Lansing", "Michigan");
      scI.put(9883360, 56804);

      sc.put("St. Paul", "Minnesota");
      scI.put(5379139, 79610);

      sc.put("Jackson", "Mississippi");
      scI.put(2984926, 46907);

      sc.put("Jefferson City", "Missouri");
      scI.put(6021988, 68886);

      sc.put("Helena", "Montana");
      scI.put(1005141, 145552);

      sc.put("Lincoln", "Nebraska ");
      scI.put(1855525, 76872);

      sc.put("Carson City", "Nevada");
      scI.put(2758931, 109826);

      sc.put("Concord", "New Hampshire");
      scI.put(1320718, 8968);

      sc.put("Trenton", "New Jersey");
      scI.put(8864590, 7417);

      sc.put("Santa Fe", "New Mexico");
      scI.put(2085538, 121356);

      sc.put("Albany", "New York");
      scI.put(19570261, 47214);

      sc.put("Raleigh", "North Carolina");
      scI.put(9752073, 48711);

      sc.put("Bismarck", "North Dakota ");
      scI.put(699628, 68976);

      sc.put("Columbus", "Ohio");
      scI.put(11544225, 40948);

      sc.put("Oklahoma City", "Oklahoma");
      scI.put(3814820, 68667);

      sc.put("Salem", "Oregon");
      scI.put(3899353, 95997);

      sc.put("Harrisburg", "Pennsylvania");
      scI.put(12763536, 44817);

      sc.put("Providence", "Rhode Island");
      scI.put(1050292, 1045);

      sc.put("Columbia", "South Carolina");
      scI.put(4723723, 30109);

      sc.put("Pierre", "South Dakota");
      scI.put(833354, 75885);

      sc.put("Nashville", "Tennessee");
      scI.put(6456243, 41217);

      sc.put("Austin", "Texas ");
      scI.put(26059203, 261797);

      sc.put("Salt Lake City", "Utah");
      scI.put(2855287, 82144);

      sc.put("Montpelier", "Vermont");
      scI.put(626011, 9250);

      sc.put("Richmond", "Virginia ");
      scI.put(8185867, 39594);

      sc.put("Olympia", "Washington");
      scI.put(6897012, 66544);

      sc.put("Charleston", "West Virginia");
      scI.put(1855413, 24078);

      sc.put("Madison", "Wisconsin");
      scI.put(5726398, 54310);

      sc.put("Cheyenne", "Wyoming ");
      scI.put(576412, 97100);
    }
Beispiel #20
0
 public String getName() {
   return wrapped.getName() + "-" + extra;
 }
 /** @see StateMachine#setInitialState(State) */
 private final void setInitialState(State initialState) {
   if (mDbg) Log.d(TAG, "setInitialState: initialState" + initialState.getName());
   mInitialState = initialState;
 }
 /** @see StateMachine#transitionTo(IState) */
 private final void transitionTo(IState destState) {
   mDestState = (State) destState;
   if (mDbg) Log.d(TAG, "StateMachine.transitionTo EX destState" + mDestState.getName());
 }