19package denoptim.integration.rcoserver;
21import java.io.BufferedReader;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.InputStreamReader;
25import java.io.OutputStream;
26import java.io.PrintWriter;
27import java.net.Socket;
28import java.util.ArrayList;
31import java.util.logging.Level;
32import java.util.logging.Logger;
34import javax.vecmath.Point3d;
36import com.google.gson.Gson;
37import com.google.gson.GsonBuilder;
38import com.google.gson.JsonArray;
39import com.google.gson.JsonElement;
40import com.google.gson.JsonObject;
41import com.google.gson.JsonSyntaxException;
43import denoptim.exception.DENOPTIMException;
44import denoptim.graph.rings.RingClosingAttractor;
45import denoptim.io.DenoptimIO;
46import denoptim.molecularmodeling.ChemicalObjectModel;
47import denoptim.molecularmodeling.zmatrix.ZMatrix;
48import denoptim.molecularmodeling.zmatrix.ZMatrixAtom;
49import denoptim.utils.ObjectPair;
143 throw new IllegalStateException(
144 "RCOSocketServerClient has not been initialized. "
145 +
"Call getInstance(hostname, port) first.");
226 Set<ObjectPair> rcaCombination, Logger logger)
229 if (chemObj.getNumberRotatableBonds() == 0)
231 logger.log(Level.FINE,
"No rotatable bond: skipping "
232 +
" conformational search.");
236 List<int[]> rcpTerms =
new ArrayList<int[]>();
237 if (rcaCombination!=
null && rcaCombination.size()>0)
243 int iZMatRcaA = chemObj.getZMatIdxOfRCA(rcaA);
244 int iZMatRcaB = chemObj.getZMatIdxOfRCA(rcaB);
245 int iZMatSrcA = chemObj.getZMatIdxOfRCASrc(rcaA);
246 int iZMatSrcB = chemObj.getZMatIdxOfRCASrc(rcaB);
247 rcpTerms.add(
new int[] {iZMatRcaA, iZMatSrcB});
248 rcpTerms.add(
new int[] {iZMatRcaB, iZMatSrcA});
252 List<int[]> rotatableBonds =
new ArrayList<int[]>();
253 for(
ObjectPair bondedAtms : chemObj.getRotatableBonds())
255 int t1 = ((Integer)bondedAtms.getFirst()).intValue();
256 int t2 = ((Integer)bondedAtms.getSecond()).intValue();
257 rotatableBonds.add(
new int[] {t1, t2});
260 List<int[]> allBonds =
new ArrayList<int[]>();
261 for(
int[] bondedAtmIds : chemObj.getZMatrix().getBondData())
263 int t1 = bondedAtmIds[0];
264 int t2 = bondedAtmIds[1];
265 allBonds.add(
new int[] {t1, t2});
269 rcpTerms, rotatableBonds, allBonds);
272 logger.log(Level.FINE,
"Request to the socket server: " + requestAsJSONString);
275 requestAsJSONString+System.lineSeparator(),
true);
278 JsonObject answer =
sendRequest(requestAsJSONString);
279 for (String requiredMember :
new String[] {
"Cartesian_coordinates",
"zmatrix"})
281 if (!answer.has(requiredMember))
283 throw new Error(
"Socket server replied without "
284 +
"including '" + requiredMember +
"' member. "
285 +
"Aborting! " + answer.toString());
290 JsonArray cartesianCoordinates = answer.get(
291 "Cartesian_coordinates").getAsJsonArray();
292 List<Point3d> newCoordinates =
new ArrayList<>();
293 for (
int i = 0; i < cartesianCoordinates.size(); i++)
295 JsonArray element = cartesianCoordinates.get(i).getAsJsonArray();
296 newCoordinates.add(
new Point3d(
297 element.get(0).getAsDouble(),
298 element.get(1).getAsDouble(),
299 element.get(2).getAsDouble()));
301 chemObj.updateXYZ(newCoordinates);
303 JsonArray zmatrixArrayAsJson = answer.get(
"zmatrix").getAsJsonArray();
304 for (
int i = 0; i < zmatrixArrayAsJson.size(); i++)
306 JsonElement element = zmatrixArrayAsJson.get(i);
307 JsonObject zmatrixObject = element.getAsJsonObject();
308 ZMatrixAtom zatm = chemObj.getZMatrix().getAtom(i);
314 zmatrixObject.get(
"bond_length").getAsDouble());
318 zmatrixObject.get(
"angle").getAsDouble());
322 zmatrixObject.get(
"dihedral").getAsDouble());
324 zmatrixObject.get(
"chirality").getAsInt());
348 List<
int[]> rotatableBonds, List<
int[]> allBonds)
throws IOException
350 JsonObject jsonObj =
new JsonObject();
355 jsonObj.addProperty(
"version",
version);
370 JsonArray zmatrixArray =
new JsonArray();
374 JsonObject atomObj =
new JsonObject();
375 atomObj.addProperty(
"id", atom.
getId()+1);
376 atomObj.addProperty(
"element", atom.
getSymbol());
405 zmatrixArray.add(atomObj);
420 JsonArray jsonArray =
new JsonArray();
421 for (
int[] intArray : intArrayList)
423 JsonArray innerArray =
new JsonArray();
424 for (
int value : intArray)
426 innerArray.add(value+1);
428 jsonArray.add(innerArray);
443 throws IOException, JsonSyntaxException
449 }
catch (IOException e1)
451 throw new IllegalArgumentException(
"Could not connect to socket",e1);
454 Runtime.getRuntime().addShutdownHook(
new Thread(){
public void run(){
457 }
catch (IOException e) { }
460 PrintWriter writerToSocket;
463 OutputStream outputSocket = socket.getOutputStream();
464 writerToSocket =
new PrintWriter(outputSocket,
true);
465 }
catch (IOException e1)
470 }
catch (IOException e)
474 throw new IllegalArgumentException(
"Could not connect to socket",e1);
477 BufferedReader readerFromSocket;
480 InputStream inputFromSocket = socket.getInputStream();
481 readerFromSocket =
new BufferedReader(
482 new InputStreamReader(inputFromSocket));
483 }
catch (IOException e1)
488 }
catch (IOException e)
492 throw new IllegalArgumentException(
"Could not read from socket",e1);
496 writerToSocket.println(requestAsJSONString);
499 socket.shutdownOutput();
500 }
catch (IOException e1)
505 }
catch (IOException e)
509 throw new IllegalStateException(
"Could not half-close socket from "
510 + this.getClass().getName(),e1);
514 JsonObject answer =
null;
518 if (!(answer.has(
"STATUS")))
521 throw new Error(
"Socket server replied without "
522 +
"including " +
"STATUS" +
" member. "
523 +
"Something is badly wrong: aborting! " + answer.toString());
525 if (!answer.get(
"STATUS").getAsString().equals(
"SUCCESS"))
527 throw new Error(
"Socket server replied but with STATUS="
528 + answer.get(
"STATUS") +
". "
529 +
"Something is badly wrong: aborting! " + answer.toString());
531 }
catch (JsonSyntaxException e) {
532 throw new Error(
"Socket server replied with invalid JSON: " + e.getMessage());
533 }
catch (IOException e) {
534 throw new Error(
"Error reading from socket: " + e.getMessage());
540 }
catch (IOException e)
The RingClosingAttractor represent the available valence/connection that allows to close a ring.
Sends the request to produce a socket server running the RingClosingMM service.
static synchronized RCOSocketServerClient getInstance(String hostname, Integer port)
Gets the singleton instance of RCOSocketServerClient.
static JsonArray convertIntArrayListToJsonArray(List< int[]> intArrayList)
Converts a List of 0-based int arrays to a JsonArray containing 1-based ints.
void setHostname(String hostname)
Sets the hostname for the socket server connection.
final int version
Version identifier.
String hostname
The name of the host or ID address used to communicate with the socket server.
JsonObject sendRequest(String requestAsJSONString)
Sends the given request to the socket server and waits for the answer, which is then processed and re...
void setRecordRequestsFileName(String requestFileName)
Sets the pathname to file where to record requests sent to the server.
static RCOSocketServerClient instance
Singleton instance.
Integer getPort()
Gets the currently configured port.
String getHostname()
Gets the currently configured hostname.
Integer port
The identifier of the port used to communicate with the socket server.
Gson jsonConverter
Converter to and from JSON/Java objects.
String requestFileName
Pathname to file where to record requests sent to the server.
void setPort(Integer port)
Sets the port for the socket server connection.
void runConformationalOptimization(ChemicalObjectModel chemObj, Logger logger)
Runs a conformational optimization using the services provided by the socket server configured for th...
static JsonArray getZMatrixAsJsonArray(ZMatrix zmat)
Gets a JsonArray representation of the Z-matrix.
static RCOSocketServerClient getInstance()
Gets the singleton instance of RCOSocketServerClient if it has been initialized.
void runConformationalOptimization(ChemicalObjectModel chemObj, Set< ObjectPair > rcaCombination, Logger logger)
Runs a conformational optimization using the services provided by the socket server configured for th...
String formulateRequest(ZMatrix zmat, List< int[]> rcpTerms, List< int[]> rotatableBonds, List< int[]> allBonds)
Formulates the request to be sent to the socket server.
RCOSocketServerClient(String hostname, Integer port)
Private constructor for singleton pattern.
Utility methods for input/output.
static void writeData(String fileName, String data, boolean append)
Write text-like data file.
Collector of molecular information, related to a single chemical object, that is deployed within the ...
Representation of an atom in the ZMatrix.
String getSymbol()
Get the symbol of the atom.
void setBondLength(Double bondLength)
Set the bond length.
void setChiralFlag(Integer chiralFlag)
Set the chiral flag.
int getId()
Get the id of the atom.
void setAngleValue(Double angleValue)
Set the angle value.
void setAngle2Value(Double angle2Value)
Set the angle2 value.
Representation of an atom container's geometry with internal coordinates.
int getAtomCount()
Get the number of atoms in the ZMatrix.
Integer getChiralFlag(int index)
Get the chiral flag for the atom at the given index.
int getBondRefAtomIndex(int index)
Get the index of the bond reference atom for the atom at the given index.
Double getBondLength(int index)
Get the bond length for the atom at the given index.
Double getAngle2Value(int index)
Get the angle2 angle for the atom at the given index.
Double getAngleValue(int index)
Get the bond angle for the atom at the given index.
ZMatrixAtom getAtom(int index)
Get the atom at the given index.
int getAngle2RefAtomIndex(int index)
Get the index of the second angle reference atom for the atom at the given index.
int getAngleRefAtomIndex(int index)
Get the index of the angle reference atom for the atom at the given index.
This class is the equivalent of the Pair data structure used in C++ Although AbstractMap....