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
;
10 import java
.util
.Random
;
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
;
33 public class CandyLearner
{
35 public static int sutInterface_portNumber
=7892;
38 * The Adapter: needed in order to let LearnLib and the sut to communicate
41 public static class SULAdapter
implements SUL
<String
, String
> {
43 // system under learning
44 private SutSocketWrapper sul
= new SutSocketWrapper(sutInterface_portNumber
);
48 System
.out
.println("pre");
53 System
.out
.println("post");
58 // public void reset() {
62 // execute one input on the SUL
64 public String
step(String in
) {
65 String output
= sul
.sendInput(in
);
66 //System.out.println(in + ":" + output);
71 public static void main(String
[] args
)
72 throws NoSuchMethodException
, IOException
{
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");
83 // Instantiate the sut
84 SUL
<String
,String
> sul
= new SULAdapter();
86 // oracle for counting queries wraps sul
87 StatisticSUL
<String
, String
> statisticSul
=
88 new ResetCounterSUL
<>("membership queries", sul
);
90 SUL
<String
,String
> effectiveSul
= statisticSul
;
91 // use caching in order to avoid duplicate queries
92 effectiveSul
= Caches
.createSULCache(inputs
, effectiveSul
);
94 SULOracle
<String
, String
> mqOracle
= new SULOracle
<>(effectiveSul
);
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"));
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
119 // create random words equivalence test
121 String
, Word
<String
>, MealyMachine
<?
,String
,?
,String
>> randomWords
=
122 new RandomWordsEQOracle
<
123 String
, Word
<String
>, MealyMachine
<?
,String
,?
,String
>>(
127 1000, // int maxTests
128 new Random(46346293) // make results reproducible
131 // create complete exploration equivalence test
132 CompleteExplorationEQOracle
<String
, Word
<String
>> completeOracle
=
133 new CompleteExplorationEQOracle
<>(
134 mqOracle
, // a membership oracle
139 // create equivalence oracle based on the W method
140 MealyWMethodEQOracle
<String
, String
> wOracle
=
141 new MealyWMethodEQOracle
<>(
143 mqOracle
// a membership oracle
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
151 MealyExperiment
<String
,String
> experiment
=
152 new MealyExperiment
<>(
154 // equivalence oracle, choose among
155 // [randomWords | completeOracle | wOracle]
156 // **remember to change their settings**
159 inputs
// input alphabet
162 // turn off time profiling
163 experiment
.setProfile(true);
165 // enable logging of models
166 experiment
.setLogModels(true);
172 MealyMachine
<?
, String
, ?
, String
> result
=
173 experiment
.getFinalHypothesis();
176 System
.out
.println("------------------------------------------------");
179 System
.out
.println(SimpleProfiler
.getResults());
181 // learning statistics
182 System
.out
.println(experiment
.getRounds().getSummary());
183 System
.out
.println(statisticSul
.getStatisticalData().getSummary());
186 System
.out
.println("States: " + result
.size());
187 System
.out
.println("Sigma: " + inputs
.size());
190 System
.out
.println();
191 System
.out
.println("Model: ");
193 GraphDOT
.write(result
, inputs
, System
.out
); // may throw IOException!
194 //Writer w = DOT.createDotWriter(true);
195 //GraphDOT.write(result, inputs, w);
198 String filename
= "CandyMachine.dot";
199 PrintStream writer
= new PrintStream(
200 new FileOutputStream(filename
));
201 GraphDOT
.write(result
, inputs
, writer
); // may throw IOException!
203 System
.out
.println(executeCommand(
204 "dot -Tpdf /path/to/yourfolder/CandyMachine.dot " +
205 "-o /path/to/yourfolder/CandyMachine.pdf"));
207 System
.out
.println("------------------------------------------------");
210 // execute command, for translation from dot to pdf
211 public static String
executeCommand(String command
) {
212 StringBuffer output
= new StringBuffer();
216 p
= Runtime
.getRuntime().exec(command
);
218 BufferedReader reader
=
219 new BufferedReader(new InputStreamReader(p
.getInputStream()));
222 while ((line
= reader
.readLine())!= null) {
223 output
.append(line
+ "\n");
226 } catch (Exception e
) {
230 return output
.toString();