Search in sources :

Example 1 with ProBParserBase

use of de.prob.parserbase.ProBParserBase in project probparsers by bendisposto.

the class LtlConsoleParser method main.

public static void main(final String[] args) {
    ConsoleOptions options = new ConsoleOptions();
    options.addOption(CLI_LANG, "set language for atomic propositions, etc. (e.g. none, B)", 1);
    options.addOption(CLI_OUT, "set output file, use stdout if omitted", 1);
    options.addOption(CLI_LTL, "use LTL (default)");
    options.addOption(CLI_CTL, "use CTL instead of LTL");
    options.setIntro("usage: LtlConsoleParser [options] <LTL file>\n\n" + "If the file is omitted, stdin is used\n" + "Available options are:");
    options.addOption(CLI_HELP, "print this message");
    options.parseOptions(args);
    if (options.isOptionSet(CLI_HELP)) {
        options.printUsage(System.out);
        return;
    }
    String[] params = options.getRemainingOptions();
    if (params.length > 1) {
        options.printUsage(System.out);
        System.exit(-1);
        return;
    }
    if (options.isOptionSet(CLI_LTL) && options.isOptionSet(CLI_CTL)) {
        System.err.println("Incopatible options -ltl and -ctl given.");
        System.exit(-1);
        return;
    }
    final Mode mode = options.isOptionSet(CLI_CTL) ? Mode.CTL : Mode.LTL;
    // please note: createOutputStream might call System.exit()
    final OutputStream out = createOutputStream(options);
    final String lang = options.isOptionSet(CLI_LANG) ? options.getOptions(CLI_LANG)[0] : null;
    final ProBParserBase extParser = getExtensionParser(lang);
    final IPrologTermOutput pto = new PrologTermOutput(out, false);
    final String input = createInputStream(params, pto);
    if (input != null) {
        final String[] formulas = input.split("###");
        final TemporalLogicParser<?> parser = createParser(extParser, mode);
        pto.openList();
        for (final String formula : formulas) {
            try {
                final PrologTerm term = parser.generatePrologTerm(formula, null);
                pto.openTerm("ltl").printTerm(term).closeTerm();
            } catch (LtlParseException e) {
                pto.openTerm("syntax_error").printAtom(e.getLocalizedMessage()).closeTerm();
            }
        }
        pto.closeList();
    }
    pto.fullstop();
    pto.flush();
    if (options.isOptionSet(CLI_OUT)) {
        try {
            out.close();
        } catch (IOException e) {
        // ignore
        }
    }
}
Also used : PrologTermOutput(de.prob.prolog.output.PrologTermOutput) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ProBParserBase(de.prob.parserbase.ProBParserBase) PrologTerm(de.prob.prolog.term.PrologTerm) IPrologTermOutput(de.prob.prolog.output.IPrologTermOutput) LtlParseException(de.be4.ltl.core.parser.LtlParseException)

Example 2 with ProBParserBase

use of de.prob.parserbase.ProBParserBase in project probparsers by bendisposto.

the class LtlConsoleParser method getExtensionParser.

public static ProBParserBase getExtensionParser(final String pattern) {
    final ProBParserBase result;
    if (pattern == null) {
        result = UNPARSED_PARSER_BASE;
    } else {
        final String[] langs = pattern.split(",");
        final ProBParserBase[] sublangs = new ProBParserBase[langs.length];
        for (int i = 0; i < langs.length; i++) {
            final String lang = langs[i];
            final ProBParserBase sub;
            if ("none".equals(lang)) {
                sub = UNPARSED_PARSER_BASE;
            } else if ("B".equals(lang)) {
                sub = new ClassicalBParser();
            } else
                throw new IllegalArgumentException("Unknown language " + lang);
            sublangs[i] = sub;
        }
        if (sublangs.length == 1) {
            result = sublangs[0];
        } else {
            result = new JoinedParserBase(sublangs);
        }
    }
    return result;
}
Also used : ClassicalBParser(de.be4.classicalb.core.parser.ClassicalBParser) JoinedParserBase(de.prob.parserbase.JoinedParserBase) ProBParserBase(de.prob.parserbase.ProBParserBase)

Example 3 with ProBParserBase

use of de.prob.parserbase.ProBParserBase in project probparsers by bendisposto.

the class CliBParser method runPRepl.

private static void runPRepl(final ParsingBehaviour behaviour) throws IOException, FileNotFoundException {
    PrintStream out;
    ServerSocket serverSocket = new ServerSocket(0, 50, InetAddress.getLoopbackAddress());
    // write port number as prolog term
    System.out.println(serverSocket.getLocalPort() + ".");
    socket = serverSocket.accept();
    socketOutputStream = socket.getOutputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), encoding));
    String line = "";
    MockedDefinitions context = new MockedDefinitions();
    boolean terminate = false;
    while (!terminate) {
        line = in.readLine();
        EPreplCommands command;
        String theFormula;
        if (line == null) {
            // the prob instance has been terminated. exit gracefully
            command = EPreplCommands.halt;
        } else {
            command = EPreplCommands.valueOf(line);
        }
        switch(command) {
            case version:
                print(CliBParser.getBuildRevision() + System.lineSeparator());
                break;
            case definition:
                String name = in.readLine();
                String type = in.readLine();
                String parameterCount = in.readLine();
                context.addMockedDefinition(name, type, parameterCount);
                break;
            case machine:
                String filename = in.readLine();
                String outFile = in.readLine();
                out = new PrintStream(outFile, encoding);
                final File bfile = new File(filename);
                int returnValue;
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                PrintStream ps = new PrintStream(baos);
                try {
                    final String fileName = bfile.getName();
                    final String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
                    if (extension.equals("rmch")) {
                        returnValue = RulesProject.parseProject(bfile, behaviour, out, ps);
                    } else {
                        final BParser parser = new BParser(bfile.getAbsolutePath());
                        returnValue = parser.fullParsing(bfile, behaviour, out, ps);
                    }
                    context = new MockedDefinitions();
                } catch (Exception e) {
                    e.printStackTrace();
                    returnValue = -4;
                } finally {
                    if (true) {
                        out.close();
                    }
                }
                if (returnValue == 0) {
                    print("exit(" + returnValue + ")." + System.lineSeparator());
                } else {
                    String output = baos.toString().replace(System.lineSeparator(), " ").trim();
                    print(output + System.lineSeparator());
                }
                break;
            case formula:
                theFormula = "#FORMULA\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case expression:
                theFormula = "#EXPRESSION\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case predicate:
                theFormula = "#PREDICATE\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case substitution:
                theFormula = "#SUBSTITUTION\n" + in.readLine();
                parseFormula(theFormula, context);
                break;
            case extendedformula:
                theFormula = "#FORMULA\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case extendedexpression:
                theFormula = "#EXPRESSION\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case extendedpredicate:
                theFormula = "#PREDICATE\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case extendedsubstitution:
                theFormula = "#SUBSTITUTION\n" + in.readLine();
                parseExtendedFormula(theFormula, context);
                break;
            case ltl:
                String extension = in.readLine();
                final ProBParserBase extParser = LtlConsoleParser.getExtensionParser(extension);
                final TemporalLogicParser<?> parser = new LtlParser(extParser);
                parseTemporalFormula(in, parser);
                break;
            case ctl:
                String extension2 = in.readLine();
                final ProBParserBase extParser2 = LtlConsoleParser.getExtensionParser(extension2);
                final TemporalLogicParser<?> parser2 = new CtlParser(extParser2);
                parseTemporalFormula(in, parser2);
                break;
            case halt:
                socket.close();
                serverSocket.close();
                terminate = true;
                break;
            default:
                throw new UnsupportedOperationException("Unsupported Command " + line);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) LtlParser(de.be4.ltl.core.parser.LtlParser) ServerSocket(java.net.ServerSocket) BParser(de.be4.classicalb.core.parser.BParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LtlParseException(de.be4.ltl.core.parser.LtlParseException) LexerException(de.be4.classicalb.core.parser.lexer.LexerException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BCompoundException(de.be4.classicalb.core.parser.exceptions.BCompoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MockedDefinitions(de.be4.classicalb.core.parser.MockedDefinitions) BufferedReader(java.io.BufferedReader) CtlParser(de.be4.ltl.core.parser.CtlParser) File(java.io.File) ProBParserBase(de.prob.parserbase.ProBParserBase)

Aggregations

ProBParserBase (de.prob.parserbase.ProBParserBase)3 LtlParseException (de.be4.ltl.core.parser.LtlParseException)2 IOException (java.io.IOException)2 BParser (de.be4.classicalb.core.parser.BParser)1 ClassicalBParser (de.be4.classicalb.core.parser.ClassicalBParser)1 MockedDefinitions (de.be4.classicalb.core.parser.MockedDefinitions)1 BCompoundException (de.be4.classicalb.core.parser.exceptions.BCompoundException)1 LexerException (de.be4.classicalb.core.parser.lexer.LexerException)1 CtlParser (de.be4.ltl.core.parser.CtlParser)1 LtlParser (de.be4.ltl.core.parser.LtlParser)1 JoinedParserBase (de.prob.parserbase.JoinedParserBase)1 IPrologTermOutput (de.prob.prolog.output.IPrologTermOutput)1 PrologTermOutput (de.prob.prolog.output.PrologTermOutput)1 PrologTerm (de.prob.prolog.term.PrologTerm)1 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1