$darkmode
DENOPTIM
VertexQueryTest.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2026 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 */
10
11package denoptim.graph;
12
13import static org.junit.jupiter.api.Assertions.assertEquals;
14import static org.junit.jupiter.api.Assertions.assertFalse;
15import static org.junit.jupiter.api.Assertions.assertTrue;
16
17import java.util.ArrayList;
18import java.util.Arrays;
19import java.util.List;
20import java.util.logging.Logger;
21
22import org.junit.jupiter.api.BeforeEach;
23import org.junit.jupiter.api.Test;
24
25import denoptim.exception.DENOPTIMException;
26import denoptim.graph.Vertex.BBType;
27import denoptim.graph.Vertex.VertexType;
28
32public class VertexQueryTest
33{
34 private DGraph graph;
35
36 @BeforeEach
37 public void setUp() throws DENOPTIMException
38 {
40 }
41
42//------------------------------------------------------------------------------
43
44 @Test
46 {
47 VertexQuery query = new VertexQuery(null, null, null, null, null,
48 null, null, null);
49 for (Vertex v : graph.getVertexList())
50 {
51 assertTrue(query.matches(v));
52 }
53 }
54
55//------------------------------------------------------------------------------
56
57 @Test
58 public void testMatchesVertexId()
59 {
61 Vertex v7 = graph.getVertexAtPosition(5); //NB: vertexes are intentionally disordered!
62 VertexQuery query = new VertexQuery(v7.getVertexId(), null, null, null,
63 null, null, null, null);
64 assertTrue(query.matches(v7));
65 assertFalse(query.matches(v2));
66 }
67
68//------------------------------------------------------------------------------
69
70 @Test
72 {
75 Vertex v7 = graph.getVertexAtPosition(5); //NB: vertexes are intentionally disordered!
76 VertexQuery query = new VertexQuery(null, VertexType.EmptyVertex, null,
77 null, null, null, null, null);
78 assertTrue(query.matches(v7));
79 assertFalse(query.matches(v1));
80 assertFalse(query.matches(v2));
81 }
82
83//------------------------------------------------------------------------------
84
85 @Test
87 {
90 Vertex v7 = graph.getVertexAtPosition(5); //NB: vertexes are intentionally disordered!
91 VertexQuery byType = new VertexQuery(null, null, BBType.SCAFFOLD, null, null,
92 null, null, null);
93 assertTrue(byType.matches(v1));
94 assertFalse(byType.matches(v2));
95
96 VertexQuery byId = new VertexQuery(null, null, null, 1, null, null,
97 null, null);
98 assertTrue(byId.matches(v2));
99 assertFalse(byId.matches(v1));
100 }
101
102//------------------------------------------------------------------------------
103
104 @Test
105 public void testMatchesLevel()
106 {
109 Vertex v7 = graph.getVertexAtPosition(5); //NB: vertexes are intentionally disordered!
110 VertexQuery query = new VertexQuery(null, null, null, null, -1,
111 null, null, null);
112 assertTrue(query.matches(v1));
113 assertFalse(query.matches(v2));
114 assertFalse(query.matches(v7));
115 query = new VertexQuery(null, null, null, null, 1,
116 null, null, null);
117 assertTrue(query.matches(v7));
118 assertFalse(query.matches(v1));
119 assertFalse(query.matches(v2));
120 }
121
122//------------------------------------------------------------------------------
123
124 @Test
126 {
130 EdgeQuery incoming = new EdgeQuery(null, null,
131 new AttachmentPointQuery(null, null, APClass.make("B",1), null, null, null),
132 null, null);
133 VertexQuery query = new VertexQuery(null, null, null, null, null,
134 null, incoming, null);
135
136 assertTrue(query.matches(v4));
137 assertFalse(query.matches(v1));
138 assertFalse(query.matches(v2));
139 }
140
141//------------------------------------------------------------------------------
142
143 @Test
145 {
149 EdgeQuery outgoing = new EdgeQuery(null, null,
150 new AttachmentPointQuery(null, null, APClass.make("B",1), null, null, null),
151 null, null);
152 VertexQuery query = new VertexQuery(null, null, null, null, null, null,
153 null, outgoing);
154
155 assertTrue(query.matches(v1));
156 assertTrue(query.matches(v2));
157 assertFalse(query.matches(v4));
158 }
159
160//------------------------------------------------------------------------------
161
162 @Test
164 {
168 EdgeQuery incoming = new EdgeQuery(null, null, null,
169 new AttachmentPointQuery(null, null, APClass.make("B",1), null, null, null),
170 null);
171 EdgeQuery outgoing = new EdgeQuery(null, null,
172 new AttachmentPointQuery(null, 1, APClass.make("B",1), null, null, null),
173 null, null);
174 VertexQuery query = new VertexQuery(null, null, null, null, null,
175 null, incoming, outgoing);
176
177 assertTrue(query.matches(v2));
178 assertFalse(query.matches(v1));
179 assertFalse(query.matches(v4));
180 }
181
182//------------------------------------------------------------------------------
183
184 @Test
186 {
190
191 // At leas one ap with this class
192 AttachmentPointQuery apClB1 = new AttachmentPointQuery(null, null,
193 APClass.make("B",1), null, null, null);
194
195 // More than two attachment points
196 AttachmentPointQuery apIdx = new AttachmentPointQuery(null, 2, null, null, null, null);
197
198 VertexQuery query = new VertexQuery(null, null, null, null, null,
199 Arrays.asList(apClB1, apIdx), null, null);
200
201 assertTrue(query.matches(v1));
202 assertFalse(query.matches(v2));
203 assertFalse(query.matches(v4));
204 }
205
206//------------------------------------------------------------------------------
207
208 @Test
210 {
211 EdgeQuery emptyEdgeQuery = new EdgeQuery();
212 VertexQuery query = new VertexQuery(null, null, null, null,
213 null, null, emptyEdgeQuery, emptyEdgeQuery);
214
215 List<Vertex> matched = new ArrayList<>();
216 for (Vertex v : graph.getVertexList())
217 {
218 if (query.matches(v))
219 {
220 matched.add(v);
221 }
222 }
223 // Should match all non-root and non-leaf vertices
224 assertEquals(2, matched.size());
225 }
226
227//------------------------------------------------------------------------------
228
229 @Test
231 {
232 VertexQuery query = new VertexQuery(null, null, BBType.UNDEFINED,
233 null, 0, null, null, null);
234 Logger logger = Logger.getLogger("test");
235
236 List<Vertex> fromFind = graph.findVertices(query, false, logger);
237 List<Vertex> fromMatches = new ArrayList<>();
238 for (Vertex v : graph.getVertexList())
239 {
240 if (query.matches(v))
241 {
242 fromMatches.add(v);
243 }
244 }
245
246 assertEquals(2, fromFind.size());
247 assertEquals(2, fromMatches.size());
248
249 assertTrue(fromMatches.containsAll(fromFind));
250 assertTrue(fromFind.containsAll(fromMatches));
251 }
252
253//------------------------------------------------------------------------------
254
255}
static APClass make(String ruleAndSubclass)
Creates an APClass if it does not exist already, or returns the reference to the existing instance.
Definition: APClass.java:164
Query for searching AttachmentPoints.
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:104
List< Vertex > findVertices(VertexQuery vrtxQuery, Logger logger)
Filters a list of vertices according to a query.
Definition: DGraph.java:6808
Vertex getVertexAtPosition(int pos)
Returns the vertex that is in the given position of the list of vertices belonging to this graph.
Definition: DGraph.java:3163
List< Vertex > getVertexList()
Returns the list of vertexes without entering Templates.
Definition: DGraph.java:973
Unit test for DENOPTIMGraph.
Definition: DGraphTest.java:72
static DGraph makeTestGraphA2()
Build a graph meant to be used in unit tests.
A query for edges: a list of properties that target edges should possess in order to match this query...
Definition: EdgeQuery.java:28
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
Query for searching vertices.
boolean matches(Vertex v)
Tests whether the given vertex satisfies all non-null criteria in this query.
Unit tests for VertexQuery#matches(Vertex).
The type of building block.
Definition: Vertex.java:86
Flag declaring the type of Vertex implementation.
Definition: Vertex.java:172