001package org.clafer.alloy.solve; 002 003import java.util.ArrayList; 004import java.util.List; 005import java.util.Map; 006import org.clafer.alloy.AlloyGenerator; 007import org.clafer.frontend.ModelFiles; 008import org.clafer.frontend.SourceSpan; 009 010/** 011 * The constraints of the Alloy code that an unsat core can name, as 012 * claferIG's {@code Constraints.hs} has them: the cardinalities of the 013 * clafers and the constraints of the model, each at its span in the Alloy 014 * code. 015 */ 016public final class Constraints { 017 018 private Constraints() { 019 } 020 021 /** 022 * A span of the Alloy code, as an unsat core gives it: lines and columns, 023 * the end column one past Alloy's. 024 */ 025 public record Range(long line, long column, long endLine, long endColumn) { 026 } 027 028 /** 029 * A cardinality, {@code upper} -1 for none. 030 */ 031 public record Cardinality(long lower, long upper) { 032 033 @Override 034 public String toString() { 035 if (upper < 0) { 036 return lower == 0 ? "*" : lower == 1 ? "+" : lower + "..*"; 037 } 038 if (lower == 0 && upper == 1) { 039 return "?"; 040 } 041 return lower == upper ? Long.toString(lower) : lower + ".." + upper; 042 } 043 } 044 045 /** 046 * A clafer and its cardinality. 047 */ 048 public record ClaferInfo(String uniqueId, Cardinality cardinality) { 049 050 @Override 051 public String toString() { 052 return uniqueId + " " + cardinality; 053 } 054 } 055 056 /** 057 * A constraint of the model: its span in the complete model, its text, 058 * and its position as shown, the file and the position in it 059 * ({@code file.cfr:3:5}), and the line in its file. 060 */ 061 public record ConstraintInfo(SourceSpan pos, String syntax, String position, int fileLine) { 062 063 @Override 064 public String toString() { 065 return syntax + " (" + position + ")"; 066 } 067 068 /** 069 * Whether the constraint spans the given line of its file. 070 */ 071 public boolean isOnLine(long n) { 072 return fileLine <= n && n <= fileLine + pos.endLine() - pos.line(); 073 } 074 } 075 076 /** 077 * A constraint: a cardinality ({@code claferInfo}) or a constraint of the 078 * model ({@code constraintInfo}). 079 */ 080 public sealed interface Constraint { 081 082 Range range(); 083 } 084 085 public record ExactCardinalityConstraint(Range range, ClaferInfo claferInfo) implements Constraint { 086 } 087 088 public record LowerCardinalityConstraint(Range range, ClaferInfo claferInfo) implements Constraint { 089 } 090 091 public record UpperCardinalityConstraint(Range range, ClaferInfo claferInfo) implements Constraint { 092 } 093 094 public record UserConstraint(Range range, ConstraintInfo constraintInfo) implements Constraint { 095 } 096 097 /** 098 * {@code parseConstraints}. 099 * 100 * @param clafers the name and cardinality of each clafer, by its Alloy 101 * name 102 */ 103 static List<Constraint> parseConstraints(ModelFiles files, Map<String, ClaferInfo> clafers, 104 List<AlloyGenerator.Mapped> mapping) { 105 List<Constraint> out = new ArrayList<>(); 106 List<String> text = text(files); 107 for (AlloyGenerator.Mapped m : mapping) { 108 Range r = new Range(m.line(), m.column(), m.endLine(), m.endColumn()); 109 switch (m.kind()) { 110 case CONSTRAINT -> out.add(new UserConstraint(r, constraintInfoOf(files, text, m.span()))); 111 case LOWER_CARD -> out.add(new LowerCardinalityConstraint(r, info(clafers, m.clafer()))); 112 case UPPER_CARD -> out.add(new UpperCardinalityConstraint(r, info(clafers, m.clafer()))); 113 case EXACT_CARD -> out.add(new ExactCardinalityConstraint(r, info(clafers, m.clafer()))); 114 } 115 } 116 return out; 117 } 118 119 private static ClaferInfo info(Map<String, ClaferInfo> clafers, String clafer) { 120 ClaferInfo i = clafers.get(clafer); 121 if (i == null) { 122 throw new IllegalStateException("Unknown clafer " + clafer); 123 } 124 return i; 125 } 126 127 // the columns count a tab up to the next multiple of 8, as the compiler does 128 static List<String> text(ModelFiles files) { 129 List<String> out = new ArrayList<>(); 130 String all = files.text(); 131 int start = 0; 132 while (start < all.length()) { 133 int end = all.indexOf('\n', start); 134 if (end < 0) { 135 end = all.length(); 136 } 137 out.add(expandTabs(all.substring(start, end))); 138 start = end + 1; 139 } 140 return out; 141 } 142 143 /** 144 * The information of a constraint: its text and position in the files of 145 * the model. 146 */ 147 static ConstraintInfo constraintInfoOf(ModelFiles files, List<String> text, SourceSpan s) { 148 ModelFiles.Position p = files.position(s.line()); 149 String position = (p.path().isEmpty() ? "" : p.path() + ":") + p.line() + ":" + s.column(); 150 String last = line(text, s.endLine()); 151 String rest = drop(last, s.endColumn() - 1); 152 return new ConstraintInfo(s, closeParentheses(extract(text, s), rest), position, p.line()); 153 } 154 155 private static String line(List<String> text, int l) { 156 return l - 1 < text.size() ? text.get(l - 1) : ""; 157 } 158 159 private static String drop(String s, int n) { 160 return n >= s.length() ? "" : s.substring(Math.max(n, 0)); 161 } 162 163 private static String take(String s, int n) { 164 return n >= s.length() ? s : s.substring(0, Math.max(n, 0)); 165 } 166 167 private static String extract(List<String> text, SourceSpan s) { 168 if (s.line() == s.endLine()) { 169 return drop(take(line(text, s.line()), s.endColumn() - 1), s.column() - 1); 170 } 171 StringBuilder sb = new StringBuilder(drop(line(text, s.line()), s.column() - 1)).append('\n'); 172 for (int l = s.line() + 1; l <= s.endLine() - 1; l++) { 173 sb.append(line(text, l)).append('\n'); 174 } 175 return sb.append(take(line(text, s.endLine()), s.endColumn() - 1)).append('\n').toString(); 176 } 177 178 // an expression's span ends before the parentheses that close its last operand, e.g., 179 // `initially (a --> b` of `initially (a --> b)`, which are added 180 private static String closeParentheses(String extracted, String rest) { 181 while (true) { 182 long open = extracted.chars().filter(c -> c == '(').count() 183 - extracted.chars().filter(c -> c == ')').count(); 184 int spaces = 0; 185 while (spaces < rest.length() && rest.charAt(spaces) == ' ') { 186 spaces++; 187 } 188 if (open > 0 && spaces < rest.length() && rest.charAt(spaces) == ')') { 189 extracted = extracted + rest.substring(0, spaces) + ")"; 190 rest = rest.substring(spaces + 1); 191 } else { 192 return extracted; 193 } 194 } 195 } 196 197 // the line with its tabs as spaces, up to the next multiple of 8 198 private static String expandTabs(String line) { 199 StringBuilder sb = new StringBuilder(); 200 for (char ch : line.toCharArray()) { 201 if (ch == '\t') { 202 int n = 8 - sb.length() % 8; 203 sb.append(" ".repeat(n)); 204 } else { 205 sb.append(ch); 206 } 207 } 208 return sb.toString(); 209 } 210}