$darkmode
DENOPTIM
GraphUtils.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no> and
4 * Marco Foscato <marco.foscato@uib.no>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20package denoptim.utils;
21
22import java.util.concurrent.atomic.AtomicInteger;
23import java.util.concurrent.atomic.AtomicLong;
24
25import org.openscience.cdk.interfaces.IAtomContainer;
26
27import denoptim.constants.DENOPTIMConstants;
28import denoptim.exception.DENOPTIMException;
29import denoptim.graph.DGraph;
30import denoptim.graph.Vertex;
31
32
39public class GraphUtils
40{
41 public static AtomicLong vertexCounter = new AtomicLong(1);
42 private static AtomicInteger graphCounter = new AtomicInteger(1);
43 private static AtomicInteger molCounter = new AtomicInteger(1);
44
45//------------------------------------------------------------------------------
46
55 public static synchronized void ensureVertexIDConsistency(long l)
57 {
59 {
61 }
62 }
63
64//------------------------------------------------------------------------------
65
77 public static synchronized void resetUniqueVertexCounter(long l)
79 {
80 if (vertexCounter.get() >= l)
81 {
82 String msg = "Attempt to reset the unique vertex ID using "
83 + l + " while the current value is "
84 + vertexCounter.get();
85 throw new DENOPTIMException(msg);
86 }
87 vertexCounter = new AtomicLong(l);
88 }
89
90//-----------------------------------------------------------------------------
91
97 public static synchronized long getUniqueVertexIndex()
98 {
99 if (vertexCounter.get() >= Long.MAX_VALUE-10)
100 throw new Error("Reached maximum value for "
101 + "Vertex identifier. It is highly likely that you should "
102 + "not be in this situation as it means you have generated "
103 + "too many vertices. Contact the authors is you really "
104 + "think you need vertex identifiers bigger than 2^64.");
105 return vertexCounter.getAndIncrement();
106 }
107
108//------------------------------------------------------------------------------
109
121 public static synchronized void resetUniqueGraphCounter(int val)
122 throws DENOPTIMException
123 {
124 if (graphCounter.get() >= val)
125 {
126 String msg = "Attempt to reset the unique graph ID using "
127 + val + " while the current value is "
128 + graphCounter.get();
129 throw new DENOPTIMException(msg);
130 }
131 graphCounter = new AtomicInteger(val);
132 }
133
134//------------------------------------------------------------------------------
135
142 public static synchronized int getUniqueGraphIndex()
143 {
144 if (graphCounter.get() >= Integer.MAX_VALUE-10)
145 throw new Error("Reached maximum value for "
146 + "Graph identifier. Please contact the authors to "
147 + "request use of 'long' IDs.");
148 return graphCounter.getAndIncrement();
149 }
150
151//------------------------------------------------------------------------------
152
164 public static synchronized void resetUniqueMoleculeCounter(int val)
165 throws DENOPTIMException
166 {
167 if (molCounter.get() >= val)
168 {
169 String msg = "Attempt to reser the unique mol ID using "
170 + val + " while the current value is "
171 + molCounter.get();
172 throw new DENOPTIMException(msg);
173 }
174 molCounter = new AtomicInteger(val);
175 }
176
177//------------------------------------------------------------------------------
178
184 public static synchronized int getUniqueMoleculeIndex()
185 {
186 if (molCounter.get() >= Integer.MAX_VALUE-10)
187 throw new Error("Reached maximum value for "
188 + "Molecule identifier. Please contact the authors to "
189 + "request use of 'long' IDs.");
190 return molCounter.getAndIncrement();
191 }
192
193//------------------------------------------------------------------------------
194
195 public static void writeSDFFields(IAtomContainer iac, DGraph g)
196 throws DENOPTIMException
197 {
198 iac.setProperty(DENOPTIMConstants.GCODETAG, g.getGraphId());
199 iac.setProperty(DENOPTIMConstants.GRAPHTAG, g.toString());
200 iac.setProperty(DENOPTIMConstants.GRAPHJSONTAG, g.toJson());
201 if (g.getLocalMsg() != null
202 && !g.getLocalMsg().toString().equals(""))
203 {
204 iac.setProperty(DENOPTIMConstants.PROVENANCE,g.getLocalMsg());
205 }
206 }
207
208//------------------------------------------------------------------------------
209
210 public static String getLabel(Vertex v)
211 {
212 if (!v.hasProperty("Label"))
213 return "";
214 return v.getGraphOwner().getGraphId()+"@"+v.getProperty(
215 "Label").toString();
216 }
217
218//------------------------------------------------------------------------------
219
220 public static String getLabel(DGraph g, int vIdx)
221 {
222 if (!g.getVertexAtPosition(vIdx).hasProperty("Label"))
223 return "";
224 return g.getGraphId() + "@" + g.getVertexAtPosition(vIdx).getProperty(
225 "Label").toString();
226 }
227
228//------------------------------------------------------------------------------
229
230}
General set of constants used in DENOPTIM.
static final String GRAPHTAG
SDF tag containing graph encoding.
static final String PROVENANCE
SDF tag containing provenance data for a graph.
static final String GCODETAG
SDF tag containing graph ID.
static final String GRAPHJSONTAG
SDF tag containing graph encoding in JSON format.
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:102
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:2514
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
DGraph getGraphOwner()
Returns the graph this vertex belongs to or null.
Definition: Vertex.java:779
Object getProperty(Object property)
Definition: Vertex.java:1136
boolean hasProperty(Object property)
Definition: Vertex.java:1126
Utilities for graphs.
Definition: GraphUtils.java:40
static AtomicLong vertexCounter
Definition: GraphUtils.java:41
static synchronized void resetUniqueGraphCounter(int val)
Reset the unique graph counter to the given value.
static AtomicInteger molCounter
Definition: GraphUtils.java:43
static synchronized void ensureVertexIDConsistency(long l)
Method used to ensure consistency between internal atomic integer and vertex id from imported graphs.
Definition: GraphUtils.java:55
static synchronized long getUniqueVertexIndex()
Unique counter for the number of graph vertices generated.
Definition: GraphUtils.java:97
static String getLabel(Vertex v)
static void writeSDFFields(IAtomContainer iac, DGraph g)
static synchronized void resetUniqueMoleculeCounter(int val)
Reset the unique mol counter to the given value.
static synchronized int getUniqueMoleculeIndex()
Unique counter for the number of molecules generated.
static synchronized void resetUniqueVertexCounter(long l)
Reset the unique vertex counter to the given value.
Definition: GraphUtils.java:77
static String getLabel(DGraph g, int vIdx)
static synchronized int getUniqueGraphIndex()
Unique counter for the number of graphs generated.
static AtomicInteger graphCounter
Definition: GraphUtils.java:42