Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

197 wiersze
6.4 KiB

  1. import java.io.*;
  2. import java.lang.*;
  3. import java.util.*;
  4. class Edge {
  5. public int s, a;
  6. Edge(int s, int a) {
  7. this.s = s;
  8. this.a = a;
  9. }
  10. public String toString() {
  11. return this.s + ", " + this.a;
  12. }
  13. }
  14. class FSM {
  15. private ArrayList<String> states, alpha, finalState, transitions, warnings;
  16. private String initialState;
  17. private Integer initialStateID;
  18. private ArrayList<ArrayList<Edge>> tr;
  19. private ArrayList<ArrayList<Edge>> tr2way;
  20. private HashMap<String, Integer> stateToInt, alphaToInt;
  21. private HashSet<Integer> isFinal;
  22. private FileWriter out;
  23. private Integer szs, sza;
  24. private Boolean used[];
  25. public void error(String msg) throws IOException {
  26. out.write("Error:\n" + msg);
  27. out.close();
  28. System.exit(0);
  29. }
  30. int dfs(int v) {
  31. int ans = 1, n = tr.get(v).size();
  32. used[v] = true;
  33. for (int i = 0; i < n; ++i) {
  34. int to = tr.get(v).get(i).s;
  35. if (!used[to])
  36. ans += dfs(to);
  37. }
  38. return ans;
  39. }
  40. void dfs2way(int v) {
  41. used[v] = true;
  42. int n = tr2way.get(v).size();
  43. for (int i = 0; i < n; ++i) {
  44. int to = tr2way.get(v).get(i).s;
  45. if (!used[to])
  46. dfs2way(to);
  47. }
  48. }
  49. FSM(String states, String alpha, String initialState, String finalState, String transitions,
  50. FileWriter out)
  51. throws IOException {
  52. this.out = out;
  53. if (!states.matches("states=\\[.*\\]$") || !alpha.matches("alpha=\\[.*\\]$")
  54. || !initialState.matches("init\\.st=\\[.*\\]$")
  55. || !finalState.matches("fin\\.st=\\[.*\\]$") || !transitions.matches("trans=\\[.*\\]$"))
  56. error("E5: Input file is malformed");
  57. this.states = new ArrayList<String>(Arrays.asList(states.split("states=\\[|,|\\]")));
  58. this.alpha = new ArrayList<String>(Arrays.asList(alpha.split("alpha=\\[|,|\\]")));
  59. String[] temp = initialState.split("init\\.st=\\[|\\]");
  60. if (temp.length <= 1)
  61. error("E4: Initial state is not defined");
  62. this.initialState = temp[1];
  63. this.finalState =
  64. new ArrayList<String>(Arrays.asList(finalState.split("fin\\.st=\\[|,|\\]")));
  65. this.transitions =
  66. new ArrayList<String>(Arrays.asList(transitions.split("trans=\\[|,|\\]")));
  67. this.szs = this.states.size();
  68. this.sza = this.alpha.size();
  69. stateToInt = new HashMap<String, Integer>();
  70. alphaToInt = new HashMap<String, Integer>();
  71. isFinal = new HashSet<Integer>();
  72. tr = new ArrayList<ArrayList<Edge>>();
  73. tr2way = new ArrayList<ArrayList<Edge>>();
  74. for (int i = 0; i < this.states.size(); ++i) {
  75. tr.add(new ArrayList<Edge>());
  76. tr2way.add(new ArrayList<Edge>());
  77. }
  78. warnings = new ArrayList<String>();
  79. used = new Boolean[szs];
  80. for (int i = 1; i < szs; ++i) this.stateToInt.put(this.states.get(i), i);
  81. if (this.finalState.size() <= 1)
  82. warnings.add("W1: Accepting state is not defined");
  83. for (int i = 1; i < this.finalState.size(); ++i) {
  84. if (this.stateToInt.containsKey(this.finalState.get(i)))
  85. isFinal.add(i);
  86. else
  87. error("E1: A state '" + this.finalState.get(i) + "' is not in the set of states");
  88. }
  89. for (int i = 1; i < sza; ++i) alphaToInt.put(this.alpha.get(i), i);
  90. for (int i = 1; i < this.transitions.size(); ++i) {
  91. String[] cur = this.transitions.get(i).split(">");
  92. if (cur.length != 3)
  93. error("E5: Input file is malformed");
  94. if (!this.stateToInt.containsKey(cur[0]))
  95. error("E1: A state '" + cur[0] + "' is not in the set of states");
  96. if (!this.alphaToInt.containsKey(cur[1]))
  97. error("E3: A transition '" + cur[1] + "' is not represented in the alphabet");
  98. if (!this.stateToInt.containsKey(cur[2]))
  99. error("E1: A state '" + cur[2] + "' is not in the set of states");
  100. int l = stateToInt.get(cur[0]), m = alphaToInt.get(cur[1]), r = stateToInt.get(cur[2]);
  101. tr.get(l).add(new Edge(r, m));
  102. tr2way.get(l).add(new Edge(r, m));
  103. tr2way.get(r).add(new Edge(l, m));
  104. }
  105. if (stateToInt.containsKey(this.initialState))
  106. initialStateID = stateToInt.get(this.initialState);
  107. else
  108. error("E4: Initial state is not defined");
  109. int components = 0;
  110. for (int i = 1; i < szs; ++i) used[i] = false;
  111. for (int i = 1; i < szs; ++i) {
  112. if (!used[i]) {
  113. dfs2way(i);
  114. components++;
  115. }
  116. }
  117. if (components > 1)
  118. error("E2: Some states are disjoint");
  119. for (int i = 1; i < szs; ++i) used[i] = false;
  120. int cnt = dfs(initialStateID);
  121. if (cnt != szs - 1)
  122. warnings.add("W2: Some states are not reachable from the initial state");
  123. Boolean isDet = true, isComplete = true;
  124. for (int i = 1; i < szs; ++i) {
  125. HashSet<Integer> st = new HashSet<Integer>();
  126. for (int j = 0; j < tr.get(i).size(); ++j) {
  127. int a = tr.get(i).get(j).a;
  128. if (st.contains(a))
  129. isDet = false;
  130. else
  131. st.add(a);
  132. }
  133. if (st.size() != this.alpha.size() - 1)
  134. isComplete = false;
  135. }
  136. if (!isDet)
  137. warnings.add("W3: FSA is nondeterministic");
  138. if (isComplete)
  139. out.write("FSA is complete");
  140. else
  141. out.write("FSA is incomplete");
  142. if (warnings.size() > 0)
  143. out.write("\nWarning: ");
  144. for (int i = 0; i < warnings.size(); ++i) {
  145. out.write("\n" + warnings.get(i));
  146. }
  147. out.close();
  148. }
  149. }
  150. public class FSA {
  151. public static void main(String[] args) throws IOException {
  152. FileReader inp = new FileReader("fsa.txt");
  153. FileWriter out = new FileWriter("result.txt");
  154. Scanner in = new Scanner(inp);
  155. String states = in.nextLine();
  156. String alpha = in.nextLine();
  157. String initialState = in.nextLine();
  158. String finalState = in.nextLine();
  159. String transitions = in.nextLine();
  160. FSM fsm = new FSM(states, alpha, initialState, finalState, transitions, out);
  161. }
  162. }