• 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

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.oracle.membership.SimulatorOracle.DFASimulatorOracle;
31
import de.learnlib.oracle.membership.SimulatorOracle.MealySimulatorOracle;
32
import de.learnlib.util.MQUtil;
33
import net.automatalib.automata.concepts.SuffixOutput;
34
import net.automatalib.automata.fsa.DFA;
35
import net.automatalib.automata.transducers.MealyMachine;
36
import net.automatalib.commons.util.Pair;
37
import net.automatalib.ts.simple.SimpleDTS;
38
import net.automatalib.words.Word;
39
import net.automatalib.words.WordBuilder;
40
import org.checkerframework.checker.nullness.qual.Nullable;
41

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

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

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

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

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

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

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

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

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

117
        S stateIter = simpleDTS.getState(wb);
2✔
118

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

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

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

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

133
            wb.append(loop);
2✔
134

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

143
            states.add(nextState);
2✔
144
            stateIter = nextState;
2✔
145
        }
146

147
        return Pair.of(null, -1);
2✔
148
    }
149

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

154
        private final DFA<S, I> automaton;
155

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

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

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

171
        private final MealyMachine<?, I, ?, O> automaton;
172

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

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