001package org.clafer.alloy.solve; 002 003import edu.mit.csail.sdg.alloy4.A4Reporter; 004import edu.mit.csail.sdg.alloy4.Err; 005import edu.mit.csail.sdg.alloy4.ErrorSyntax; 006import edu.mit.csail.sdg.alloy4.ErrorWarning; 007import edu.mit.csail.sdg.alloy4.Pos; 008import edu.mit.csail.sdg.alloy4.SafeList; 009import edu.mit.csail.sdg.ast.Command; 010import edu.mit.csail.sdg.ast.CommandScope; 011import edu.mit.csail.sdg.ast.Decl; 012import edu.mit.csail.sdg.ast.Expr; 013import edu.mit.csail.sdg.ast.ExprConstant; 014import edu.mit.csail.sdg.ast.ExprQt; 015import edu.mit.csail.sdg.ast.Func; 016import edu.mit.csail.sdg.ast.Sig; 017import edu.mit.csail.sdg.parser.CompModule; 018import edu.mit.csail.sdg.parser.CompUtil; 019import edu.mit.csail.sdg.translator.A4Options; 020import edu.mit.csail.sdg.translator.A4Solution; 021import edu.mit.csail.sdg.translator.A4Tuple; 022import edu.mit.csail.sdg.translator.A4TupleSet; 023import edu.mit.csail.sdg.translator.TranslateAlloyToKodkod; 024import java.io.PrintWriter; 025import java.io.StringWriter; 026import java.util.ArrayList; 027import java.util.Collections; 028import java.util.LinkedHashMap; 029import java.util.List; 030import java.util.Map; 031import java.util.Optional; 032import java.util.OptionalInt; 033import java.util.Set; 034import kodkod.engine.satlab.SATFactory; 035import org.alloytools.solvers.natv.minisatprover.FixedMiniSatProver; 036 037/** 038 * An Alloy model in this JVM and its solutions: claferIG's {@code AlloyIG.java}, 039 * whose operations are the methods here rather than messages on a pipe. A 040 * solution is Alloy's XML of it, as {@code AlloyIG} sends it. 041 * <p> 042 * The model is parsed from its text, in memory. The solutions of a dynamic 043 * model, a trace each, are solved for one by one, without the traces given so 044 * far ({@link #next}); {@link #fork} and {@link #nextConfiguration} give 045 * other traces, and {@link #check} the counterexample of an assertion. The 046 * unsat core of the model comes from the minisat prover, claferIG's fixed 047 * one ({@link FixedMiniSatProver}). 048 */ 049public final class AlloySession { 050 051 // The only way to detect unsatisfiability is to wait for the minimized callback to be called. 052 // If it is called, then unsatisfiable. Otherwise not. See alloy4whole/SimpleReporter.java. 053 private static final class Reporter extends A4Reporter { 054 055 private int minimizedBefore; 056 private int minimizedAfter; 057 058 @Override 059 public void warning(ErrorWarning msg) { 060 System.err.print("Relevance Warning:\n" + (msg.toString().trim()) + "\n\n"); 061 System.err.flush(); 062 } 063 064 @Override 065 public void minimized(Object command, int before, int after) { 066 minimizedBefore = before; 067 minimizedAfter = after; 068 super.minimized(command, before, after); 069 } 070 } 071 072 /** 073 * A sig of the model, as {@code load} sends it. 074 * 075 * @param label its label, e.g., {@code this/c0_A} 076 * @param multiplicity {@code One}, {@code Lone}, {@code Some} or 077 * {@code Any} 078 * @param subset the type of a subset sig, without its braces, or empty 079 * @param startingScope its scope in the command, if any 080 */ 081 public record SigInfo(String label, String multiplicity, String subset, OptionalInt startingScope) { 082 } 083 084 /** 085 * The sigs of the model and the global scope of its command. 086 */ 087 public record Loaded(List<SigInfo> sigs, int globalScope) { 088 } 089 090 static { 091 // Alloy logs its progress at the info level and warns whenever a native solver is garbage collected. 092 if (System.getProperty("org.slf4j.simpleLogger.defaultLogLevel") == null) { 093 System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "error"); 094 } 095 } 096 097 private final Reporter rep = new Reporter(); 098 private CompModule world; 099 private SafeList<Sig> sigs; 100 private Command command; 101 // Alloy parses the model from a temporary file; positions only match with its name. 102 private String modelFile = ""; 103 private A4Solution ans; 104 // The solution of the model, as solved by the last resolve: if it is unsatisfiable, the 105 // model has no instances, and its unsat core explains why. 106 private A4Solution resolved; 107 private boolean dynamic; 108 // In a dynamic model, the traces given so far are excluded from the next solutions, and the 109 // configurations given so far from the next configuration. The next trace is solved for 110 // when it is asked for, after the trace shown last. 111 private Expr givenTraces = ExprConstant.TRUE; 112 private Expr givenConfigurations = ExprConstant.TRUE; 113 private A4Solution shown; 114 private boolean stale; 115 // the static model gave its last instance 116 private boolean exhausted; 117 private final A4Options options = new A4Options(); 118 private Util.State<StateExtra> state; 119 120 public AlloySession() { 121 // Use fastest 122 options.coreMinimization = 2; 123 // The minisat prover computes unsat cores. Alloy has none for Windows, so instances come from SAT4J there. 124 // The optimized build of the prover is given in the system property alloy.native.lib.minisatprover. 125 SATFactory prover = new FixedMiniSatProver.Factory(); 126 options.solver = prover.isPresent() ? prover : SATFactory.get("sat4j"); 127 } 128 129 /** 130 * {@code load}: parses the model. 131 * 132 * @throws Err the errors of the model 133 */ 134 public Loaded load(String model) throws Err { 135 world = CompUtil.parseEverything_fromString(rep, model); 136 sigs = world.getAllSigs(); 137 Util.splitFacts(world.getAllReachableSigs()); 138 dynamic = isDynamic(sigs); 139 command = world.getAllCommands().get(0); 140 modelFile = command.pos.filename; 141 List<SigInfo> infos = new ArrayList<>(); 142 for (Sig sig : sigs) { 143 CommandScope scope = command.getScope(sig); 144 infos.add(new SigInfo(sig.label, multiplicity(sig), 145 sig instanceof Sig.PrimSig ? "" : removeCurly(sig.type().toString()), 146 scope == null ? OptionalInt.empty() : OptionalInt.of(scope.startingScope))); 147 } 148 return new Loaded(infos, command.overall); 149 } 150 151 /** 152 * @return whether the model has mutable sigs or fields: its solutions are 153 * traces 154 */ 155 public boolean isDynamic() { 156 return dynamic; 157 } 158 159 /** 160 * {@code resolve}: solves the command again, from its first solution. 161 */ 162 public void resolve() throws Err { 163 rep.minimizedBefore = 0; 164 rep.minimizedAfter = 0; 165 givenTraces = ExprConstant.TRUE; 166 givenConfigurations = ExprConstant.TRUE; 167 shown = null; 168 stale = false; 169 exhausted = false; 170 ans = TranslateAlloyToKodkod.execute_command(rep, world.getAllReachableSigs(), command, options); 171 resolved = ans; 172 } 173 174 /** 175 * {@code next}: the next solution, if any. 176 */ 177 public Optional<String> next() throws Err { 178 if (exhausted) { 179 return Optional.empty(); 180 } 181 if (dynamic && stale) { 182 // Alloy does not enumerate the traces of a temporal model: it gives few of them, 183 // of the length it found first, or none at all, depending on the model. Instead, 184 // the next trace is solved for, without the traces given so far. The reporter of 185 // the model stays as it is, since no more traces is not an unsat core of the model. 186 ans = TranslateAlloyToKodkod.execute_command(new Reporter(), world.getAllReachableSigs(), 187 command.change(command.formula.and(givenTraces)), withoutCore(options)); 188 stale = false; 189 } 190 if (!ans.satisfiable()) { 191 return Optional.empty(); 192 } 193 String xml = toXml(ans); 194 if (dynamic) { 195 List<Sig.Field> fields = stateFields(world.getAllReachableSigs()); 196 givenTraces = givenTraces.and(sameStates(ans, visible(fields, fields), ans.getTraceLength(), true).not()); 197 givenConfigurations = givenConfigurations.and( 198 sameStates(ans, visible(staticFields(fields), fields), 1, false).not()); 199 shown = ans; 200 stale = true; 201 } else { 202 givenConfigurations = givenConfigurations.and( 203 sameStates(ans, structure(world.getAllReachableSigs()), 1, false).not()); 204 shown = ans; 205 A4Solution nextAns = ans.next(); 206 // AlloyIG quits here: the static model has no more instances 207 if (nextAns == ans) { 208 exhausted = true; 209 } 210 ans = nextAns; 211 } 212 return Optional.of(xml); 213 } 214 215 /** 216 * {@code fork}: a trace with the same states as the trace shown last 217 * before the given state, which differs from the traces given so far. 218 */ 219 public Optional<String> fork(int forkState) throws Err { 220 return other(true, forkState); 221 } 222 223 /** 224 * {@code nextConfiguration}: a trace whose relations that do not change 225 * over time differ from those of the traces given so far, or an instance 226 * of a static model whose structure differs from those of the instances 227 * given so far. 228 */ 229 public Optional<String> nextConfiguration() throws Err { 230 return other(false, 0); 231 } 232 233 private Optional<String> other(boolean fork, int forkState) throws Err { 234 // Both differ from the traces given so far. 235 A4Solution other = null; 236 if ((dynamic || !fork) && shown != null) { 237 List<Sig.Field> fields = stateFields(world.getAllReachableSigs()); 238 Expr formula = fork 239 ? sameStates(shown, visible(fields, fields), forkState, false) 240 : givenConfigurations; 241 other = TranslateAlloyToKodkod.execute_command(new Reporter(), world.getAllReachableSigs(), 242 command.change(command.formula.and(givenTraces).and(formula)), withoutCore(options)); 243 } 244 if (other == null || !other.satisfiable()) { 245 return Optional.empty(); 246 } 247 String xml = toXml(other); 248 List<Sig.Field> fields = stateFields(world.getAllReachableSigs()); 249 shown = other; 250 if (dynamic) { 251 givenTraces = givenTraces.and(sameStates(other, visible(fields, fields), other.getTraceLength(), true).not()); 252 givenConfigurations = givenConfigurations.and( 253 sameStates(other, visible(staticFields(fields), fields), 1, false).not()); 254 ans = other; 255 stale = true; 256 } else { 257 // the next instances continue from this one, without the configurations so far 258 givenConfigurations = givenConfigurations.and( 259 sameStates(other, structure(world.getAllReachableSigs()), 1, false).not()); 260 ans = other.next(); 261 exhausted = ans == other; 262 } 263 return Optional.of(xml); 264 } 265 266 /** 267 * {@code check}: the counterexample of the assertion with the given label, 268 * at the scopes and the trace length of the model, if any. It has no unsat 269 * core. The solutions of the model stay as they are. 270 */ 271 public Optional<String> check(String assertion) throws Err { 272 A4Options checkOptions = withoutCore(options); 273 A4Solution counterexample = null; 274 for (Command a : world.getAllCommands()) { 275 if (a.check && a.label.equals(assertion)) { 276 Command c = command; 277 counterexample = TranslateAlloyToKodkod.execute_command(new Reporter(), world.getAllReachableSigs(), 278 new Command(a.pos, a.nameExpr, a.label, true, c.overall, c.bitwidth, c.maxseq, c.minprefix, 279 c.maxprefix, a.expects, c.scope, c.additionalExactScopes, a.commandKeyword, 280 a.formula, a.parent), 281 checkOptions); 282 } 283 } 284 return counterexample != null && counterexample.satisfiable() 285 ? Optional.of(toXml(counterexample)) : Optional.empty(); 286 } 287 288 /** 289 * {@code setGlobalScope}. 290 */ 291 public void setGlobalScope(int scopeSize) { 292 Command c = command; 293 command = new Command(c.pos, c.nameExpr, c.label, c.check, scopeSize, c.bitwidth, c.maxseq, c.minprefix, 294 c.maxprefix, c.expects, c.scope, c.additionalExactScopes, c.commandKeyword, c.formula, c.parent); 295 } 296 297 /** 298 * {@code setScope}: the scope of the sig with the given label. 299 */ 300 public void setScope(String sigName, int scopeSize) throws ErrorSyntax { 301 Sig sig = Util.findSig(sigName, sigs); 302 List<CommandScope> scope = setScopeSize(sig, scopeSize, command.scope); 303 Command c = command; 304 command = new Command(c.pos, c.nameExpr, c.label, c.check, c.overall, c.bitwidth, c.maxseq, c.minprefix, 305 c.maxprefix, c.expects, scope, c.additionalExactScopes, c.commandKeyword, c.formula, c.parent); 306 } 307 308 /** 309 * {@code setBitwidth}. 310 */ 311 public void setBitwidth(int bitwidth) { 312 if (bitwidth < 1) { 313 throw new IllegalArgumentException(bitwidth + " is an invalid bitwidth."); 314 } 315 Command c = command; 316 command = new Command(c.pos, c.nameExpr, c.label, c.check, c.overall, bitwidth, c.maxseq, c.minprefix, 317 c.maxprefix, c.expects, c.scope, c.additionalExactScopes, c.commandKeyword, c.formula, c.parent); 318 } 319 320 /** 321 * {@code setMaxTraceLength}. 322 */ 323 public void setMaxTraceLength(int maxTraceLength) { 324 if (maxTraceLength < 1) { 325 throw new IllegalArgumentException(maxTraceLength + " is an invalid trace length."); 326 } 327 command = withTraceLength(command, Math.min(command.minprefix, maxTraceLength), maxTraceLength); 328 } 329 330 /** 331 * {@code unsatCoreMinimization}: 0 (best), 1 or 2 (fastest). 332 */ 333 public void setUnsatCoreMinimization(int level) { 334 if (level < 0 || level > 2) { 335 throw new IllegalArgumentException(level + " is in invalid optimization level."); 336 } 337 options.coreMinimization = level; 338 } 339 340 /** 341 * {@code saveState}: to generate counterexamples, the model is mutated; 342 * its state is saved first and restored once completed. 343 */ 344 public void saveState() { 345 state = Util.saveState(sigs, new StateExtra(rep.minimizedBefore, rep.minimizedAfter, ans, resolved, command, 346 givenTraces, exhausted)); 347 } 348 349 /** 350 * {@code restoreState}. 351 */ 352 public void restoreState() { 353 StateExtra extra = Util.restoreState(state, sigs); 354 rep.minimizedBefore = extra.minimizedBefore(); 355 rep.minimizedAfter = extra.minimizedAfter(); 356 ans = extra.answer(); 357 resolved = extra.resolved(); 358 command = extra.command(); 359 givenTraces = extra.givenTraces(); 360 exhausted = extra.exhausted(); 361 } 362 363 /** 364 * {@code removeConstraint}: the constraint at the span, as the unsat core 365 * gives it (the second column one more than Alloy's). 366 * 367 * @throws IllegalStateException if there is no constraint at the span 368 */ 369 public void removeConstraint(int y, int x, int y2, int x2) { 370 // The second column is one less than it should be. Not sure if Alloy bug or intended. 371 Pos constraint = new Pos(modelFile, x, y, x2 - 1, y2); 372 Command newCommand = Util.removeGlobalConstraint(constraint, command); 373 if (newCommand == null) { 374 if (!Util.removeLocalConstraint(constraint, sigs)) { 375 throw new IllegalStateException(String.format( 376 "Cannot remove constraint (line=%d, column=%d)-(line=%d, column=%d)", 377 constraint.y, constraint.x, constraint.y2, constraint.x2)); 378 } 379 } else { 380 command = newCommand; 381 } 382 } 383 384 /** 385 * {@code unsatCore}: the spans of the constraints of the unsat core of the 386 * model, each as {line, column, line, column + 1}, or none. Only the model 387 * has an unsat core: not the next solution, when the solutions of the 388 * model run out. Alloy reports no minimized core when it finds the model 389 * unsatisfiable without solving, e.g., {@code [ some b ]} and 390 * {@code [ no b ]}, so the core is not checked by its size. 391 */ 392 public List<int[]> unsatCore() { 393 List<int[]> core = new ArrayList<>(); 394 if (ans == resolved && !ans.satisfiable()) { 395 Set<Pos> unsatCore = ans.highLevelCore().a; 396 for (Pos p : unsatCore) { 397 // The second column is one less than it should be. 398 core.add(new int[]{p.y, p.x, p.y2, p.x2 + 1}); 399 } 400 } 401 return core; 402 } 403 404 // ---------------------------------------------------------------------- 405 // AlloyIG's helpers 406 // ---------------------------------------------------------------------- 407 private record StateExtra(int minimizedBefore, int minimizedAfter, A4Solution answer, A4Solution resolved, 408 Command command, Expr givenTraces, boolean exhausted) { 409 } 410 411 private static CommandScope setCommandScopeSize(int scopeSize, CommandScope cs) throws ErrorSyntax { 412 return new CommandScope(cs.pos, cs.sigPos, cs.sig, cs.isExact, scopeSize, scopeSize, cs.increment); 413 } 414 415 private static List<CommandScope> setScopeSize(Sig sig, int scopeSize, List<CommandScope> scope) 416 throws ErrorSyntax { 417 List<CommandScope> newScope = new ArrayList<>(); 418 boolean found = false; 419 for (CommandScope cs : scope) { 420 if (sig.equals(cs.sig)) { 421 found = true; 422 newScope.add(setCommandScopeSize(scopeSize, cs)); 423 } else { 424 newScope.add(cs); 425 } 426 } 427 if (!found) { 428 newScope.add(new CommandScope(sig, false, scopeSize)); 429 } 430 return newScope; 431 } 432 433 private static String toXml(A4Solution ans) throws Err { 434 StringWriter xml = new StringWriter(); 435 ans.writeXML(new PrintWriter(xml), Collections.<Func>emptyList(), null); 436 return xml.toString(); 437 } 438 439 // A dynamic model has mutable sigs or fields; its solutions are traces. 440 private static boolean isDynamic(SafeList<Sig> sigs) { 441 for (Sig sig : sigs) { 442 if (sig.isVariable != null) { 443 return true; 444 } 445 for (Sig.Field field : sig.getFields()) { 446 if (field.isVariable != null) { 447 return true; 448 } 449 } 450 } 451 return false; 452 } 453 454 // The relations of the instance in a state of a trace: the containment relations of the 455 // clafers (r_c0_name) and their references (c0_name_ref). 456 private static List<Sig.Field> stateFields(Iterable<Sig> sigs) { 457 List<Sig.Field> fields = new ArrayList<>(); 458 for (Sig sig : sigs) { 459 for (Sig.Field field : sig.getFields()) { 460 if (field.label.startsWith("r_") || field.label.endsWith("_ref")) { 461 fields.add(field); 462 } 463 } 464 } 465 return fields; 466 } 467 468 // The given relations as they are seen in a state: the references of the clafers in it. The 469 // model constrains a reference only while its clafer is alive, so a clafer that is not in the 470 // state has any value, and a trace that differs only in such values is the same trace. A 471 // clafer is alive when it is the child of a clafer, which is alive itself (the parent of 472 // each child is alive in the model). 473 private static List<Expr> visible(List<Sig.Field> fields, List<Sig.Field> all) { 474 Expr alive = null; 475 for (Sig.Field field : all) { 476 if (field.label.startsWith("r_")) { 477 Expr children = Sig.UNIV.join(field); 478 alive = alive == null ? children : alive.plus(children); 479 } 480 } 481 List<Expr> visible = new ArrayList<>(); 482 for (Sig.Field field : fields) { 483 visible.add(alive != null && field.label.endsWith("_ref") ? alive.domain(field) : field); 484 } 485 return visible; 486 } 487 488 // The expression applied n times. 489 private static Expr repeat(Expr e, int n, boolean prime) { 490 for (int i = 0; i < n; i++) { 491 e = prime ? e.prime() : e.after(); 492 } 493 return e; 494 } 495 496 // The expression of an atom: the sig of a one sig, a number, or a variable for the atom. 497 private static Expr atom(String label, Sig.PrimSig sig, Map<String, Decl> vars) throws Err { 498 if (sig.isOne != null) { 499 return sig; 500 } 501 if (sig.builtin) { 502 return ExprConstant.makeNUMBER(Integer.parseInt(label)); 503 } 504 Decl decl = vars.get(label); 505 if (decl == null) { 506 decl = sig.oneOf("atom" + vars.size()); 507 vars.put(label, decl); 508 } 509 return decl.get(); 510 } 511 512 // The relations of a configuration of a dynamic model, which do not change over time. 513 private static List<Sig.Field> staticFields(List<Sig.Field> fields) { 514 List<Sig.Field> statics = new ArrayList<>(); 515 for (Sig.Field field : fields) { 516 if (field.isVariable == null) { 517 statics.add(field); 518 } 519 } 520 return statics; 521 } 522 523 // The relations of a configuration of a static model: its structure, which clafers there 524 // are, without the values of its references. A top-level clafer has no parent relation, 525 // so the sigs say which clafers there are. 526 private static List<Expr> structure(Iterable<Sig> sigs) { 527 List<Expr> structure = new ArrayList<>(); 528 for (Sig sig : sigs) { 529 if (!sig.builtin && sig.isAbstract == null) { 530 structure.add(sig); 531 } 532 for (Sig.Field field : sig.getFields()) { 533 if (field.label.startsWith("r_")) { 534 structure.add(field); 535 } 536 } 537 } 538 return structure; 539 } 540 541 // Some distinct atoms make the given relations in the first states of the trace equal to those 542 // of the given trace, up to a renaming of its atoms; with the loop, from the loop state, the 543 // trace repeats the loop, so that it is the given trace. 544 private static Expr sameStates(A4Solution ans, List<? extends Expr> fields, int count, boolean withLoop) 545 throws Err { 546 Map<String, Decl> vars = new LinkedHashMap<>(); 547 // an instance of a static model has no trace, but it is one state 548 int length = Math.max(ans.getTraceLength(), 1); 549 int loop = ans.getLoopState(); 550 Expr trace = ExprConstant.TRUE; 551 for (int s = Math.min(count, length) - 1; s >= 0; s--) { 552 Expr st = ExprConstant.TRUE; 553 for (Expr field : fields) { 554 Expr value = null; 555 for (A4Tuple tuple : (A4TupleSet) ans.eval(field, s)) { 556 Expr t = null; 557 for (int i = 0; i < tuple.arity(); i++) { 558 Expr a = atom(tuple.atom(i), tuple.sig(i), vars); 559 t = t == null ? a : t.product(a); 560 } 561 value = value == null ? t : value.plus(t); 562 } 563 st = st.and(value == null ? field.no() : field.equal(value)); 564 } 565 trace = s == Math.min(count, length) - 1 ? st : st.and(trace.after()); 566 } 567 if (withLoop) { 568 Expr periodic = ExprConstant.TRUE; 569 for (Expr field : fields) { 570 periodic = periodic.and(field.equal(repeat(field, length - loop, true))); 571 } 572 trace = trace.and(repeat(periodic.always(), loop, false)); 573 } 574 List<Decl> decls = new ArrayList<>(vars.values()); 575 for (int i = 0; i < decls.size(); i++) { 576 for (int j = i + 1; j < decls.size(); j++) { 577 trace = decls.get(i).get().equal(decls.get(j).get()).not().and(trace); 578 } 579 } 580 return decls.isEmpty() ? trace : ExprQt.Op.SOME.make(Pos.UNKNOWN, Pos.UNKNOWN, decls, trace); 581 } 582 583 // The command with the given bounds of the trace length. 584 private static Command withTraceLength(Command c, int minTraceLength, int maxTraceLength) { 585 return new Command(c.pos, c.nameExpr, c.label, c.check, c.overall, c.bitwidth, c.maxseq, minTraceLength, 586 maxTraceLength, c.expects, c.scope, c.additionalExactScopes, c.commandKeyword, c.formula, c.parent); 587 } 588 589 private static String multiplicity(Sig sig) { 590 if (sig.isOne != null) { 591 return "One"; 592 } else if (sig.isLone != null) { 593 return "Lone"; 594 } else if (sig.isSome != null) { 595 return "Some"; 596 } 597 return "Any"; 598 } 599 600 // The options for a solve whose unsatisfiability is not shown as an unsat core: a check, and the next 601 // trace, fork or configuration. The solver is not the prover, which logs the translation to a file for 602 // an unsat core; that log of a large model can take gigabytes, more than a temporary folder holds. 603 private static A4Options withoutCore(A4Options options) { 604 A4Options without = options.dup(); 605 SATFactory minisat = SATFactory.get("minisat"); 606 without.solver = minisat.isPresent() ? minisat : SATFactory.get("sat4j"); 607 return without; 608 } 609 610 private static String removeCurly(String string) { 611 int length = string.length(); 612 if (length > 0 && string.charAt(0) == '{' && string.charAt(length - 1) == '}') { 613 return string.substring(1, length - 1); 614 } 615 throw new IllegalArgumentException("\"" + string + "\" is not surrounded by curly brackets."); 616 } 617}