400d6e5cc7f698f688e78e6ccd327ca72f7ef14d
[tt2015.git] / a4 / code / src / learner / CandyLearner.java
1 package learner;
2
3 import java.io.BufferedReader;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.PrintStream;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Random;
11
12 import net.automatalib.automata.transout.MealyMachine;
13 import net.automatalib.util.graphs.dot.GraphDOT;
14 import net.automatalib.words.Alphabet;
15 import net.automatalib.words.Word;
16 import net.automatalib.words.impl.SimpleAlphabet;
17 import de.learnlib.algorithms.lstargeneric.ce.ObservationTableCEXHandlers;
18 import de.learnlib.algorithms.lstargeneric.closing.ClosingStrategies;
19 import de.learnlib.algorithms.lstargeneric.mealy.ExtensibleLStarMealy;
20 import de.learnlib.api.LearningAlgorithm.MealyLearner;
21 import de.learnlib.api.SUL;
22 import de.learnlib.cache.Caches;
23 import de.learnlib.eqtests.basic.CompleteExplorationEQOracle;
24 import de.learnlib.eqtests.basic.RandomWordsEQOracle;
25 import de.learnlib.eqtests.basic.WMethodEQOracle.MealyWMethodEQOracle;
26 import de.learnlib.experiments.Experiment.MealyExperiment;
27 import de.learnlib.oracles.ResetCounterSUL;
28 import de.learnlib.oracles.SULOracle;
29 import de.learnlib.statistics.SimpleProfiler;
30 import de.learnlib.statistics.StatisticSUL;
31
32
33 public class CandyLearner {
34
35 public static int sutInterface_portNumber=7892;
36
37 /*
38 * The Adapter: needed in order to let LearnLib and the sut to communicate
39 *
40 */
41 public static class SULAdapter implements SUL<String, String> {
42
43 // system under learning
44 private SutSocketWrapper sul = new SutSocketWrapper(sutInterface_portNumber);
45
46 @Override
47 public void pre(){
48 System.out.println("pre");
49 }
50
51 @Override
52 public void post(){
53 System.out.println("post");
54 }
55
56 // // reset the SUL
57 // @Override
58 // public void reset() {
59 // sul.sendReset();
60 // }
61
62 // execute one input on the SUL
63 @Override
64 public String step(String in) {
65 String output = sul.sendInput(in);
66 //System.out.println(in + ":" + output);
67 return output;
68 }
69 }
70
71 public static void main(String[] args)
72 throws NoSuchMethodException, IOException {
73
74 // create alphabet
75 Alphabet<String> inputs = new SimpleAlphabet<>();
76 inputs.add("IREFUND");
77 inputs.add("IBUTTONMARS");
78 inputs.add("IBUTTONSNICKERS");
79 inputs.add("IBUTTONBOUNTY");
80 inputs.add("ICOIN10");
81 inputs.add("ICOIN5");
82
83 // Instantiate the sut
84 SUL<String,String> sul = new SULAdapter();
85
86 // oracle for counting queries wraps sul
87 StatisticSUL<String, String> statisticSul =
88 new ResetCounterSUL<>("membership queries", sul);
89
90 SUL<String,String> effectiveSul = statisticSul;
91 // use caching in order to avoid duplicate queries
92 effectiveSul = Caches.createSULCache(inputs, effectiveSul);
93
94 SULOracle<String, String> mqOracle = new SULOracle<>(effectiveSul);
95
96 // create initial set of suffixes
97 List<Word<String>> suffixes = new ArrayList<>();
98 suffixes.add(Word.fromSymbols("IREFUND"));
99 suffixes.add(Word.fromSymbols("IBUTTONMARS"));
100 suffixes.add(Word.fromSymbols("IBUTTONSNICKERS"));
101 suffixes.add(Word.fromSymbols("IBUTTONBOUNTY"));
102 suffixes.add(Word.fromSymbols("ICOIN10"));
103 suffixes.add(Word.fromSymbols("ICOIN5"));
104
105 // construct L* instance (almost classic Mealy version)
106 // almost: we use words (Word<String>) in cells of the table
107 // instead of single outputs.
108 MealyLearner<String,String> lstar =
109 new ExtensibleLStarMealy<>(
110 inputs, // input alphabet
111 mqOracle, // mq oracle
112 suffixes, // initial suffixes
113 // handling of counterexamples
114 ObservationTableCEXHandlers.FIND_LINEAR_ALLSUFFIXES,
115 // choose row for closing the table
116 ClosingStrategies.CLOSE_SHORTEST
117 );
118
119 // create random words equivalence test
120 RandomWordsEQOracle<
121 String, Word<String>, MealyMachine<?,String,?,String>> randomWords =
122 new RandomWordsEQOracle<
123 String, Word<String>, MealyMachine<?,String,?,String>>(
124 mqOracle,
125 3, // int minLength
126 8, // int maxLength
127 1000, // int maxTests
128 new Random(46346293) // make results reproducible
129 );
130
131 // create complete exploration equivalence test
132 CompleteExplorationEQOracle<String, Word<String>> completeOracle =
133 new CompleteExplorationEQOracle<>(
134 mqOracle, // a membership oracle
135 5, // int minDepth
136 7 // int maxDepth
137 );
138
139 // create equivalence oracle based on the W method
140 MealyWMethodEQOracle<String, String> wOracle=
141 new MealyWMethodEQOracle<>(
142 5, //int maxDepth
143 mqOracle // a membership oracle
144 );
145
146
147 // construct a learning experiment from
148 // the learning algorithm and one of the equivalence oracles.
149 // The experiment will execute the main loop of
150 // active learning
151 MealyExperiment<String,String> experiment =
152 new MealyExperiment<>(
153 lstar,
154 // equivalence oracle, choose among
155 // [randomWords | completeOracle | wOracle]
156 // **remember to change their settings**
157 //randomWords,
158 completeOracle,
159 inputs // input alphabet
160 );
161
162 // turn off time profiling
163 experiment.setProfile(true);
164
165 // enable logging of models
166 experiment.setLogModels(true);
167
168 // run experiment
169 experiment.run();
170
171 // get learned model
172 MealyMachine<?, String, ?, String> result =
173 experiment.getFinalHypothesis();
174
175 // report results
176 System.out.println("------------------------------------------------");
177
178 // profiling
179 System.out.println(SimpleProfiler.getResults());
180
181 // learning statistics
182 System.out.println(experiment.getRounds().getSummary());
183 System.out.println(statisticSul.getStatisticalData().getSummary());
184
185 // model statistics
186 System.out.println("States: " + result.size());
187 System.out.println("Sigma: " + inputs.size());
188
189 // show model
190 System.out.println();
191 System.out.println("Model: ");
192
193 GraphDOT.write(result, inputs, System.out); // may throw IOException!
194 //Writer w = DOT.createDotWriter(true);
195 //GraphDOT.write(result, inputs, w);
196 //w.close();
197
198 String filename = "CandyMachine.dot";
199 PrintStream writer = new PrintStream(
200 new FileOutputStream(filename));
201 GraphDOT.write(result, inputs, writer); // may throw IOException!
202
203 System.out.println(executeCommand(
204 "dot -Tpdf /path/to/yourfolder/CandyMachine.dot " +
205 "-o /path/to/yourfolder/CandyMachine.pdf"));
206
207 System.out.println("------------------------------------------------");
208 }
209
210 // execute command, for translation from dot to pdf
211 public static String executeCommand(String command) {
212 StringBuffer output = new StringBuffer();
213
214 Process p;
215 try {
216 p = Runtime.getRuntime().exec(command);
217 p.waitFor();
218 BufferedReader reader =
219 new BufferedReader(new InputStreamReader(p.getInputStream()));
220
221 String line = "";
222 while ((line = reader.readLine())!= null) {
223 output.append(line + "\n");
224 }
225
226 } catch (Exception e) {
227 e.printStackTrace();
228 }
229
230 return output.toString();
231 }
232 }