1 package nl
.ru
.cs
.TT1415
.assignment3
;
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
);
52 // execute one input on the SUL
54 public String
step(String in
) {
55 String output
= sul
.sendInput(in
);
56 //System.out.println(in + ":" + output);
61 public static void main(String
[] args
) throws NoSuchMethodException
, IOException
{
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");
72 // Instantiate the sut
73 SUL
<String
,String
> sul
= new SULAdapter();
75 // oracle for counting queries wraps sul
76 StatisticSUL
<String
, String
> statisticSul
= new ResetCounterSUL
<>("membership queries", sul
);
78 SUL
<String
,String
> effectiveSul
= statisticSul
;
79 // use caching in order to avoid duplicate queries
80 effectiveSul
= Caches
.createSULCache(inputs
, effectiveSul
);
82 SULOracle
<String
, String
> mqOracle
= new SULOracle
<>(effectiveSul
);
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"));
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
105 // create random words equivalence test
106 RandomWordsEQOracle
<String
, Word
<String
>, MealyMachine
<?
,String
,?
,String
>> randomWords
=
107 new RandomWordsEQOracle
<String
, Word
<String
>, MealyMachine
<?
,String
,?
,String
>>(
111 1000, // int maxTests
112 new Random(46346293) // make results reproducible
115 // create complete exploration equivalence test
116 CompleteExplorationEQOracle
<String
, Word
<String
>> completeOracle
=
117 new CompleteExplorationEQOracle
<>(
118 mqOracle
, // a membership oracle
123 // create equivalence oracle based on the W method
124 MealyWMethodEQOracle
<String
, String
> wOracle
=
125 new MealyWMethodEQOracle
<>(
127 mqOracle
// a membership oracle
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
135 MealyExperiment
<String
,String
> experiment
=
136 new MealyExperiment
<>(
138 randomWords
, // equivalence oracle, choose among [randomWords | completeOracle | wOracle] **remember to change their settings**
139 inputs
// input alphabet
142 // turn off time profiling
143 experiment
.setProfile(true);
145 // enable logging of models
146 experiment
.setLogModels(true);
152 MealyMachine
<?
, String
, ?
, String
> result
= experiment
.getFinalHypothesis();
155 System
.out
.println("-------------------------------------------------------");
158 System
.out
.println(SimpleProfiler
.getResults());
160 // learning statistics
161 System
.out
.println(experiment
.getRounds().getSummary());
162 System
.out
.println(statisticSul
.getStatisticalData().getSummary());
165 System
.out
.println("States: " + result
.size());
166 System
.out
.println("Sigma: " + inputs
.size());
169 System
.out
.println();
170 System
.out
.println("Model: ");
172 GraphDOT
.write(result
, inputs
, System
.out
); // may throw IOException!
173 // Writer w = DOT.createDotWriter(true);
174 // GraphDOT.write(result, inputs, w);
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!
182 System
.out
.println(executeCommand("dot -Tpdf /path/to/yourfolder/CandyMachine.dot -o /path/to/yourfolder/CandyMachine.pdf"));
185 System
.out
.println("-------------------------------------------------------");
189 // execute command, for translation from dot to pdf
190 public static String
executeCommand(String command
) {
192 StringBuffer output
= new StringBuffer();
196 p
= Runtime
.getRuntime().exec(command
);
198 BufferedReader reader
=
199 new BufferedReader(new InputStreamReader(p
.getInputStream()));
202 while ((line
= reader
.readLine())!= null) {
203 output
.append(line
+ "\n");
206 } catch (Exception e
) {
210 return output
.toString();