$darkmode
DENOPTIM
Edge.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.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;
22
23import org.openscience.cdk.interfaces.IBond;
24import org.openscience.cdk.interfaces.IBond.Order;
25
26import com.google.gson.JsonElement;
27import com.google.gson.JsonObject;
28import com.google.gson.JsonSerializationContext;
29import com.google.gson.JsonSerializer;
30
31import denoptim.utils.GeneralUtils;
32
37public class Edge
38{
43
48
53
54
55//------------------------------------------------------------------------------
56
69 this.srcAP = srcAP;
70 this.trgAP = trgAP;
71 this.bondType = bondType;
72 this.srcAP.setUser(this);
73 this.trgAP.setUser(this);
74 }
75
76//------------------------------------------------------------------------------
77
90 }
91
92//------------------------------------------------------------------------------
93
95 {
96 return srcAP;
97 }
98
99//------------------------------------------------------------------------------
100
102 {
103 return srcAP.getEmbeddedAP();
104 }
105
106//------------------------------------------------------------------------------
107
109 {
110 return trgAP.getEmbeddedAP();
111 }
112
113//------------------------------------------------------------------------------
114
116 {
117 return trgAP;
118 }
119
120//------------------------------------------------------------------------------
121
122 public long getSrcVertex()
123 {
124 return srcAP.getOwner().getVertexId();
125 }
126
127//------------------------------------------------------------------------------
128
129 public int getSrcAPID()
130 {
132 }
133
134//------------------------------------------------------------------------------
135
136 public int getTrgAPID()
137 {
139 }
140
141//------------------------------------------------------------------------------
142
143 public long getTrgVertex()
144 {
145 return trgAP.getOwner().getVertexId();
146 }
147
148//------------------------------------------------------------------------------
149
151 {
152 return srcAP.getAPClass();
153 }
154
155//------------------------------------------------------------------------------
156
158 {
159 return trgAP.getAPClass();
160 }
161
162//------------------------------------------------------------------------------
163
165 {
166 return bondType;
167 }
168
169//------------------------------------------------------------------------------
170
180 public boolean sameAs(Edge other, StringBuilder reason)
181 {
182 if (!this.getSrcAP().sameAs(other.getSrcAP(), reason))
183 {
184 reason.append("Different source AP.");
185 return false;
186 }
187 if (!this.getTrgAP().sameAs(other.getTrgAP(), reason))
188 {
189 reason.append("Different target AP.");
190 return false;
191 }
192 if (this.getBondType() != (other.getBondType()))
193 {
194 reason.append("Different bond type ("+this.getBondType()+":"
195 +other.getBondType()+");");
196 return false;
197 }
198 return true;
199 }
200
201//------------------------------------------------------------------------------
202
221 public int compareAsUndirected(Edge other)
222 {
223 Vertex tvA = srcAP.getOwner();
224 Vertex tvB = trgAP.getOwner();
225 Vertex ovA = other.srcAP.getOwner();
226 Vertex ovB = other.trgAP.getOwner();
227
228 String invariantTA = tvA.getBuildingBlockType().toOldInt() +
231
232 String invariantTB = tvB.getBuildingBlockType().toOldInt() +
235
236 String invariantOA = ovA.getBuildingBlockType().toOldInt() +
239
240 String invariantOB = ovB.getBuildingBlockType().toOldInt() +
243
244 String invariantThis = invariantTA + invariantTB;
245 if (invariantTA.compareTo(invariantTB) > 0)
246 invariantThis = invariantTB + invariantTA;
247
248 String invariantOther = invariantOA + invariantOB;
249 if (invariantOA.compareTo(invariantOB) > 0)
250 invariantOther = invariantOB + invariantOA;
251
252 int resultIgnoringBondType = invariantThis.compareTo(invariantOther);
253
254 if (resultIgnoringBondType == 0)
255 {
256 return this.getBondType().compareTo(other.getBondType());
257 } else {
258 return resultIgnoringBondType;
259 }
260 }
261
262//------------------------------------------------------------------------------
263
264 @Override
265 public String toString()
266 {
267 Vertex srcVertex = srcAP.getOwner();
268 int srcAPID = this.getSrcAPID();
269 Vertex trgVertex = trgAP.getOwner();
270 int trgAPID = this.getTrgAPID();
271
272 StringBuilder sb = new StringBuilder(64);
273 sb.append(srcVertex.getVertexId()).append("_")
274 .append(srcAPID).append("_").
275 append(trgVertex.getVertexId()).append("_")
276 .append(trgAPID).append("_").
277 append(bondType.toString());
278 if (srcAP.getAPClass()!=null && trgAP.getAPClass()!=null)
279 {
280 sb.append("_").append(srcAP.getAPClass()).append("_").append(
281 trgAP.getAPClass());
282 }
283
284 return sb.toString();
285 }
286
287//------------------------------------------------------------------------------
288
292 public void flipEdge() {
293 AttachmentPoint newTrgAP = getSrcAP();
294 srcAP = getTrgAP();
295 trgAP = newTrgAP;
296 }
297
298//------------------------------------------------------------------------------
299
303 public enum BondType {
304
305 NONE, UNDEFINED, ANY, SINGLE, DOUBLE, TRIPLE, QUADRUPLE;
306
307 private int valenceUsed = 0;
308
309 private IBond.Order bo = null;
310
311 static {
312 ANY.bo = IBond.Order.SINGLE;
313 SINGLE.bo = IBond.Order.SINGLE;
314 DOUBLE.bo = IBond.Order.DOUBLE;
315 TRIPLE.bo = IBond.Order.TRIPLE;
316 QUADRUPLE.bo = IBond.Order.QUADRUPLE;
317
318 SINGLE.valenceUsed = 1;
319 DOUBLE.valenceUsed = 2;
320 TRIPLE.valenceUsed = 3;
321 QUADRUPLE.valenceUsed = 4;
322 }
323
328 public boolean hasCDKAnalogue() {
329 return (bo != null);
330 }
331
335 public Order getCDKOrder() {
336 return bo;
337 }
338
343 public static BondType parseInt(int i)
344 {
345 switch (i)
346 {
347 case 0:
348 return NONE;
349 case 1:
350 return SINGLE;
351 case 2:
352 return DOUBLE;
353 case 3:
354 return TRIPLE;
355 case 4:
356 return QUADRUPLE;
357 case 8:
358 return ANY;
359 default:
360 return UNDEFINED;
361 }
362 }
363
369 public static BondType parseStr(String string)
370 {
371 for (BondType bt : BondType.values())
372 {
373 if (bt.name().equals(string.trim().toUpperCase()))
374 return bt;
375 }
376 switch (string.trim())
377 {
378 case "1":
379 return SINGLE;
380 case "2":
381 return DOUBLE;
382 case "3":
383 return TRIPLE;
384 case "4":
385 return QUADRUPLE;
386 case "8":
387 return ANY;
388 default:
389 return UNDEFINED;
390 }
391 }
392
396 public int getValence()
397 {
398 return valenceUsed;
399 }
400 }
401
402//------------------------------------------------------------------------------
403
404 public static class DENOPTIMEdgeSerializer
405 implements JsonSerializer<Edge>
406 {
407 @Override
408 public JsonElement serialize(Edge edge, Type typeOfSrc,
409 JsonSerializationContext context)
410 {
411 JsonObject jsonObject = new JsonObject();
412 jsonObject.addProperty("srcAPID", edge.getSrcAP().getID());
413 jsonObject.addProperty("trgAPID", edge.getTrgAP().getID());
414 jsonObject.add("bondType", context.serialize(edge.getBondType()));
415 return jsonObject;
416 }
417 }
418
419//------------------------------------------------------------------------------
420
421}
An attachment point (AP) is a possibility to attach a Vertex onto the vertex holding the AP (i....
AttachmentPoint getEmbeddedAP()
For vertices that are templates this method returns the attachment point that is embedded in the temp...
void setUser(Edge edge)
Sets the reference to the edge that is using this attachment point.
APClass getAPClass()
Returns the Attachment Point class.
int getID()
Returns a unique integer that is used to sort list of attachment points.
JsonElement serialize(Edge edge, Type typeOfSrc, JsonSerializationContext context)
Definition: Edge.java:408
This class represents the edge between two vertices.
Definition: Edge.java:38
boolean sameAs(Edge other, StringBuilder reason)
Compares this and another edge ignoring edge and vertex IDs.
Definition: Edge.java:180
AttachmentPoint getSrcAPThroughout()
Definition: Edge.java:101
int compareAsUndirected(Edge other)
Compares this and another edge ignoring the directionality of both, i.e., as if both edges were undir...
Definition: Edge.java:221
BondType bondType
The bond type associated with the connection between the fragments.
Definition: Edge.java:52
AttachmentPoint getTrgAP()
Definition: Edge.java:115
void flipEdge()
Exchanges source and target vertices and respective APs of this edge.
Definition: Edge.java:292
long getSrcVertex()
Definition: Edge.java:122
AttachmentPoint srcAP
Attachment point at source end.
Definition: Edge.java:42
AttachmentPoint getSrcAP()
Definition: Edge.java:94
APClass getTrgAPClass()
Definition: Edge.java:157
APClass getSrcAPClass()
Definition: Edge.java:150
long getTrgVertex()
Definition: Edge.java:143
String toString()
Definition: Edge.java:265
Edge(AttachmentPoint srcAP, AttachmentPoint trgAP, BondType bondType)
Constructor for an edge that connects two APs.
Definition: Edge.java:67
BondType getBondType()
Definition: Edge.java:164
AttachmentPoint trgAP
Attachment point at target end.
Definition: Edge.java:47
Edge(AttachmentPoint srcAP, AttachmentPoint trgAP)
Constructor for an edge that connects two APs.
Definition: Edge.java:87
AttachmentPoint getTrgAPThroughout()
Definition: Edge.java:108
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
int getBuildingBlockId()
Returns the index of the building block that should correspond to the position of the building block ...
Definition: Vertex.java:284
int getIndexOfAP(AttachmentPoint ap)
Returns the position of the given AP in the list of APs of this vertex.
Definition: Vertex.java:949
Vertex.BBType getBuildingBlockType()
Definition: Vertex.java:298
static String getPaddedString(int count, int number)
returns the padded string with zeroes placed to the left of 'number' up to reach the desired number o...
Possible chemical bond types an edge can represent.
Definition: Edge.java:303
static BondType parseStr(String string)
Definition: Edge.java:369
boolean hasCDKAnalogue()
Checks if it is possible to convert this edge type into a CDK bond.
Definition: Edge.java:328
static BondType parseInt(int i)
Definition: Edge.java:343