package StateMachine import java util import StateMachine Node import

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package StateMachine;
import java.util.*;
import StateMachine.Node;
import java.io.FileInputStream;
public class StateMachine {//extends Thread {
String[] alphabet;
State[] states;
TreeMap<String, TreeMap<String, String> > transitions;
boolean finished = false;
LinkedList<Node> queue = new LinkedList<Node>();
String startingState = new String();
String endingState = new String();
String[] badwords = {};
public StateMachine(String[] alphabet, String startingState, String endingState,
TreeMap<String, TreeMap<String, String>> transitions ) {
this.alphabet = alphabet;
this.transitions = transitions;
this.startingState = startingState;
this.endingState = endingState;
}
public String make_step() {
String answer = new String();
while (this.queue.size()>0 && !this.finished) {
Node current = this.queue.pop();
String newstate = this.transitions.get(current.state)
.get(current.current);
if (newstate == null) continue;
if (newstate.equals(this.endingState)) {
boolean bad = false;
for(String w : this.badwords) {
if (w.equals(newstate)) {
bad = true;
break;
}
}
if (!bad) {
this.finished = true;
answer = current.value;
break;
}
}
this.queue.addAll(current.expand(newstate));
}
return answer;
}
public void run() {
String ans = new String();
for(String sym : this.alphabet)
this.queue.add(new Node(this.alphabet, (new String())+sym,
sym, this.startingState));
while (!this.finished) {
ans = this.make_step();
}
System.out.println("Answer is: "+ans);
}
/**
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first state machine file name: ");
String filename = in.nextLine();
String text = new String();
FileInputStream input;
try {
input = new FileInputStream(filename);
while (input.available() > 0) {
text += (char)input.read();
}
TreeMap<String, TreeMap<String, String> > transitions = new TreeMap<String, TreeMap<String, String>>();
String start = new String();
String end = new String();
String[] alphabet = {};
for(String line: text.split("\n"))
{
System.out.println(line);
if (line.startsWith("ALPHABET: ")) {
alphabet = line.replace("ALPHABET: ", "").split(", ");
continue;
}
else if (line.startsWith("START: ")) {
start = line.replace("START: ", "");
continue;
}
else if (line.startsWith("END: ")) {
end = line.replace("END: ", "");
continue;
}
System.out.println(line);
String from = line.split(" ")[0];
String at = line.split(" ")[1];
String to = line.split(" ")[2];
TreeMap<String, String> newval = new TreeMap<String, String>();
newval.put(at, to);
transitions.put(from, newval);
}
System.out.println("Initializin state machine...");
System.out.print("Alphabet: ");
for(String sym: alphabet) System.out.print(sym+" ");
System.out.print('\n');
System.out.println("Starting state: " + start);
System.out.println("Ending state: " + end);
System.out.println("Transitions map: " + transitions.toString());
StateMachine machine = new StateMachine(alphabet, start, end, transitions);
System.out.println("Starting state machine...");
machine.run();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}