$darkmode
DENOPTIM
AttachmentPointQueryTest.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.List;
19import java.util.logging.Logger;
20
21import org.junit.jupiter.api.BeforeEach;
22import org.junit.jupiter.api.Test;
23
24import denoptim.exception.DENOPTIMException;
25import denoptim.graph.Edge.BondType;
26import denoptim.graph.Vertex.BBType;
27
32{
33 private DGraph graph;
34 private Vertex v1;
35 private Vertex v3;
39
40 @BeforeEach
41 public void setUp()
42 {
46 v1Ap0 = v1.getAP(0);
47 v1Ap1 = v1.getAP(1);
48 v3Ap0 = v3.getAP(0);
49 }
50
51//------------------------------------------------------------------------------
52
53 @Test
55 {
56 AttachmentPointQuery query = new AttachmentPointQuery(null, null, null,
57 null, null, null);
59 {
60 assertTrue(query.matches(ap));
61 }
62 }
63
64//------------------------------------------------------------------------------
65
66 @Test
68 {
70 (long) v1Ap0.getID(), null, null, null, null, null);
71 assertTrue(query.matches(v1Ap0));
72 assertFalse(query.matches(v1Ap1));
73 }
74
75//------------------------------------------------------------------------------
76
77 @Test
79 {
80 AttachmentPointQuery query = new AttachmentPointQuery(null, 1, null,
81 null, null, null);
82 assertTrue(query.matches(v1Ap1));
83 assertFalse(query.matches(v1Ap0));
84 }
85
86//------------------------------------------------------------------------------
87
88 @Test
90 {
91 AttachmentPointQuery query = new AttachmentPointQuery(null, null,
92 v3Ap0.getAPClass(), null, null, null);
93 assertTrue(query.matches(v3Ap0));
94 assertFalse(query.matches(v1Ap0));
95 }
96
97//------------------------------------------------------------------------------
98
99 @Test
101 {
102 VertexQuery vertexQuery = new VertexQuery(null, null, BBType.SCAFFOLD,
103 null, null, null, null, null);
104 AttachmentPointQuery query = new AttachmentPointQuery(null, null, null,
105 vertexQuery, null, null);
106
107 assertTrue(query.matches(v1Ap0));
108 assertFalse(query.matches(v3Ap0));
109 }
110
111//------------------------------------------------------------------------------
112
113 @Test
115 {
116 Edge userEdge = v1Ap0.getEdgeUser();
117 EdgeQuery edgeQuery = new EdgeQuery(
118 new VertexQuery(v1.getVertexId(), null, null, null, null, null, null, null),
119 new VertexQuery(graph.getVertexAtPosition(1).getVertexId(), null, null, null, null, null, null, null),
120 new AttachmentPointQuery(null, 0, null, null, null, null),
121 new AttachmentPointQuery(null, 0, null, null, null, null),
123 AttachmentPointQuery query = new AttachmentPointQuery(null, null, null,
124 null, edgeQuery, null);
125
126 assertTrue(query.matches(v1Ap0));
127 assertFalse(query.matches(v1Ap1));
128 assertTrue(edgeQuery.matches(userEdge));
129 }
130
131//------------------------------------------------------------------------------
132
133 @Test
135 {
136 AttachmentPoint usedAp = null;
137 AttachmentPoint unusedAp = null;
139 {
140 if (!ap.isAvailable())
141 {
142 usedAp = ap;
143 }
144 if (ap.isAvailable())
145 {
146 unusedAp = ap;
147 }
148 if (usedAp != null && unusedAp != null)
149 {
150 break;
151 }
152 }
153 EdgeQuery emptyEdgeQuery = new EdgeQuery(null, null, null, null, null);
154 AttachmentPointQuery query = new AttachmentPointQuery(null, null, null,
155 null, emptyEdgeQuery, null);
156
157 assertFalse(query.matches(unusedAp));
158 assertTrue(query.matches(usedAp));
159 }
160
161//------------------------------------------------------------------------------
162
163 @Test
164 public void testLinkedAPQuery()
165 {
166 AttachmentPoint usedAp = null;
167 AttachmentPoint unusedAp = null;
169 {
170 if (!ap.isAvailable())
171 {
172 usedAp = ap;
173 }
174 if (ap.isAvailable())
175 {
176 unusedAp = ap;
177 }
178 if (usedAp != null && unusedAp != null)
179 {
180 break;
181 }
182 }
183 AttachmentPoint linkedAp = usedAp.getLinkedAP();
184
185 AttachmentPointQuery linkedAPQuery = new AttachmentPointQuery(
186 Long.valueOf(linkedAp.getID()), null,
187 linkedAp.getAPClass(),
188 new VertexQuery(linkedAp.getOwner().getVertexId(), null, null, null, null, null, null, null),
189 null, null);
190 AttachmentPointQuery query = new AttachmentPointQuery(null, null, null,
191 null, null, linkedAPQuery);
192
193 assertTrue(query.matches(usedAp));
194 assertFalse(query.matches(unusedAp));
195 assertFalse(query.matches(linkedAp));
196 }
197
198//------------------------------------------------------------------------------
199
200 @Test
202 {
203 VertexQuery vertexQuery = new VertexQuery(null, null, null,
204 null, 0, null, null, null);
205 AttachmentPointQuery query = new AttachmentPointQuery(null, null,
206 APClass.make("B",1),
207 vertexQuery, null, null);
208 Logger logger = Logger.getLogger("test");
209
210 List<AttachmentPoint> fromFind = graph.findAPs(query, logger);
211 List<AttachmentPoint> fromMatches = new ArrayList<>();
213 {
214 if (query.matches(ap))
215 {
216 fromMatches.add(ap);
217 }
218 }
219
220 assertEquals(2, fromFind.size());
221 assertEquals(2, fromMatches.size());
222
223 assertTrue(fromMatches.containsAll(fromFind));
224 assertTrue(fromFind.containsAll(fromMatches));
225 }
226
227//------------------------------------------------------------------------------
228
229}
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
An attachment point (AP) is a possibility to attach a Vertex onto the vertex holding the AP (i....
AttachmentPoint getLinkedAP()
Gets the attachment point (AP) that is connected to this AP via the edge user.
APClass getAPClass()
Returns the Attachment Point class.
int getID()
Returns a unique integer that is used to sort list of attachment points.
Edge getEdgeUser()
Gets the edge that is using this AP, or null if no edge is using this AP.
Query for searching AttachmentPoints.
boolean matches(AttachmentPoint ap)
Tests whether the given attachment point satisfies all non-null criteria in this query.
Unit tests for AttachmentPointQuery#matches(AttachmentPoint).
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:104
ArrayList< AttachmentPoint > getAttachmentPoints()
Returns the list of all attachment points contained in this graph.
Definition: DGraph.java:4596
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:3139
List< List< AttachmentPoint > > findAPs(List< AttachmentPointQuery > apQueries, Logger logger)
Search the graph for AttachmentPoints that match the criteria defined in a list of queries.
Definition: DGraph.java:6718
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
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
AttachmentPoint getAP(int i)
Get attachment point i on this vertex.
Definition: Vertex.java:1007
Query for searching vertices.
Possible chemical bond types an edge can represent.
Definition: Edge.java:305
The type of building block.
Definition: Vertex.java:86