2015-02-13 15:29:19 +01:00
package org.gcube.dataanalysis.executor.nodes.algorithms ;
import java.io.File ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.UUID ;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger ;
import org.gcube.dataanalysis.ecoengine.configuration.ALG_PROPS ;
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration ;
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.ecoengine.interfaces.ActorNode ;
import org.gcube.dataanalysis.ecoengine.utils.IOHelper ;
import org.gcube.dataanalysis.ecoengine.utils.Transformations ;
import org.gcube.dataanalysis.executor.scripts.OSCommand ;
2016-09-29 12:49:16 +02:00
import org.gcube.dataanalysis.executor.util.LocalRScriptsManager ;
2015-02-13 15:29:19 +01:00
import org.gcube.dataanalysis.executor.util.RScriptsManager ;
import org.gcube.dataanalysis.executor.util.StorageUtils ;
public class CMSY extends ActorNode {
public int count ;
public float status = 0 ;
@Override
public ALG_PROPS [ ] getProperties ( ) {
ALG_PROPS [ ] p = { ALG_PROPS . PHENOMENON_VS_PARALLEL_PHENOMENON } ;
return p ;
}
@Override
public String getName ( ) {
return " CMSY " ;
}
@Override
public String getDescription ( ) {
return " An algorithm to estimate the Maximum Sustainable Yield from a catch statistic. If also a Biomass trend is provided, MSY estimation is provided also with higher precision. The method has been developed by R. Froese, G. Coro, N. Demirel and K. Kleisner. " ;
}
static String idsFile = " IDsFile " ;
static String stocksFile = " StocksFile " ;
static String stock = " SelectedStock " ;
static String processOutput = " ProcessOutput " ;
static String scriptName = " CMSY_22_noplot.R " ;
@Override
public List < StatisticalType > getInputParameters ( ) {
List < StatisticalType > parameters = new ArrayList < StatisticalType > ( ) ;
2021-05-31 12:58:05 +02:00
IOHelper . addStringInput ( parameters , idsFile , " Http link to a file containing prior information about the stocks, in WKLife IV format. " , " " ) ;
IOHelper . addStringInput ( parameters , stocksFile , " Http link to a file containing catch and biomass (or CPUE) trends , in WKLife IV format. " , " " ) ;
2015-02-13 15:29:19 +01:00
IOHelper . addStringInput ( parameters , stock , " The stock on which the procedure has to focus e.g. HLH_M07 " , " " ) ;
return parameters ;
}
@Override
public StatisticalType getOutput ( ) {
File outfile = new File ( config . getPersistencePath ( ) , config . getParam ( processOutput ) ) ;
PrimitiveType o = new PrimitiveType ( File . class . getName ( ) , outfile , PrimitiveTypes . FILE , " OutputFile " , " Output file " ) ;
return o ;
}
@Override
public void initSingleNode ( AlgorithmConfiguration config ) {
}
@Override
public float getInternalStatus ( ) {
return status ;
}
String outputFileName ;
AlgorithmConfiguration config ;
@Override
public int executeNode ( int leftStartIndex , int numberOfLeftElementsToProcess , int rightStartIndex , int numberOfRightElementsToProcess , boolean duplicate , String sandboxFolder , String nodeConfigurationFileObject , String logfileNameToProduce ) {
try {
status = 0 ;
2016-09-29 12:49:16 +02:00
config = Transformations . restoreConfig ( new File ( sandboxFolder , nodeConfigurationFileObject ) . getAbsolutePath ( ) ) ;
2015-02-13 15:29:19 +01:00
String outputFile = config . getParam ( processOutput ) ;
AnalysisLogger . getLogger ( ) . info ( " CMSY expected output " + outputFile ) ;
String fileid = new File ( sandboxFolder , " WKLIFE4ID.csv " ) . getAbsolutePath ( ) ;
String filestock = new File ( sandboxFolder , " WKLIFE4Stocks.csv " ) . getAbsolutePath ( ) ;
StorageUtils . downloadInputFile ( config . getParam ( idsFile ) , fileid ) ;
StorageUtils . downloadInputFile ( config . getParam ( stocksFile ) , filestock ) ;
AnalysisLogger . getLogger ( ) . debug ( " Check fileID: " + fileid + " " + new File ( fileid ) . exists ( ) ) ;
AnalysisLogger . getLogger ( ) . debug ( " Check fileStocks: " + filestock + " " + new File ( filestock ) . exists ( ) ) ;
2016-09-29 12:49:16 +02:00
//RScriptsManager scriptmanager = new RScriptsManager();
LocalRScriptsManager scriptmanager = new LocalRScriptsManager ( ) ;
2015-02-13 15:29:19 +01:00
HashMap < String , String > codeinj = new HashMap < String , String > ( ) ;
codeinj . put ( " HLH_M07 " , config . getParam ( stock ) ) ;
2016-09-29 12:49:16 +02:00
//config.setConfigPath("./");
2015-06-30 12:38:16 +02:00
scriptmanager . executeRScript ( config , scriptName , " " , new HashMap < String , String > ( ) , " " , " outputfile.txt " , codeinj , true , false , false , sandboxFolder ) ;
2015-02-13 15:29:19 +01:00
outputFileName = scriptmanager . getCurrentOutputFileName ( ) ;
String outputFilePath = new File ( sandboxFolder , outputFile ) . getAbsolutePath ( ) ;
AnalysisLogger . getLogger ( ) . info ( " CMSY writing output file in path " + outputFilePath ) ;
OSCommand . FileCopy ( outputFileName , outputFilePath ) ;
AnalysisLogger . getLogger ( ) . info ( " CMSY uploading output file " + outputFile ) ;
StorageUtils . uploadFilesOnStorage ( config . getGcubeScope ( ) , config . getParam ( " ServiceUserName " ) , sandboxFolder , outputFile ) ;
AnalysisLogger . getLogger ( ) . info ( " CMSY Finished " ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2016-09-29 12:49:16 +02:00
return - 1 ;
2015-02-13 15:29:19 +01:00
}
return 0 ;
}
@Override
public void setup ( AlgorithmConfiguration config ) throws Exception {
this . config = config ;
AnalysisLogger . getLogger ( ) . info ( " CMSY process is initialized " ) ;
config . setParam ( processOutput , " CMSY_ " + " output_ " + ( UUID . randomUUID ( ) + " .txt " ) . replace ( " - " , " " ) ) ;
}
@Override
public int getNumberOfRightElements ( ) {
return 1 ;
}
@Override
public int getNumberOfLeftElements ( ) {
return 1 ;
}
@Override
public void stop ( ) {
AnalysisLogger . getLogger ( ) . info ( " CMSY process stopped " ) ;
}
boolean haspostprocessed = false ;
@Override
public void postProcess ( boolean manageDuplicates , boolean manageFault ) {
try {
String filename = config . getParam ( processOutput ) ;
StorageUtils . downloadFilefromStorage ( config . getGcubeScope ( ) , config . getParam ( " ServiceUserName " ) , config . getPersistencePath ( ) , filename ) ;
AnalysisLogger . getLogger ( ) . debug ( " CMSY - Got file from Storage: " + filename ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
}