Skip to content
Snippets Groups Projects
Commit 8a3ea320 authored by Mirco Nasuti's avatar Mirco Nasuti
Browse files

merged list of algorithms working

parent 91dd9f89
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,9 @@ import org.hbp.mip.MIPApplication;
import org.hbp.mip.model.Experiment;
import org.hbp.mip.model.Model;
import org.hbp.mip.model.User;
import org.hbp.mip.model.algorithm.Algorithm;
import org.hbp.mip.model.algorithm.Catalog;
import org.hbp.mip.utils.HTTPUtil;
import org.hbp.mip.utils.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
......@@ -24,7 +27,10 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.*;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
......@@ -37,6 +43,8 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringMVCServerCodegen", date = "2016-01-07T07:38:20.227Z")
public class ExperimentApi {
private static final String EXAREME_ALGO_JSON_FILE="data/exareme_algorithms.json";
private static final Gson gson = new GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
......@@ -371,22 +379,25 @@ public class ExperimentApi {
@RequestMapping(path = "/methods", method = RequestMethod.GET)
public ResponseEntity<String> listAvailableMethodsAndValidations() throws Exception {
URL obj = new URL(listMethodsUrl);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int respCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(respCode == 200 ? con.getInputStream() : con.getErrorStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
int code = HTTPUtil.sendGet(listMethodsUrl, response);
if (code < 200 || code > 299) {
return new ResponseEntity<>(response.toString(), HttpStatus.valueOf(code));
}
in.close();
return new ResponseEntity<>(response.toString(), HttpStatus.valueOf(respCode));
Catalog catalog = new Gson().fromJson(response.toString(), Catalog.class);
for (Algorithm algo: catalog.getAlgorithms()) {
algo.setSource("ML");
}
InputStream is = MiningApi.class.getClassLoader().getResourceAsStream(EXAREME_ALGO_JSON_FILE);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
Algorithm exaremeGLR = new Gson().fromJson(br, Algorithm.class);
exaremeGLR.setSource("exareme");
catalog.getAlgorithms().add(exaremeGLR);
return new ResponseEntity<>(new Gson().toJson(catalog), HttpStatus.valueOf(code));
}
}
......@@ -4,11 +4,9 @@
package org.hbp.mip.controllers;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import io.swagger.annotations.*;
import org.hbp.mip.model.algorithm.Algorithm;
import org.hbp.mip.model.algorithm.Catalog;
import org.hbp.mip.utils.HTTPUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......@@ -17,12 +15,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
@RestController
......@@ -30,8 +22,6 @@ import java.net.UnknownHostException;
@Api(value = "/mining", description = "Forward mining API")
public class MiningApi {
private static final String EXAREME_ALGO_JSON_FILE="data/exareme_algorithms.json";
@Value("#{'${workflow.listMethodsUrl:http://hbps1.chuv.ch:8087/list-methods}'}")
private String listMethodsUrl;
......@@ -49,32 +39,13 @@ public class MiningApi {
) throws Exception {
// TODO : switch between sources
StringBuilder response = new StringBuilder();
int code = sendGet(listMethodsUrl, response);
if (code < 200 || code > 299) {
return new ResponseEntity<>(response.toString(), HttpStatus.valueOf(code));
}
Catalog catalog = new Gson().fromJson(response.toString(), Catalog.class);
for (Algorithm algo: catalog.getAlgorithms()) {
algo.setSource("ML");
}
InputStream is = MiningApi.class.getClassLoader().getResourceAsStream(EXAREME_ALGO_JSON_FILE);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
Algorithm exaremeGLR = new Gson().fromJson(br, Algorithm.class);
exaremeGLR.setSource("exareme");
catalog.getAlgorithms().add(exaremeGLR);
return new ResponseEntity<>(new Gson().toJson(catalog), HttpStatus.valueOf(code));
return null;
}
private ResponseEntity<String> postMipMining(String query) throws Exception {
try {
StringBuilder results = new StringBuilder();
int code = sendPost(miningMipUrl, query, results);
int code = HTTPUtil.sendPost(miningMipUrl, query, results);
return new ResponseEntity<>(results.toString(), HttpStatus.valueOf(code));
}
......@@ -92,7 +63,7 @@ public class MiningApi {
String url = miningExaremeQueryUrl +"/"+algo+"/?format=true";
StringBuilder results = new StringBuilder();
int code = sendPost(url, query, results);
int code = HTTPUtil.sendPost(url, query, results);
if (code < 200 || code > 299)
{
return new ResponseEntity<>(results.toString(), HttpStatus.valueOf(code));
......@@ -109,7 +80,7 @@ public class MiningApi {
while (progress < 100) {
Thread.sleep(200);
results = new StringBuilder();
code = sendPost(url, query, results);
code = HTTPUtil.sendPost(url, query, results);
if (code < 200 || code > 299)
{
return new ResponseEntity<>(results.toString(), HttpStatus.valueOf(code));
......@@ -121,7 +92,7 @@ public class MiningApi {
url = miningExaremeQueryUrl +"/"+key+"/result";
results = new StringBuilder();
code = sendPost(url, query, results);
code = HTTPUtil.sendPost(url, query, results);
return new ResponseEntity<>(results.toString(), HttpStatus.valueOf(code));
}
......@@ -131,54 +102,4 @@ public class MiningApi {
}
}
private static int sendGet(String url, StringBuilder resp) throws Exception {
return sendHTTP(url, "", resp, "GET");
}
private static int sendPost(String url, String query, StringBuilder resp) throws Exception {
return sendHTTP(url, query, resp, "POST");
}
private static int sendHTTP(String url, String query, StringBuilder resp, String httpVerb) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
if(!httpVerb.equals("GET")) {
con.setRequestMethod(httpVerb);
if(query != null && query.length() > 0)
{
con.addRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Content-Length", Integer.toString(query.length()));
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(query.getBytes("UTF8"));
wr.flush();
wr.close();
}
}
int respCode = con.getResponseCode();
BufferedReader in;
if(respCode == 200) {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
}
else
{
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
resp.append(response.toString());
return respCode;
}
}
package org.hbp.mip.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by mirco on 20.06.16.
*/
public class HTTPUtil {
public static int sendGet(String url, StringBuilder resp) throws Exception {
return sendHTTP(url, "", resp, "GET");
}
public static int sendPost(String url, String query, StringBuilder resp) throws Exception {
return sendHTTP(url, query, resp, "POST");
}
public static int sendHTTP(String url, String query, StringBuilder resp, String httpVerb) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
if(!httpVerb.equals("GET")) {
con.setRequestMethod(httpVerb);
if(query != null && query.length() > 0)
{
con.addRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Content-Length", Integer.toString(query.length()));
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(query.getBytes("UTF8"));
wr.flush();
wr.close();
}
}
int respCode = con.getResponseCode();
BufferedReader in;
if(respCode == 200) {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
}
else
{
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
resp.append(response.toString());
return respCode;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment