300 lines
9.3 KiB
Java
300 lines
9.3 KiB
Java
package org.gcube.portlets.user.statisticalalgorithmsimporter.server.generator;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.StandardOpenOption;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterServiceException;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.GlobalVariables;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.IOType;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.InputOutputVariables;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Project;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
*
|
|
* @author Giancarlo Panichi email: <a
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
*
|
|
*/
|
|
public class AlgorithmGenerator {
|
|
|
|
private static final String EXTENTION_JAVA = ".java";
|
|
public static final Logger logger = LoggerFactory
|
|
.getLogger(AlgorithmGenerator.class);
|
|
|
|
private Project project;
|
|
|
|
public AlgorithmGenerator(Project project) {
|
|
super();
|
|
this.project = project;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "AlgorithmGenerator [project=" + project + "]";
|
|
}
|
|
|
|
public Path createAlgorithm() throws StatAlgoImporterServiceException {
|
|
|
|
try {
|
|
Path tempFile = Files.createTempFile(project.getInputData()
|
|
.getProjectInfo().getAlgorithmNameToClassName(), EXTENTION_JAVA);
|
|
|
|
List<String> lines = createJavaCode();
|
|
Files.write(tempFile, lines, Charset.defaultCharset(),
|
|
StandardOpenOption.WRITE);
|
|
logger.debug(tempFile.toString());
|
|
return tempFile;
|
|
|
|
} catch (IOException e) {
|
|
logger.error(e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage(),
|
|
e);
|
|
}
|
|
|
|
}
|
|
|
|
private List<String> createJavaCode() {
|
|
String mainScriptRelativePath = retrieveMainScriptRelativePath();
|
|
String packageUrl = retrievePackageUrl();
|
|
|
|
ArrayList<String> code = new ArrayList<String>();
|
|
|
|
code.addAll(Arrays
|
|
.asList("package org.gcube.dataanalysis.executor.rscripts;",
|
|
"",
|
|
"import java.io.File;",
|
|
"import java.util.ArrayList;",
|
|
"import java.util.LinkedHashMap;",
|
|
"import org.gcube.dataanalysis.ecoengine.datatypes.PrimitiveType;",
|
|
"import org.gcube.dataanalysis.ecoengine.datatypes.StatisticalType;",
|
|
"import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.PrimitiveTypes;",
|
|
"import org.gcube.dataanalysis.executor.rscripts.generic.GenericRScript;",
|
|
"", "public class "
|
|
+ project.getInputData().getProjectInfo()
|
|
.getAlgorithmNameToClassName()
|
|
+ " extends GenericRScript {", "",
|
|
|
|
" public static enum operators {",
|
|
" EQUAL, NOT_EQUAL, CONTAINS, BEGINS_WITH, ENDS_WITH",
|
|
" };", "", " @Override",
|
|
" public String getDescription() {", " return \""
|
|
+ project.getInputData().getProjectInfo()
|
|
.getAlgorithmDescription() + "\";", " }", "",
|
|
" protected void initVariables(){",
|
|
" mainScriptName=\"" + mainScriptRelativePath + "\";",
|
|
" packageURL=\"" + packageUrl + "\";",
|
|
" environmentalvariables = new ArrayList<String>();"));
|
|
|
|
for (GlobalVariables envVariable : project.getInputData()
|
|
.getListGlobalVariables()) {
|
|
code.add(" environmentalvariables.add(\"" + envVariable.getName()
|
|
+ "\");");
|
|
}
|
|
|
|
for (InputOutputVariables selVariable : project.getInputData()
|
|
.getListInputOutputVariables()) {
|
|
switch (selVariable.getIoType()) {
|
|
case INPUT:
|
|
code.add(" inputvariables.add(\"" + selVariable.getName()
|
|
+ "\");");
|
|
break;
|
|
case OUTPUT:
|
|
code.add(" outputvariables.add(\"" + selVariable.getName()
|
|
+ "\");");
|
|
break;
|
|
default:
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
code.add(" }");
|
|
code.add("");
|
|
code.add(" @Override");
|
|
code.add(" protected void setInputParameters() {");
|
|
createInputParameters(code);
|
|
|
|
code.add(" }");
|
|
code.add("");
|
|
code.add(" @Override");
|
|
code.add(" public StatisticalType getOutput() {");
|
|
createOutputParameters(code);
|
|
code.add(" PrimitiveType o = new PrimitiveType(LinkedHashMap.class.getName(), output, PrimitiveTypes.MAP, \"Output\", \"\");");
|
|
code.add(" return o;");
|
|
code.add(" }");
|
|
code.add("}");
|
|
return code;
|
|
}
|
|
|
|
private void createInputParameters(ArrayList<String> code) {
|
|
|
|
for (InputOutputVariables selVariable : project.getInputData()
|
|
.getListInputOutputVariables()) {
|
|
if (selVariable.getIoType().compareTo(IOType.INPUT) == 0) {
|
|
switch (selVariable.getDataType()) {
|
|
case BOOLEAN:
|
|
code.add(" inputs.add(new PrimitiveType(Boolean.class.getName(), null,PrimitiveTypes.BOOLEAN, \""
|
|
+ selVariable.getName()
|
|
+ "\", \""
|
|
+ selVariable.getDescription()
|
|
+ "\", \""
|
|
+ selVariable.getDefaultValue() + "\"));");
|
|
break;
|
|
case DOUBLE:
|
|
code.add(" inputs.add(new PrimitiveType(Double.class.getName(), null,PrimitiveTypes.NUMBER, \""
|
|
+ selVariable.getName()
|
|
+ "\", \""
|
|
+ selVariable.getDescription()
|
|
+ "\", \""
|
|
+ selVariable.getDefaultValue() + "\"));");
|
|
break;
|
|
case ENUMERATED:
|
|
code.add(" inputs.add(new PrimitiveType(Enum.class.getName(), operators.values(),PrimitiveTypes.ENUMERATED, \""
|
|
+ selVariable.getName()
|
|
+ "\", \""
|
|
+ selVariable.getDescription()
|
|
+ "\", \""
|
|
+ selVariable.getDefaultValue() + "\"));");
|
|
break;
|
|
case FILE:
|
|
code.add(" inputs.add(new PrimitiveType(File.class.getName(), null,PrimitiveTypes.FILE, \""
|
|
+ selVariable.getName()
|
|
+ "\", \""
|
|
+ selVariable.getDescription()
|
|
+ "\", \""
|
|
+ selVariable.getDefaultValue() + "\"));");
|
|
|
|
break;
|
|
case INTEGER:
|
|
code.add(" inputs.add(new PrimitiveType(Integer.class.getName(), null,PrimitiveTypes.NUMBER, \""
|
|
+ selVariable.getName()
|
|
+ "\", \""
|
|
+ selVariable.getDescription()
|
|
+ "\", \""
|
|
+ selVariable.getDefaultValue() + "\"));");
|
|
break;
|
|
case STRING:
|
|
code.add(" inputs.add(new PrimitiveType(String.class.getName(), null,PrimitiveTypes.STRING, \""
|
|
+ selVariable.getName()
|
|
+ "\", \""
|
|
+ selVariable.getDescription()
|
|
+ "\", \""
|
|
+ selVariable.getDefaultValue() + "\"));");
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
* PrimitiveTypes
|
|
*
|
|
* STRING, NUMBER, ENUMERATED, CONSTANT, RANDOM, FILE, MAP, BOOLEAN, IMAGES
|
|
*/
|
|
|
|
private void createOutputParameters(ArrayList<String> code) {
|
|
for (InputOutputVariables selVariable : project.getInputData()
|
|
.getListInputOutputVariables()) {
|
|
if (selVariable.getIoType().compareTo(IOType.OUTPUT) == 0) {
|
|
switch (selVariable.getDataType()) {
|
|
case BOOLEAN:
|
|
code.add(" output.put(\""
|
|
+ selVariable.getName()
|
|
+ "\",new PrimitiveType(Boolean.class.getName(), new File(outputValues.get(\""
|
|
+ selVariable.getName()
|
|
+ "\")), PrimitiveTypes.BOOLEAN, \""
|
|
+ selVariable.getName() + "\", \""
|
|
+ selVariable.getName() + "\"));");
|
|
break;
|
|
case DOUBLE:
|
|
code.add(" output.put(\""
|
|
+ selVariable.getName()
|
|
+ "\",new PrimitiveType(Double.class.getName(), new File(outputValues.get(\""
|
|
+ selVariable.getName()
|
|
+ "\")), PrimitiveTypes.NUMBER, \""
|
|
+ selVariable.getName() + "\", \""
|
|
+ selVariable.getName() + "\"));");
|
|
break;
|
|
case ENUMERATED:
|
|
break;
|
|
case FILE:
|
|
code.add(" output.put(\""
|
|
+ selVariable.getName()
|
|
+ "\",new PrimitiveType(File.class.getName(), new File(outputValues.get(\""
|
|
+ selVariable.getName()
|
|
+ "\")), PrimitiveTypes.FILE, \""
|
|
+ selVariable.getName() + "\", \""
|
|
+ selVariable.getName() + "\"));");
|
|
|
|
break;
|
|
case INTEGER:
|
|
code.add(" output.put(\""
|
|
+ selVariable.getName()
|
|
+ "\",new PrimitiveType(Integer.class.getName(), new File(outputValues.get(\""
|
|
+ selVariable.getName()
|
|
+ "\")), PrimitiveTypes.NUMBER, \""
|
|
+ selVariable.getName() + "\", \""
|
|
+ selVariable.getName() + "\"));");
|
|
break;
|
|
case STRING:
|
|
code.add(" output.put(\""
|
|
+ selVariable.getName()
|
|
+ "\",new PrimitiveType(String.class.getName(), new File(outputValues.get(\""
|
|
+ selVariable.getName()
|
|
+ "\")), PrimitiveTypes.STRING, \""
|
|
+ selVariable.getName() + "\", \""
|
|
+ selVariable.getName() + "\"));");
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private String retrieveMainScriptRelativePath() {
|
|
String projectPath = project.getProjectFolder().getFolder()
|
|
.getPath();
|
|
String mainCodePath = project.getMainCode().getItemDescription()
|
|
.getPath();
|
|
|
|
String relativePath = project.getProjectFolder().getFolder()
|
|
.getName()
|
|
+ mainCodePath.substring(projectPath.length());
|
|
return relativePath;
|
|
|
|
}
|
|
|
|
private String retrievePackageUrl() {
|
|
String packageUrl = "";
|
|
if (project.getProjectTarget()!=null&&
|
|
project.getProjectTarget().getProjectDeploy() != null
|
|
&& project.getProjectTarget().getProjectDeploy().getPackageProject() != null) {
|
|
if (project.getProjectTarget().getProjectDeploy().getPackageProject().getPublicLink() != null) {
|
|
packageUrl = project.getProjectTarget().getProjectDeploy().getPackageProject()
|
|
.getPublicLink();
|
|
}
|
|
}
|
|
return packageUrl;
|
|
}
|
|
|
|
}
|