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

LearnLib / automatalib / 13138848026

04 Feb 2025 02:53PM UTC coverage: 92.108% (+2.2%) from 89.877%
13138848026

push

github

mtf90
[maven-release-plugin] prepare release automatalib-0.12.0

16609 of 18032 relevant lines covered (92.11%)

1.7 hits per line

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

93.02
/util/src/main/java/net/automatalib/util/minimizer/Block.java
1
/* Copyright (C) 2013-2025 TU Dortmund University
2
 * This file is part of AutomataLib <https://automatalib.net>.
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 net.automatalib.util.minimizer;
17

18
import java.util.ArrayList;
19
import java.util.List;
20

21
import net.automatalib.common.smartcollection.AbstractBasicLinkedListEntry;
22
import net.automatalib.common.smartcollection.ElementReference;
23
import net.automatalib.common.smartcollection.IntrusiveLinkedList;
24
import net.automatalib.common.smartcollection.UnorderedCollection;
25
import org.checkerframework.checker.nullness.qual.Nullable;
26

27
/**
28
 * A block in the partition calculated during minimization.
29
 * <p>
30
 * At the end of the minimization process, all states in the same block may be identified.
31
 *
32
 * @param <S>
33
 *         state class.
34
 * @param <L>
35
 *         transition label class.
36
 */
37
public final class Block<S, L> extends AbstractBasicLinkedListEntry<Block<S, L>, Block<S, L>> {
38

39
    // The states contained in this block
40
    private final UnorderedCollection<State<S, L>> states;
41
    private final int id;
42
    // The references for both the partition and splitter collection.
43
    private @Nullable ElementReference partitionReference, splitterQueueReference;
44
    // The bucket of this block, used for initially arranging the
45
    // states ordered by their respective blocks during the weak sort.
46
    private final IntrusiveLinkedList<State<S, L>> bucket = new IntrusiveLinkedList<>();
2✔
47
    // The sub blocks, i.e., the new blocks that result from
48
    // splitting this block.
49
    private final List<UnorderedCollection<State<S, L>>> subBlocks = new ArrayList<>();
2✔
50
    // The total number of elements in all sub blocks, this is used
51
    // to detect whether an actual split has to be performed.
52
    private int elementsInSubBlocks;
53
    // The sub block currently being created.
54
    private UnorderedCollection<State<S, L>> currSubBlock;
55

56
    /**
57
     * Constructor.
58
     */
59
    Block(int id) {
60
        this(id, new UnorderedCollection<>());
2✔
61
    }
2✔
62

63
    /**
64
     * Constructor.
65
     *
66
     * @param states
67
     *         creates a block for the given collection of states. Ownership of this collection is assumed.
68
     */
69
    Block(int id, UnorderedCollection<State<S, L>> states) {
2✔
70
        this.id = id;
2✔
71
        this.states = states;
2✔
72
    }
2✔
73

74
    /**
75
     * Adds a state to this block.
76
     *
77
     * @param state
78
     *         the state to add.
79
     */
80
    void addState(State<S, L> state) {
81
        ElementReference ref = states.referencedAdd(state);
2✔
82
        state.setBlockReference(ref);
2✔
83
        state.setBlock(this);
2✔
84
    }
2✔
85

86
    /**
87
     * Retrieves the collection of states in this block.
88
     *
89
     * @return the states in this block.
90
     */
91
    UnorderedCollection<State<S, L>> getStates() {
92
        return states;
2✔
93
    }
94

95
    /**
96
     * Retrieves the bucket of this block.
97
     *
98
     * @return this blocks bucket.
99
     */
100
    IntrusiveLinkedList<State<S, L>> getBucket() {
101
        return bucket;
2✔
102
    }
103

104
    /**
105
     * Adds a state to this blocks bucket.
106
     *
107
     * @param state
108
     *         the state to add.
109
     *
110
     * @return {@code true} iff this was the first state to be added to the bucket, {@code false} otherwise.
111
     */
112
    boolean addToBucket(State<S, L> state) {
113
        boolean first = bucket.isEmpty();
2✔
114
        bucket.pushBack(state);
2✔
115
        return first;
2✔
116
    }
117

118
    /**
119
     * Initializes a new sub block.
120
     */
121
    void createSubBlock() {
122
        currSubBlock = new UnorderedCollection<>();
2✔
123
        subBlocks.add(currSubBlock);
2✔
124
    }
2✔
125

126
    /**
127
     * Adds a state to the current sub block.
128
     *
129
     * @param state
130
     *         the state to be added.
131
     */
132
    void addToSubBlock(State<S, L> state) {
133
        if (currSubBlock == null) {
2✔
134
            throw new IllegalStateException("No current sub block");
×
135
        }
136
        currSubBlock.referencedAdd(state);
2✔
137
        elementsInSubBlocks++;
2✔
138
    }
2✔
139

140
    /**
141
     * Retrieves the size of this block, i.e., the number of states it contains.
142
     *
143
     * @return the size of this block.
144
     */
145
    public int size() {
146
        return states.size();
2✔
147
    }
148

149
    /**
150
     * Retrieves the number of elements contained in sub blocks.
151
     *
152
     * @return the number of elements in sub blocks.
153
     */
154
    int getElementsInSubBlocks() {
155
        return elementsInSubBlocks;
2✔
156
    }
157

158
    /**
159
     * Retrieves the {@link ElementReference} that references this block in the partition collection, for efficient
160
     * removal.
161
     *
162
     * @return the reference.
163
     */
164
    @Nullable ElementReference getPartitionReference() {
165
        return partitionReference;
×
166
    }
167

168
    /**
169
     * Sets the partition reference.
170
     *
171
     * @param partitionReference
172
     *         the reference.
173
     */
174
    void setPartitionReference(@Nullable ElementReference partitionReference) {
175
        this.partitionReference = partitionReference;
2✔
176
    }
2✔
177

178
    /**
179
     * Retrieves the {@link ElementReference} referencing this block in the splitter collection, for efficient removal.
180
     * If this block is no potential splitter, {@code null} is returned.
181
     *
182
     * @return the reference or {@code null}.
183
     */
184
    @Nullable ElementReference getSplitterQueueReference() {
185
        return splitterQueueReference;
2✔
186
    }
187

188
    /**
189
     * Sets the splitter queue reference.
190
     *
191
     * @param splitterQueueReference
192
     *         the reference
193
     */
194
    void setSplitterQueueReference(@Nullable ElementReference splitterQueueReference) {
195
        this.splitterQueueReference = splitterQueueReference;
2✔
196
    }
2✔
197

198
    /**
199
     * Removes a state from this block.
200
     *
201
     * @param ref
202
     *         the reference for this state.
203
     */
204
    void removeState(ElementReference ref) {
205
        states.remove(ref);
2✔
206
    }
2✔
207

208
    /**
209
     * Retrieves the list of sub blocks, if any.
210
     *
211
     * @return the sub blocks.
212
     */
213
    List<UnorderedCollection<State<S, L>>> getSubBlocks() {
214
        return subBlocks;
2✔
215
    }
216

217
    /**
218
     * Resets all sub block information.
219
     */
220
    void clearSubBlocks() {
221
        subBlocks.clear();
2✔
222
        elementsInSubBlocks = 0;
2✔
223
    }
2✔
224

225
    /**
226
     * Checks whether this block is empty, i.e., contains no states.
227
     *
228
     * @return {@code true} iff the block is empty, {@code false} otherwise.
229
     */
230
    public boolean isEmpty() {
231
        return states.isEmpty();
2✔
232
    }
233

234
    /**
235
     * Checks whether this block is a singleton, i.e., contains only a single state.
236
     *
237
     * @return {@code true} iff this block is a singleton, {@code false} otherwise.
238
     */
239
    public boolean isSingleton() {
240
        return states.size() == 1;
×
241
    }
242

243
    /**
244
     * Retrieves the ID of this block.
245
     *
246
     * @return the id of this block.
247
     */
248
    public int getId() {
249
        return id;
2✔
250
    }
251

252
    @Override
253
    public Block<S, L> getElement() {
254
        return this;
2✔
255
    }
256
}
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