• 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

93.33
/oracles/filters/cache/src/main/java/de/learnlib/filter/cache/sul/AbstractSULCache.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.filter.cache.sul;
17

18
import de.learnlib.api.Resumable;
19
import de.learnlib.api.SUL;
20
import de.learnlib.api.oracle.EquivalenceOracle.MealyEquivalenceOracle;
21
import de.learnlib.filter.cache.LearningCache.MealyLearningCache;
22
import de.learnlib.filter.cache.mealy.MealyCacheConsistencyTest;
23
import de.learnlib.filter.cache.sul.AbstractSULCache.SULCacheState;
24
import net.automatalib.SupportsGrowingAlphabet;
25
import net.automatalib.incremental.mealy.IncrementalMealyBuilder;
26
import net.automatalib.ts.output.MealyTransitionSystem;
27
import net.automatalib.words.WordBuilder;
28
import org.checkerframework.checker.nullness.qual.Nullable;
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

32
abstract class AbstractSULCache<I, O, C extends SULCacheState<I, O>>
33
        implements SUL<I, O>, MealyLearningCache<I, O>, SupportsGrowingAlphabet<I>, Resumable<C> {
34

35
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSULCache.class);
2✔
36

37
    private final AbstractSULCacheImpl<?, I, ?, O, C> impl;
38

39
    <S, T> AbstractSULCache(AbstractSULCacheImpl<S, I, T, O, C> cacheImpl) {
2✔
40
        this.impl = cacheImpl;
2✔
41
    }
2✔
42

43
    @Override
44
    public void pre() {
45
        impl.pre();
2✔
46
    }
2✔
47

48
    @Override
49
    public void post() {
50
        impl.post();
2✔
51
    }
2✔
52

53
    @Override
54
    public O step(I in) {
55
        return impl.step(in);
2✔
56
    }
57

58
    @Override
59
    public boolean canFork() {
60
        return impl.canFork();
1✔
61
    }
62

63
    @Override
64
    public SUL<I, O> fork() {
65
        return impl.fork();
1✔
66
    }
67

68
    @Override
69
    public MealyEquivalenceOracle<I, O> createCacheConsistencyTest() {
70
        return impl.createCacheConsistencyTest();
2✔
71
    }
72

73
    @Override
74
    public void addAlphabetSymbol(I symbol) {
75
        impl.addAlphabetSymbol(symbol);
×
76
    }
×
77

78
    @Override
79
    public C suspend() {
80
        return impl.suspend();
2✔
81
    }
82

83
    @Override
84
    public void resume(C state) {
85
        this.impl.resume(state);
2✔
86
    }
2✔
87

88
    public int size() {
89
        return impl.incMealy.asGraph().size();
2✔
90
    }
91

92
    /**
93
     * Implementation class; we need this to bind the {@code T} and {@code S} type parameters of the transition system
94
     * returned by {@link IncrementalMealyBuilder#asTransitionSystem()}.
95
     *
96
     * @param <S>
97
     *         transition system state type
98
     * @param <I>
99
     *         input symbol type
100
     * @param <T>
101
     *         transition system transition type
102
     * @param <O>
103
     *         output symbol type
104
     * @param <C>
105
     *         cache type
106
     */
107
    abstract static class AbstractSULCacheImpl<S, I, T, O, C extends SULCacheState<I, O>>
2✔
108
            implements SUL<I, O>, MealyLearningCache<I, O>, SupportsGrowingAlphabet<I>, Resumable<C> {
109

110
        protected IncrementalMealyBuilder<I, O> incMealy;
111
        protected MealyTransitionSystem<S, I, T, O> mealyTs;
112
        protected final SUL<I, O> delegate;
113

114
        private final WordBuilder<I> inputWord = new WordBuilder<>();
2✔
115
        private final WordBuilder<O> outputWord = new WordBuilder<>();
2✔
116

117
        private boolean delegatePreCalled;
118
        protected @Nullable S current;
119

120
        AbstractSULCacheImpl(IncrementalMealyBuilder<I, O> incMealy,
121
                             MealyTransitionSystem<S, I, T, O> mealyTs,
122
                             SUL<I, O> sul) {
2✔
123
            this.incMealy = incMealy;
2✔
124
            this.mealyTs = mealyTs;
2✔
125
            this.delegate = sul;
2✔
126
        }
2✔
127

128
        @Override
129
        public void pre() {
130
            this.current = mealyTs.getInitialState();
2✔
131
        }
2✔
132

133
        @Override
134
        public O step(I in) {
135
            O out = null;
2✔
136

137
            if (current != null) {
2✔
138
                T trans = mealyTs.getTransition(current, in);
2✔
139

140
                if (trans != null) {
2✔
141
                    out = mealyTs.getTransitionOutput(trans);
2✔
142
                    current = mealyTs.getSuccessor(trans);
2✔
143
                    assert current != null;
2✔
144
                } else {
145
                    requiredInitializedDelegate();
2✔
146
                    current = null;
2✔
147
                    for (I prevSym : inputWord) {
2✔
148
                        outputWord.append(delegate.step(prevSym));
2✔
149
                    }
2✔
150
                }
151
            }
152

153
            inputWord.append(in);
2✔
154

155
            if (current == null) {
2✔
156
                out = delegate.step(in);
2✔
157
                postNewStepHook();
2✔
158
                outputWord.add(out);
2✔
159
            }
160

161
            return out;
2✔
162
        }
163

164
        // TODO: The SUL interface might need a cleanup() method which, by contract,
165
        // is to be called regardless of whether preceding step()s threw unrecoverable
166
        // errors!
167
        @Override
168
        public void post() {
169
            updateCache(inputWord, outputWord);
2✔
170

171
            if (delegatePreCalled) {
2✔
172
                delegate.post();
2✔
173
                delegatePreCalled = false;
2✔
174
            }
175
            inputWord.clear();
2✔
176
            outputWord.clear();
2✔
177
            current = null;
2✔
178
        }
2✔
179

180
        @Override
181
        public MealyEquivalenceOracle<I, O> createCacheConsistencyTest() {
182
            return new MealyCacheConsistencyTest<>(incMealy);
2✔
183
        }
184

185
        @Override
186
        public void addAlphabetSymbol(I symbol) {
187
            incMealy.addAlphabetSymbol(symbol);
×
188
        }
×
189

190
        @Override
191
        @SuppressWarnings("unchecked")
192
        public void resume(C state) {
193
            final Class<?> thisClass = this.incMealy.getClass();
2✔
194
            final Class<?> stateClass = state.builder.getClass();
2✔
195

196
            if (!thisClass.equals(stateClass)) {
2✔
197
                LOGGER.warn(
×
198
                        "You currently plan to use a '{}', but the state contained a '{}'. This may yield unexpected behavior.",
199
                        thisClass,
200
                        stateClass);
201
            }
202

203
            this.incMealy = state.builder;
2✔
204
            this.mealyTs = (MealyTransitionSystem<S, I, T, O>) this.incMealy.asTransitionSystem();
2✔
205
        }
2✔
206

207
        protected void requiredInitializedDelegate() {
208
            if (!delegatePreCalled) {
2✔
209
                delegate.pre();
2✔
210
            }
211
            delegatePreCalled = true;
2✔
212
        }
2✔
213

214
        protected void postNewStepHook() {}
2✔
215

216
        protected void updateCache(WordBuilder<I> inputBuilder, WordBuilder<O> outputBuilder) {
217
            if (!outputBuilder.isEmpty()) {
2✔
218
                incMealy.insert(inputBuilder.toWord(), outputBuilder.toWord());
2✔
219
            }
220
        }
2✔
221
    }
222

223
    public static class SULCacheState<I, O> {
224

225
        final IncrementalMealyBuilder<I, O> builder;
226

227
        SULCacheState(IncrementalMealyBuilder<I, O> builder) {
2✔
228
            this.builder = builder;
2✔
229
        }
2✔
230
    }
231
}
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