61be178d1ce66882112e632d27d4bd68de1df67d
[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("hoi");
49 }
50
51 @Override
52 public void post(){
53 System.out.println("hoi");
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) throws NoSuchMethodException, IOException {
72
73 // create alphabet
74 Alphabet<String> inputs = new SimpleAlphabet<>();
75 inputs.add("IREFUND");
76 inputs.add("IBUTTONMARS");
77 inputs.add("IBUTTONSNICKERS");
78 inputs.add("IBUTTONBOUNTY");
79 inputs.add("ICOIN10");
80 inputs.add("ICOIN5");
81
82 // Instantiate the sut
83 SUL<String,String> sul = new SULAdapter();
84
85 // oracle for counting queries wraps sul
86 StatisticSUL<String, String> statisticSul = new ResetCounterSUL<>("membership queries", sul);
87
88 SUL<String,String> effectiveSul = statisticSul;
89 // use caching in order to avoid duplicate queries
90 effectiveSul = Caches.createSULCache(inputs, effectiveSul);
91
92 SULOracle<String, String> mqOracle = new SULOracle<>(effectiveSul);
93
94 // create initial set of suffixes
95 List<Word<String>> suffixes = new ArrayList<>();
96 suffixes.add(Word.fromSymbols("IREFUND"));
97 suffixes.add(Word.fromSymbols("IBUTTONMARS"));
98 suffixes.add(Word.fromSymbols("IBUTTONSNICKERS"));
99 suffixes.add(Word.fromSymbols("IBUTTONBOUNTY"));
100 suffixes.add(Word.fromSymbols("ICOIN10"));
101 suffixes.add(Word.fromSymbols("ICOIN5"));
102
103 // construct L* instance (almost classic Mealy version)
104 // almost: we use words (Word<String>) in cells of the table
105 // instead of single outputs.
106 MealyLearner<String,String> lstar =
107 new ExtensibleLStarMealy<>(
108 inputs, // input alphabet
109 mqOracle, // mq oracle
110 suffixes, // initial suffixes
111 ObservationTableCEXHandlers.FIND_LINEAR_ALLSUFFIXES, // handling of counterexamples
112 ClosingStrategies.CLOSE_SHORTEST // choose row for closing the table
113 );
114
115 // create random words equivalence test
116 RandomWordsEQOracle<String, Word<String>, MealyMachine<?,String,?,String>> randomWords =
117 new RandomWordsEQOracle<String, Word<String>, MealyMachine<?,String,?,String>>(
118 mqOracle,
119 3, // int minLength
120 8, // int maxLength
121 1000, // int maxTests
122 new Random(46346293) // make results reproducible
123 );
124
125 // create complete exploration equivalence test
126 CompleteExplorationEQOracle<String, Word<String>> completeOracle =
127 new CompleteExplorationEQOracle<>(
128 mqOracle, // a membership oracle
129 5, // int minDepth
130 7 // int maxDepth
131 );
132
133 // create equivalence oracle based on the W method
134 MealyWMethodEQOracle<String, String> wOracle=
135 new MealyWMethodEQOracle<>(
136 5, //int maxDepth
137 mqOracle // a membership oracle
138 );
139
140
141 // construct a learning experiment from
142 // the learning algorithm and one of the equivalence oracles.
143 // The experiment will execute the main loop of
144 // active learning
145 MealyExperiment<String,String> experiment =
146 new MealyExperiment<>(
147 lstar,
148 randomWords, // equivalence oracle, choose among [randomWords | completeOracle | wOracle] **remember to change their settings**
149 inputs // input alphabet
150 );
151
152 // turn off time profiling
153 experiment.setProfile(true);
154
155 // enable logging of models
156 experiment.setLogModels(true);
157
158 // run experiment
159 experiment.run();
160
161 // get learned model
162 MealyMachine<?, String, ?, String> result = experiment.getFinalHypothesis();
163
164 // report results
165 System.out.println("-------------------------------------------------------");
166
167 // profiling
168 System.out.println(SimpleProfiler.getResults());
169
170 // learning statistics
171 System.out.println(experiment.getRounds().getSummary());
172 System.out.println(statisticSul.getStatisticalData().getSummary());
173
174 // model statistics
175 System.out.println("States: " + result.size());
176 System.out.println("Sigma: " + inputs.size());
177
178 // show model
179 System.out.println();
180 System.out.println("Model: ");
181
182 GraphDOT.write(result, inputs, System.out); // may throw IOException!
183 // Writer w = DOT.createDotWriter(true);
184 // GraphDOT.write(result, inputs, w);
185 // w.close();
186
187 String filename = "/path/to/yourfolder/CandyMachine.dot";
188 PrintStream writer = new PrintStream(
189 new FileOutputStream(filename));
190 GraphDOT.write(result, inputs, writer); // may throw IOException!
191
192 System.out.println(executeCommand("dot -Tpdf /path/to/yourfolder/CandyMachine.dot -o /path/to/yourfolder/CandyMachine.pdf"));
193
194
195 System.out.println("-------------------------------------------------------");
196
197 }
198
199 // execute command, for translation from dot to pdf
200 public static String executeCommand(String command) {
201
202 StringBuffer output = new StringBuffer();
203
204 Process p;
205 try {
206 p = Runtime.getRuntime().exec(command);
207 p.waitFor();
208 BufferedReader reader =
209 new BufferedReader(new InputStreamReader(p.getInputStream()));
210
211 String line = "";
212 while ((line = reader.readLine())!= null) {
213 output.append(line + "\n");
214 }
215
216 } catch (Exception e) {
217 e.printStackTrace();
218 }
219
220 return output.toString();
221
222 }
223 }