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

LearnLib / automatalib / 6673214172

27 Oct 2023 11:30PM UTC coverage: 89.166% (-0.6%) from 89.796%
6673214172

push

github

mtf90
cleanup release configuration

15399 of 17270 relevant lines covered (89.17%)

1.67 hits per line

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

0.0
/api/src/main/java/net/automatalib/graph/ads/RecursiveADSNode.java
1
/* Copyright (C) 2013-2023 TU Dortmund
2
 * This file is part of AutomataLib, http://www.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.VisualizationHelper;
28
import org.checkerframework.checker.nullness.qual.NonNull;
29
import org.checkerframework.checker.nullness.qual.Nullable;
30
import org.checkerframework.dataflow.qual.Pure;
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 e.g.
36
 * GraphDOT methods.
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
 *         (hypothesis) 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 {@code null} if {@code this} is a leaf node (see {@link #isLeaf()}), the associated input symbol
56
     * otherwise.
57
     */
58
    @Pure
59
    @Nullable I getSymbol();
60

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

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

81
    void setParent(N parent);
82

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

96
        queue.add(root);
×
97

98
        // level-order iteration of the tree nodes
99
        while (!queue.isEmpty()) {
×
100
            @SuppressWarnings("nullness") // false positive https://github.com/typetools/checker-framework/issues/399
101
            @NonNull final N node = queue.poll();
×
102
            result.add(node);
×
103
            queue.addAll(node.getChildren().values());
×
104
        }
×
105

106
        return Collections.unmodifiableList(result);
×
107
    }
108

109
    /**
110
     * Returns a mapping to the child nodes of {@code this} ADS node.
111
     *
112
     * @return A mapping from hypothesis outputs to child ADS nodes. May be empty/unmodifiable (for leaf nodes), but
113
     * never {@code null}.
114
     */
115
    Map<O, N> getChildren();
116

117
    @Override
118
    default Collection<N> getOutgoingEdges(N node) {
119
        return Collections.unmodifiableCollection(node.getChildren().values());
×
120
    }
121

122
    @Override
123
    default N getTarget(N edge) {
124
        return edge;
×
125
    }
126

127
    // default methods for graph interface
128

129
    @Override
130
    default VisualizationHelper<N, N> getVisualizationHelper() {
131
        return new VisualizationHelper<N, N>() {
×
132

133
            @Override
134
            public boolean getNodeProperties(N node, Map<String, String> properties) {
135
                if (node.isLeaf()) {
×
136
                    properties.put(NodeAttrs.SHAPE, NodeShapes.BOX);
×
137
                    properties.put(NodeAttrs.LABEL, String.valueOf(node.getHypothesisState()));
×
138
                } else {
139
                    properties.put(NodeAttrs.LABEL, node.toString());
×
140
                    properties.put(NodeAttrs.SHAPE, NodeShapes.OVAL);
×
141
                }
142

143
                return true;
×
144
            }
145

146
            @Override
147
            public boolean getEdgeProperties(N src, N edge, N tgt, Map<String, String> properties) {
148

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

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

168
    /**
169
     * Returns the hypothesis state associated with this ADS node.
170
     *
171
     * @return {@code null} if {@code this} is an inner node (see {@link #isLeaf()}), the associated hypothesis state
172
     * otherwise.
173
     */
174
    @Nullable S getHypothesisState();
175

176
    /**
177
     * See {@link #getHypothesisState()}.
178
     *
179
     * @param state
180
     *         the hypothesis state to be associated with this ADS node.
181
     *
182
     * @throws UnsupportedOperationException
183
     *         if trying to set a hypothesis state on an inner node (see {@link #isLeaf()}).
184
     */
185
    void setHypothesisState(S state);
186
}
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