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
}
}
}
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;
}
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);
}
}
}
Aggregations