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