• 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

97.83
/filters/cache/src/main/java/de/learnlib/filter/cache/dfa/DFACacheOracle.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.HashSet;
21
import java.util.List;
22
import java.util.Set;
23

24
import de.learnlib.api.Resumable;
25
import de.learnlib.api.logging.Category;
26
import de.learnlib.api.oracle.EquivalenceOracle.DFAEquivalenceOracle;
27
import de.learnlib.api.oracle.MembershipOracle;
28
import de.learnlib.api.query.Query;
29
import de.learnlib.filter.cache.LearningCacheOracle.DFALearningCacheOracle;
30
import de.learnlib.filter.cache.dfa.DFACacheOracle.DFACacheOracleState;
31
import net.automatalib.SupportsGrowingAlphabet;
32
import net.automatalib.common.util.Pair;
33
import net.automatalib.incremental.dfa.Acceptance;
34
import net.automatalib.incremental.dfa.IncrementalDFABuilder;
35
import net.automatalib.word.Word;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

39
/**
40
 * DFA cache. This cache is implemented as a membership oracle: upon construction, it is provided with a delegate
41
 * oracle. Queries that can be answered from the cache are answered directly, others are forwarded to the delegate
42
 * oracle. When the delegate oracle has finished processing these remaining queries, the results are incorporated into
43
 * the cache.
44
 * <p>
45
 * <b>Note:</b> this implementation is <b>not</b> thread-safe. If you require a cache that is usable in a parallel
46
 * environment. use the {@code ThreadSafeDFACacheOracle} (or rather the {@code ThreadSafeDFACaches} factory) from the
47
 * {@code learnlib-parallelism} artifact.
48
 *
49
 * @param <I>
50
 *         input symbol class
51
 */
52
public class DFACacheOracle<I>
53
        implements DFALearningCacheOracle<I>, SupportsGrowingAlphabet<I>, Resumable<DFACacheOracleState<I>> {
54

55
    private static final Logger LOGGER = LoggerFactory.getLogger(DFACacheOracle.class);
2✔
56

57
    private IncrementalDFABuilder<I> incDfa;
58
    private final MembershipOracle<I, Boolean> delegate;
59

60
    DFACacheOracle(IncrementalDFABuilder<I> incDfa, MembershipOracle<I, Boolean> delegate) {
2✔
61
        this.incDfa = incDfa;
2✔
62
        this.delegate = delegate;
2✔
63
    }
2✔
64

65
    @Override
66
    public DFAEquivalenceOracle<I> createCacheConsistencyTest() {
67
        return new DFACacheConsistencyTest<>(incDfa);
2✔
68
    }
69

70
    @Override
71
    public void processQueries(Collection<? extends Query<I, Boolean>> queries) {
72
        if (queries.isEmpty()) {
2✔
73
            return;
2✔
74
        }
75

76
        final Pair<Collection<ProxyQuery<I>>, Collection<Query<I, Boolean>>> cacheResult = queryCache(queries);
2✔
77
        final Collection<ProxyQuery<I>> unanswered = cacheResult.getFirst();
2✔
78
        final Collection<Query<I, Boolean>> duplicates = cacheResult.getSecond();
2✔
79

80
        delegate.processQueries(unanswered);
2✔
81
        updateCache(unanswered);
2✔
82

83
        if (!duplicates.isEmpty()) {
2✔
84
            queryCache(duplicates);
2✔
85
        }
86
    }
2✔
87

88
    @Override
89
    public void addAlphabetSymbol(I symbol) {
90
        incDfa.addAlphabetSymbol(symbol);
1✔
91
    }
1✔
92

93
    @Override
94
    public DFACacheOracleState<I> suspend() {
95
        return new DFACacheOracleState<>(incDfa);
2✔
96
    }
97

98
    @Override
99
    public void resume(DFACacheOracleState<I> state) {
100
        final Class<?> thisClass = this.incDfa.getClass();
2✔
101
        final Class<?> stateClass = state.getBuilder().getClass();
2✔
102

103
        if (!thisClass.equals(stateClass)) {
2✔
104
            LOGGER.warn(Category.DATASTRUCTURE,
×
105
                        "You currently plan to use a '{}', but the state contained a '{}'. This may yield unexpected behavior.",
106
                        thisClass,
107
                        stateClass);
108
        }
109

110
        this.incDfa = state.getBuilder();
2✔
111
    }
2✔
112

113
    protected Pair<Collection<ProxyQuery<I>>, Collection<Query<I, Boolean>>> queryCache(Collection<? extends Query<I, Boolean>> queries) {
114
        final List<ProxyQuery<I>> unanswered = new ArrayList<>();
2✔
115
        final List<Query<I, Boolean>> duplicates = new ArrayList<>();
2✔
116
        final Set<Word<I>> cache = new HashSet<>();
2✔
117

118
        for (Query<I, Boolean> q : queries) {
2✔
119
            final Word<I> input = q.getInput();
2✔
120
            final Acceptance acc = incDfa.lookup(input);
2✔
121
            if (acc != Acceptance.DONT_KNOW) {
2✔
122
                q.answer(acc.toBoolean());
2✔
123
            } else {
124
                if (cache.add(input)) { // never seen before
2✔
125
                    unanswered.add(new ProxyQuery<>(q));
2✔
126
                } else {
127
                    duplicates.add(q);
2✔
128
                }
129
            }
130
        }
2✔
131

132
        return Pair.of(unanswered, duplicates);
2✔
133
    }
134

135
    protected void updateCache(Collection<? extends ProxyQuery<I>> queries) {
136
        for (ProxyQuery<I> q : queries) {
2✔
137
            incDfa.insert(q.getInput(), q.getAnswer());
2✔
138
        }
2✔
139
    }
2✔
140

141
    public static class DFACacheOracleState<I> {
142

143
        private final IncrementalDFABuilder<I> builder;
144

145
        DFACacheOracleState(IncrementalDFABuilder<I> builder) {
2✔
146
            this.builder = builder;
2✔
147
        }
2✔
148

149
        IncrementalDFABuilder<I> getBuilder() {
150
            return builder;
2✔
151
        }
152
    }
153
}
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