diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/AlgorithmGenerator.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/AlgorithmGenerator.java index 95c0dc8..687b4bd 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/AlgorithmGenerator.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/AlgorithmGenerator.java @@ -1,16 +1,20 @@ package org.gcube.portlets.user.statisticalalgorithmsimporter.server.generator; -import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.util.Arrays; 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.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.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -21,6 +25,9 @@ import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Proj 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) { @@ -33,23 +40,240 @@ public class AlgorithmGenerator { return "AlgorithmGenerator [project=" + project + "]"; } - public File createAlgorithm() throws StatAlgoImporterServiceException { + public Path createAlgorithm() throws StatAlgoImporterServiceException, + IOException { + try { - File fileTmp = File.createTempFile(project.getClassName(), + Path tempFile = Files.createTempFile(project.getClassName(), EXTENTION_JAVA); - fileTmp.deleteOnExit(); - /* - List lines = Arrays.asList("The first line", "The second line"); - Path file = Paths.g; - Files.write(file, lines, Charset.forName("UTF-8")); - */ - return fileTmp; + + List 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()); + throw new StatAlgoImporterServiceException(e.getLocalizedMessage(), + e); } } + private List createJavaCode() { + String mainScriptRelativePath = retrieveMainScriptRelativePath(); + String packageUrl = retrievePackageUrl(); + + List 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();"); + + 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 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 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(); + } + } diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/project/Project.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/project/Project.java index aabec9b..7e452cb 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/project/Project.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/project/Project.java @@ -9,8 +9,7 @@ import java.io.Serializable; * */ public class Project implements Serializable { - - + private static final long serialVersionUID = -7906477664944910362L; private String name; @@ -19,6 +18,7 @@ public class Project implements Serializable { private ProjectFolder projectFolder; private MainCode mainCode; private InputData inputData; + private ProjectTarget projectTarget; public Project() { super(); @@ -78,13 +78,25 @@ public class Project implements Serializable { this.inputData = inputData; } + public ProjectTarget getProjectTarget() { + return projectTarget; + } + + public void setProjectTarget(ProjectTarget projectTarget) { + this.projectTarget = projectTarget; + } + @Override public String toString() { return "Project [name=" + name + ", description=" + description + ", className=" + className + ", projectFolder=" + projectFolder + ", mainCode=" + mainCode + ", inputData=" - + inputData + "]"; + + inputData + ", projectTarget=" + projectTarget + "]"; } + + + + } diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/project/ProjectTarget.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/project/ProjectTarget.java new file mode 100644 index 0000000..4ae0dca --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/project/ProjectTarget.java @@ -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: g.panichi@isti.cnr.it + * + */ +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 + "]"; + } + +} diff --git a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/workspace/ItemDescription.java b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/workspace/ItemDescription.java index da5ac1f..6ec39f8 100644 --- a/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/workspace/ItemDescription.java +++ b/src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/shared/workspace/ItemDescription.java @@ -17,6 +17,7 @@ public class ItemDescription implements Serializable { private String owner; private String path; private String type; + private String publicLink; public ItemDescription(){ super(); @@ -71,13 +72,26 @@ public class ItemDescription implements Serializable { public void setType(String type) { this.type = type; } + + public String getPublicLink() { + return publicLink; + } + + public void setPublicLink(String publicLink) { + this.publicLink = publicLink; + } @Override public String toString() { return "ItemDescription [id=" + id + ", name=" + name + ", owner=" - + owner + ", path=" + path + ", type=" + type + "]"; + + owner + ", path=" + path + ", type=" + type + ", publicLink=" + + publicLink + "]"; } + + + + } diff --git a/src/test/resources/org/gcube/portlets/user/statisticalalgorithmsimporter/example/FileTemplate.java b/src/test/resources/org/gcube/portlets/user/statisticalalgorithmsimporter/example/FileTemplate.java index 24298e0..8e0fb3a 100644 --- a/src/test/resources/org/gcube/portlets/user/statisticalalgorithmsimporter/example/FileTemplate.java +++ b/src/test/resources/org/gcube/portlets/user/statisticalalgorithmsimporter/example/FileTemplate.java @@ -3,7 +3,7 @@ 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; @@ -11,6 +11,11 @@ import org.gcube.dataanalysis.executor.rscripts.generic.GenericRScript; public class KnitrCompiler extends GenericRScript { + public static enum operators { + EQUAL, NOT_EQUAL, CONTAINS, BEGINS_WITH, ENDS_WITH + }; + + @Override public String getDescription() { 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 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(String.class.getName(), null, PrimitiveTypes.STRING, "file.inout", "The name of the R file in the zip package", "main.r")); }