$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//TODO. this returns an Index not the ID
130 public int getSrcAPID()
131 {
133 }
134
135//------------------------------------------------------------------------------
136
137//TODO. this returns an Index not the ID
138 public int getTrgAPID()
139 {
141 }
142
143//------------------------------------------------------------------------------
144
145 public long getTrgVertex()
146 {
147 return trgAP.getOwner().getVertexId();
148 }
149
150//------------------------------------------------------------------------------
151
153 {
154 return srcAP.getAPClass();
155 }
156
157//------------------------------------------------------------------------------
158
160 {
161 return trgAP.getAPClass();
162 }
163
164//------------------------------------------------------------------------------
165
167 {
168 return bondType;
169 }
170
171//------------------------------------------------------------------------------
172
182 public boolean sameAs(Edge other, StringBuilder reason)
183 {
184 if (!this.getSrcAP().sameAs(other.getSrcAP(), reason))
185 {
186 reason.append("Different source AP.");
187 return false;
188 }
189 if (!this.getTrgAP().sameAs(other.getTrgAP(), reason))
190 {
191 reason.append("Different target AP.");
192 return false;
193 }
194 if (this.getBondType() != (other.getBondType()))
195 {
196 reason.append("Different bond type ("+this.getBondType()+":"
197 +other.getBondType()+");");
198 return false;
199 }
200 return true;
201 }
202
203//------------------------------------------------------------------------------
204
223 public int compareAsUndirected(Edge other)
224 {
225 Vertex tvA = srcAP.getOwner();
226 Vertex tvB = trgAP.getOwner();
227 Vertex ovA = other.srcAP.getOwner();
228 Vertex ovB = other.trgAP.getOwner();
229
230 String invariantTA = tvA.getBuildingBlockType().toOldInt() +
233
234 String invariantTB = tvB.getBuildingBlockType().toOldInt() +
237
238 String invariantOA = ovA.getBuildingBlockType().toOldInt() +
241
242 String invariantOB = ovB.getBuildingBlockType().toOldInt() +
245
246 String invariantThis = invariantTA + invariantTB;
247 if (invariantTA.compareTo(invariantTB) > 0)
248 invariantThis = invariantTB + invariantTA;
249
250 String invariantOther = invariantOA + invariantOB;
251 if (invariantOA.compareTo(invariantOB) > 0)
252 invariantOther = invariantOB + invariantOA;
253
254 int resultIgnoringBondType = invariantThis.compareTo(invariantOther);
255
256 if (resultIgnoringBondType == 0)
257 {
258 return this.getBondType().compareTo(other.getBondType());
259 } else {
260 return resultIgnoringBondType;
261 }
262 }
263
264//------------------------------------------------------------------------------
265
266 @Override
267 public String toString()
268 {
269 Vertex srcVertex = srcAP.getOwner();
270 int srcAPID = this.getSrcAPID();
271 Vertex trgVertex = trgAP.getOwner();
272 int trgAPID = this.getTrgAPID();
273
274 StringBuilder sb = new StringBuilder(64);
275 sb.append(srcVertex.getVertexId()).append("_")
276 .append(srcAPID).append("_").
277 append(trgVertex.getVertexId()).append("_")
278 .append(trgAPID).append("_").
279 append(bondType.toString());
280 if (srcAP.getAPClass()!=null && trgAP.getAPClass()!=null)
281 {
282 sb.append("_").append(srcAP.getAPClass()).append("_").append(
283 trgAP.getAPClass());
284 }
285
286 return sb.toString();
287 }
288
289//------------------------------------------------------------------------------
290
294 public void flipEdge() {
295 AttachmentPoint newTrgAP = getSrcAP();
296 srcAP = getTrgAP();
297 trgAP = newTrgAP;
298 }
299
300//------------------------------------------------------------------------------
301
305 public enum BondType {
306
307 NONE, UNDEFINED, ANY, SINGLE, DOUBLE, TRIPLE, QUADRUPLE;
308
309 private int valenceUsed = 0;
310
311 private IBond.Order bo = null;
312
313 static {
314 ANY.bo = IBond.Order.SINGLE;
315 SINGLE.bo = IBond.Order.SINGLE;
316 DOUBLE.bo = IBond.Order.DOUBLE;
317 TRIPLE.bo = IBond.Order.TRIPLE;
318 QUADRUPLE.bo = IBond.Order.QUADRUPLE;
319
320 SINGLE.valenceUsed = 1;
321 DOUBLE.valenceUsed = 2;
322 TRIPLE.valenceUsed = 3;
323 QUADRUPLE.valenceUsed = 4;
324 }
325
330 public boolean hasCDKAnalogue() {
331 return (bo != null);
332 }
333
337 public Order getCDKOrder() {
338 return bo;
339 }
340
345 public static BondType parseInt(int i)
346 {
347 switch (i)
348 {
349 case 0:
350 return NONE;
351 case 1:
352 return SINGLE;
353 case 2:
354 return DOUBLE;
355 case 3:
356 return TRIPLE;
357 case 4:
358 return QUADRUPLE;
359 case 8:
360 return ANY;
361 default:
362 return UNDEFINED;
363 }
364 }
365
371 public static BondType parseStr(String string)
372 {
373 for (BondType bt : BondType.values())
374 {
375 if (bt.name().equals(string.trim().toUpperCase()))
376 return bt;
377 }
378 switch (string.trim())
379 {
380 case "1":
381 return SINGLE;
382 case "2":
383 return DOUBLE;
384 case "3":
385 return TRIPLE;
386 case "4":
387 return QUADRUPLE;
388 case "8":
389 return ANY;
390 default:
391 return UNDEFINED;
392 }
393 }
394
398 public int getValence()
399 {
400 return valenceUsed;
401 }
402 }
403
404//------------------------------------------------------------------------------
405
406 public static class DENOPTIMEdgeSerializer
407 implements JsonSerializer<Edge>
408 {
409 @Override
410 public JsonElement serialize(Edge edge, Type typeOfSrc,
411 JsonSerializationContext context)
412 {
413 JsonObject jsonObject = new JsonObject();
414 jsonObject.addProperty("srcAPID", edge.getSrcAP().getID());
415 jsonObject.addProperty("trgAPID", edge.getTrgAP().getID());
416 jsonObject.add("bondType", context.serialize(edge.getBondType()));
417 return jsonObject;
418 }
419 }
420
421//------------------------------------------------------------------------------
422
423}
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:410
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:182
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:223
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:294
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:159
APClass getSrcAPClass()
Definition: Edge.java:152
long getTrgVertex()
Definition: Edge.java:145
String toString()
Definition: Edge.java:267
Edge(AttachmentPoint srcAP, AttachmentPoint trgAP, BondType bondType)
Constructor for an edge that connects two APs.
Definition: Edge.java:67
BondType getBondType()
Definition: Edge.java:166
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:304
int getIndexOfAP(AttachmentPoint ap)
Returns the position of the given AP in the list of APs of this vertex.
Definition: Vertex.java:1036
Vertex.BBType getBuildingBlockType()
Definition: Vertex.java:318
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:305
static BondType parseStr(String string)
Definition: Edge.java:371
boolean hasCDKAnalogue()
Checks if it is possible to convert this edge type into a CDK bond.
Definition: Edge.java:330
static BondType parseInt(int i)
Definition: Edge.java:345