$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;
28
29import javax.vecmath.Point3d;
30
31import org.junit.jupiter.api.Test;
32import org.openscience.cdk.Atom;
33import org.openscience.cdk.silent.Bond;
34
35import denoptim.constants.DENOPTIMConstants;
36import denoptim.graph.Vertex.BBType;
37import denoptim.graph.Vertex.VertexType;
38import denoptim.utils.MutationType;
39
46public class VertexTest
47{
48 private StringBuilder reason = new StringBuilder();
49
50//------------------------------------------------------------------------------
51
52 @Test
53 public void testFromToJSON_minimal() throws Exception
54 {
55 EmptyVertex ev = new EmptyVertex();
56 assertEquals(VertexType.EmptyVertex,ev.vertexType);
57 String evStr = ev.toJson();
58 Vertex ev2 = Vertex.fromJson(evStr);
59 assertTrue(ev2 instanceof EmptyVertex);
60
61 Fragment f = new Fragment();
63 String fStr = f.toJson();
64 Vertex f2 = Vertex.fromJson(fStr);
65 assertTrue(f2 instanceof Fragment);
66
68 assertEquals(VertexType.Template,t.vertexType);
69 String tStr = t.toJson();
70 Vertex t2 = Vertex.fromJson(tStr);
71 assertTrue(t2 instanceof Template);
72 }
73
74//------------------------------------------------------------------------------
75
76 @Test
77 public void testFromToJSON_withSymmetricAPs() throws Exception
78 {
79 EmptyVertex ev = new EmptyVertex();
80 ev.addAP(APClass.make("TT:1"));
81 ev.addAP(APClass.make("TT:0"));
82 ev.addAP(APClass.make("TT:0"));
83 ev.addAP(APClass.make("TT:1"));
84 ev.addAP(APClass.make("TT:2"));
85 SymmetricAPs ss1 = new SymmetricAPs();
86 ss1.add(ev.getAP(0));
87 ss1.add(ev.getAP(3));
88 SymmetricAPs ss2 = new SymmetricAPs();
89 ss2.add(ev.getAP(2));
90 ss2.add(ev.getAP(1));
91 ev.addSymmetricAPSet(ss1);
92 ev.addSymmetricAPSet(ss2);
93 String evStr = ev.toJson();
94 Vertex ev2 = Vertex.fromJson(evStr);
95 assertTrue(ev2 instanceof EmptyVertex);
96 assertEquals(2, ev2.getSymmetricAPSets().size());
97 SymmetricAPs rebuilt1 = ev2.getSymmetricAPSets().get(0);
98 SymmetricAPs rebuilt2 = ev2.getSymmetricAPSets().get(1);
99 assertTrue(rebuilt1.contains(ev2.getAP(3)));
100 assertTrue(rebuilt1.contains(ev2.getAP(0)));
101 assertTrue(rebuilt2.contains(ev2.getAP(1)));
102 assertTrue(rebuilt2.contains(ev2.getAP(2)));
103 }
104
105//------------------------------------------------------------------------------
106
107 @Test
108 public void testSameAs_Equal()
109 {
110 EmptyVertex vA = new EmptyVertex(0);
111 EmptyVertex vB = new EmptyVertex(90);
112 vA.addAP();
113 vA.addAP();
114 vA.addAP();
115 vA.addAP();
116
117 vB.addAP();
118 vB.addAP();
119 vB.addAP();
120 vB.addAP();
121 //NB: vertex ID must be ignores by the sameAs method
122 assertTrue(vA.sameAs(vB, reason));
123
124 // ... one can use properties to uniquefy empty vertexes
125 String k = "MyPropKey";
126 vA.setProperty(k, 123);
128 vB.setProperty(k, 123);
130 // if the value of the uniquefying property is the same
131 assertTrue(vA.sameAs(vB, reason));
132
133 // otherwise
134 vB.setProperty(k, 456);
135 assertFalse(vA.sameAs(vB, reason));
136 }
137
138//------------------------------------------------------------------------------
139
140 @Test
142 {
143 EmptyVertex vA = new EmptyVertex(0);
144 vA.addAP();
145 vA.addAP();
146 vA.addAP();
147 vA.addAP();
148
149 EmptyVertex vB = new EmptyVertex(90);
150 vB.addAP();
151 vB.addAP();
152 vB.addAP();
153 //NB: vertex ID must be ignores by the sameAs method
154
155 assertFalse(vA.sameAs(vB, reason));
156 }
157
158//------------------------------------------------------------------------------
159
160 @Test
161 public void testClone() throws Exception
162 {
163 EmptyVertex v = new EmptyVertex(0);
164 v.addAP();
165 v.addAP();
166 v.addAP();
167 v.setProperty("PROPNAME","PROVALUE");
168
169 Vertex c = v.clone();
170
171 assertEquals(v.getVertexId(), c.getVertexId(), "Vertex ID");
172 assertEquals(v.getNumberOfAPs(), c.getNumberOfAPs(), "Number of APS");
173 assertEquals(v.getSymmetricAPSets().size(),
174 c.getSymmetricAPSets().size(), "Number of SymAPs sets");
175 assertEquals(v.isRCV(), c.isRCV(), "RCV flag");
176 assertNotEquals(v.hashCode(), c.hashCode(), "Hash code");
177 assertEquals("PROVALUE",c.getProperty("PROPNAME"));
178
179
180 Fragment v2 = new Fragment();
181 Atom a1 = new Atom("C", new Point3d(new double[]{0.0, 1.1, 2.2}));
182 Atom a2 = new Atom("C", new Point3d(new double[]{1.0, 1.1, 2.2}));
183 Atom a3 = new Atom("C", new Point3d(new double[]{2.0, 1.1, 2.2}));
184 v2.addAtom(a1);
185 v2.addAtom(a2);
186 v2.addAtom(a3);
187 v2.addBond(new Bond(a1, a2));
188 v2.addBond(new Bond(a2, a3));
189 String APCLASS = "apc" + DENOPTIMConstants.SEPARATORAPPROPSCL +"0";
190 v2.addAPOnAtom(a3, APClass.make(APCLASS), new Point3d(
191 new double[]{0.0, 2.2, 3.3}));
192 v2.addAPOnAtom(a3, APClass.make(APCLASS), new Point3d(
193 new double[]{0.0, 0.0, 3.3}));
194 v2.addAPOnAtom(a3, APClass.make(APCLASS), new Point3d(
195 new double[]{0.0, 0.0, 1.1}));
196 v2.addAPOnAtom(a1, APClass.make(APCLASS), new Point3d(
197 new double[]{3.0, 0.0, 3.3}));
198
199 Vertex c2 = v2.clone();
200
201 assertEquals(v2.getVertexId(), c2.getVertexId(), "Vertex ID");
202 assertEquals(v2.getNumberOfAPs(), c2.getNumberOfAPs(), "Number of APS");
203 assertEquals(v2.getSymmetricAPSets().size(),
204 c2.getSymmetricAPSets().size(), "Number of SymAPs sets");
205 assertEquals(v2.isRCV(), c2.isRCV(), "RCV flag");
206 assertNotEquals(v2.hashCode(), c2.hashCode(), "Hash code");
207 assertEquals(v2.getAllAPClasses(),c2.getAllAPClasses(),"APClass list");
208 assertEquals(v2.getAllAPClasses().get(0).hashCode(),
209 c2.getAllAPClasses().get(0).hashCode(),"APClass hash code");
210 }
211
212//------------------------------------------------------------------------------
213
214 @Test
215 public void testGetMutationSites() throws Exception
216 {
218 assertEquals(1,v.getMutationSites().size(),
219 "Fragments return themselves as mutable sites.");
221 assertEquals(0,v.getMutationSites().size(),
222 "Scaffolds so not return any mutable site.");
223 v = new EmptyVertex(Vertex.BBType.CAP);
224 assertEquals(0,v.getMutationSites().size(),
225 "Capping groups so not return any mutable site.");
227 assertEquals(1,v.getMutationSites().size(),
228 "Undefined building block return themselves as mutable sites.");
229 v = new EmptyVertex(Vertex.BBType.NONE);
230 assertEquals(1,v.getMutationSites().size(),
231 "'None' building block return themselves as mutable sites.");
232
233 v.setMutationTypes(new ArrayList<>(Arrays.asList(MutationType.EXTEND)));
234 assertEquals(1,v.getMutationSites().size(),
235 "Consistency with restricted list of mutation types.");
236 assertEquals(0,v.getMutationSites(new ArrayList<>(Arrays.asList(
237 MutationType.EXTEND))).size(), "Vertex that allows only "
238 + "ignored mutation types is not a mutable site");
239 }
240
241//------------------------------------------------------------------------------
242}
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:136
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:1148
String toJson()
Produces a string that represents this vertex and that adheres to the JSON format.
Definition: Fragment.java:1188
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:1008
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
void setMutationTypes(List< MutationType > lst)
Definition: Vertex.java:809
ArrayList< APClass > getAllAPClasses()
Returns the list of all APClasses present on this vertex.
Definition: Vertex.java:720
List< Vertex > getMutationSites()
A list of mutation sites from within this vertex.
Definition: Vertex.java:790
abstract List< SymmetricAPs > getSymmetricAPSets()
Object getProperty(Object property)
Definition: Vertex.java:1136
static Vertex fromJson(String json)
Definition: Vertex.java:1202
void setUniquefyingProperty(String key)
Add the given key among the properties that are checked for equality when comparing vertices with the...
Definition: Vertex.java:1112
final VertexType vertexType
Field distinguishing implementations of Vertex when deserializing JSON representations.
Definition: Vertex.java:166
void setProperty(Object key, Object property)
Definition: Vertex.java:1148
AttachmentPoint getAP(int i)
Get attachment point i on this vertex.
Definition: Vertex.java:920
Unit test for DENOPTIMVertex.
Definition: VertexTest.java:47
void testFromToJSON_withSymmetricAPs()
Definition: VertexTest.java:77
The type of building block.
Definition: Vertex.java:86
Flag declaring the type of Vertex implementation.
Definition: Vertex.java:172
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.