• 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

96.3
/commons/util/src/main/java/de/learnlib/util/moore/MooreUtil.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.util.moore;
17

18
import java.util.Objects;
19

20
import de.learnlib.api.algorithm.LearningAlgorithm;
21
import de.learnlib.api.algorithm.LearningAlgorithm.MooreLearner;
22
import de.learnlib.api.oracle.MembershipOracle;
23
import de.learnlib.api.query.DefaultQuery;
24
import net.automatalib.automata.transducers.MooreMachine;
25
import net.automatalib.words.Word;
26
import org.checkerframework.checker.nullness.qual.Nullable;
27

28
/**
29
 * Utility class helping to unify various approaches to actively learning Moore machines.
30
 */
31
public final class MooreUtil {
1✔
32

33
    public static final int NO_MISMATCH = -1;
34

35
    private MooreUtil() {
36
        // prevent instantiation
37
    }
38

39
    public static <O> int findMismatch(Word<O> out1, Word<O> out2) {
40
        int len = out1.length();
1✔
41
        assert len == out2.length();
1✔
42

43
        for (int i = 0; i < len; i++) {
1✔
44
            O sym1 = out1.getSymbol(i);
1✔
45
            O sym2 = out2.getSymbol(i);
1✔
46

47
            if (!Objects.equals(sym1, sym2)) {
1✔
48
                return i;
1✔
49
            }
50
        }
51

52
        return NO_MISMATCH;
1✔
53
    }
54

55
    public static <I, O> @Nullable DefaultQuery<I, Word<O>> shortenCounterExample(MooreMachine<?, I, ?, O> hypothesis,
56
                                                                                  DefaultQuery<I, Word<O>> ceQuery) {
57
        Word<I> cePrefix = ceQuery.getPrefix(), ceSuffix = ceQuery.getSuffix();
1✔
58
        Word<O> hypOut = hypothesis.computeSuffixOutput(cePrefix, ceSuffix);
1✔
59
        Word<O> ceOut = ceQuery.getOutput();
1✔
60
        assert ceOut.length() == hypOut.length();
1✔
61

62
        int mismatchIdx = findMismatch(hypOut, ceOut);
1✔
63
        if (mismatchIdx == NO_MISMATCH) {
1✔
64
            return null;
1✔
65
        }
66

67
        return new DefaultQuery<>(cePrefix, ceSuffix.prefix(mismatchIdx), ceOut.prefix(mismatchIdx + 1));
1✔
68
    }
69

70
    public static <I, O> @Nullable DefaultQuery<I, O> reduceCounterExample(MooreMachine<?, I, ?, O> hypothesis,
71
                                                                           DefaultQuery<I, Word<O>> ceQuery) {
72
        Word<I> cePrefix = ceQuery.getPrefix(), ceSuffix = ceQuery.getSuffix();
1✔
73
        Word<O> hypOut = hypothesis.computeSuffixOutput(cePrefix, ceSuffix);
1✔
74
        Word<O> ceOut = ceQuery.getOutput();
1✔
75
        assert ceOut.length() == hypOut.length();
1✔
76

77
        int mismatchIdx = findMismatch(hypOut, ceOut);
1✔
78
        if (mismatchIdx == NO_MISMATCH) {
1✔
79
            return null;
×
80
        }
81

82
        return new DefaultQuery<>(cePrefix, ceSuffix.prefix(mismatchIdx), ceOut.getSymbol(mismatchIdx));
1✔
83
    }
84

85
    public static <M extends MooreMachine<?, I, ?, O>, I, O> MooreLearner<I, O> wrapSymbolLearner(LearningAlgorithm<M, I, O> learner) {
86
        return new MooreLearnerWrapper<>(learner);
1✔
87
    }
88

89
    public static <I, O> MembershipOracle<I, O> wrapWordOracle(MembershipOracle<I, Word<O>> oracle) {
90
        return new SymbolOracleWrapper<>(oracle);
1✔
91
    }
92

93
}
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