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

add mining API

parent 34ac118f
No related branches found
No related tags found
No related merge requests found
package eu.hbp.mip.controllers;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.pattern.Patterns;
import akka.util.Timeout;
import eu.hbp.mip.messages.external.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import java.io.IOException;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
/**
* Created by mirco on 06.01.17.
*/
@RestController
@RequestMapping(value = "/mining", produces = {APPLICATION_JSON_VALUE})
@Api(value = "/mining", description = "the mining API")
public class MiningApi {
private static final Logger LOGGER = Logger.getLogger(MiningApi.class);
@Autowired
public ActorSystem actorSystem;
@Autowired
public String wokenRefPath;
@ApiOperation(value = "Run an algorithm", response = String.class)
@Cacheable("mining")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity runAlgorithm(@RequestBody eu.hbp.mip.model.MiningQuery query) throws IOException {
LOGGER.info("Run an algorithm");
LOGGER.info("Akka is trying to reach remote " + wokenRefPath);
ActorSelection wokenActor = actorSystem.actorSelection(wokenRefPath);
Timeout timeout = new Timeout(Duration.create(5, "seconds"));
Future<Object> future = Patterns.ask(wokenActor, query.prepareQuery(), timeout);
QueryResult result;
try {
result = (QueryResult) Await.result(future, timeout.duration());
} catch (Exception e) {
LOGGER.error("Cannot receive methods list from woken !");
LOGGER.trace(e.getMessage());
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
}
return ResponseEntity.ok(result.data().get());
}
}
package eu.hbp.mip.model; package eu.hbp.mip.model;
import com.google.gson.*; import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import eu.hbp.mip.messages.external.Algorithm; import eu.hbp.mip.messages.external.Algorithm;
import eu.hbp.mip.messages.external.ExperimentQuery; import eu.hbp.mip.messages.external.ExperimentQuery;
import eu.hbp.mip.messages.external.*; import eu.hbp.mip.messages.external.*;
import eu.hbp.mip.utils.TypesConvert;
import org.hibernate.annotations.Cascade; import org.hibernate.annotations.Cascade;
import scala.collection.JavaConverters; import scala.collection.JavaConverters;
import scala.collection.Seq; import scala.collection.Seq;
import scala.collection.immutable.HashMap;
import javax.persistence.*; import javax.persistence.*;
import java.lang.reflect.Type; import java.lang.reflect.Type;
...@@ -94,7 +97,7 @@ public class Experiment { ...@@ -94,7 +97,7 @@ public class Experiment {
List<eu.hbp.mip.model.Algorithm> algos = new Gson().fromJson(this.algorithms, algoList); List<eu.hbp.mip.model.Algorithm> algos = new Gson().fromJson(this.algorithms, algoList);
for (eu.hbp.mip.model.Algorithm a: algos for (eu.hbp.mip.model.Algorithm a: algos
) { ) {
algorithms.add(new Algorithm(a.getCode(), a.getName(), algoParamsToHashMap(a.getParameters()))); algorithms.add(new Algorithm(a.getCode(), a.getName(), TypesConvert.algoParamsToHashMap(a.getParameters())));
} }
List<Validation> validations = new LinkedList<>(); List<Validation> validations = new LinkedList<>();
...@@ -102,15 +105,15 @@ public class Experiment { ...@@ -102,15 +105,15 @@ public class Experiment {
List<eu.hbp.mip.model.ExperimentValidator> valids = new Gson().fromJson(this.validations, validList); List<eu.hbp.mip.model.ExperimentValidator> valids = new Gson().fromJson(this.validations, validList);
for (ExperimentValidator v: valids for (ExperimentValidator v: valids
) { ) {
validations.add(new Validation(v.getCode(), v.getName(), algoParamsToHashMap(v.getParameters()))); validations.add(new Validation(v.getCode(), v.getName(), TypesConvert.algoParamsToHashMap(v.getParameters())));
} }
Seq<VariableId> variablesSeq = JavaConverters.asScalaIteratorConverter( Seq<VariableId> variablesSeq = JavaConverters.asScalaIteratorConverter(
variablesToVariableIds(model.getQuery().getVariables()).iterator()).asScala().toSeq().toList(); TypesConvert.variablesToVariableIds(model.getQuery().getVariables()).iterator()).asScala().toSeq().toList();
Seq<VariableId> covariablesSeq = JavaConverters.asScalaIteratorConverter( Seq<VariableId> covariablesSeq = JavaConverters.asScalaIteratorConverter(
variablesToVariableIds(model.getQuery().getCovariables()).iterator()).asScala().toSeq().toList(); TypesConvert.variablesToVariableIds(model.getQuery().getCovariables()).iterator()).asScala().toSeq().toList();
Seq<VariableId> groupingSeq = JavaConverters.asScalaIteratorConverter( Seq<VariableId> groupingSeq = JavaConverters.asScalaIteratorConverter(
variablesToVariableIds(model.getQuery().getGrouping()).iterator()).asScala().toSeq().toList(); TypesConvert.variablesToVariableIds(model.getQuery().getGrouping()).iterator()).asScala().toSeq().toList();
Seq<Filter> filtersSeq = JavaConverters.asScalaIteratorConverter( Seq<Filter> filtersSeq = JavaConverters.asScalaIteratorConverter(
new LinkedList<Filter>().iterator()).asScala().toSeq().toList(); new LinkedList<Filter>().iterator()).asScala().toSeq().toList();
Seq<Algorithm> algorithmsSeq = JavaConverters.asScalaIteratorConverter( Seq<Algorithm> algorithmsSeq = JavaConverters.asScalaIteratorConverter(
...@@ -121,23 +124,6 @@ public class Experiment { ...@@ -121,23 +124,6 @@ public class Experiment {
return new ExperimentQuery(variablesSeq, covariablesSeq, groupingSeq, filtersSeq, algorithmsSeq, validationsSeq); return new ExperimentQuery(variablesSeq, covariablesSeq, groupingSeq, filtersSeq, algorithmsSeq, validationsSeq);
} }
private HashMap<String, String> algoParamsToHashMap(List<AlgorithmParam> aps) {
HashMap<String, String> params = new HashMap<>();
for (AlgorithmParam ap: aps
) {
params = params.updated(ap.getCode(), ap.getValue());
}
return params;
}
private List<VariableId> variablesToVariableIds(List<Variable> vars) {
List<VariableId> varIds = new LinkedList<>();
for (Variable v: vars
) {
varIds.add(new VariableId(v.getCode()));
}
return varIds;
}
public String computeExaremeQuery() { public String computeExaremeQuery() {
List<ExaremeQueryElement> queryElements = new LinkedList<>(); List<ExaremeQueryElement> queryElements = new LinkedList<>();
......
package eu.hbp.mip.model;
import eu.hbp.mip.messages.external.Filter;
import eu.hbp.mip.messages.external.VariableId;
import eu.hbp.mip.utils.TypesConvert;
import scala.collection.JavaConverters;
import scala.collection.Seq;
import java.util.LinkedList;
import java.util.List;
/**
* Created by mirco on 06.01.17.
*/
public class MiningQuery {
private List<Variable> variables;
private List<Variable> covariables;
private List<Variable> grouping;
private List<Filter> filters;
private Algorithm algorithm;
public List<Variable> getVariables() {
return variables;
}
public void setVariables(List<Variable> variables) {
this.variables = variables;
}
public List<Variable> getCovariables() {
return covariables;
}
public void setCovariables(List<Variable> covariables) {
this.covariables = covariables;
}
public List<Variable> getGrouping() {
return grouping;
}
public void setGrouping(List<Variable> grouping) {
this.grouping = grouping;
}
public List<Filter> getFilters() {
return filters;
}
public void setFilters(List<Filter> filters) {
this.filters = filters;
}
public Algorithm getAlgorithm() {
return algorithm;
}
public void setAlgorithm(Algorithm algorithm) {
this.algorithm = algorithm;
}
public eu.hbp.mip.messages.external.MiningQuery prepareQuery() {
eu.hbp.mip.messages.external.Algorithm scalaAlgorithm = new eu.hbp.mip.messages.external.Algorithm(
algorithm.getCode(), algorithm.getName(), TypesConvert.algoParamsToHashMap(algorithm.getParameters()));
Seq<VariableId> variablesSeq = JavaConverters.asScalaIteratorConverter(
TypesConvert.variablesToVariableIds(variables).iterator()).asScala().toSeq().toList();
Seq<VariableId> covariablesSeq = JavaConverters.asScalaIteratorConverter(
TypesConvert.variablesToVariableIds(covariables).iterator()).asScala().toSeq().toList();
Seq<VariableId> groupingSeq = JavaConverters.asScalaIteratorConverter(
TypesConvert.variablesToVariableIds(grouping).iterator()).asScala().toSeq().toList();
Seq<Filter> filtersSeq = JavaConverters.asScalaIteratorConverter(
new LinkedList<Filter>().iterator()).asScala().toSeq().toList();
return new eu.hbp.mip.messages.external.MiningQuery(variablesSeq, covariablesSeq,groupingSeq,filtersSeq, scalaAlgorithm);
}
}
package eu.hbp.mip.utils;
import eu.hbp.mip.messages.external.VariableId;
import eu.hbp.mip.model.AlgorithmParam;
import eu.hbp.mip.model.Variable;
import scala.collection.immutable.HashMap;
import java.util.LinkedList;
import java.util.List;
/**
* Created by mirco on 06.01.17.
*/
public class TypesConvert {
public static List<VariableId> variablesToVariableIds(List<Variable> vars) {
List<VariableId> varIds = new LinkedList<>();
for (Variable v: vars
) {
varIds.add(new VariableId(v.getCode()));
}
return varIds;
}
public static HashMap<String, String> algoParamsToHashMap(List<AlgorithmParam> aps) {
HashMap<String, String> params = new HashMap<>();
for (AlgorithmParam ap: aps
) {
params = params.updated(ap.getCode(), ap.getValue());
}
return params;
}
}
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