19package denoptim.fitness.descriptors;
21import static org.junit.jupiter.api.Assertions.assertFalse;
22import static org.junit.jupiter.api.Assertions.assertTrue;
24import java.io.BufferedReader;
25import java.io.IOException;
26import java.io.InputStreamReader;
27import java.io.PrintWriter;
28import java.lang.reflect.Constructor;
29import java.net.ServerSocket;
30import java.net.Socket;
32import org.junit.jupiter.api.AfterEach;
33import org.junit.jupiter.api.BeforeEach;
34import org.junit.jupiter.api.Test;
35import org.openscience.cdk.DefaultChemObjectBuilder;
36import org.openscience.cdk.interfaces.IAtomContainer;
37import org.openscience.cdk.qsar.result.DoubleResult;
38import org.openscience.cdk.smiles.SmilesParser;
40import com.google.gson.Gson;
41import com.google.gson.GsonBuilder;
42import com.google.gson.JsonObject;
43import com.google.gson.JsonSyntaxException;
57 private static final String
HOSTNAME =
"localhost";
72 Constructor<SocketProvidedDescriptor> defaultConstructor =
74 this.descriptor = defaultConstructor.newInstance();
99 server =
new ServerSocket(0);
101 Runtime.getRuntime().addShutdownHook(
new Thread(){
106 }
catch (IOException e) { }
111 catch (IOException e)
119 return server.getLocalPort();
127 }
catch (IOException e)
145 catch (IOException e)
173 BufferedReader in =
new BufferedReader(
new InputStreamReader(
174 socket.getInputStream()));
177 StringBuilder sb =
new StringBuilder();
178 String line = in.readLine();
179 while(line !=
null && line.length()>0)
181 sb.append(line).append(System.getProperty(
"line.separator"));
182 line = in.readLine();
191 String jsonStr = sb.toString();
192 JsonObject request =
null;
195 }
catch (JsonSyntaxException e) {
197 assertFalse(
true,
"JsonSyntaxException unpon converting "
198 +
"requst to socket server");
201 assertTrue(request.has(smiKey),
"JSON request has no " + smiKey);
209 String smiles = request.get(smiKey).getAsString();
210 long count = smiles.chars().filter(c -> c ==
'c').count();
211 double score = Math.pow((
double) count,2.5);
213 JsonObject jsonAnswer =
new JsonObject();
216 jsonAnswer.addProperty(
219 jsonAnswer.addProperty(
221 "#SocketServer: fake error.");
225 PrintWriter out =
new PrintWriter(
socket.getOutputStream());
245 SmilesParser sp =
new SmilesParser(DefaultChemObjectBuilder.getInstance());
248 IAtomContainer mol1 = sp.parseSmiles(smiles);
249 mol1.setProperty(
"SMILES", smiles);
252 double expected = Double.NaN;
253 assertTrue(Double.isNaN(value),
"Wrong socket-provided "
254 +
"descriptor: expected " + expected +
", found "+ value +
"(0)");
256 mol1.setProperty(
"SMILES",
"c");
260 assertTrue(
closeEnough(expected, value),
"Wrong socket-provided "
261 +
"descriptor: expected " + expected +
", found "+ value +
"(1)");
263 mol1.setProperty(
"SMILES",
"ccc");
267 assertTrue(
closeEnough(expected, value),
"Wrong socket-provided "
268 +
"descriptor: expected " + expected +
", found "+ value +
"(2)");
270 smiles =
"COc1ccccc1";
271 mol1 = sp.parseSmiles(smiles);
272 mol1.setProperty(
"SMILES", smiles);
276 assertTrue(
closeEnough(expected, value),
"Wrong socket-provided "
277 +
"descriptor: expected " + expected +
", found "+ value +
"(3)");
284 double threshold = 0.01;
285 double delta = Math.abs(expected-actual);
286 return delta < threshold;
Sends the request to produce a numerical descriptor to a defined socket and receives back the respons...
void setParameters(Object[] params)
Set the parameters attributes.
DescriptorValue calculate(IAtomContainer mol)
static final String KEYJSONMEMBERSCORE
The key of the JSON member defining the score/s for the descriptor calculated.
static final String KEYJSONMEMBERSMILES
The key of the JSON member defining the SMILES of the candidate for which the socket server should pr...
static final String KEYJSONMEMBERERR
The key of the JSON member defining an error in the calculation of the score.
RequestHandler(Socket socket)
Unit test for descriptor SocketProvidedDescriptor.
boolean closeEnough(double expected, double actual)
SocketProvidedDescriptor descriptor
static final String HOSTNAME
void testSocketProvidedDescriptor()