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

LearnLib / learnlib / 6605620268

22 Oct 2023 06:53PM UTC coverage: 93.218% (+0.9%) from 92.296%
6605620268

push

github

mtf90
cleanup passive integration tests

11546 of 12386 relevant lines covered (93.22%)

1.68 hits per line

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

93.33
/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.logging.Category;
21
import de.learnlib.api.oracle.EquivalenceOracle.MealyEquivalenceOracle;
22
import de.learnlib.filter.cache.LearningCache.MealyLearningCache;
23
import de.learnlib.filter.cache.mealy.MealyCacheConsistencyTest;
24
import de.learnlib.filter.cache.sul.AbstractSULCache.SULCacheState;
25
import net.automatalib.SupportsGrowingAlphabet;
26
import net.automatalib.incremental.mealy.IncrementalMealyBuilder;
27
import net.automatalib.ts.output.MealyTransitionSystem;
28
import net.automatalib.word.WordBuilder;
29
import org.checkerframework.checker.nullness.qual.Nullable;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

154
            inputWord.append(in);
2✔
155

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

162
            return out;
2✔
163
        }
164

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

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

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

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

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

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

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

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

215
        protected void postNewStepHook() {}
2✔
216

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

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

226
        final IncrementalMealyBuilder<I, O> builder;
227

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