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("hoi");
53 System
.out
.println("hoi");
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
) throws NoSuchMethodException
, IOException
{
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");
82 // Instantiate the sut
83 SUL
<String
,String
> sul
= new SULAdapter();
85 // oracle for counting queries wraps sul
86 StatisticSUL
<String
, String
> statisticSul
= new ResetCounterSUL
<>("membership queries", sul
);
88 SUL
<String
,String
> effectiveSul
= statisticSul
;
89 // use caching in order to avoid duplicate queries
90 effectiveSul
= Caches
.createSULCache(inputs
, effectiveSul
);
92 SULOracle
<String
, String
> mqOracle
= new SULOracle
<>(effectiveSul
);
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"));
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
115 // create random words equivalence test
116 RandomWordsEQOracle
<String
, Word
<String
>, MealyMachine
<?
,String
,?
,String
>> randomWords
=
117 new RandomWordsEQOracle
<String
, Word
<String
>, MealyMachine
<?
,String
,?
,String
>>(
121 1000, // int maxTests
122 new Random(46346293) // make results reproducible
125 // create complete exploration equivalence test
126 CompleteExplorationEQOracle
<String
, Word
<String
>> completeOracle
=
127 new CompleteExplorationEQOracle
<>(
128 mqOracle
, // a membership oracle
133 // create equivalence oracle based on the W method
134 MealyWMethodEQOracle
<String
, String
> wOracle
=
135 new MealyWMethodEQOracle
<>(
137 mqOracle
// a membership oracle
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
145 MealyExperiment
<String
,String
> experiment
=
146 new MealyExperiment
<>(
148 randomWords
, // equivalence oracle, choose among [randomWords | completeOracle | wOracle] **remember to change their settings**
149 inputs
// input alphabet
152 // turn off time profiling
153 experiment
.setProfile(true);
155 // enable logging of models
156 experiment
.setLogModels(true);
162 MealyMachine
<?
, String
, ?
, String
> result
= experiment
.getFinalHypothesis();
165 System
.out
.println("-------------------------------------------------------");
168 System
.out
.println(SimpleProfiler
.getResults());
170 // learning statistics
171 System
.out
.println(experiment
.getRounds().getSummary());
172 System
.out
.println(statisticSul
.getStatisticalData().getSummary());
175 System
.out
.println("States: " + result
.size());
176 System
.out
.println("Sigma: " + inputs
.size());
179 System
.out
.println();
180 System
.out
.println("Model: ");
182 GraphDOT
.write(result
, inputs
, System
.out
); // may throw IOException!
183 // Writer w = DOT.createDotWriter(true);
184 // GraphDOT.write(result, inputs, w);
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!
192 System
.out
.println(executeCommand("dot -Tpdf /path/to/yourfolder/CandyMachine.dot -o /path/to/yourfolder/CandyMachine.pdf"));
195 System
.out
.println("-------------------------------------------------------");
199 // execute command, for translation from dot to pdf
200 public static String
executeCommand(String command
) {
202 StringBuffer output
= new StringBuffer();
206 p
= Runtime
.getRuntime().exec(command
);
208 BufferedReader reader
=
209 new BufferedReader(new InputStreamReader(p
.getInputStream()));
212 while ((line
= reader
.readLine())!= null) {
213 output
.append(line
+ "\n");
216 } catch (Exception e
) {
220 return output
.toString();