added learnlibloblab
[tt2015.git] / a4 / code / src / learner / ExampleSUL.java
1 package learner;
2
3 import de.learnlib.api.SUL;
4 import de.learnlib.api.SULException;
5
6 /**
7 * Example of a three-state system, hard-coded.
8 *
9 * @author Ramon Janssen
10 */
11 public class ExampleSUL implements SUL<String, String> {
12 private enum State{s0,s1,s2};
13 private State currentState;
14 private static boolean VERBOSE = false;
15
16 @Override
17 public void pre() {
18 // add any code here that should be run at the beginning of every 'session',
19 // i.e. put the system in its initial state
20 if (VERBOSE) {
21 System.out.println("Starting SUL");
22 }
23 currentState = State.s0;
24 }
25
26 @Override
27 public void post() {
28 // add any code here that should be run at the end of every 'session'
29 if (VERBOSE) {
30 System.out.println("Shutting down SUL");
31 }
32 }
33
34 @Override
35 public String step(String input) throws SULException {
36 State previousState = this.currentState;
37 String output = makeTransition(input);
38 State nextState = this.currentState;
39 if (VERBOSE) {
40 System.out.println(previousState + " --" + input + "/" + output + "-> " + nextState);
41 }
42 return output;
43 }
44
45 /**
46 * The behaviour of the SUL. It takes one input, and returns an output. It now
47 * contains a hardcoded state-machine (so the result is easy to check). To learn
48 * an external program/system, connect this method to the SUL (e.g. via sockets
49 * or stdin/stdout) and make it perform an actual input, and retrieve an actual
50 * output.
51 * @param input
52 * @return
53 */
54 public String makeTransition(String input) {
55 switch (currentState) {
56 case s0:
57 switch(input) {
58 case "a":
59 currentState = State.s1;
60 return "x";
61 case "b":
62 currentState = State.s2;
63 return "y";
64 case "c":
65 return "z";
66 }
67 case s1:
68 switch(input) {
69 case "a":
70 return "z";
71 case "b":
72 currentState = State.s2;
73 return "y";
74 case "c":
75 return "z";
76 }
77 case s2:
78 switch(input) {
79 case "a":
80 return "z";
81 case "b":
82 currentState = State.s0;
83 return "y";
84 case "c":
85 return "z";
86 }
87 }
88 throw new SULException(new IllegalArgumentException("Argument '" + input + "' was not handled"));
89 }
90 }