• 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

95.83
/api/src/main/java/net/automatalib/graph/ads/RecursiveADSNode.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.graph.ads;
17

18
import java.util.ArrayDeque;
19
import java.util.ArrayList;
20
import java.util.Collection;
21
import java.util.Collections;
22
import java.util.List;
23
import java.util.Map;
24
import java.util.Queue;
25

26
import net.automatalib.graph.Graph;
27
import net.automatalib.visualization.Visualization;
28
import net.automatalib.visualization.VisualizationHelper;
29
import org.checkerframework.checker.nullness.qual.NonNull;
30
import org.checkerframework.checker.nullness.qual.Nullable;
31

32
/**
33
 * An interface representing a node in an adaptive distinguishing sequence (which essentially forms a decision tree).
34
 * <p>
35
 * For convenience, this interface extends the {@link Graph} interface so that an ADS may be passed easily to the
36
 * {@link Visualization} factory.
37
 * <p>
38
 * This is a utility interface with a recursive generic type parameter to allow for better inheritance with this
39
 * recursive data structure. Algorithms may use more simplified sub-interfaces such as {@link ADSNode}.
40
 *
41
 * @param <S>
42
 *         state type
43
 * @param <I>
44
 *         input alphabet type
45
 * @param <O>
46
 *         output alphabet type
47
 * @param <N>
48
 *         the concrete node type
49
 */
50
public interface RecursiveADSNode<S, I, O, N extends RecursiveADSNode<S, I, O, N>> extends Graph<N, N> {
51

52
    /**
53
     * Returns the input symbol associated with this ADS node.
54
     *
55
     * @return the associated input symbol
56
     *
57
     * @throws UnsupportedOperationException
58
     *         if trying to get an input symbol from a leaf node (see {@link #isLeaf()}).
59
     */
60
    I getSymbol();
61

62
    /**
63
     * Sets the input symbol associated with this ADS node.
64
     *
65
     * @param symbol
66
     *         the input symbol to be associated with this ADS node.
67
     *
68
     * @throws UnsupportedOperationException
69
     *         if trying to set an input symbol on a leaf node (see {@link #isLeaf()}).
70
     */
71
    void setSymbol(I symbol);
72

73
    /**
74
     * Returns the parent node of this node.
75
     *
76
     * @return The parent node of this ADS node. May be {@code null}, if this node is the root node of an ADS.
77
     */
78
    @Nullable N getParent();
79

80
    /**
81
     * Sets the parent node of this node.
82
     *
83
     * @param parent
84
     *         the parent node to set
85
     */
86
    void setParent(N parent);
87

88
    /**
89
     * A utility method to collect all nodes of a subtree specified by the given root node. May be used for the
90
     * {@link Graph#getNodes()} implementation where a concrete type for {@link N} is needed.
91
     *
92
     * @param root
93
     *         the node for which all subtree nodes should be collected
94
     *
95
     * @return all nodes in the specified subtree, including the root node itself
96
     */
97
    default Collection<N> getNodesForRoot(N root) {
98
        final List<N> result = new ArrayList<>();
1✔
99
        final Queue<N> queue = new ArrayDeque<>();
1✔
100

101
        queue.add(root);
1✔
102

103
        // level-order iteration of the tree nodes
104
        while (!queue.isEmpty()) {
1✔
105
            @SuppressWarnings("nullness") // false positive https://github.com/typetools/checker-framework/issues/399
106
            final @NonNull N node = queue.poll();
1✔
107
            result.add(node);
1✔
108
            queue.addAll(node.getChildren().values());
1✔
109
        }
1✔
110

111
        return Collections.unmodifiableList(result);
1✔
112
    }
113

114
    /**
115
     * Returns a mapping to the child nodes of this ADS node.
116
     *
117
     * @return A mapping from hypothesis outputs to child ADS nodes. May be empty/unmodifiable for leaf nodes.
118
     */
119
    Map<O, N> getChildren();
120

121
    @Override
122
    default Collection<N> getOutgoingEdges(N node) {
123
        return Collections.unmodifiableCollection(node.getChildren().values());
1✔
124
    }
125

126
    @Override
127
    default N getTarget(N edge) {
128
        return edge;
1✔
129
    }
130

131
    // default methods for graph interface
132

133
    @Override
134
    default VisualizationHelper<N, N> getVisualizationHelper() {
135
        return new VisualizationHelper<N, N>() {
1✔
136

137
            @Override
138
            public boolean getNodeProperties(N node, Map<String, String> properties) {
139
                if (node.isLeaf()) {
1✔
140
                    properties.put(NodeAttrs.SHAPE, NodeShapes.BOX);
1✔
141
                    properties.put(NodeAttrs.LABEL, String.valueOf(node.getState()));
1✔
142
                } else {
143
                    properties.put(NodeAttrs.LABEL, node.toString());
1✔
144
                    properties.put(NodeAttrs.SHAPE, NodeShapes.OVAL);
1✔
145
                }
146

147
                return true;
1✔
148
            }
149

150
            @Override
151
            public boolean getEdgeProperties(N src, N edge, N tgt, Map<String, String> properties) {
152

153
                for (Map.Entry<O, N> e : src.getChildren().entrySet()) {
1✔
154
                    if (e.getValue().equals(tgt)) {
1✔
155
                        properties.put(EdgeAttrs.LABEL, String.valueOf(e.getKey()));
1✔
156
                        return true;
1✔
157
                    }
158
                }
1✔
159
                return true;
×
160
            }
161
        };
162
    }
163

164
    /**
165
     * A utility method indicating whether this node represents a leaf of an ADS (and therefore referencing an automaton
166
     * state) or an inner node (and therefore referencing an input symbol).
167
     *
168
     * @return {@code true} if this node is a leaf of an ADS, {@code false} otherwise.
169
     */
170
    boolean isLeaf();
171

172
    /**
173
     * Returns the automaton state associated with this ADS node.
174
     *
175
     * @return the associated state
176
     *
177
     * @throws UnsupportedOperationException
178
     *         if trying to get a state from a symbol node (see {@link #isLeaf()}).
179
     */
180
    S getState();
181

182
    /**
183
     * Sets the automaton state associated with this ADS node.
184
     *
185
     * @param state
186
     *         the state to be associated with this ADS node.
187
     *
188
     * @throws UnsupportedOperationException
189
     *         if trying to set a state on a symbol node (see {@link #isLeaf()}).
190
     */
191
    void setState(S state);
192
}
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