$darkmode
DENOPTIM
EdgeQueryTest.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 org.junit.jupiter.api.BeforeEach;
18import org.junit.jupiter.api.Test;
19
20import denoptim.exception.DENOPTIMException;
21import denoptim.graph.Edge.BondType;
22
26public class EdgeQueryTest
27{
28 private DGraph graph;
29
30 @BeforeEach
31 public void setUp() throws DENOPTIMException
32 {
34 }
35
36//------------------------------------------------------------------------------
37
38 @Test
39 public void testMatchesNullEdge()
40 {
41 EdgeQuery query = new EdgeQuery(null, null, null, null, null);
42 assertFalse(query.matches(null));
43 }
44
45//------------------------------------------------------------------------------
46
47 @Test
49 {
50 EdgeQuery query = new EdgeQuery(null, null, null, null, null);
53 assertTrue(query.matches(edgeV0V1));
54 assertTrue(query.matches(edgeEV4EV5));
55 }
56
57//------------------------------------------------------------------------------
58
59 @Test
61 {
64 Vertex v7 = graph.getVertexAtPosition(5); //NB: vertexes are intentionally disordered!
65 Vertex v5 = graph.getVertexAtPosition(6); //NB: vertexes are intentionally disordered!
66 Edge edgeV0V1 = v2.getEdgeToParent();
67 Edge edgeEV5EV7 = v7.getEdgeToParent();
68
69 EdgeQuery match = new EdgeQuery(
70 new VertexQuery(v1.getVertexId(), null, null, null, null, null, null, null),
71 new VertexQuery(v2.getVertexId(), null, null, null, null, null, null, null),
72 null, null, null);
73 assertTrue(match.matches(edgeV0V1));
74
75 EdgeQuery matchSrcOnly = new EdgeQuery(new VertexQuery(v5.getVertexId(), null, null, null, null, null, null, null),
76 null, null, null, null);
77 assertTrue(matchSrcOnly.matches(edgeEV5EV7));
78
79 EdgeQuery matchTrgOnly = new EdgeQuery(null, new VertexQuery(v7.getVertexId(), null, null, null, null, null, null, null),
80 null, null, null);
81 assertTrue(matchTrgOnly.matches(edgeEV5EV7));
82
83 EdgeQuery wrongTarget = new EdgeQuery(new VertexQuery(v1.getVertexId(), null, null, null, null, null, null, null),
84 new VertexQuery(v7.getVertexId(), null, null, null, null, null, null, null),
85 null, null, null);
86 assertFalse(wrongTarget.matches(edgeEV5EV7));
87 }
88
89//------------------------------------------------------------------------------
90
91 @Test
93 {
96 Vertex v5 = graph.getVertexAtPosition(6); //NB: vertexes are intentionally disordered!
97 Edge edgeV1V2 = v2.getEdgeToParent(); // ap#0 to ap#0
98 Edge edgeV1V3 = v3.getEdgeToParent(); // ap#1 to ap#0
99 Edge edgeV1V5 = v5.getEdgeToParent(); // ap#2 to ap#1
100
101 AttachmentPointQuery ap0 = new AttachmentPointQuery(null, 0, null, null, null, null);
102 AttachmentPointQuery ap1 = new AttachmentPointQuery(null, 1, null, null, null, null);
103 AttachmentPointQuery ap2 = new AttachmentPointQuery(null, 2, null, null, null, null);
104 EdgeQuery match = new EdgeQuery(null, null, ap0, null, null);
105 assertTrue(match.matches(edgeV1V2));
106 assertFalse(match.matches(edgeV1V5));
107 match = new EdgeQuery(null, null, ap0, null, null);
108 assertTrue(match.matches(edgeV1V2));
109 assertFalse(match.matches(edgeV1V5));
110 match = new EdgeQuery(null, null, ap1, null, null);
111 assertTrue(match.matches(edgeV1V3));
112 assertFalse(match.matches(edgeV1V5));
113 match = new EdgeQuery(null, null, null, ap1, null);
114 assertTrue(match.matches(edgeV1V5));
115 assertFalse(match.matches(edgeV1V3));
116 match = new EdgeQuery(null, null, ap2, ap1, null);
117 assertTrue(match.matches(edgeV1V5));
118 assertFalse(match.matches(edgeV1V3));
119 }
120
121//------------------------------------------------------------------------------
122
123 @Test
125 {
128 Edge edgeV1V2 = v2.getEdgeToParent(); // triple
129 Edge edgeV1V3 = v3.getEdgeToParent(); // single
130 EdgeQuery triple = new EdgeQuery(null, null, null, null,
132 EdgeQuery single = new EdgeQuery(null, null, null, null,
134
135 assertTrue(triple.matches(edgeV1V2));
136 assertFalse(triple.matches(edgeV1V3));
137 assertTrue(single.matches(edgeV1V3));
138 assertFalse(single.matches(edgeV1V2));
139 }
140
141//------------------------------------------------------------------------------
142
143 @Test
145 {
148 Edge edgeV1V2 = v2.getEdgeToParent(); // A:0 -> B:1
149 Edge edgeV1V3 = v3.getEdgeToParent(); // B:1 -> C:0
150 EdgeQuery srcClass = new EdgeQuery(null, null,
151 new AttachmentPointQuery(null, null, APClass.make("A",0), null, null, null),
152 null, null);
153 EdgeQuery trgClass = new EdgeQuery(null, null, null,
154 new AttachmentPointQuery(null, null, APClass.make("B",1), null, null, null), null);
155
156 assertTrue(srcClass.matches(edgeV1V2));
157 assertTrue(trgClass.matches(edgeV1V2));
158 assertFalse(trgClass.matches(edgeV1V3));
159 }
160
161//------------------------------------------------------------------------------
162
163 @Test
165 {
169 Edge edgeV1V2 = v2.getEdgeToParent(); // ap#0 to ap#0, A:0 -> B:1, triple
170 Edge edgeV1V3 = v3.getEdgeToParent(); // ap#1 to ap#0, B:1 -> C:0, single
171 EdgeQuery query = new EdgeQuery(
172 new VertexQuery(v1.getVertexId(), null, null, null, null, null, null, null),
173 new VertexQuery(v2.getVertexId(), null, null, null, null, null, null, null),
174 new AttachmentPointQuery(null, 0, APClass.make("A",0), null, null, null),
175 new AttachmentPointQuery(null, 0, APClass.make("B",1), null, null, null),
177 assertTrue(query.matches(edgeV1V2));
178 assertFalse(query.matches(edgeV1V3));
179 EdgeQuery almost = new EdgeQuery(
180 new VertexQuery(v1.getVertexId(), null, null, null, null, null, null, null),
181 new VertexQuery(v2.getVertexId(), null, null, null, null, null, null, null),
182 new AttachmentPointQuery(null, 0, APClass.make("A",0), null, null, null),
183 new AttachmentPointQuery(null, 0, APClass.make("B",1), null, null, null),
185 assertFalse(almost.matches(edgeV1V2));
186 assertFalse(query.matches(edgeV1V3));
187 }
188
189//------------------------------------------------------------------------------
190
191 @Test
193 {
194 EdgeQuery query = new EdgeQuery(null, null, null,
195 new AttachmentPointQuery(null, 0, null, null, null, null),
196 null);
197
198 int directMatches = 0;
199 for (Edge e : graph.getEdgeList())
200 {
201 if (query.matches(e))
202 {
203 directMatches++;
204 }
205 }
206
207 assertEquals(5, directMatches);
208 assertEquals(directMatches,
209 graph.findEdges(query,
210 java.util.logging.Logger.getLogger("test")).size());
211 }
212
213//------------------------------------------------------------------------------
214
215 @Test
217 {
220 Edge edgeV1V2 = v2.getEdgeToParent();
221
222 VertexQuery srcVertex = new VertexQuery(v1.getVertexId(), null, null,
223 null, null, null, null, null);
224 VertexQuery trgVertex = new VertexQuery(v2.getVertexId(), null, null,
225 null, null, null, null, null);
226 AttachmentPointQuery srcAp = new AttachmentPointQuery(null, 0,
227 APClass.make("A", 0), null, null, null);
228 AttachmentPointQuery trgAp = new AttachmentPointQuery(null, 0,
229 APClass.make("B", 1), null, null, null);
230
231 EdgeQuery query = new EdgeQuery(srcVertex, trgVertex, srcAp, trgAp,
233 assertTrue(query.matches(edgeV1V2));
234 assertFalse(query.matches(graph.getVertexAtPosition(2).getEdgeToParent()));
235 }
236
237//------------------------------------------------------------------------------
238}
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
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< Edge > findEdges(EdgeQuery edgeQuery, Logger logger)
Search this graph for edges that match the criteria defined in a query.
Definition: DGraph.java:6859
List< Edge > getEdgeList()
Definition: DGraph.java:1020
Unit test for DENOPTIMGraph.
Definition: DGraphTest.java:72
static DGraph makeTestGraphA2()
Build a graph meant to be used in unit tests.
This class represents the edge between two vertices.
Definition: Edge.java:38
A query for edges: a list of properties that target edges should possess in order to match this query...
Definition: EdgeQuery.java:28
boolean matches(Edge e)
Tests whether the given edge satisfies this query.
Definition: EdgeQuery.java:97
Unit tests for EdgeQuery#matches(Edge).
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
Edge getEdgeToParent()
Looks into the edges that use any of the APs that belong to this vertex and returns the edge that has...
Definition: Vertex.java:1059
Query for searching vertices.
Possible chemical bond types an edge can represent.
Definition: Edge.java:305