• 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

92.11
/oracles/membership-oracles/src/main/java/de/learnlib/oracle/membership/SimulatorOmegaOracle.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.oracle.membership;
17

18
import java.util.ArrayList;
19
import java.util.Collection;
20
import java.util.List;
21

22
import de.learnlib.api.oracle.MembershipOracle;
23
import de.learnlib.api.oracle.MembershipOracle.DFAMembershipOracle;
24
import de.learnlib.api.oracle.MembershipOracle.MealyMembershipOracle;
25
import de.learnlib.api.oracle.OmegaMembershipOracle;
26
import de.learnlib.api.oracle.OmegaQueryAnswerer;
27
import de.learnlib.api.oracle.SingleQueryOmegaOracle;
28
import de.learnlib.api.query.OmegaQuery;
29
import de.learnlib.api.query.Query;
30
import de.learnlib.util.MQUtil;
31
import net.automatalib.automaton.concept.SuffixOutput;
32
import net.automatalib.automaton.fsa.DFA;
33
import net.automatalib.automaton.transducer.MealyMachine;
34
import net.automatalib.common.util.Pair;
35
import net.automatalib.ts.simple.SimpleDTS;
36
import net.automatalib.word.Word;
37
import net.automatalib.word.WordBuilder;
38
import org.checkerframework.checker.nullness.qual.Nullable;
39

40
/**
41
 * Answers {@link OmegaQuery}s by simulating an automaton.
42
 * <p>
43
 * <b>Implementation note</b>: Under the assumption that read-operations do not alter the internal state of the
44
 * automaton, this oracle is thread-safe.
45
 *
46
 * @see SimulatorOracle
47
 *
48
 * @param <S> the state type.
49
 * @param <I> the input type.
50
 * @param <D> the output type.
51
 */
52
public class SimulatorOmegaOracle<S extends Object, I, D> implements SingleQueryOmegaOracle<S, I, D> {
2✔
53

54
    /**
55
     * The automaton to simulate.
56
     */
57
    private final SimpleDTS<S, I> simpleDTS;
58

59
    /**
60
     * @see #getMembershipOracle()
61
     */
62
    private final SimulatorOracle<I, D> simulatorOracle;
63

64
    /**
65
     * Constructs a new {@link SimulatorOmegaOracle}.
66
     *
67
     * @param automaton the automaton to simulate.
68
     * @param simulatorOracle the {@link SimulatorOracle} used to answer {@link Query}s.
69
     * @param <A> the automaton type.
70
     */
71
    public <A extends SuffixOutput<I, D> & SimpleDTS<S, I>> SimulatorOmegaOracle(A automaton, SimulatorOracle<I, D> simulatorOracle) {
2✔
72
        this.simpleDTS = automaton;
2✔
73
        this.simulatorOracle = simulatorOracle;
2✔
74
    }
2✔
75

76
    /**
77
     * Gets the {@link SimulatorOracle} used to answer {@link de.learnlib.api.query.Query}s.
78
     *
79
     * @return the SimulatorOracle.
80
     */
81
    @Override
82
    public MembershipOracle<I, D> getMembershipOracle() {
83
        return simulatorOracle;
×
84
    }
85

86
    /**
87
     * Test for state equivalence by simply invoking {@link Object#equals(Object)}.
88
     *
89
     * @see OmegaMembershipOracle#isSameState(Word, Object, Word, Object)
90
     */
91
    @Override
92
    public boolean isSameState(Word<I> input1, S s1, Word<I> input2, S s2) {
93
        return s1.equals(s2);
2✔
94
    }
95

96
    @Override
97
    public void processQueries(Collection<? extends OmegaQuery<I, D>> queries) {
98
        MQUtil.answerOmegaQueries(this, queries);
2✔
99
    }
2✔
100

101
    /**
102
     * Returns an answer for an {@link OmegaQuery}.
103
     * <p>
104
     * The output is obtained through the {@link SimulatorOracle}, while the states are obtained by means of creating
105
     * two access sequences to states in the simulated automaton.
106
     *
107
     * @see OmegaQueryAnswerer#answerQuery(Word, Word, int)
108
     */
109
    @Override
110
    public Pair<@Nullable D, Integer> answerQuery(Word<I> prefix, Word<I> loop, int repeat) {
111
        assert repeat > 0;
2✔
112

113
        final WordBuilder<I> wb = new WordBuilder<>(prefix.length() + loop.length() * repeat, prefix);
2✔
114

115
        S stateIter = simpleDTS.getState(wb);
2✔
116

117
        if (stateIter == null) {
2✔
118
            return Pair.of(null, -1);
×
119
        }
120

121
        final List<S> states = new ArrayList<>(repeat + 1);
2✔
122
        states.add(stateIter);
2✔
123

124
        for (int i = 0; i < repeat; i++) {
2✔
125
            final S nextState = simpleDTS.getSuccessor(stateIter, loop);
2✔
126

127
            if (nextState == null) {
2✔
128
                return Pair.of(null, -1);
×
129
            }
130

131
            wb.append(loop);
2✔
132

133
            int prefixLength = prefix.length();
2✔
134
            for (S s : states) {
2✔
135
                if (isSameState(wb.toWord(0, prefixLength), s, wb.toWord(), nextState)) {
2✔
136
                    return Pair.of(simulatorOracle.answerQuery(wb.toWord()), i + 1);
2✔
137
                }
138
                prefixLength += loop.length();
2✔
139
            }
2✔
140

141
            states.add(nextState);
2✔
142
            stateIter = nextState;
2✔
143
        }
144

145
        return Pair.of(null, -1);
2✔
146
    }
147

148
    public static class DFASimulatorOmegaOracle<S extends Object, I>
149
            extends SimulatorOmegaOracle<S, I, Boolean>
150
            implements SingleQueryOmegaOracleDFA<S, I> {
151

152
        private final DFA<S, I> automaton;
153

154
        public DFASimulatorOmegaOracle(DFA<S, I> automaton) {
155
            super(automaton, new DFASimulatorOracle<>(automaton));
2✔
156
            this.automaton = automaton;
2✔
157
        }
2✔
158

159
        @Override
160
        public DFAMembershipOracle<I> getMembershipOracle() {
161
            return new DFASimulatorOracle<>(automaton);
1✔
162
        }
163
    }
164

165
    public static class MealySimulatorOmegaOracle<S extends Object, I, O>
166
            extends SimulatorOmegaOracle<S, I, Word<O>>
167
            implements SingleQueryOmegaOracleMealy<S, I, O> {
168

169
        private final MealyMachine<?, I, ?, O> automaton;
170

171
        public MealySimulatorOmegaOracle(MealyMachine<S, I, ?, O> automaton) {
172
            super(automaton, new MealySimulatorOracle<>(automaton));
1✔
173
            this.automaton = automaton;
1✔
174
        }
1✔
175

176
        @Override
177
        public MealyMembershipOracle<I, O> getMembershipOracle() {
178
            return new MealySimulatorOracle<>(automaton);
1✔
179
        }
180
    }
181
}
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