Verified Commit 9054efae authored by Amin Bandali's avatar Amin Bandali
Browse files

Add viewer command for going through results one by one

launches two instances of user’s editor ($EDITOR by default, can be
overriden using --editor) one with the ssr resulting output file and
one with the original file.  proceeds to the next result pair once
both spawned editor processes have exit
parent 34273e4c
......@@ -27,6 +27,7 @@ import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
......@@ -110,7 +111,8 @@ public abstract class CatalystCommand {
BufferedWriter w;
try {
w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(p.toString())));
w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(p.toString()),
StandardCharsets.UTF_8));
}
catch (FileNotFoundException e) {
String msg = "Failed to set up file output stream for " +
......
......@@ -38,7 +38,7 @@ import picocli.CommandLine.Command;
description = "A framework for analysis/optimization of performance of Alloy models.",
customSynopsis = "@|bold catalyst|@ [OPTION] COMMAND",
mixinStandardHelpOptions = true,
subcommands = { SSR.class, Mut.class })
subcommands = { SSR.class, Mut.class, View.class })
public final class MainCLI implements Runnable {
public static void main(String[] args) {
System.exit(new CommandLine(new MainCLI()).execute(args));
......
/*
* Catalyst -- A framework for performance analysis/optimization of Alloy models
* Copyright (C) 2019 Amin Bandali
*
* This file is part of Catalyst.
*
* Catalyst is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Catalyst is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Catalyst. If not, see <https://www.gnu.org/licenses/>.
*/
package org.shemshak.alloy.catalyst;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.gson.Gson;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@Command(name = "view", description = "Viewer for generated output files")
public final class View extends CatalystCommand implements Runnable {
@Option(names = "--editor",
description = "The editor to open the files in. Falls back to $EDITOR")
protected String editor = System.getenv("EDITOR");
public View() {}
public void run() {
gson = new Gson();
for (String corpus : corpora)
analyzeCorpus(corpus);
}
protected CorpusResult analyzeCorpus(String corpus) {
try {
List<Path> files = Files.walk(Paths.get(outDir, corpus))
.filter(Files::isRegularFile)
.collect(Collectors.toList());
Stream<Path> fs = parallel ? files.parallelStream() : files.stream();
AtomicInteger i = new AtomicInteger(1);
fs.forEach(p -> analyzeFile(corpus, p, i.getAndIncrement(), files.size()));
} catch (Exception e) {
System.out.println("Failed while trying to iterate through the " + corpus + " corpus");
System.out.println(Utils.toString(e));
}
return null;
}
protected FileResult analyzeFile(String corpus, Path p, int index, int total) {
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(p.toString()),
StandardCharsets.UTF_8))) {
FileResult fr = gson.fromJson(reader, FileResult.class);
if (fr != null) {
System.out.println("Opening:");
System.out.println(" Result file: " + p.toString());
System.out.println(" Original file: " + fr.file_name);
Process p1 = new ProcessBuilder(editor, p.toString()).start();
new ProcessBuilder(editor, fr.file_name).start().waitFor();
if (p1.isAlive())
p1.waitFor();
} else
throw new Exception("Failed to parse JSON back into FileResult");
} catch (Exception e) {
System.out.println("Failed to read or parse " + p.toString());
System.out.println(Utils.toString(e));
}
return null;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment