$darkmode
DENOPTIM
VertexTest.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 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 static org.junit.jupiter.api.Assertions.assertEquals;
22import static org.junit.jupiter.api.Assertions.assertFalse;
23import static org.junit.jupiter.api.Assertions.assertNotEquals;
24import static org.junit.jupiter.api.Assertions.assertTrue;
25
26import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.List;
29
30import javax.vecmath.Point3d;
31
32import org.junit.jupiter.api.Test;
33import org.openscience.cdk.Atom;
34import org.openscience.cdk.silent.Bond;
35
36import denoptim.constants.DENOPTIMConstants;
37import denoptim.fragspace.FragmentSpace;
38import denoptim.graph.Vertex.BBType;
39import denoptim.graph.Vertex.VertexType;
40import denoptim.utils.MutationType;
41
48public class VertexTest
49{
50 private StringBuilder reason = new StringBuilder();
51
52//------------------------------------------------------------------------------
53
54 @Test
55 public void testFromToJSON_minimal() throws Exception
56 {
57 EmptyVertex ev = new EmptyVertex();
58 assertEquals(VertexType.EmptyVertex,ev.vertexType);
59 String evStr = ev.toJson();
60 Vertex ev2 = Vertex.fromJson(evStr);
61 assertTrue(ev2 instanceof EmptyVertex);
62
63 Fragment f = new Fragment();
65 String fStr = f.toJson();
66 Vertex f2 = Vertex.fromJson(fStr);
67 assertTrue(f2 instanceof Fragment);
68
70 assertEquals(VertexType.Template,t.vertexType);
71 String tStr = t.toJson();
72 Vertex t2 = Vertex.fromJson(tStr);
73 assertTrue(t2 instanceof Template);
74 }
75
76//------------------------------------------------------------------------------
77
78 @Test
79 public void testFromToJSON_withSymmetricAPs() throws Exception
80 {
81 EmptyVertex ev = new EmptyVertex();
82 ev.addAP(APClass.make("TT:1"));
83 ev.addAP(APClass.make("TT:0"));
84 ev.addAP(APClass.make("TT:0"));
85 ev.addAP(APClass.make("TT:1"));
86 ev.addAP(APClass.make("TT:2"));
87 SymmetricAPs ss1 = new SymmetricAPs();
88 ss1.add(ev.getAP(0));
89 ss1.add(ev.getAP(3));
90 SymmetricAPs ss2 = new SymmetricAPs();
91 ss2.add(ev.getAP(2));
92 ss2.add(ev.getAP(1));
93 ev.addSymmetricAPSet(ss1);
94 ev.addSymmetricAPSet(ss2);
95 String evStr = ev.toJson();
96 Vertex ev2 = Vertex.fromJson(evStr);
97 assertTrue(ev2 instanceof EmptyVertex);
98 assertEquals(2, ev2.getSymmetricAPSets().size());
99 SymmetricAPs rebuilt1 = ev2.getSymmetricAPSets().get(0);
100 SymmetricAPs rebuilt2 = ev2.getSymmetricAPSets().get(1);
101 assertTrue(rebuilt1.contains(ev2.getAP(3)));
102 assertTrue(rebuilt1.contains(ev2.getAP(0)));
103 assertTrue(rebuilt2.contains(ev2.getAP(1)));
104 assertTrue(rebuilt2.contains(ev2.getAP(2)));
105 }
106
107//------------------------------------------------------------------------------
108
109 @Test
110 public void testSameAs_Equal()
111 {
112 EmptyVertex vA = new EmptyVertex(0);
113 EmptyVertex vB = new EmptyVertex(90);
114 vA.addAP();
115 vA.addAP();
116 vA.addAP();
117 vA.addAP();
118
119 vB.addAP();
120 vB.addAP();
121 vB.addAP();
122 vB.addAP();
123 //NB: vertex ID must be ignores by the sameAs method
124 assertTrue(vA.sameAs(vB, reason));
125
126 // ... one can use properties to uniquefy empty vertexes
127 String k = "MyPropKey";
128 vA.setProperty(k, 123);
130 vB.setProperty(k, 123);
132 // if the value of the uniquefying property is the same
133 assertTrue(vA.sameAs(vB, reason));
134
135 // otherwise
136 vB.setProperty(k, 456);
137 assertFalse(vA.sameAs(vB, reason));
138 }
139
140//------------------------------------------------------------------------------
141
142 @Test
144 {
145 EmptyVertex vA = new EmptyVertex(0);
146 vA.addAP();
147 vA.addAP();
148 vA.addAP();
149 vA.addAP();
150
151 EmptyVertex vB = new EmptyVertex(90);
152 vB.addAP();
153 vB.addAP();
154 vB.addAP();
155 //NB: vertex ID must be ignores by the sameAs method
156
157 assertFalse(vA.sameAs(vB, reason));
158 }
159
160//------------------------------------------------------------------------------
161
162 @Test
163 public void testClone() throws Exception
164 {
165 EmptyVertex v = new EmptyVertex(0);
166 v.addAP();
167 v.addAP();
168 v.addAP();
169 v.setProperty("PROPNAME","PROVALUE");
170
171 Vertex c = v.clone();
172
173 assertEquals(v.getVertexId(), c.getVertexId(), "Vertex ID");
174 assertEquals(v.getNumberOfAPs(), c.getNumberOfAPs(), "Number of APS");
175 assertEquals(v.getSymmetricAPSets().size(),
176 c.getSymmetricAPSets().size(), "Number of SymAPs sets");
177 assertEquals(v.isRCV(), c.isRCV(), "RCV flag");
178 assertNotEquals(v.hashCode(), c.hashCode(), "Hash code");
179 assertEquals("PROVALUE",c.getProperty("PROPNAME"));
180
181
182 Fragment v2 = new Fragment();
183 Atom a1 = new Atom("C", new Point3d(new double[]{0.0, 1.1, 2.2}));
184 Atom a2 = new Atom("C", new Point3d(new double[]{1.0, 1.1, 2.2}));
185 Atom a3 = new Atom("C", new Point3d(new double[]{2.0, 1.1, 2.2}));
186 v2.addAtom(a1);
187 v2.addAtom(a2);
188 v2.addAtom(a3);
189 v2.addBond(new Bond(a1, a2));
190 v2.addBond(new Bond(a2, a3));
191 String APCLASS = "apc" + DENOPTIMConstants.SEPARATORAPPROPSCL +"0";
192 v2.addAPOnAtom(a3, APClass.make(APCLASS), new Point3d(
193 new double[]{0.0, 2.2, 3.3}));
194 v2.addAPOnAtom(a3, APClass.make(APCLASS), new Point3d(
195 new double[]{0.0, 0.0, 3.3}));
196 v2.addAPOnAtom(a3, APClass.make(APCLASS), new Point3d(
197 new double[]{0.0, 0.0, 1.1}));
198 v2.addAPOnAtom(a1, APClass.make(APCLASS), new Point3d(
199 new double[]{3.0, 0.0, 3.3}));
200
201 Vertex c2 = v2.clone();
202
203 assertEquals(v2.getVertexId(), c2.getVertexId(), "Vertex ID");
204 assertEquals(v2.getNumberOfAPs(), c2.getNumberOfAPs(), "Number of APS");
205 assertEquals(v2.getSymmetricAPSets().size(),
206 c2.getSymmetricAPSets().size(), "Number of SymAPs sets");
207 assertEquals(v2.isRCV(), c2.isRCV(), "RCV flag");
208 assertNotEquals(v2.hashCode(), c2.hashCode(), "Hash code");
209 assertEquals(v2.getAllAPClasses(),c2.getAllAPClasses(),"APClass list");
210 assertEquals(v2.getAllAPClasses().get(0).hashCode(),
211 c2.getAllAPClasses().get(0).hashCode(),"APClass hash code");
212 }
213
214//------------------------------------------------------------------------------
215
216 @Test
217 public void testGetMutationSites() throws Exception
218 {
220 assertEquals(1,v.getMutationSites().size(),
221 "Fragments return themselves as mutable sites.");
223 assertEquals(0,v.getMutationSites().size(),
224 "Scaffolds so not return any mutable site.");
225 v = new EmptyVertex(Vertex.BBType.CAP);
226 assertEquals(0,v.getMutationSites().size(),
227 "Capping groups so not return any mutable site.");
229 assertEquals(1,v.getMutationSites().size(),
230 "Undefined building block return themselves as mutable sites.");
231 v = new EmptyVertex(Vertex.BBType.NONE);
232 assertEquals(1,v.getMutationSites().size(),
233 "'None' building block return themselves as mutable sites.");
234
235 v.setMutationTypes(new ArrayList<>(Arrays.asList(MutationType.EXTEND)));
236 assertEquals(1,v.getMutationSites().size(),
237 "Consistency with restricted list of mutation types.");
238 assertEquals(0,v.getMutationSites(new ArrayList<>(Arrays.asList(
239 MutationType.EXTEND))).size(), "Vertex that allows only "
240 + "ignored mutation types is not a mutable site");
241 }
242
243//------------------------------------------------------------------------------
244
245 @Test
246 public void testGetAPsWithAPClassStartingWith() throws Exception
247 {
248 EmptyVertex ev = new EmptyVertex();
249 ev.addAP(APClass.make("Abc:1"));
250 ev.addAP(APClass.make("Abc:0"));
251 ev.addAP(APClass.make("TT:0"));
252 ev.addAP(APClass.make("AbT:1"));
253 ev.addAP(APClass.make("TT:2"));
254
255 List<AttachmentPoint> aps = ev.getAPsWithAPClassStartingWith("Ab");
256 assertEquals(3, aps.size());
257
258 for (AttachmentPoint ap : aps)
259 {
260 assertTrue(ap.getAPClass().toString().startsWith("Ab"));
261 }
262 }
263
264//------------------------------------------------------------------------------
265
266 @Test
267 public void testIsConnectedToAromaticBridge() throws Exception
268 {
269 EmptyVertex aromBridge = new EmptyVertex();
270 aromBridge.addAP(APClass.APCAROMBRIDGE2EL);
271 aromBridge.addAP(APClass.APCAROMBRIDGE2EL);
272 aromBridge.addAP(APClass.make("ab:0"));
273
274 EmptyVertex nonBridge = new EmptyVertex();
275 nonBridge.addAP(APClass.make("Abc:1"));
276 nonBridge.addAP(APClass.make("Abc:0"));
277 nonBridge.addAP(APClass.make("Abc:0"));
278
279 EmptyVertex nonBridge2 = new EmptyVertex();
280 nonBridge2.addAP(APClass.make("nb2:1"));
281
282 EmptyVertex nonBridge3 = new EmptyVertex();
283 nonBridge3.addAP(APClass.make("nb2:1"));
284 nonBridge3.addAP(APClass.make("nb2:1"));
285
287
289
290 DGraph graph = new DGraph();
291 graph.addVertex(nonBridge);
292 graph.appendVertexOnAP(nonBridge.getAP(0), nonBridge2.getAP(0));
293 graph.appendVertexOnAP(nonBridge.getAP(1), aromBridge.getAP(0));
294 graph.appendVertexOnAP(nonBridge.getAP(2), nonBridge3.getAP(0));
295 graph.appendVertexOnAP(nonBridge3.getAP(1), rcvA.getAP(0));
296 graph.appendVertexOnAP(aromBridge.getAP(1), rcvB.getAP(0));
297 graph.addRing(rcvA, rcvB);
298
299 assertFalse(nonBridge.isAromaticBridge());
300 assertFalse(nonBridge2.isAromaticBridge());
301 assertFalse(nonBridge3.isAromaticBridge());
302 assertTrue(aromBridge.isAromaticBridge());
303
304 assertTrue(nonBridge.isConnectedToAromaticBridge());
305 assertFalse(nonBridge2.isConnectedToAromaticBridge());
306 assertTrue(nonBridge3.isConnectedToAromaticBridge());
307 assertTrue(aromBridge.isConnectedToAromaticBridge());
308 }
309
310//------------------------------------------------------------------------------
311}
Class defining a space of building blocks.
static Vertex getPolarizedRCV(boolean polarity)
Returns a newly-built vertex that can play the role of a ring-closing vertex even when working with 3...
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
static final APClass APCAROMBRIDGE2EL
Conventional class of attachment points on aromatic-bridge vertexes bringing 2 electrons to the aroma...
Definition: APClass.java:106
An attachment point (AP) is a possibility to attach a Vertex onto the vertex holding the AP (i....
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:102
void addVertex(Vertex vertex)
Appends a vertex to this graph without creating any edge.
Definition: DGraph.java:1325
void appendVertexOnAP(AttachmentPoint srcAP, AttachmentPoint trgAP)
Append a vertex to this graph: adds the new vertex to the list of vertices belonging to the graph,...
Definition: DGraph.java:6005
void addRing(Ring ring)
Definition: DGraph.java:1258
An empty vertex has the behaviors of a vertex, but has no molecular structure.
void addSymmetricAPSet(SymmetricAPs symAPs)
boolean sameAs(EmptyVertex other, StringBuilder reason)
Compares this and another vertex ignoring vertex IDs.
EmptyVertex clone()
Returns a deep-copy of this vertex.
String toJson()
Produces a string that represents this vertex and that adheres to the JSON format.
List< SymmetricAPs > getSymmetricAPSets()
void addAP()
Adds an attachment point with no APClass or other attribute.
Class representing a continuously connected portion of chemical object holding attachment points.
Definition: Fragment.java:61
AttachmentPoint addAPOnAtom(IAtom srcAtm, APClass apc, Point3d vector)
Add an attachment point to the specifies atom.
Definition: Fragment.java:424
void addBond(IBond bond)
Definition: Fragment.java:871
Fragment clone()
Returns a deep copy of this fragments.
Definition: Fragment.java:733
void addAtom(IAtom atom)
Definition: Fragment.java:836
List< SymmetricAPs > getSymmetricAPSets()
Definition: Fragment.java:1169
String toJson()
Produces a string that represents this vertex and that adheres to the JSON format.
Definition: Fragment.java:1209
A collection of AttachmentPoints that are related by a relation that we call "symmetry",...
boolean add(T item)
Adds an item to this list, if not already present.
String toJson()
Produces a string that represents this vertex and that adheres to the JSON format.
Definition: Template.java:1022
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:62
void setMutationTypes(List< MutationType > lst)
Definition: Vertex.java:882
List< AttachmentPoint > getAPsWithAPClassStartingWith(String root)
Finds only APs that have APClass starting with the given string.
Definition: Vertex.java:261
ArrayList< APClass > getAllAPClasses()
Returns the list of all APClasses present on this vertex.
Definition: Vertex.java:793
boolean isConnectedToAromaticBridge()
Checks if this vertex is connected to any vertex meant to be a piece of an aromatic system.
Definition: Vertex.java:583
List< Vertex > getMutationSites()
A list of mutation sites from within this vertex.
Definition: Vertex.java:863
boolean isAromaticBridge()
Checks if this vertex is meant to be a piece of an aromatic system.
Definition: Vertex.java:564
abstract List< SymmetricAPs > getSymmetricAPSets()
Object getProperty(Object property)
Definition: Vertex.java:1224
static Vertex fromJson(String json)
Definition: Vertex.java:1290
void setUniquefyingProperty(String key)
Add the given key among the properties that are checked for equality when comparing vertices with the...
Definition: Vertex.java:1200
final VertexType vertexType
Field distinguishing implementations of Vertex when deserializing JSON representations.
Definition: Vertex.java:167
void setProperty(Object key, Object property)
Definition: Vertex.java:1236
AttachmentPoint getAP(int i)
Get attachment point i on this vertex.
Definition: Vertex.java:1008
Unit test for DENOPTIMVertex.
Definition: VertexTest.java:49
void testGetAPsWithAPClassStartingWith()
void testFromToJSON_withSymmetricAPs()
Definition: VertexTest.java:79
The type of building block.
Definition: Vertex.java:87
Flag declaring the type of Vertex implementation.
Definition: Vertex.java:173
Types of mutation defined in relation to what happens to the target vertex (i.e., the actual mutation...
EXTEND
append vertices on the target vertex according to substitution probability.