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

Simple Mining Queries should work

parent da9de5ac
No related branches found
No related tags found
No related merge requests found
......@@ -4,11 +4,17 @@
package org.hbp.mip.controllers;
import com.google.gson.*;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import io.swagger.annotations.*;
import org.hbp.mip.model.Model;
import org.hbp.mip.model.SimpleMiningQuery;
import org.hbp.mip.model.Variable;
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.Session;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......@@ -21,6 +27,9 @@ import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
@RestController
@RequestMapping(value = "/mining")
......@@ -72,16 +81,19 @@ public class MiningApi {
.get("algorithm").getAsJsonObject()
.get("code").getAsString();
String modelSlug = new JsonParser().parse(query).getAsJsonObject()
.get("model").getAsString();
for(Algorithm algo : catalog.getAlgorithms())
{
if (algo.getCode().equals(algoCode))
{
if(algo.getSource().equals(ML_SOURCE)) {
return postMipMining(query);
return postMipMining(algoCode, modelSlug);
}
else if(algo.getSource().equals(EXAREME_SOURCE))
{
return postExaremeMining("WP_LINEAR_REGRESSION", query);
return postExaremeMining("WP_LINEAR_REGRESSION", modelSlug);
}
}
}
......@@ -89,10 +101,61 @@ public class MiningApi {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
private ResponseEntity<String> postMipMining(String query) throws Exception {
private ResponseEntity<String> postMipMining(String algoCode, String modelSlug) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Model model = null;
try {
session.beginTransaction();
model = (Model) session.createQuery("FROM Model WHERE slug= :slug")
.setString("slug", modelSlug)
.uniqueResult();
session.getTransaction().commit();
} catch (Exception e)
{
if(session.getTransaction() != null)
{
session.getTransaction().rollback();
throw e;
}
}
SimpleMiningQuery smq = new SimpleMiningQuery();
smq.setAlgorithm(algoCode);
LinkedList<Map<String,String>> vars = new LinkedList<>();
for(Variable var : model.getQuery().getVariables())
{
Map<String,String> m = new HashMap<>();
m.put("code", var.getCode());
vars.add(m);
}
smq.setVariables(vars);
LinkedList<Map<String,String>> covars = new LinkedList<>();
for(Variable var : model.getQuery().getCovariables())
{
Map<String,String> m = new HashMap<>();
m.put("code", var.getCode());
covars.add(m);
}
smq.setCovariables(covars);
LinkedList<Map<String,String>> grps = new LinkedList<>();
for(Variable var : model.getQuery().getGrouping())
{
Map<String,String> m = new HashMap<>();
m.put("code", var.getCode());
grps.add(m);
}
smq.setGrouping(grps);
smq.setFilters(new LinkedList<>());
try {
StringBuilder results = new StringBuilder();
int code = HTTPUtil.sendPost(miningMipUrl, query, results);
int code = HTTPUtil.sendPost(miningMipUrl, new Gson().toJson(smq), results);
return new ResponseEntity<>(results.toString(), HttpStatus.valueOf(code));
}
......@@ -103,14 +166,14 @@ public class MiningApi {
}
private ResponseEntity<String> postExaremeMining(String algo, String query) throws Exception {
private ResponseEntity<String> postExaremeMining(String algoCode, String modelSlug) throws Exception {
try {
/* Launch computation */
String url = miningExaremeQueryUrl +"/"+algo+"/?format=true";
String url = miningExaremeQueryUrl +"/"+algoCode+"/?format=true";
StringBuilder results = new StringBuilder();
int code = HTTPUtil.sendPost(url, query, results);
int code = HTTPUtil.sendPost(url, modelSlug, results);
if (code < 200 || code > 299)
{
return new ResponseEntity<>(results.toString(), HttpStatus.valueOf(code));
......@@ -127,7 +190,7 @@ public class MiningApi {
while (progress < 100) {
Thread.sleep(200);
results = new StringBuilder();
code = HTTPUtil.sendPost(url, query, results);
code = HTTPUtil.sendPost(url, modelSlug, results);
if (code < 200 || code > 299)
{
return new ResponseEntity<>(results.toString(), HttpStatus.valueOf(code));
......@@ -139,7 +202,7 @@ public class MiningApi {
url = miningExaremeQueryUrl +"/"+key+"/result";
results = new StringBuilder();
code = HTTPUtil.sendPost(url, query, results);
code = HTTPUtil.sendPost(url, modelSlug, results);
return new ResponseEntity<>(results.toString(), HttpStatus.valueOf(code));
}
......
package org.hbp.mip.model;
/**
* Created by mirco on 20.06.16.
*/
public class ExaremeQuery {
}
package org.hbp.mip.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import java.util.List;
import java.util.Map;
/**
* Created by mirco on 20.06.16.
*/
@ApiModel
@JsonInclude(JsonInclude.Include.NON_NULL)
public class SimpleMiningQuery {
private String algorithm;
private List<Map<String,String>> variables;
private List<Map<String,String>> covariables;
private List<Map<String,String>> grouping;
private List<Map<String,String>> filters;
public String getAlgorithm() {
return algorithm;
}
public void setAlgorithm(String algorithm) {
this.algorithm = algorithm;
}
public List<Map<String, String>> getVariables() {
return variables;
}
public void setVariables(List<Map<String, String>> variables) {
this.variables = variables;
}
public List<Map<String, String>> getCovariables() {
return covariables;
}
public void setCovariables(List<Map<String, String>> covariables) {
this.covariables = covariables;
}
public List<Map<String, String>> getGrouping() {
return grouping;
}
public void setGrouping(List<Map<String, String>> grouping) {
this.grouping = grouping;
}
public List<Map<String, String>> getFilters() {
return filters;
}
public void setFilters(List<Map<String, String>> filters) {
this.filters = filters;
}
}
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