• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LearnLib / learnlib / 6433387082

06 Oct 2023 03:10PM UTC coverage: 92.296% (-0.007%) from 92.303%
6433387082

push

github

mtf90
update Falk's developer id

11573 of 12539 relevant lines covered (92.3%)

1.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.62
/examples/src/main/java/de/learnlib/examples/resumable/ResumableExample.java
1
/* Copyright (C) 2013-2023 TU Dortmund
2
 * This file is part of LearnLib, http://www.learnlib.de/.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package de.learnlib.examples.resumable;
17

18
import java.nio.charset.StandardCharsets;
19
import java.util.Random;
20

21
import com.thoughtworks.xstream.XStream;
22
import de.learnlib.algorithms.lstar.dfa.ClassicLStarDFA;
23
import de.learnlib.api.Resumable;
24
import de.learnlib.api.oracle.EquivalenceOracle.DFAEquivalenceOracle;
25
import de.learnlib.api.oracle.MembershipOracle.DFAMembershipOracle;
26
import de.learnlib.api.query.DefaultQuery;
27
import de.learnlib.filter.cache.dfa.DFACacheOracle;
28
import de.learnlib.filter.cache.dfa.DFACaches;
29
import de.learnlib.filter.statistic.oracle.DFAJointCounterOracle;
30
import de.learnlib.oracle.equivalence.DFASimulatorEQOracle;
31
import de.learnlib.oracle.membership.SimulatorOracle.DFASimulatorOracle;
32
import net.automatalib.automata.fsa.impl.compact.CompactDFA;
33
import net.automatalib.util.automata.random.RandomAutomata;
34
import net.automatalib.words.Alphabet;
35
import net.automatalib.words.impl.Alphabets;
36
import net.automatalib.words.impl.GrowingMapAlphabet;
37

38
/**
39
 * An example to demonstrate the {@link Resumable} feature of LearnLib to continue learning setups from previously
40
 * stored snapshots.
41
 */
42
@SuppressWarnings("PMD.SystemPrintln")
43
public final class ResumableExample {
44

45
    private static final CompactDFA<Character> TARGET;
46
    private static final Alphabet<Character> INITIAL_ALPHABET;
47
    private static final XStream X_STREAM;
48

49
    static {
50
        final int seed = 42;
1✔
51
        final int size = 100;
1✔
52

53
        TARGET = RandomAutomata.randomDFA(new Random(seed), size, Alphabets.characters('a', 'd'));
1✔
54
        INITIAL_ALPHABET = Alphabets.characters('a', 'b');
1✔
55

56
        X_STREAM = new XStream();
1✔
57
        X_STREAM.allowTypesByRegExp(new String[] {"net.automatalib.*", "de.learnlib.*"});
1✔
58
    }
1✔
59

60
    private ResumableExample() {
61
        // prevent instantiation
62
    }
63

64
    public static void main(String[] args) {
65

66
        final Setup setup = new Setup();
1✔
67

68
        // construct initial model with inputs 'a' and 'b'
69
        setup.learner.startLearning();
1✔
70
        DefaultQuery<Character, Boolean> ce;
71
        while ((ce = setup.eqo.findCounterExample(setup.learner.getHypothesisModel(), INITIAL_ALPHABET)) != null) {
1✔
72
            setup.learner.refineHypothesis(ce);
1✔
73
        }
74

75
        System.out.println("## Initial setup");
1✔
76
        printStats(setup);
1✔
77

78
        // serialize the current state of the learning setup which may be stored somewhere external
79
        final byte[] learnerData = toBytes(setup.learner);
1✔
80
        final byte[] cacheData = toBytes(setup.cache);
1✔
81

82
        // continue exploring the previous snapshot with new input symbol 'c'
83
        continueExploring(learnerData, cacheData, 'c');
1✔
84

85
        // continue exploring the previous snapshot with new input symbol 'd'. Note that in this scenario we have never added 'c'.
86
        continueExploring(learnerData, cacheData, 'd');
1✔
87
    }
1✔
88

89
    private static void continueExploring(byte[] learnerData, byte[] cacheData, char newSymbol) {
90

91
        // re-initialize setup
92
        final Setup setup = new Setup();
1✔
93

94
        // resume from previous states and add new symbol
95
        setup.cache.resume(fromBytes(cacheData));
1✔
96
        setup.cache.addAlphabetSymbol(newSymbol);
1✔
97

98
        setup.learner.resume(fromBytes(learnerData));
1✔
99
        setup.learner.addAlphabetSymbol(newSymbol);
1✔
100

101
        // continue learning-loop
102
        DefaultQuery<Character, Boolean> ce;
103
        while ((ce = setup.eqo.findCounterExample(setup.learner.getHypothesisModel(), INITIAL_ALPHABET)) != null) {
1✔
104
            setup.learner.refineHypothesis(ce);
×
105
        }
106

107
        System.out.println("## After exploring '" + newSymbol + '\'');
1✔
108
        printStats(setup);
1✔
109
    }
1✔
110

111
    private static void printStats(Setup setup) {
112
        System.out.println("Hypothesis size: " + setup.learner.getHypothesisModel().size());
1✔
113
        System.out.println("Query performance: " + setup.counter.getQueryCount() + " new queries");
1✔
114
        System.out.println("Symbol performance: " + setup.counter.getSymbolCount() + " new symbols");
1✔
115
        System.out.println();
1✔
116
    }
1✔
117

118
    private static <T> byte[] toBytes(Resumable<T> resumable) {
119
        return X_STREAM.toXML(resumable.suspend()).getBytes(StandardCharsets.UTF_8);
1✔
120
    }
121

122
    @SuppressWarnings("unchecked")
123
    private static <T> T fromBytes(byte[] bytes) {
124
        return (T) X_STREAM.fromXML(new String(bytes, StandardCharsets.UTF_8));
1✔
125
    }
126

127
    private static class Setup {
128

129
        private final DFAJointCounterOracle<Character> counter;
130
        private final DFACacheOracle<Character> cache;
131
        private final DFAEquivalenceOracle<Character> eqo;
132
        private final ClassicLStarDFA<Character> learner;
133

134
        Setup() {
1✔
135
            final DFAMembershipOracle<Character> mqo = new DFASimulatorOracle<>(TARGET);
1✔
136
            this.counter = new DFAJointCounterOracle<>(mqo);
1✔
137
            this.cache = DFACaches.createCache(new GrowingMapAlphabet<>(INITIAL_ALPHABET), counter);
1✔
138
            this.eqo = new DFASimulatorEQOracle<>(TARGET);
1✔
139
            this.learner = new ClassicLStarDFA<>(new GrowingMapAlphabet<>(INITIAL_ALPHABET), cache);
1✔
140
        }
1✔
141
    }
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc