$darkmode
DENOPTIM
Ring.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 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 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.graph;
20
21import java.lang.reflect.Type;
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.List;
25
26import com.google.gson.JsonElement;
27import com.google.gson.JsonObject;
28import com.google.gson.JsonSerializationContext;
29import com.google.gson.JsonSerializer;
30
31import denoptim.exception.DENOPTIMException;
32import denoptim.graph.Edge.BondType;
33
39public class Ring
40{
44 private List<Vertex> vertices;
45
50
51//------------------------------------------------------------------------------
52
53 public Ring()
54 {
55 vertices = new ArrayList<>();
56 }
57
58//------------------------------------------------------------------------------
59
60 public Ring(List<Vertex> vertices)
61 {
62 this.vertices = vertices;
63 }
64
65//------------------------------------------------------------------------------
66
71 public void addVertex(Vertex v)
72 {
73 vertices.add(v);
74 }
75
76//------------------------------------------------------------------------------
77
84 {
85 return vertices.get(0);
86 }
87
88//------------------------------------------------------------------------------
89
96 {
97 return vertices.get(vertices.size() - 1);
98 }
99
100//------------------------------------------------------------------------------
101
108 {
109 if (i>= vertices.size() || i<0)
110 {
111 return null;
112 }
113 return vertices.get(i);
114 }
115
116//------------------------------------------------------------------------------
117
122 public int indexOf(Vertex v)
123 {
124 return vertices.indexOf(v);
125 }
126
127//------------------------------------------------------------------------------
128
134 public int getSize()
135 {
136 return vertices.size();
137 }
138
139//------------------------------------------------------------------------------
140
147 {
148 return bndTyp;
149 }
150
151//------------------------------------------------------------------------------
152
158 public void setBondType(BondType bndType)
159 {
160 this.bndTyp = bndType;
161 }
162
163//------------------------------------------------------------------------------
164
172 public boolean contains(Vertex v)
173 {
174 return vertices.contains(v);
175 }
176
177//------------------------------------------------------------------------------
178
187 public boolean containsID(int vid)
188 {
189 boolean result = false;
190 for (Vertex v : vertices)
191 {
192 if (v.getVertexId() == vid)
193 {
194 result = true;
195 break;
196 }
197 }
198 return result;
199 }
200
201//------------------------------------------------------------------------------
202
207 @Override
208 public String toString() {
209 return "DENOPTIMRing [vertices=" + vertices + "]";
210 }
211
212//------------------------------------------------------------------------------
213
214 public List<Vertex> getVertices() {
215 return Collections.unmodifiableList(vertices);
216 }
217
218//------------------------------------------------------------------------------
219
220 public static class DENOPTIMRingSerializer
221 implements JsonSerializer<Ring>
222 {
223 @Override
224 public JsonElement serialize(Ring ring, Type typeOfSrc,
225 JsonSerializationContext context)
226 {
227 JsonObject jsonObject = new JsonObject();
228 ArrayList<Long> vertexIDs = new ArrayList<Long>();
229 for (int i=0; i<ring.getSize(); i++)
230 {
231 Vertex v = ring.getVertexAtPosition(i);
232 vertexIDs.add(v.getVertexId());
233 }
234 jsonObject.add("vertices",context.serialize(vertexIDs));
235 jsonObject.add("bndTyp",context.serialize(ring.getBondType()));
236 return jsonObject;
237 }
238 }
239
240//------------------------------------------------------------------------------
241
242 public void removeVertex(Vertex oldVrtx)
243 {
244 int idx = vertices.indexOf(oldVrtx);
245 vertices.remove(idx);
246 }
247
248//------------------------------------------------------------------------------
249
255 public void replaceVertex(Vertex oldVrtx, Vertex newVrtx)
256 {
257 int idx = vertices.indexOf(oldVrtx);
258 vertices.set(idx, newVrtx);
259 }
260
261
262//------------------------------------------------------------------------------
263
275 public boolean insertVertex(int position, Vertex newLink)
276 {
277 if (this.contains(newLink) || position>=vertices.size())
278 return false;
279
280 vertices.add(position,newLink);
281 return true;
282 }
283
284//------------------------------------------------------------------------------
285
298 public boolean insertVertex(Vertex newLink, Vertex vA,
299 Vertex vB)
300 {
301 if (this.contains(newLink) || !this.contains(vA) || !this.contains(vB))
302 return false;
303
304 int idA = vertices.indexOf(vA);
305 int idB = vertices.indexOf(vB);
306 if (idA < idB)
307 {
308 vertices.add(idB,newLink);
309 } else {
310 vertices.add(idA,newLink);
311 }
312 return true;
313 }
314
315//------------------------------------------------------------------------------
316
325 public int getDistance(Vertex v1, Vertex v2) throws DENOPTIMException
326 {
327 if (!contains(v1))
328 throw new DENOPTIMException("Cannot measure distance along ring "
329 + "because " + v1 + " does not belong ring " + this);
330 if (!contains(v2))
331 throw new DENOPTIMException("Cannot measure distance along ring "
332 + "because " + v2 + " does not belong ring " + this);
333
334 int pV1 = vertices.indexOf(v1);
335 int pV2 = vertices.indexOf(v2);
336
337 return Math.max(pV1,pV2) - Math.min(pV1, pV2);
338 }
339
340//------------------------------------------------------------------------------
341
350 {
351 return getCloserTo(vA, vB, vertices.get(0));
352 }
353
354//------------------------------------------------------------------------------
355
364 {
365 return getCloserTo(vA, vB, vertices.get(vertices.size() - 1));
366 }
367
368//------------------------------------------------------------------------------
369
379 Vertex vT)
380 {
381 int dA = -1, dB = -1;
382 try
383 {
384 dA = getDistance(vA, vT);
385 } catch (DENOPTIMException e)
386 {
387 if (contains(vB))
388 return vB;
389 else
390 return null;
391 }
392 try
393 {
394 dB = getDistance(vB, vT);
395 } catch (DENOPTIMException e)
396 {
397 if (contains(vA))
398 return vA;
399 else
400 return null;
401 }
402 if (dA<=dB)
403 return vA;
404 else
405 return vB;
406 }
407
408//------------------------------------------------------------------------------
409
415 public int getPositionOf(Vertex v)
416 {
417 return vertices.indexOf(v);
418 }
419
420//------------------------------------------------------------------------------
421
422}
JsonElement serialize(Ring ring, Type typeOfSrc, JsonSerializationContext context)
Definition: Ring.java:224
This class represents the closure of a ring in a spanning tree.
Definition: Ring.java:40
void replaceVertex(Vertex oldVrtx, Vertex newVrtx)
Replaces a vertex that belong to this ring with a new one.
Definition: Ring.java:255
Vertex getTailVertex()
Definition: Ring.java:95
int getPositionOf(Vertex v)
Definition: Ring.java:415
String toString()
Definition: Ring.java:208
Vertex getCloserToHead(Vertex vA, Vertex vB)
Chooses among the two given vertices the one that is closer to the head vertex.
Definition: Ring.java:349
void addVertex(Vertex v)
Append a DENOPTIMVertex to the list.
Definition: Ring.java:71
boolean containsID(int vid)
Checks whether a given vertex is part of this ring.
Definition: Ring.java:187
Vertex getVertexAtPosition(int i)
Definition: Ring.java:107
void removeVertex(Vertex oldVrtx)
Definition: Ring.java:242
void setBondType(BondType bndType)
Set the bond type (i.e., bond order) of the chord connecting the head and the tail vertices.
Definition: Ring.java:158
boolean contains(Vertex v)
Checks whether a given vertex is part of this ring.
Definition: Ring.java:172
int getDistance(Vertex v1, Vertex v2)
Measures how many edges there are between two edges along the sequence of vertices that defined this ...
Definition: Ring.java:325
BondType getBondType()
Definition: Ring.java:146
List< Vertex > vertices
List of DENOPTIMVertex involved in the ring.
Definition: Ring.java:44
List< Vertex > getVertices()
Definition: Ring.java:214
Vertex getHeadVertex()
Definition: Ring.java:83
int indexOf(Vertex v)
Returns the index of the first occurrence of the specified element in this ring, or -1 if this list d...
Definition: Ring.java:122
Ring(List< Vertex > vertices)
Definition: Ring.java:60
boolean insertVertex(int position, Vertex newLink)
Adds a vertex to the ring, in the given position.
Definition: Ring.java:275
BondType bndTyp
Bond type (i.e., bond order) to be used between head and tail vertices.
Definition: Ring.java:49
Vertex getCloserTo(Vertex vA, Vertex vB, Vertex vT)
Chooses among the two given vertices the one that is closer to the target vertex.
Definition: Ring.java:378
boolean insertVertex(Vertex newLink, Vertex vA, Vertex vB)
Adds a vertex to the ring, and in between two defined vertices.
Definition: Ring.java:298
Vertex getCloserToTail(Vertex vA, Vertex vB)
Chooses among the two given vertices the one that is closer to the tail vertex.
Definition: Ring.java:363
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
Possible chemical bond types an edge can represent.
Definition: Edge.java:303