2018-02-20 08:39:31 +01:00
|
|
|
package org.gcube.nlphub.nlp;
|
|
|
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import org.gcube.data.analysis.dataminermanagercl.server.dmservice.SClient;
|
|
|
|
import org.gcube.data.analysis.dataminermanagercl.shared.data.OutputData;
|
|
|
|
import org.gcube.data.analysis.dataminermanagercl.shared.data.computations.ComputationId;
|
|
|
|
import org.gcube.data.analysis.dataminermanagercl.shared.data.output.FileResource;
|
|
|
|
import org.gcube.data.analysis.dataminermanagercl.shared.data.output.MapResource;
|
|
|
|
import org.gcube.data.analysis.dataminermanagercl.shared.data.output.Resource;
|
|
|
|
import org.gcube.data.analysis.dataminermanagercl.shared.data.output.Resource.ResourceType;
|
|
|
|
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.FileParameter;
|
|
|
|
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ObjectParameter;
|
|
|
|
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.ListParameter;
|
|
|
|
import org.gcube.data.analysis.dataminermanagercl.shared.parameters.Parameter;
|
|
|
|
import org.gcube.nlphub.NLPUploader;
|
|
|
|
import org.gcube.nlphub.legacy.Constants;
|
|
|
|
import org.gcube.nlphub.legacy.DataminerClient;
|
|
|
|
import org.gcube.nlphub.legacy.JsonManager;
|
|
|
|
import org.gcube.nlphub.legacy.NlpHubException;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
|
|
|
public class NlpNerRunner extends DataminerClient {
|
|
|
|
private Logger logger = Logger.getLogger(NlpNerRunner.class.getSimpleName());
|
|
|
|
private HttpServletResponse response = null;
|
|
|
|
private ArrayList<String> outputLinks;
|
|
|
|
private String[] identifiers;
|
|
|
|
private String currentId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This constructor is useful for local test cases...
|
|
|
|
*
|
|
|
|
* @param service
|
|
|
|
* @param identifier
|
|
|
|
* @param token
|
|
|
|
*/
|
|
|
|
public NlpNerRunner(String service, String identifier, String token) {
|
|
|
|
super(service, identifier, token);
|
|
|
|
outputLinks = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This constructor is the "canonical one"
|
|
|
|
*
|
|
|
|
* @param service
|
|
|
|
* @param identifier
|
|
|
|
* @param token
|
|
|
|
* @param response
|
|
|
|
*/
|
|
|
|
public NlpNerRunner(String service, String[] identifiers, String token) {
|
|
|
|
super(service, "", token);
|
|
|
|
outputLinks = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This constructor is useful when multiple alogrithm identifiers are given
|
|
|
|
*
|
|
|
|
* @param service
|
|
|
|
* @param identifiers
|
|
|
|
* @param token
|
|
|
|
*/
|
|
|
|
public NlpNerRunner(String service, String[] identifiers, String token, HttpServletResponse response) {
|
|
|
|
super(service, "", token);
|
|
|
|
this.response = response;
|
|
|
|
this.identifiers = identifiers;
|
|
|
|
outputLinks = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void retrieveOutput(ComputationId computationId, SClient sClient) {
|
|
|
|
try {
|
|
|
|
OutputData output = sClient.getOutputDataByComputationId(computationId);
|
|
|
|
Resource resource = output.getResource();
|
|
|
|
if (resource.isMap()) {
|
|
|
|
MapResource mapResource = (MapResource) resource;
|
|
|
|
for (String key : mapResource.getMap().keySet()) {
|
|
|
|
Resource r = mapResource.getMap().get(key);
|
|
|
|
if (r.isFile()) {
|
|
|
|
FileResource f = (FileResource) r;
|
2018-03-06 14:26:22 +01:00
|
|
|
String mimeType = f.getMimeType();
|
|
|
|
if (mimeType.equalsIgnoreCase("application/d4science")) {
|
|
|
|
String link = f.getUrl();
|
|
|
|
System.out.println("url: " + link);
|
2018-03-08 14:28:45 +01:00
|
|
|
logger.debug("url: " + link);
|
2018-03-06 14:26:22 +01:00
|
|
|
String op = computationId.getOperatorId();
|
|
|
|
op = op.substring(op.lastIndexOf(".") + 1);
|
|
|
|
testEndOfProcess(op + ":::" + link);
|
|
|
|
}
|
2018-02-20 08:39:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e.getLocalizedMessage());
|
|
|
|
writeResponse(e.getLocalizedMessage(), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run(String filePublicLink, String annotations, String language) throws NlpHubException {
|
|
|
|
for (String id : identifiers) {
|
|
|
|
try {
|
|
|
|
super.identifier = id;
|
2018-03-09 14:40:21 +01:00
|
|
|
System.out.println("Running: " + id);
|
2018-02-20 08:39:31 +01:00
|
|
|
super.init();
|
|
|
|
List<Parameter> parameters = mapParameters(filePublicLink, annotations);
|
|
|
|
super.execute(parameters);
|
2018-03-09 14:40:21 +01:00
|
|
|
System.out.println("Runned: " + id);
|
2018-02-20 08:39:31 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e.getLocalizedMessage());
|
|
|
|
throw new NlpHubException(e.getLocalizedMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<String> getOutputLinks() {
|
|
|
|
return outputLinks;
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void testEndOfProcess(String link) {
|
|
|
|
outputLinks.add(link);
|
|
|
|
if (outputLinks.size() == identifiers.length) {
|
|
|
|
String[] links = new String[outputLinks.size()];
|
|
|
|
links = outputLinks.toArray(links);
|
2018-03-09 14:40:21 +01:00
|
|
|
//System.out.println("testEndOfProcess");
|
2018-02-20 08:39:31 +01:00
|
|
|
writeResponse(links);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeResponse(String[] content) {
|
|
|
|
if (response != null) {
|
|
|
|
response.setContentType("application/json;charset=UTF-8");
|
2018-03-06 14:26:22 +01:00
|
|
|
PrintWriter writer = null;
|
2018-02-20 08:39:31 +01:00
|
|
|
try {
|
2018-03-06 14:26:22 +01:00
|
|
|
writer = response.getWriter();
|
|
|
|
String msg = new JsonManager().getSuccessJsonResponse(content);
|
|
|
|
System.out.println("msg: " + msg);
|
|
|
|
writer.println(msg);
|
|
|
|
if(writer.checkError()) {
|
|
|
|
logger.error("writeResponse method has some problem in writing result.");
|
|
|
|
System.out.println("writeResponse method has some problem in writing result.");
|
|
|
|
}
|
|
|
|
|
2018-02-20 08:39:31 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
logger.error(ex.getLocalizedMessage());
|
2018-03-06 14:26:22 +01:00
|
|
|
ex.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
if(writer != null)
|
|
|
|
writer.close();
|
2018-02-20 08:39:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeResponse(String content, boolean isOk) {
|
|
|
|
if (response != null) {
|
|
|
|
response.setContentType("application/json;charset=UTF-8");
|
2018-03-06 14:26:22 +01:00
|
|
|
PrintWriter writer = null;
|
2018-02-20 08:39:31 +01:00
|
|
|
try {
|
2018-03-06 14:26:22 +01:00
|
|
|
writer = response.getWriter();
|
2018-02-20 08:39:31 +01:00
|
|
|
if (isOk) {
|
|
|
|
writer.println(new JsonManager().getSuccessJsonResponse("" + content));
|
|
|
|
} else {
|
|
|
|
writer.println(new JsonManager().getErrorJsonResponse("" + content));
|
|
|
|
}
|
2018-03-06 14:26:22 +01:00
|
|
|
writer.flush();
|
2018-02-20 08:39:31 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
logger.error(ex.getLocalizedMessage());
|
2018-03-06 14:26:22 +01:00
|
|
|
} finally {
|
|
|
|
if(writer != null)
|
|
|
|
writer.close();
|
2018-02-20 08:39:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<Parameter> mapParameters(String publicLink, String annotations) {
|
|
|
|
List<Parameter> parameters = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
List<Parameter> inputParameters = super.getOperatorInputParameters();
|
2018-03-08 14:28:45 +01:00
|
|
|
//System.out.println("n. " + inputParameters.size());
|
2018-02-20 08:39:31 +01:00
|
|
|
for (Parameter p : inputParameters) {
|
|
|
|
switch (p.getTypology()) {
|
|
|
|
case FILE:
|
|
|
|
FileParameter fileName = new FileParameter();
|
|
|
|
fileName.setName(p.getName());
|
|
|
|
fileName.setValue(publicLink);
|
|
|
|
parameters.add(fileName);
|
|
|
|
System.out.println(fileName.toString());
|
|
|
|
break;
|
|
|
|
case LIST:
|
|
|
|
ListParameter list = new ListParameter();
|
|
|
|
list.setName(p.getName());
|
|
|
|
list.setValue(annotations.replace(",", "|"));
|
|
|
|
parameters.add(list);
|
|
|
|
System.out.println(list.toString());
|
|
|
|
break;
|
|
|
|
case ENUM:
|
|
|
|
// to be managed...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
logger.error(ex.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
return parameters;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* public static void main(String[] args) { String service =
|
|
|
|
* "http://dataminer-prototypes.d4science.org/wps/"; String identifier = "";
|
|
|
|
* identifier =
|
|
|
|
* "org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.ENGLISH_NAMED_ENTITY_RECOGNIZER";
|
2018-03-06 14:26:22 +01:00
|
|
|
* // identifier = // //
|
2018-02-20 08:39:31 +01:00
|
|
|
* "org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.NEWSTANBOLWRAPPER";
|
2018-03-06 14:26:22 +01:00
|
|
|
* // identifier = // //
|
2018-02-20 08:39:31 +01:00
|
|
|
* "org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses.transducerers.GENERIC_OPINION_MINING_ENGLISH";
|
|
|
|
* String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462"; String
|
|
|
|
* pLink =
|
|
|
|
* "http://data.d4science.org/RW9KZDJ0eU9ZZlRNWEhLYmtVa3ltL0Y1UWdhdmZvTlVHbWJQNStIS0N6Yz0";
|
|
|
|
*
|
|
|
|
* ArrayList<NlpParameter> params = new ArrayList<>(); NlpParameter p = new
|
|
|
|
* NlpParameter(NlpParameter.ANNOTATION_LIST, "", "Person,Token", 0);
|
|
|
|
* params.add(p);
|
|
|
|
*
|
|
|
|
* NlpNerRunner runner = new NlpNerRunner(service, identifier, token); //
|
2018-03-06 14:26:22 +01:00
|
|
|
* try { runner.run(pLink, service, identifier, params); } catch
|
|
|
|
* (NlpHubException e) { e.printStackTrace(); } }
|
2018-02-20 08:39:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
}
|