package net.timbusproject.extractors.modules.tavernaextractor;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map.Entry;
import java.util.Properties;
import net.timbusproject.extractors.modules.tavernaextractor.provenance.ProvenanceAccessClient;
import net.timbusproject.extractors.modules.tavernaextractor.provenance.ProvenanceAccessEntry;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class Main {
private static Logger LOGGER = LogManager.getLogger("Main");
private static boolean verbose = false;
public static void main(String[] args) {
LOGGER.setLevel(Level.INFO);
try {
CommandLineParser parser = new GnuParser();
HelpFormatter helpFormater = new HelpFormatter();
try {
CommandLine parse = parser.parse(createOptions(), args);
// Program started without any option
if (parse.getOptions().length <= 0) {
System.out.println("Incorrect arguments. ");
helpFormater.printHelp("TavernaExtractor", createOptions());
System.exit(1);
}
Properties props = Main.loadProperties();
String tavernaHome = props.getProperty("taverna.home");
String dotExeLocation = props.getProperty("dot.exe.location");
String tavernaT2DatabasePath = props.getProperty("taverna.t2Database.path");
// Convert Taverna2 WF to Archimate model
if (parse.hasOption("i") && parse.hasOption("o")) {
String archimateOutputPath = parse.getOptionValue("o");
String inputFileName = parse.getOptionValue("i");
if (parse.hasOption("v"))
verbose = true;
// setup the extractor
TavernaExtractor extractor = new TavernaExtractor(verbose);
extractor.setTavernaHome(Main.getTavernaHome(tavernaHome));
extractor.setArchimateOutputPath(Paths.get(archimateOutputPath));
extractor.setInputFileName(Paths.get(inputFileName));
extractor.setDotLocation(Paths.get(dotExeLocation));
extractor.process(false); //false = local execution
}
// List all provenance entries OR Running DROID identification
if (parse.hasOption("p")) {
ProvenanceAccessClient pac;
if (tavernaT2DatabasePath != null && !tavernaT2DatabasePath.isEmpty()) {
pac = new ProvenanceAccessClient(tavernaT2DatabasePath);
} else {
pac = new ProvenanceAccessClient();
}
System.out.println("===== [ID] | [Workflow Name (Timestamp)] ===== | "
+ pac.listAllWorkflowsReadable().size() + " entries found.");
for (Entry<Integer, ProvenanceAccessEntry> entry : pac.listAllWorkflowsReadable().entrySet()) {
System.out.println("[" + entry.getKey() + "] " + entry.getValue().printToConsole());
}
if (parse.getOptionValue("p") != null) {
String input = readLine("Your choice: ");
String filePath = parse.getOptionValue("p");
try {
Integer id = Integer.parseInt(input);
Path path = pac.processWorkflow(id, filePath);
System.out.println("Path to the tmp directory is: "+path.toString());
} catch (NumberFormatException e) {
System.out.println("Your input '" + input + "' was not a valid number. Process stopped.");
}
}
}
// Extend DSO with PRONOM information
if (parse.hasOption("ont") && parse.hasOption("r")) {
LOGGER.debug("Start extending ontology ...");
System.out.println("Start extending ontology ...");
File ontology = null;
File report = null;
String ontologyFilePath = parse.getOptionValue("ont");
String reportFilePath = parse.getOptionValue("r");
if (ontologyFilePath != null) {
ontology = new File(ontologyFilePath);
}
if (reportFilePath != null) {
report = new File(reportFilePath);
}
if (ontology.exists() && report.exists()) {
LOGGER.debug("ontology file = " + ontology.getAbsolutePath());
LOGGER.debug("report file = " + report.getAbsolutePath());
System.out.println("ontology file = " + ontology.getAbsolutePath());
System.out.println("report file = " + report.getAbsolutePath());
new ExtendedOWL().process(ontology, report);
} else {
LOGGER.error("ERROR: Can not find either onotoly file (" + ontologyFilePath + ") or ("
+ reportFilePath + ").");
System.out.println("ERROR: Can not find either onotoly file (" + ontologyFilePath + ") or ("
+ reportFilePath + ").");
}
}
} catch (ParseException exp) {
helpFormater.printHelp("TavernaExtractor", createOptions());
LOGGER.error("ERROR: Wrong usage:" + exp.getMessage());
exp.printStackTrace();
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
// also print the error stack trace
if(verbose)
e.printStackTrace();
System.exit(1);
}
}
private static Options createOptions() {
Options options = new Options();
OptionBuilder.withDescription("Specifies the archimate (output) file.");
OptionBuilder.hasArg();
OptionBuilder.withLongOpt("output-file");
options.addOption(OptionBuilder.create("o"));
OptionBuilder.withDescription("Specifies the taverna t2flow (input) file.");
OptionBuilder.hasArg();
OptionBuilder.withLongOpt("input-file");
options.addOption(OptionBuilder.create("i"));
OptionBuilder.withDescription("Fetching provenance information from a workflow execution");
OptionBuilder.withArgName("\\path\\to\\copy\\provenance-trace");
OptionBuilder.hasArg();
OptionBuilder.withLongOpt("provenance");
options.addOption(OptionBuilder.create("p"));
OptionBuilder.withDescription("Ontology file");
OptionBuilder.withArgName("\\path\\to\\myOntology.owl");
OptionBuilder.hasArg();
OptionBuilder.withLongOpt("ontology-file");
options.addOption(OptionBuilder.create("ont"));
OptionBuilder.withDescription("DROID report file");
OptionBuilder.withArgName("\\path\\to\\myDroidCSVreportFile.droid.csv");
OptionBuilder.hasArg();
OptionBuilder.withLongOpt("reportFile");
options.addOption(OptionBuilder.create("r"));
OptionBuilder.withDescription("verbose mode one");
OptionBuilder.withLongOpt("verbose");
options.addOption(OptionBuilder.create("v"));
return options;
}
private static Path getTavernaHome(String tavernaHome){
// set default value in case of missing argument
if(tavernaHome == null){
return Paths.get(System.getProperty("user.home"),"taverna-2.4.0");
}
return Paths.get(tavernaHome);
}
// Due to a bug in eclipse
// from http://stackoverflow.com/questions/4203646/system-console-returns-null
private static String readLine(String format, Object... args) throws IOException {
if (System.console() != null) {
return System.console().readLine(format, args);
}
System.out.print(String.format(format, args));
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
return reader.readLine();
}
private static Properties loadProperties() {
Properties prop = new Properties();
try {
InputStream input = Main.class.getClassLoader().getResourceAsStream("TavernaExtractor.properties");
prop.load(input);
LOGGER.debug("Load properties successfully.");
} catch (IOException io) {
LOGGER.error(io.getMessage());
io.printStackTrace();
}
return prop;
}
}