• 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

80.0
/oracles/parallelism/src/main/java/de/learnlib/filter/cache/dfa/ThreadSafeDFACaches.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.dfa;
17

18
import java.util.ArrayList;
19
import java.util.Collection;
20
import java.util.List;
21
import java.util.concurrent.locks.ReadWriteLock;
22
import java.util.concurrent.locks.ReentrantReadWriteLock;
23
import java.util.function.Function;
24
import java.util.function.Supplier;
25

26
import de.learnlib.api.oracle.MembershipOracle;
27
import de.learnlib.oracle.parallelism.ParallelOracleBuilders;
28
import net.automatalib.automata.fsa.DFA;
29
import net.automatalib.incremental.dfa.IncrementalDFABuilder;
30
import net.automatalib.incremental.dfa.dag.IncrementalDFADAGBuilder;
31
import net.automatalib.incremental.dfa.dag.IncrementalPCDFADAGBuilder;
32
import net.automatalib.incremental.dfa.tree.IncrementalDFATreeBuilder;
33
import net.automatalib.incremental.dfa.tree.IncrementalPCDFATreeBuilder;
34
import net.automatalib.words.Alphabet;
35

36
/**
37
 * A factory for creating thread-safe caches for {@link DFA}-based {@link MembershipOracle}s. Parameters and return
38
 * types are tailored towards the use with {@link ParallelOracleBuilders}.
39
 */
40
public final class ThreadSafeDFACaches {
41

42
    private ThreadSafeDFACaches() {
43
        // prevent instantiation
44
    }
45

46
    /**
47
     * Enhances a given oracle supplier with a shared, thread-safe cache for a DFA learning setup.
48
     * <p>
49
     * Note that this method does not specify the implementation to use for the cache. Currently, a DAG ({@link
50
     * #createDAGCache(Alphabet, Supplier)}) is used; however, this may change in the future.
51
     *
52
     * @param alphabet
53
     *         the alphabet containing the symbols of possible queries
54
     * @param oracleSupplier
55
     *         the supplier that is used to construct the delegate oracle for the cache, in case of a cache-miss.
56
     * @param <I>
57
     *         input symbol type
58
     *
59
     * @return a supplier for {@link ThreadSafeDFACacheOracle}s.
60
     */
61
    public static <I> Supplier<ThreadSafeDFACacheOracle<I>> createCache(Alphabet<I> alphabet,
62
                                                                        Supplier<? extends MembershipOracle<I, Boolean>> oracleSupplier) {
63
        return createDAGCache(alphabet, oracleSupplier);
2✔
64
    }
65

66
    /**
67
     * Enhances each oracle of a given collection with a shared, thread-safe cache for a DFA learning setup.
68
     * <p>
69
     * Note that this method does not specify the implementation to use for the cache. Currently, a DAG ({@link
70
     * #createDAGCache(Alphabet, Collection)}) is used; however, this may change in the future.
71
     *
72
     * @param alphabet
73
     *         the alphabet containing the symbols of possible queries
74
     * @param oracles
75
     *         the collection of oracles which will be used as delegates (in case of a cache-miss) by the corresponding
76
     *         oracles caches.
77
     * @param <I>
78
     *         input symbol type
79
     *
80
     * @return a collection of {@link ThreadSafeDFACacheOracle}s.
81
     */
82
    public static <I> Collection<ThreadSafeDFACacheOracle<I>> createCache(Alphabet<I> alphabet,
83
                                                                          Collection<? extends MembershipOracle<I, Boolean>> oracles) {
84
        return createDAGCache(alphabet, oracles);
2✔
85
    }
86

87
    /**
88
     * Enhances a given oracle supplier with a shared, thread-safe cache for a DFA learning setup, using a DAG for
89
     * internal cache organization.
90
     *
91
     * @param alphabet
92
     *         the alphabet containing the symbols of possible queries
93
     * @param oracleSupplier
94
     *         the supplier that is used to construct the delegate oracle for the cache, in case of a cache-miss.
95
     * @param <I>
96
     *         input symbol type
97
     *
98
     * @return a supplier for {@link ThreadSafeDFACacheOracle}s.
99
     *
100
     * @see IncrementalDFADAGBuilder
101
     */
102
    public static <I> Supplier<ThreadSafeDFACacheOracle<I>> createDAGCache(Alphabet<I> alphabet,
103
                                                                           Supplier<? extends MembershipOracle<I, Boolean>> oracleSupplier) {
104
        return createSupplierBasedCache(alphabet, oracleSupplier, IncrementalDFADAGBuilder::new);
2✔
105
    }
106

107
    /**
108
     * Enhances each oracle of a given collection with a shared, thread-safe cache for a DFA learning setup, using a DAG
109
     * for internal cache organization.
110
     *
111
     * @param alphabet
112
     *         the alphabet containing the symbols of possible queries
113
     * @param oracles
114
     *         the collection of oracles which will be used as delegates (in case of a cache-miss) by the corresponding
115
     *         oracles caches.
116
     * @param <I>
117
     *         input symbol type
118
     *
119
     * @return a collection of {@link ThreadSafeDFACacheOracle}s.
120
     *
121
     * @see IncrementalDFADAGBuilder
122
     */
123
    public static <I> Collection<ThreadSafeDFACacheOracle<I>> createDAGCache(Alphabet<I> alphabet,
124
                                                                             Collection<? extends MembershipOracle<I, Boolean>> oracles) {
125
        return createCollectionBasedCache(alphabet, oracles, IncrementalDFADAGBuilder::new);
2✔
126
    }
127

128
    /**
129
     * The prefix-closed version of {@link #createDAGCache(Alphabet, Supplier)}.
130
     *
131
     * @param alphabet
132
     *         the alphabet containing the symbols of possible queries
133
     * @param oracleSupplier
134
     *         the supplier that is used to construct the delegate oracle for the cache, in case of a cache-miss.
135
     * @param <I>
136
     *         input symbol type
137
     *
138
     * @return a supplier for {@link ThreadSafeDFACacheOracle}s.
139
     *
140
     * @see IncrementalPCDFADAGBuilder
141
     */
142
    public static <I> Supplier<ThreadSafeDFACacheOracle<I>> createDAGPCCache(Alphabet<I> alphabet,
143
                                                                             Supplier<? extends MembershipOracle<I, Boolean>> oracleSupplier) {
144
        return createSupplierBasedCache(alphabet, oracleSupplier, IncrementalPCDFADAGBuilder::new);
×
145
    }
146

147
    /**
148
     * The prefix-closed version of {@link #createDAGCache(Alphabet, Collection)}.
149
     *
150
     * @param alphabet
151
     *         the alphabet containing the symbols of possible queries
152
     * @param oracles
153
     *         the collection of oracles which will be used as delegates (in case of a cache-miss) by the corresponding
154
     *         oracles caches.
155
     * @param <I>
156
     *         input symbol type
157
     *
158
     * @return a collection of {@link ThreadSafeDFACacheOracle}s.
159
     *
160
     * @see IncrementalPCDFADAGBuilder
161
     */
162
    public static <I> Collection<ThreadSafeDFACacheOracle<I>> createDAGPCCache(Alphabet<I> alphabet,
163
                                                                               Collection<? extends MembershipOracle<I, Boolean>> oracles) {
164
        return createCollectionBasedCache(alphabet, oracles, IncrementalPCDFADAGBuilder::new);
×
165
    }
166

167
    /**
168
     * Enhances a given oracle supplier with a shared, thread-safe cache for a DFA learning setup, using a tree for
169
     * internal cache organization.
170
     *
171
     * @param alphabet
172
     *         the alphabet containing the symbols of possible queries
173
     * @param oracleSupplier
174
     *         the supplier that is used to construct the delegate oracle for the cache, in case of a cache-miss.
175
     * @param <I>
176
     *         input symbol type
177
     *
178
     * @return a supplier for {@link ThreadSafeDFACacheOracle}s.
179
     *
180
     * @see IncrementalDFATreeBuilder
181
     */
182
    public static <I> Supplier<ThreadSafeDFACacheOracle<I>> createTreeCache(Alphabet<I> alphabet,
183
                                                                            Supplier<? extends MembershipOracle<I, Boolean>> oracleSupplier) {
184
        return createSupplierBasedCache(alphabet, oracleSupplier, IncrementalDFATreeBuilder::new);
2✔
185
    }
186

187
    /**
188
     * Enhances each oracle of a given collection with a shared, thread-safe cache for a DFA learning setup, using a
189
     * tree for internal cache organization.
190
     *
191
     * @param alphabet
192
     *         the alphabet containing the symbols of possible queries
193
     * @param oracles
194
     *         the collection of oracles which will be used as delegates (in case of a cache-miss) by the corresponding
195
     *         oracles caches.
196
     * @param <I>
197
     *         input symbol type
198
     *
199
     * @return a collection of {@link ThreadSafeDFACacheOracle}s.
200
     *
201
     * @see IncrementalDFATreeBuilder
202
     */
203
    public static <I> Collection<ThreadSafeDFACacheOracle<I>> createTreeCache(Alphabet<I> alphabet,
204
                                                                              Collection<? extends MembershipOracle<I, Boolean>> oracles) {
205
        return createCollectionBasedCache(alphabet, oracles, IncrementalDFATreeBuilder::new);
2✔
206
    }
207

208
    /**
209
     * Prefix-closed version of {@link #createTreeCache(Alphabet, Supplier)}.
210
     *
211
     * @param alphabet
212
     *         the alphabet containing the symbols of possible queries
213
     * @param oracleSupplier
214
     *         the supplier that is used to construct the delegate oracle for the cache, in case of a cache-miss.
215
     * @param <I>
216
     *         input symbol type
217
     *
218
     * @return a supplier for {@link ThreadSafeDFACacheOracle}s.
219
     *
220
     * @see IncrementalPCDFATreeBuilder
221
     */
222
    public static <I> Supplier<ThreadSafeDFACacheOracle<I>> createTreePCCache(Alphabet<I> alphabet,
223
                                                                              Supplier<? extends MembershipOracle<I, Boolean>> oracleSupplier) {
224
        return createSupplierBasedCache(alphabet, oracleSupplier, IncrementalPCDFATreeBuilder::new);
×
225
    }
226

227
    /**
228
     * Prefix-closed version of {@link #createTreeCache(Alphabet, Collection)}.
229
     *
230
     * @param alphabet
231
     *         the alphabet containing the symbols of possible queries
232
     * @param oracles
233
     *         the collection of oracles which will be used as delegates (in case of a cache-miss) by the corresponding
234
     *         oracles caches.
235
     * @param <I>
236
     *         input symbol type
237
     *
238
     * @return a supplier for {@link ThreadSafeDFACacheOracle}s.
239
     *
240
     * @see IncrementalPCDFATreeBuilder
241
     */
242
    public static <I> Collection<ThreadSafeDFACacheOracle<I>> createTreePCCache(Alphabet<I> alphabet,
243
                                                                                Collection<? extends MembershipOracle<I, Boolean>> oracles) {
244
        return createCollectionBasedCache(alphabet, oracles, IncrementalPCDFATreeBuilder::new);
×
245
    }
246

247
    private static <I> Supplier<ThreadSafeDFACacheOracle<I>> createSupplierBasedCache(Alphabet<I> alphabet,
248
                                                                                      Supplier<? extends MembershipOracle<I, Boolean>> oracleSupplier,
249
                                                                                      Function<? super Alphabet<I>, ? extends IncrementalDFABuilder<I>> builder) {
250
        final ReadWriteLock lock = new ReentrantReadWriteLock();
2✔
251
        final IncrementalDFABuilder<I> incremental = builder.apply(alphabet);
2✔
252
        return () -> new ThreadSafeDFACacheOracle<>(incremental, oracleSupplier.get(), lock);
2✔
253
    }
254

255
    private static <I> Collection<ThreadSafeDFACacheOracle<I>> createCollectionBasedCache(Alphabet<I> alphabet,
256
                                                                                          Collection<? extends MembershipOracle<I, Boolean>> oracles,
257
                                                                                          Function<? super Alphabet<I>, ? extends IncrementalDFABuilder<I>> builder) {
258
        final ReadWriteLock lock = new ReentrantReadWriteLock();
2✔
259
        final IncrementalDFABuilder<I> incremental = builder.apply(alphabet);
2✔
260
        final List<ThreadSafeDFACacheOracle<I>> result = new ArrayList<>(oracles.size());
2✔
261

262
        for (MembershipOracle<I, Boolean> oracle : oracles) {
2✔
263
            result.add(new ThreadSafeDFACacheOracle<>(incremental, oracle, lock));
2✔
264
        }
2✔
265

266
        return result;
2✔
267
    }
268
}
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