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

LearnLib / learnlib / 6660471831

26 Oct 2023 10:29PM UTC coverage: 93.327% (-0.001%) from 93.328%
6660471831

push

github

mtf90
cleanup statistics filters

32 of 32 new or added lines in 7 files covered. (100.0%)

11734 of 12573 relevant lines covered (93.33%)

1.69 hits per line

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

95.45
/examples/src/main/java/de/learnlib/example/aaar/AlternatingBitExampleExplicit.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.example.aaar;
17

18
import java.io.IOException;
19
import java.io.StringWriter;
20
import java.util.ArrayList;
21
import java.util.Arrays;
22
import java.util.Collection;
23
import java.util.List;
24
import java.util.Map;
25
import java.util.function.Function;
26

27
import de.learnlib.algorithm.aaar.ExplicitInitialAbstraction;
28
import de.learnlib.algorithm.aaar.abstraction.ExplicitAbstractionTree;
29
import de.learnlib.algorithm.aaar.explicit.ExplicitAAARLearnerMealy;
30
import de.learnlib.algorithm.lstar.mealy.ExtensibleLStarMealy;
31
import de.learnlib.algorithm.lstar.mealy.ExtensibleLStarMealyBuilder;
32
import de.learnlib.api.algorithm.LearnerConstructor;
33
import de.learnlib.api.query.DefaultQuery;
34
import de.learnlib.datastructure.observationtable.OTLearner.OTLearnerMealy;
35
import de.learnlib.datastructure.observationtable.writer.ObservationTableASCIIWriter;
36
import de.learnlib.example.aaar.Event.Msg;
37
import de.learnlib.example.aaar.Event.Recv;
38
import net.automatalib.automaton.transducer.MealyMachine;
39
import net.automatalib.common.util.Pair;
40
import net.automatalib.graph.Graph;
41
import net.automatalib.serialization.dot.GraphDOT;
42
import net.automatalib.visualization.dot.DOT;
43
import net.automatalib.word.Word;
44

45
/**
46
 * Example from the paper "Automata Learning with Automated Alphabet Abstraction Refinement" by Howar et al., which uses
47
 * the explicit version of the AAAR learner.
48
 */
49
@SuppressWarnings("PMD.SystemPrintln")
50
public final class AlternatingBitExampleExplicit {
51

52
    private static final int ID1 = 72;
53
    private static final int ID2 = 73;
54

55
    private AlternatingBitExampleExplicit() {
56
        // prevent instantiation
57
    }
58

59
    public static void main(String[] args) throws IOException {
60

61
        final Protocol mqo = new Protocol();
1✔
62

63
        final LearnerConstructor<ExtensibleLStarMealy<Event, String>, Event, Word<String>> lstar =
1✔
64
                (alph, mq) -> new ExtensibleLStarMealyBuilder<Event, String>().withAlphabet(alph)
1✔
65
                                                                              .withOracle(mq)
1✔
66
                                                                              .create();
1✔
67

68
        final ExplicitAAARLearnerMealy<ExtensibleLStarMealy<Event, String>, String, Event, String> learner =
1✔
69
                new ExplicitAAARLearnerMealy<>(lstar, mqo, new InitialAbstraction(), new Incrementor());
70

71
        learner.startLearning();
1✔
72
        printInfo(learner);
1✔
73

74
        // Since we directly start with the two concrete symbols, this counterexample does not refine the hypothesis
75
        learner.refineHypothesis(new DefaultQuery<>(Word.epsilon(),
1✔
76
                                                    Word.fromSymbols(new Msg<>(0, "d"), new Recv()),
1✔
77
                                                    Word.fromSymbols("ind", "ack(0)")));
1✔
78
        printInfo(learner);
1✔
79

80
        learner.refineHypothesis(new DefaultQuery<>(Word.epsilon(),
1✔
81
                                                    Word.fromSymbols(new Msg<>(ID1, "d'"),
1✔
82
                                                                     new Recv(),
83
                                                                     new Msg<>(ID2, "d''")),
84
                                                    Word.fromSymbols("ind", "ack(0)", "ind")));
1✔
85
        printInfo(learner);
1✔
86
    }
1✔
87

88
    private static <AI, CI, O> void printInfo(ExplicitAAARLearnerMealy<? extends OTLearnerMealy<CI, O>, AI, CI, O> learner)
89
            throws IOException {
90

91
        System.out.println("-------------------------------------------------------");
1✔
92
        new ObservationTableASCIIWriter<>().write(learner.getLearner().getObservationTable(), System.out);
1✔
93

94
        if (DOT.checkUsable()) {
1✔
95
            final MealyMachine<?, AI, ?, O> hyp = learner.getHypothesisModel();
1✔
96
            final Map<AI, ExplicitAbstractionTree<AI, CI, Word<O>>> trees = learner.getAbstractionTrees();
1✔
97
            final List<Graph<?, ?>> treesAsGraphs = new ArrayList<>(trees.size());
1✔
98

99
            for (ExplicitAbstractionTree<AI, CI, Word<O>> t : trees.values()) {
1✔
100
                treesAsGraphs.add(t.graphView());
1✔
101
            }
1✔
102

103
            try (StringWriter hypWriter = new StringWriter();
1✔
104
                 StringWriter treeWriter = new StringWriter()) {
1✔
105

106
                GraphDOT.write(hyp.transitionGraphView(learner.getAbstractAlphabet()), hypWriter);
1✔
107
                GraphDOT.write(treesAsGraphs, treeWriter);
1✔
108

109
                DOT.renderDOTStrings(Arrays.asList(Pair.of("Hypothesis", hypWriter.toString()),
1✔
110
                                                   Pair.of("Abstraction Trees", treeWriter.toString())), true);
1✔
111
            }
112
        }
113
    }
1✔
114

115
    private static class InitialAbstraction implements ExplicitInitialAbstraction<String, Event> {
116

117
        @Override
118
        public String getAbstractSymbol(Event c) {
119
            if (c instanceof Recv) {
1✔
120
                return "recv";
1✔
121
            } else if (c instanceof Msg) {
1✔
122
                return "msg";
1✔
123
            } else {
124
                throw new IllegalArgumentException("Unknown event: " + c);
×
125
            }
126
        }
127

128
        @Override
129
        public Event getRepresentative(String a) {
130
            switch (a) {
1✔
131
                case "recv":
132
                    return new Recv();
1✔
133
                case "msg":
134
                    return new Msg<>(0, "d");
1✔
135
                default:
136
                    throw new IllegalArgumentException("Unknown abstract: " + a);
×
137
            }
138
        }
139

140
        @Override
141
        public Collection<String> getInitialAbstracts() {
142
            return Arrays.asList("recv", "msg");
1✔
143
        }
144
    }
145

146
    private static class Incrementor implements Function<String, String> {
147

148
        private int cnt;
149

150
        @Override
151
        public String apply(String s) {
152
            return s + cnt++;
1✔
153
        }
154
    }
155
}
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