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;
59 private static final String
HOSTNAME =
"localhost";
74 Constructor<SocketProvidedDescriptor> defaultConstructor =
76 this.descriptor = defaultConstructor.newInstance();
101 server =
new ServerSocket(0);
103 Runtime.getRuntime().addShutdownHook(
new Thread(){
108 }
catch (IOException e) { }
113 catch (IOException e)
121 return server.getLocalPort();
129 }
catch (IOException e)
147 catch (IOException e)
175 BufferedReader in =
new BufferedReader(
new InputStreamReader(
176 socket.getInputStream()));
179 StringBuilder sb =
new StringBuilder();
180 String line = in.readLine();
181 while(line !=
null && line.length()>0)
183 sb.append(line).append(System.getProperty(
"line.separator"));
184 line = in.readLine();
193 String jsonStr = sb.toString();
194 JsonObject request =
null;
197 }
catch (JsonSyntaxException e) {
199 assertFalse(
true,
"JsonSyntaxException unpon converting "
200 +
"requst to socket server");
203 assertTrue(request.has(smiKey),
"JSON request has no " + smiKey);
211 String smiles = request.get(smiKey).getAsString();
212 long count = smiles.chars().filter(c -> c ==
'c').count();
213 double score = Math.pow((
double) count,2.5);
215 JsonObject jsonAnswer =
new JsonObject();
218 jsonAnswer.addProperty(
221 jsonAnswer.addProperty(
227 PrintWriter out =
new PrintWriter(
socket.getOutputStream());
247 SmilesParser sp =
new SmilesParser(DefaultChemObjectBuilder.getInstance());
250 IAtomContainer mol1 = sp.parseSmiles(smiles);
251 mol1.setProperty(
"SMILES", smiles);
254 double expected = Double.NaN;
255 assertTrue(Double.isNaN(value),
"Wrong socket-provided "
256 +
"descriptor: expected " + expected +
", found "+ value +
"(0)");
258 mol1.setProperty(
"SMILES",
"c");
262 assertTrue(
closeEnough(expected, value),
"Wrong socket-provided "
263 +
"descriptor: expected " + expected +
", found "+ value +
"(1)");
265 mol1.setProperty(
"SMILES",
"ccc");
269 assertTrue(
closeEnough(expected, value),
"Wrong socket-provided "
270 +
"descriptor: expected " + expected +
", found "+ value +
"(2)");
272 smiles =
"COc1ccccc1";
273 mol1 = sp.parseSmiles(smiles);
274 mol1.setProperty(
"SMILES", smiles);
278 assertTrue(
closeEnough(expected, value),
"Wrong socket-provided "
279 +
"descriptor: expected " + expected +
", found "+ value +
"(3)");
286 double threshold = 0.01;
287 double delta = Math.abs(expected-actual);
288 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()