1452: Implement a GUI for StatMan Algorithms Importer

Task-Url: https://support.d4science.org/issues/1452

Updated Code Generator

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/statistical-algorithms-importer@122182 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2016-01-11 11:45:05 +00:00
parent d5fe4657c5
commit 3cdac18166
5 changed files with 330 additions and 19 deletions

View File

@ -1,16 +1,20 @@
package org.gcube.portlets.user.statisticalalgorithmsimporter.server.generator; package org.gcube.portlets.user.statisticalalgorithmsimporter.server.generator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.StandardOpenOption;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.apache.commons.io.IOUtils;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterServiceException; import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterServiceException;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.EnvironmentVariables;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.IOType;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.SelectedRowsVariables;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Project; import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Project;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* *
@ -21,6 +25,9 @@ import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Proj
public class AlgorithmGenerator { public class AlgorithmGenerator {
private static final String EXTENTION_JAVA = ".java"; private static final String EXTENTION_JAVA = ".java";
public static final Logger logger = LoggerFactory
.getLogger(AlgorithmGenerator.class);
private Project project; private Project project;
public AlgorithmGenerator(Project project) { public AlgorithmGenerator(Project project) {
@ -33,23 +40,240 @@ public class AlgorithmGenerator {
return "AlgorithmGenerator [project=" + project + "]"; return "AlgorithmGenerator [project=" + project + "]";
} }
public File createAlgorithm() throws StatAlgoImporterServiceException { public Path createAlgorithm() throws StatAlgoImporterServiceException,
IOException {
try { try {
File fileTmp = File.createTempFile(project.getClassName(), Path tempFile = Files.createTempFile(project.getClassName(),
EXTENTION_JAVA); EXTENTION_JAVA);
fileTmp.deleteOnExit();
/* List<String> lines = createJavaCode();
List<String> lines = Arrays.asList("The first line", "The second line"); Files.write(tempFile, lines, Charset.defaultCharset(),
Path file = Paths.g; StandardOpenOption.WRITE);
Files.write(file, lines, Charset.forName("UTF-8")); logger.debug(tempFile.toString());
*/ return tempFile;
return fileTmp;
} catch (IOException e) { } catch (IOException e) {
logger.error(e.getLocalizedMessage());
e.printStackTrace(); e.printStackTrace();
throw new StatAlgoImporterServiceException(e.getLocalizedMessage()); throw new StatAlgoImporterServiceException(e.getLocalizedMessage(),
e);
} }
} }
private List<String> createJavaCode() {
String mainScriptRelativePath = retrieveMainScriptRelativePath();
String packageUrl = retrievePackageUrl();
List<String> code = 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.getClassName()
+ " extends GenericRScript {", "",
" public static enum operators {",
" EQUAL, NOT_EQUAL, CONTAINS, BEGINS_WITH, ENDS_WITH",
" };",
" @Override", " public String getDescription() {",
" return \"" + project.getDescription() + "\";", " }",
"", " protected void initVariables(){",
" mainScriptName=\"" + mainScriptRelativePath + "\";",
" packageURL=\"" + packageUrl + "\";",
" environmentalvariables = new ArrayList<String>();");
for (EnvironmentVariables envVariable : project.getInputData()
.getListEnvironmentVariables()) {
code.add(" environmentalvariables.add(\"" + envVariable.getName()
+ "\")");
}
for (SelectedRowsVariables selVariable : project.getInputData()
.getListSelectedRows()) {
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(" @Override");
code.add(" protected void setInputParameters() {");
createInputParameters(code);
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(List<String> code) {
for (SelectedRowsVariables selVariable : project.getInputData()
.getListSelectedRows()) {
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(List<String> code) {
for (SelectedRowsVariables selVariable : project.getInputData()
.getListSelectedRows()) {
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().getItemDescription()
.getPath();
String mainCodePath = project.getMainCode().getItemDescription()
.getPath();
return mainCodePath.substring(projectPath.length());
}
private String retrievePackageUrl() {
return project.getProjectTarget().getPackageUrl().getPublicLink();
}
} }

View File

@ -9,8 +9,7 @@ import java.io.Serializable;
* *
*/ */
public class Project implements Serializable { public class Project implements Serializable {
private static final long serialVersionUID = -7906477664944910362L; private static final long serialVersionUID = -7906477664944910362L;
private String name; private String name;
@ -19,6 +18,7 @@ public class Project implements Serializable {
private ProjectFolder projectFolder; private ProjectFolder projectFolder;
private MainCode mainCode; private MainCode mainCode;
private InputData inputData; private InputData inputData;
private ProjectTarget projectTarget;
public Project() { public Project() {
super(); super();
@ -78,13 +78,25 @@ public class Project implements Serializable {
this.inputData = inputData; this.inputData = inputData;
} }
public ProjectTarget getProjectTarget() {
return projectTarget;
}
public void setProjectTarget(ProjectTarget projectTarget) {
this.projectTarget = projectTarget;
}
@Override @Override
public String toString() { public String toString() {
return "Project [name=" + name + ", description=" + description return "Project [name=" + name + ", description=" + description
+ ", className=" + className + ", projectFolder=" + ", className=" + className + ", projectFolder="
+ projectFolder + ", mainCode=" + mainCode + ", inputData=" + projectFolder + ", mainCode=" + mainCode + ", inputData="
+ inputData + "]"; + inputData + ", projectTarget=" + projectTarget + "]";
} }
} }

View File

@ -0,0 +1,54 @@
package org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project;
import java.io.Serializable;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.workspace.ItemDescription;
/**
*
* @author Giancarlo Panichi email: <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class ProjectTarget implements Serializable {
private static final long serialVersionUID = 480665662744105383L;
private ItemDescription packageUrl;
private ItemDescription codeSource;
private ItemDescription codeJar;
public ProjectTarget() {
super();
}
public ItemDescription getPackageUrl() {
return packageUrl;
}
public void setPackageUrl(ItemDescription packageUrl) {
this.packageUrl = packageUrl;
}
public ItemDescription getCodeSource() {
return codeSource;
}
public void setCodeSource(ItemDescription codeSource) {
this.codeSource = codeSource;
}
public ItemDescription getCodeJar() {
return codeJar;
}
public void setCodeJar(ItemDescription codeJar) {
this.codeJar = codeJar;
}
@Override
public String toString() {
return "ProjectTarget [packageUrl=" + packageUrl + ", codeSource="
+ codeSource + ", codeJar=" + codeJar + "]";
}
}

View File

@ -17,6 +17,7 @@ public class ItemDescription implements Serializable {
private String owner; private String owner;
private String path; private String path;
private String type; private String type;
private String publicLink;
public ItemDescription(){ public ItemDescription(){
super(); super();
@ -71,13 +72,26 @@ public class ItemDescription implements Serializable {
public void setType(String type) { public void setType(String type) {
this.type = type; this.type = type;
} }
public String getPublicLink() {
return publicLink;
}
public void setPublicLink(String publicLink) {
this.publicLink = publicLink;
}
@Override @Override
public String toString() { public String toString() {
return "ItemDescription [id=" + id + ", name=" + name + ", owner=" return "ItemDescription [id=" + id + ", name=" + name + ", owner="
+ owner + ", path=" + path + ", type=" + type + "]"; + owner + ", path=" + path + ", type=" + type + ", publicLink="
+ publicLink + "]";
} }
} }

View File

@ -3,7 +3,7 @@ package org.gcube.dataanalysis.executor.rscripts;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import org.gcube.dataanalysis.ecoengine.datatypes.PrimitiveType; import org.gcube.dataanalysis.ecoengine.datatypes.PrimitiveType;
import org.gcube.dataanalysis.ecoengine.datatypes.StatisticalType; import org.gcube.dataanalysis.ecoengine.datatypes.StatisticalType;
import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.PrimitiveTypes; import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.PrimitiveTypes;
@ -11,6 +11,11 @@ import org.gcube.dataanalysis.executor.rscripts.generic.GenericRScript;
public class KnitrCompiler extends GenericRScript { public class KnitrCompiler extends GenericRScript {
public static enum operators {
EQUAL, NOT_EQUAL, CONTAINS, BEGINS_WITH, ENDS_WITH
};
@Override @Override
public String getDescription() { public String getDescription() {
return "An algorithm to compile Knitr documents. Developed by IRD (reference Julien Bard, julien.barde@ird.fr)"; return "An algorithm to compile Knitr documents. Developed by IRD (reference Julien Bard, julien.barde@ird.fr)";
@ -28,6 +33,8 @@ public class KnitrCompiler extends GenericRScript {
@Override @Override
protected void setInputParameters() { protected void setInputParameters() {
inputs.add(new PrimitiveType(Enum.class.getName(), operators.values(), PrimitiveTypes.ENUMERATED, name, description, defaultvalue));
inputs.add(new PrimitiveType(File.class.getName(), null, PrimitiveTypes.FILE, "zipfile", "The file containing R and the markdown (Rnw) files to compile","knitr_wfs.zip")); inputs.add(new PrimitiveType(File.class.getName(), null, PrimitiveTypes.FILE, "zipfile", "The file containing R and the markdown (Rnw) files to compile","knitr_wfs.zip"));
inputs.add(new PrimitiveType(String.class.getName(), null, PrimitiveTypes.STRING, "file.inout", "The name of the R file in the zip package", "main.r")); inputs.add(new PrimitiveType(String.class.getName(), null, PrimitiveTypes.STRING, "file.inout", "The name of the R file in the zip package", "main.r"));
} }