Gianpaolo Coro 2016-01-25 16:07:09 +00:00
parent 1f510c74a6
commit a3c03e56dd
1 changed files with 124 additions and 90 deletions

View File

@ -16,7 +16,6 @@ import org.gcube.dataanalysis.ecoengine.utils.ZipTools;
import org.gcube.dataanalysis.executor.util.LocalRScriptsManager;
import org.gcube.dataanalysis.executor.util.StorageUtils;
public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
// FIXED part
@ -31,6 +30,7 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
scriptmanager.stop();
System.gc();
}
@Override
public void init() throws Exception {
AnalysisLogger.getLogger().debug("Initializing " + this.getClass().getCanonicalName());
@ -39,6 +39,7 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
@Override
protected void process() throws Exception {
boolean deletefiles = false; // for test only
// init status
status = 0;
// instantiate the R Script executor
@ -57,7 +58,6 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
ZipTools.unZip(localPackage.getAbsolutePath(), folder.getAbsolutePath());
localPackage.delete();
// File folder = new File("C:/Users/coro/Desktop/WorkFolder/Workspace/EcologicalEngineSmartExecutor/./cfg/rscr_7d329495-b048-4ce0-8bcc-bd74966db56d/");
AnalysisLogger.getLogger().debug("Analysing inputs");
@ -68,7 +68,21 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
for (String input : inputvariables) {
String value = config.getParam(input);
String defaultValue = inputs.get(i).getDefaultValue();
inputParameters.put(defaultValue, value);
//inputParameters.put(defaultValue, value);
String punct = "[ \";]*";
String regexp = punct+input+punct+"(<-|=)"+punct+defaultValue+punct+".*";
boolean string = true;
try{
Double.parseDouble(value);
string = false;
}catch(Exception ee){
}
String line = input+"="+value;
if (string)
line = input+"=\""+value+"\"";
inputParameters.put(regexp, line);
i++;
}
AnalysisLogger.getLogger().debug("Inputs: " + inputParameters.toString());
@ -105,7 +119,8 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
status = 10;
AnalysisLogger.getLogger().debug("Executing the script...");
// execute the script in multi-user mode
scriptmanager.executeRScript(config, scriptName, null, environmentalParameters, outputParameters, null, null,inputParameters, scriptMustReturnAFile,uploadScriptOnTheInfrastructureWorkspace, true, scriptSubfolder);
scriptmanager.executeRScript(config, scriptName, null, environmentalParameters, outputParameters, null, null, inputParameters, scriptMustReturnAFile, uploadScriptOnTheInfrastructureWorkspace, deletefiles, scriptSubfolder);
AnalysisLogger.getLogger().debug("..execution finished!");
// get the output: one file should have been produced for each output
AnalysisLogger.getLogger().debug("Getting output");
@ -114,17 +129,21 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
AnalysisLogger.getLogger().debug("Output " + output + " - loading respective file from " + outPath.getAbsolutePath());
AnalysisLogger.getLogger().debug("File exists? " + outPath.exists());
String fileContent = FileTools.loadString(outPath.getAbsolutePath(), "UTF-8");
fileContent=analyseContent(fileContent);
fileContent = analyseContent(fileContent, scriptSubfolder);
AnalysisLogger.getLogger().debug("Retrieved output content: " + output + ": " + fileContent);
outputValues.put(output, fileContent);
}
// delete the script folder
AnalysisLogger.getLogger().debug("Deleting sandbox folder");
try {
if (deletefiles) {
AnalysisLogger.getLogger().debug("Deleting sandbox folder");
FileUtils.cleanDirectory(folder);
FileUtils.deleteDirectory(folder);
AnalysisLogger.getLogger().debug("Folder " + folder.getAbsolutePath() + " deleted");
}
} catch (Exception e) {
AnalysisLogger.getLogger().debug(e);
AnalysisLogger.getLogger().debug("Could not delete sandbox folder " + folder.getAbsolutePath());
@ -133,27 +152,43 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
status = 100;
}
protected String analyseContent(String filecontent) throws Exception{
String [] rows = filecontent.split("\n");
protected String analyseContent(String filecontent, String scriptfolder) throws Exception {
AnalysisLogger.getLogger().debug("Analysing file content");
String[] rows = filecontent.split(System.lineSeparator());
List<String> files = new ArrayList<String>();
for (String row : rows) {
if (row!=null)
AnalysisLogger.getLogger().debug("Analysing -> " + row);
if (row != null) {
row = row.replace("\"", "");
if ((new File(row)).exists()){
String preparedFile = new File(config.getConfigPath(),new File(row).getName()).getAbsolutePath();
AnalysisLogger.getLogger().debug("Copying "+row+" to "+preparedFile);
FileUtils.moveFileToDirectory(new File(row), new File(config.getConfigPath()), false);
File rowFile = new File(row.trim());
AnalysisLogger.getLogger().debug("Checking row file-> "+rowFile.getAbsolutePath());
if (!rowFile.exists()){
rowFile = new File(scriptfolder, row.trim());
AnalysisLogger.getLogger().debug("File does not exist - checking complete row file-> "+rowFile.getAbsolutePath());
}
if (rowFile.exists()) {
String preparedFile = new File(config.getConfigPath(), rowFile.getName()).getAbsolutePath();
AnalysisLogger.getLogger().debug("Copying " + rowFile.getAbsolutePath() + " to " + preparedFile);
FileUtils.moveFileToDirectory(rowFile, new File(config.getConfigPath()), false);
files.add(preparedFile);
break;
}
else
AnalysisLogger.getLogger().debug("Checking row file does not exist - treating as a String");
}
}
if (files.size()>0)
if (files.size() > 0) {
AnalysisLogger.getLogger().debug("A File was recognized as output");
return files.get(0);
else
} else {
AnalysisLogger.getLogger().debug("A String was recognized as output");
return filecontent;
}
}
// DYNAMIC part
protected String mainScriptName = "";
protected String packageURL = "";
@ -162,7 +197,6 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
protected List<String> inputvariables = new ArrayList<String>();
protected List<String> outputvariables = new ArrayList<String>();
protected void initVariables() {
}