001package org.dllearner.cli;
002
003import org.dllearner.configuration.IConfiguration;
004import org.dllearner.configuration.spring.ApplicationContextBuilder;
005import org.dllearner.configuration.spring.DefaultApplicationContextBuilder;
006import org.dllearner.confparser.ConfParserConfiguration;
007import org.dllearner.utilities.semkernel.SemKernelWorkflow;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010import org.springframework.context.ApplicationContext;
011import org.springframework.core.io.FileSystemResource;
012import org.springframework.core.io.Resource;
013
014import java.io.*;
015import java.util.ArrayList;
016import java.util.List;
017import java.util.Map.Entry;
018
019/**
020 * SemKernel command line interface
021 *
022 * @author Patrick Westphal
023 */
024public class SemKernelCLI extends CLIBase2 {
025    private static Logger logger = LoggerFactory.getLogger(SemKernelCLI.class);
026
027    private ApplicationContext context;
028    private File confFile;
029    private IConfiguration configuration;
030
031    private SemKernelWorkflow semkernelWorkflow;
032
033    public SemKernelCLI() {
034    }
035
036    public SemKernelCLI(File confFile) {
037        this();
038        this.setConfFile(confFile);
039    }
040
041    public static void main(String[] args) throws FileNotFoundException {
042        System.out.println("SemKernel command line interface");
043
044        if(args.length == 0) {
045            System.out.println("You need to give a conf file as argument.");
046            System.exit(0);
047        }
048
049        // read file and print a message if it does not exist
050        File file = new File(args[args.length - 1]);
051        if(!file.exists()) {
052            System.out.println("File \"" + file + "\" does not exist.");
053            System.exit(0);
054        }
055
056        Resource confFile = new FileSystemResource(file);
057
058        List<Resource> springConfigResources = new ArrayList<>();
059
060        try {
061            //SemKernel configuration object
062            IConfiguration configuration = new ConfParserConfiguration(confFile);
063
064            ApplicationContextBuilder builder =
065                    new DefaultApplicationContextBuilder();
066            ApplicationContext context = builder.buildApplicationContext(
067                    configuration, springConfigResources);
068
069            CLIBase2 cli = new SemKernelCLI();
070
071            cli.setContext(context);
072            cli.setConfFile(file);
073            cli.run();
074
075        } catch (Exception e) {e.printStackTrace();
076            String stacktraceFileName = "log/error.log";
077
078            //Find the primary cause of the exception.
079            Throwable primaryCause = findPrimaryCause(e);
080
081            // Get the Root Error Message
082            logger.error("An Error Has Occurred During Processing.");
083            logger.debug("Stack Trace: ", e);
084            logger.error("Terminating DL-Learner...and writing stacktrace " +
085                    "to: " + stacktraceFileName);
086            FileOutputStream fos = new FileOutputStream(stacktraceFileName);
087            PrintStream ps = new PrintStream(fos);
088            e.printStackTrace(ps);
089        }
090    }
091
092    public void run() {
093        for(Entry<String, SemKernelWorkflow> entry : getContext().
094                getBeansOfType(SemKernelWorkflow.class).entrySet()){
095
096            semkernelWorkflow = entry.getValue();
097            logger.info("Running SemKernel workflow \"" + entry.getKey() + "\" (" + semkernelWorkflow.getClass().getSimpleName() + ")");
098            semkernelWorkflow.start();
099        }
100    }
101
102}