Skip to content
Snippets Groups Projects
Commit 26fe2558 authored by Jan Gruteser's avatar Jan Gruteser
Browse files

simplify file creation in Translator

parent 2e7d2fca
No related branches found
No related tags found
No related merge requests found
Pipeline #144922 passed
...@@ -39,6 +39,8 @@ import java.util.List; ...@@ -39,6 +39,8 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Translator implements TranslationGlobals { public class Translator implements TranslationGlobals {
private static final String GENERATED_BY_TLA2B_HEADER = "/*@ generated by TLA2B ";
private String parentPath; private String parentPath;
private File moduleFile; private File moduleFile;
private File configFile; private File configFile;
...@@ -106,14 +108,11 @@ public class Translator implements TranslationGlobals { ...@@ -106,14 +108,11 @@ public class Translator implements TranslationGlobals {
dir.mkdirs(); dir.mkdirs();
dir.deleteOnExit(); dir.deleteOnExit();
try {
moduleFile = new File("temp/" + moduleName + ".tla"); moduleFile = new File("temp/" + moduleName + ".tla");
moduleFile.createNewFile(); try (BufferedWriter out = Files.newBufferedWriter(moduleFile.toPath(), StandardCharsets.UTF_8)) {
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(moduleFile.toPath()), StandardCharsets.UTF_8))) {
out.write(moduleString); out.write(moduleString);
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); System.err.println("Error while writing file '" + moduleFile.getAbsolutePath() + "':\n" + e.getMessage());
} }
} }
...@@ -121,14 +120,10 @@ public class Translator implements TranslationGlobals { ...@@ -121,14 +120,10 @@ public class Translator implements TranslationGlobals {
modelConfig = null; modelConfig = null;
if (configString != null) { if (configString != null) {
configFile = new File("temp/" + moduleName + ".cfg"); configFile = new File("temp/" + moduleName + ".cfg");
try { try (BufferedWriter out = Files.newBufferedWriter(configFile.toPath(), StandardCharsets.UTF_8)) {
configFile.createNewFile();
try (BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(Files.newOutputStream(configFile.toPath()), StandardCharsets.UTF_8))) {
out.write(configString); out.write(configString);
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); System.err.println("Error while writing file '" + configFile.getAbsolutePath() + "':\n" + e.getMessage());
} }
} }
} }
...@@ -210,44 +205,26 @@ public class Translator implements TranslationGlobals { ...@@ -210,44 +205,26 @@ public class Translator implements TranslationGlobals {
} }
public void createProbFile() { public void createProbFile() {
String fileName = FileUtils.removeExtension(moduleFile.getAbsolutePath()); File probFile = new File(FileUtils.removeExtension(moduleFile.getAbsolutePath()) + ".prob");
fileName = fileName + ".prob";
File probFile;
probFile = new File(fileName);
try {
probFile.createNewFile();
} catch (IOException e) {
System.err.printf("Could not create File %s.%n", probFile.getName());
System.exit(-1);
}
BParser bParser = new BParser(); BParser bParser = new BParser();
try (BufferedWriter outWriter = Files.newBufferedWriter(probFile.toPath(), StandardCharsets.UTF_8)) {
try {
bParser.getDefinitions().addDefinitions(getBDefinitions()); bParser.getDefinitions().addDefinitions(getBDefinitions());
final RecursiveMachineLoader rml = parseAllMachines(getBAST(), getModuleFile(), bParser); final RecursiveMachineLoader rml = parseAllMachines(getBAST(), getModuleFile(), bParser);
PrintWriter outWriter = new PrintWriter(probFile, "UTF-8");
rml.printAsProlog(new PrologTermOutput(outWriter, false)); rml.printAsProlog(new PrologTermOutput(outWriter, false));
outWriter.close();
System.out.println(probFile.getAbsolutePath() + " created."); System.out.println(probFile.getAbsolutePath() + " created.");
} catch (BCompoundException | IOException | PreParseException e) {
} catch (BCompoundException | FileNotFoundException | PreParseException | UnsupportedEncodingException e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
System.exit(-1); System.exit(-1);
} }
} }
public void createMachineFile() { public void createMachineFile() {
String bFile = FileUtils.removeExtension(moduleFile.getAbsolutePath()) + "_tla.txt"; File machineFile = new File(FileUtils.removeExtension(moduleFile.getAbsolutePath()) + "_tla.txt");
File machineFile = new File(bFile);
if (machineFile.exists()) { if (machineFile.exists()) {
try { try (BufferedReader in = new BufferedReader(new FileReader(machineFile))) {
BufferedReader in = new BufferedReader(new FileReader(machineFile));
String firstLine = in.readLine(); String firstLine = in.readLine();
in.close(); if (firstLine != null && !firstLine.startsWith(GENERATED_BY_TLA2B_HEADER)) {
if (firstLine != null && !firstLine.startsWith("/*@ generated by TLA2B ")) {
System.err.println("Error: File " + machineFile.getName() + " already exists" System.err.println("Error: File " + machineFile.getName() + " already exists"
+ " and was not generated by TLA2B.\n" + "Delete or move this file."); + " and was not generated by TLA2B.\n" + "Delete or move this file.");
System.exit(-1); System.exit(-1);
...@@ -258,28 +235,16 @@ public class Translator implements TranslationGlobals { ...@@ -258,28 +235,16 @@ public class Translator implements TranslationGlobals {
} }
} }
try {
machineFile.createNewFile();
} catch (IOException e) {
System.err.printf("Could not create File %s.%n", machineFile.getName());
System.exit(-1);
}
PrettyPrinter pp = new PrettyPrinter(); PrettyPrinter pp = new PrettyPrinter();
pp.setRenaming(new SuffixIdentifierRenaming()); pp.setRenaming(new SuffixIdentifierRenaming());
getBAST().apply(pp); getBAST().apply(pp);
String result = "/*@ generated by TLA2B " + VERSION_NUMBER + " */\n" + pp.getPrettyPrint(); try (BufferedWriter out = Files.newBufferedWriter(machineFile.toPath(), StandardCharsets.UTF_8)) {
out.write(GENERATED_BY_TLA2B_HEADER + VERSION_NUMBER + " */\n" + pp.getPrettyPrint());
try {
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(machineFile.toPath()), StandardCharsets.UTF_8))) {
out.write(result);
}
System.out.println("B-Machine " + machineFile.getAbsolutePath() + " created."); System.out.println("B-Machine " + machineFile.getAbsolutePath() + " created.");
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error while creating file '" + machineFile.getAbsolutePath() + "'."); System.err.println("Error while creating file '" + machineFile.getAbsolutePath() + "':\n" + e.getMessage());
System.exit(-1); System.exit(-1);
} }
} }
public RecursiveMachineLoader parseAllMachines(final Start ast, final File f, final BParser bparser) throws BCompoundException { public RecursiveMachineLoader parseAllMachines(final Start ast, final File f, final BParser bparser) throws BCompoundException {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment