001package org.clafer.ig; 002 003import java.io.BufferedReader; 004import java.io.IOException; 005import java.io.PrintStream; 006import java.nio.charset.StandardCharsets; 007import java.nio.file.Files; 008import java.nio.file.Path; 009import java.util.ArrayList; 010import java.util.Arrays; 011import java.util.List; 012import org.clafer.ig.ClaferModel.Clafer; 013 014/** 015 * The one loop of the solutions, whatever the backend: claferIG's 016 * {@code CommandLine.hs}, with clafersg's commands. A solution is an instance 017 * (Choco) or a trace (Alloy). The loop keeps the solutions shown, the most 018 * recent first, and their views: the full view or the diff view 019 * ({@code d}), the saved files ({@code e}), the Graphviz graph 020 * ({@code dot}) and the clafers found ({@code f}); the backend solves and 021 * handles its own commands. 022 */ 023public final class Repl { 024 025 /** 026 * The input of the commands and the outputs: claferIG prints its 027 * solutions on the standard output and its unsat cores on the standard 028 * error. 029 */ 030 public record Io(BufferedReader in, PrintStream out, PrintStream err) { 031 } 032 033 /** 034 * Shows a solution, which becomes the most recent one. 035 */ 036 @FunctionalInterface 037 public interface Displayer { 038 039 void display(Backend.Shown shown); 040 } 041 042 /** 043 * The solutions shown so far, the most recent first: those not saved yet 044 * and those saved; the Alloy XML of the most recent; and whether they are 045 * shown as the changes from the previous snapshot. 046 */ 047 public static final class Context { 048 049 final List<Solution> saved = new ArrayList<>(); 050 final List<Solution> unsaved = new ArrayList<>(); 051 String currentXml; 052 boolean diffView; 053 Displayer displayer; 054 055 /** 056 * Shows a solution that a backend's command found, e.g., clafersg's 057 * {@code x} and {@code z}, which becomes the most recent one. 058 */ 059 public void display(Backend.Shown shown) { 060 displayer.display(shown); 061 } 062 063 /** 064 * @return how many solutions are shown and not saved 065 */ 066 public int unsavedCount() { 067 return unsaved.size(); 068 } 069 070 /** 071 * @return whether a solution is shown 072 */ 073 public boolean hasSolution() { 074 return !unsaved.isEmpty() || !saved.isEmpty(); 075 } 076 077 /** 078 * @return the Alloy XML of the most recent solution, or null 079 */ 080 public String currentXml() { 081 return currentXml; 082 } 083 } 084 085 private final Backend backend; 086 private final Io io; 087 private final Context context = new Context(); 088 089 /** 090 * @param diffView whether the solutions are shown as their changes 091 * ({@code --diff}) 092 */ 093 public Repl(Backend backend, Io io, boolean diffView) { 094 this.backend = backend; 095 this.io = io; 096 this.context.diffView = diffView; 097 this.context.displayer = this::display; 098 } 099 100 /** 101 * Runs the loop until {@code q} or the end of the input. 102 */ 103 public void run() throws Exception { 104 try { 105 backend.start(io); 106 next(); 107 while (true) { 108 io.out().print(backend.prompt()); 109 io.out().flush(); 110 String line = io.in().readLine(); 111 if (line == null) { 112 break; 113 } 114 if (!command(line)) { 115 break; 116 } 117 } 118 } finally { 119 backend.quit(); 120 } 121 } 122 123 /** 124 * @return whether the loop goes on 125 */ 126 private boolean command(String input) throws Exception { 127 String trimmed = input.trim(); 128 List<String> words = trimmed.isEmpty() ? List.of() : Arrays.asList(trimmed.split("\\s+")); 129 String name = words.isEmpty() ? "" : words.get(0); 130 switch (name) { 131 case "", "n", "next" -> { 132 if (words.size() > 1) { 133 return other(input, words); 134 } 135 next(); 136 } 137 case "q", "quit" -> { 138 return false; 139 } 140 case "h", "help" -> io.out().print(backend.help()); 141 case "e", "save" -> save(); 142 case "d", "diff" -> { 143 io.out().println(context.diffView 144 ? "The instances and states are shown in full." 145 : "Each next instance or state is shown as its changes from the previous one."); 146 context.diffView = !context.diffView; 147 } 148 case "dot" -> saveGraph(); 149 case "f", "find" -> { 150 if (words.size() != 2) { 151 return other(input, words); 152 } 153 find(words.get(1)); 154 } 155 case "fork" -> { 156 Integer state = words.size() == 2 ? number(words.get(1)) : null; 157 if (state == null) { 158 return other(input, words); 159 } 160 if (!backend.isDynamic()) { 161 io.out().println("The model is static: its instances are not traces."); 162 } else { 163 otherSolution(() -> backend.fork(io, state)); 164 } 165 } 166 case "config" -> otherSolution(() -> backend.nextConfiguration(io)); 167 case "check" -> { 168 Integer line = null; 169 if (words.size() == 2) { 170 try { 171 int n = Integer.parseInt(words.get(1)); 172 line = n > 0 ? n : null; 173 } catch (NumberFormatException e) { 174 return other(input, words); 175 } 176 } else if (words.size() > 2) { 177 return other(input, words); 178 } 179 backend.check(io, line, this::display); 180 } 181 case "alloyInstance" -> io.out().println(context.currentXml == null ? "No instance" : context.currentXml); 182 default -> { 183 return other(input, words); 184 } 185 } 186 return true; 187 } 188 189 private boolean other(String input, List<String> words) throws Exception { 190 if (!backend.command(io, input, words, context)) { 191 backend.unknown(io, input); 192 } 193 return true; 194 } 195 196 private static Integer number(String s) { 197 return s.chars().allMatch(Character::isDigit) && !s.isEmpty() ? Integer.valueOf(s) : null; 198 } 199 200 private void next() throws Exception { 201 Backend.Shown shown = backend.next(io); 202 if (shown != null) { 203 display(shown); 204 } 205 } 206 207 @FunctionalInterface 208 private interface Other { 209 210 Backend.Shown get() throws Exception; 211 } 212 213 // shows a solution other than the next one: a fork, or another configuration 214 private void otherSolution(Other command) throws Exception { 215 if (!context.hasSolution()) { 216 io.out().println("No solution yet: type 'n' for the first one."); 217 return; 218 } 219 Backend.Shown shown = command.get(); 220 if (shown != null) { 221 display(shown); 222 } 223 } 224 225 /** 226 * Shows a solution, which becomes the most recent one. 227 */ 228 public void display(Backend.Shown shown) { 229 int n = backend.number(context); 230 Solution previous = latest(); 231 io.out().print(context.diffView 232 ? SolutionView.showSolutionDiff(backend.names(), n, previous, shown.solution()) + "\n" 233 : backend.show(n, shown)); 234 io.out().flush(); 235 context.unsaved.add(0, shown.solution()); 236 context.currentXml = shown.xml(); 237 } 238 239 private Solution latest() { 240 return !context.unsaved.isEmpty() ? context.unsaved.get(0) 241 : !context.saved.isEmpty() ? context.saved.get(0) : null; 242 } 243 244 private void save() throws IOException { 245 int counter = context.saved.size(); 246 for (Solution s : context.unsaved) { 247 String saveName = backend.modelFileName() + "." + counter + ".data"; 248 Files.writeString(Path.of(saveName), SolutionView.solutionText(s), StandardCharsets.UTF_8); 249 io.out().println("Saved to " + saveName); 250 counter++; 251 } 252 context.saved.addAll(0, context.unsaved); 253 context.unsaved.clear(); 254 } 255 256 private void saveGraph() throws IOException { 257 Solution solution = latest(); 258 if (solution == null) { 259 io.out().println("No solution yet: type 'n' for the first one."); 260 return; 261 } 262 int count = context.unsaved.size() + context.saved.size(); 263 String saveName = backend.modelFileName() + "." + count + ".dot"; 264 Files.writeString(Path.of(saveName), context.diffView 265 ? SolutionView.solutionDiffDot(backend.names(), solution) 266 : SolutionView.solutionDot(solution), StandardCharsets.UTF_8); 267 io.out().println("Saved to " + saveName); 268 } 269 270 private void find(String name) { 271 Solution solution = latest(); 272 if (solution == null) { 273 io.out().println("No instance"); 274 return; 275 } 276 for (ClaferModel m : solution.models()) { 277 for (Clafer c : m.traverse()) { 278 if (c.name().equals(name)) { 279 io.out().println(ClaferModel.show(c)); 280 return; 281 } 282 } 283 } 284 io.out().println("\"" + name + "\" not found in the model."); 285 } 286}