-1

我在让这段代码正常工作时遇到了一些麻烦。我应该获取一个单行文件并在图表中移动并输出特定输入数字的每个状态。 在此处输入图像描述

示例文件将具有以下内容:开始;1个;0; 1个;1个;0; 1

输出将是:

输入 = 1; 状态 = ArraySet[开始]

输入 = 0; state = ArraySet[开始,接近]

输入 = 1; state = ArraySet[开始,结束]

输入 = 1; 状态 = ArraySet[开始]

输入 = 0; state = ArraySet[开始,接近]

输入 = 1; 状态 = ArraySet[开始,结束]

我完成了下面的代码。

public class NDFA {

  public static void main(String[] args) {

  Map<String, Map<String,Set<String>>> mainMap = new ArrayMap<String, Map<String,Set<String>>>();

  TypedBufferReader input = new TypedBufferReader("Enter Non-Deterministic Finite Automaton Description File: ");


  for (;;) {
     try {
          String line = input.readLine();
          StringTokenizer st = new StringTokenizer(line, ";");
          String  state = st.nextToken();

          Map<String,Set<String>> transitions = mainMap.get(state);

          transitions = new ArrayMap<String, Set<String>>();
          Set<String> stateSet = new ArraySet<String>();

          while (st.hasMoreTokens()) {
              String intStateInput = st.nextToken();
              String inputState = st.nextToken();
              stateSet.add(inputState);
              transitions.put(intStateInput, stateSet);
          }
        mainMap.put(state, transitions);
     } catch (EOFException e) { break;}
  }

//sort
List<String> mapList = new ArrayList<String>(mainMap.keys());
Collections.sort(mapList);
System.out.println("Non-Deterministic Finite Automaton");
for (String s : mapList) {
    Map<String, Set<String>> tempMap = mainMap.get(s);
    System.out.println(s + " transitions = " + tempMap.toString());
}

//Process one line file.
  TypedBufferReader oneLineInput = new TypedBufferReader("Enter start state/inputs file: ");

  try {
      String oneLine = oneLineInput.readLine();
      StringTokenizer st = new StringTokenizer(oneLine,";");
      String currentState = st.nextToken();
      System.out.println("Initial state = " + currentState);
      String inputNum = "";

      Set<String> inputSet;

             //stuck while storing inputNums in a set.
      while (st.hasMoreTokens()) {
          inputNum = st.nextToken();
          inputSet.add(inputNum);
          Map<String,Set<String>> transitions = mainMap.get(currentState);
          currentState = transitions.get(inputSet.values());
          System.out.println("input = " + inputNum + "; new state = " + currentState) ;
      }
      System.out.println("Final state = " + currentState);
  } catch (EOFException e) {}

  }

}

我无法让当前状态输出任何正确的集合。我可以轻松验证输入数字,但从头到尾让我感到困惑,我无法输出正确的 ArraySet。

4

1 回答 1

1

我以前在 python 中做过类似的事情。不确定这是否对您的 java 有帮助。

>>> g = {}
>>> g['start'] = {1: ['start'], 0: ['start', 'near']}
>>> g['near'] = {1: ['end']}
>>> g['end'] = {}
>>> data = [1, 0, 1, 1, 0, 1]
>>> states = ['start']
>>> while data:
...     x = data.pop(0)
...     new_states = []
...     for s in states:
...         reach = g[s]
...         if x in reach:
...             new_states += reach[x]
...     states = list(set(new_states))
...     print states
...
['start', 'near']
['start', 'end']
['start']
['start', 'near']
['start', 'end']
于 2012-10-11T21:28:19.970 回答