use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class ScopeComputer method derive_scope_from_parent.
// ===========================================================================================================================//
/**
* If A is not toplevel, and we haven't been able to derive its scope yet, then
* give it its parent's scope.
*/
private boolean derive_scope_from_parent(Iterable<Sig> sigs) throws Err {
boolean changed = false;
Sig trouble = null;
for (Sig s : sigs) if (!s.builtin && !s.isTopLevel() && sig2scope(s) < 0 && (s instanceof PrimSig)) {
PrimSig p = ((PrimSig) s).parent;
int pb = sig2scope(p);
if (pb >= 0) {
sig2scope(s, pb);
changed = true;
} else {
trouble = s;
}
}
if (changed)
return true;
if (trouble == null)
return false;
throw new ErrorSyntax(cmd.pos, "You must specify a scope for sig \"" + trouble + "\"");
}
use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class ScopeComputer method derive_overall_scope.
// ===========================================================================================================================//
/**
* If A is toplevel, and we haven't been able to derive its scope yet, then let
* it get the "overall" scope.
*/
private boolean derive_overall_scope(Iterable<Sig> sigs) throws Err {
boolean changed = false;
final int overall = (cmd.overall < 0 && cmd.scope.size() == 0) ? 3 : cmd.overall;
for (Sig s : sigs) if (!s.builtin && s.isTopLevel() && sig2scope(s) < 0) {
if (s.isEnum != null) {
sig2scope(s, 0);
continue;
}
// enum without children should get the empty set
if (overall < 0)
throw new ErrorSyntax(cmd.pos, "You must specify a scope for sig \"" + s + "\"");
sig2scope(s, overall);
changed = true;
}
return changed;
}
use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class TranslateAlloyToKodkod method execute_greedyCommand.
private static A4Solution execute_greedyCommand(A4Reporter rep, Iterable<Sig> sigs, Command usercommand, A4Options opt) throws Exception {
// FIXTHIS: if the next command has a "smaller scope" than the last
// command, we would get a Kodkod exception...
// FIXTHIS: if the solver is "toCNF" or "toKodkod" then this method will
// throw an Exception...
// FIXTHIS: does solution enumeration still work when we're doing a
// greedy solve?
TranslateAlloyToKodkod tr = null;
try {
long start = System.currentTimeMillis();
GreedySimulator sim = new GreedySimulator();
sim.allSigs = sigs;
sim.partial = null;
A4Reporter rep2 = new A4Reporter(rep) {
private boolean first = true;
@Override
public void translate(String solver, int bitwidth, int maxseq, int skolemDepth, int symmetry) {
if (first)
super.translate(solver, bitwidth, maxseq, skolemDepth, symmetry);
first = false;
}
@Override
public void resultSAT(Object command, long solvingTime, Object solution) {
}
@Override
public void resultUNSAT(Object command, long solvingTime, Object solution) {
}
};
// Form the list of commands
List<Command> commands = new ArrayList<Command>();
while (usercommand != null) {
commands.add(usercommand);
usercommand = usercommand.parent;
}
// For each command...
A4Solution sol = null;
for (int i = commands.size() - 1; i >= 0; i--) {
Command cmd = commands.get(i);
sim.growableSigs = cmd.getGrowableSigs();
while (cmd != null) {
rep.debug(cmd.scope.toString());
usercommand = cmd;
tr = new TranslateAlloyToKodkod(rep2, opt, sigs, cmd);
tr.makeFacts(cmd.formula);
sim.totalOrderPredicates = tr.totalOrderPredicates;
sol = tr.frame.solve(rep2, cmd, sim.partial == null || cmd.check ? new Simplifier() : sim, false);
if (!sol.satisfiable() && !cmd.check) {
start = System.currentTimeMillis() - start;
if (sim.partial == null) {
rep.resultUNSAT(cmd, start, sol);
return sol;
} else {
rep.resultSAT(cmd, start, sim.partial);
return sim.partial;
}
}
if (sol.satisfiable() && cmd.check) {
start = System.currentTimeMillis() - start;
rep.resultSAT(cmd, start, sol);
return sol;
}
sim.partial = sol;
if (sim.growableSigs.isEmpty())
break;
for (Sig s : sim.growableSigs) {
CommandScope sc = cmd.getScope(s);
if (sc.increment > sc.endingScope - sc.startingScope) {
cmd = null;
break;
}
cmd = cmd.change(s, sc.isExact, sc.startingScope + sc.increment, sc.endingScope, sc.increment);
}
}
}
if (sol.satisfiable())
rep.resultSAT(usercommand, System.currentTimeMillis() - start, sol);
else
rep.resultUNSAT(usercommand, System.currentTimeMillis() - start, sol);
return sol;
} catch (CapacityExceededException ex) {
throw rethrow(ex);
} catch (HigherOrderDeclException ex) {
Pos p = tr != null ? tr.frame.kv2typepos(ex.decl().variable()).b : Pos.UNKNOWN;
throw new ErrorType(p, "Analysis cannot be performed since it requires higher-order quantification that could not be skolemized.");
}
}
use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class CompUtil method areIntsUsed.
// =============================================================================================================//
/**
* Whether or not Int appears in the relation types found in these sigs
*/
public static boolean areIntsUsed(Iterable<Sig> sigs, Command cmd) {
/* check for Int-typed relations */
for (Sig s : sigs) {
for (Field f : s.getFields()) {
for (ProductType pt : f.type()) {
for (int k = 0; k < pt.arity(); k++) {
if (pt.get(k) == SIGINT || pt.get(k) == SEQIDX)
return true;
}
}
}
}
if (cmd == null)
return false;
/* check expressions; look for CAST2SIGING (Int[]) */
try {
Object intTriggerNode;
intTriggerNode = cmd.formula.accept(new VisitQueryOnce<Object>() {
@Override
public Object visit(ExprCall x) throws Err {
// Int[]
if (x.fun.label.startsWith("integer/"))
return null;
return super.visit(x);
}
@Override
public Object visit(ExprUnary x) throws Err {
if (x.op == Op.CAST2SIGINT)
return x;
return super.visit(x);
}
});
if (intTriggerNode != null)
return true;
} catch (Err e) {
}
return false;
}
use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class A4SolutionReader method parseSig.
/**
* Parse sig/set.
*/
private Sig parseSig(String id, int depth) throws IOException, Err {
Sig ans = id2sig.get(id);
if (ans != null)
return ans;
XMLNode node = nmap.get(id);
if (node == null)
throw new IOException("Unknown SigID " + id + " encountered.");
if (!node.is("sig"))
throw new IOException("ID " + id + " is not a sig.");
String label = label(node);
Attr isAbstract = yes(node, "abstract") ? Attr.ABSTRACT : null;
Attr isOne = yes(node, "one") ? Attr.ONE : null;
Attr isLone = yes(node, "lone") ? Attr.LONE : null;
Attr isSome = yes(node, "some") ? Attr.SOME : null;
Attr isPrivate = yes(node, "private") ? Attr.PRIVATE : null;
Attr isMeta = yes(node, "meta") ? Attr.META : null;
Attr isEnum = yes(node, "enum") ? Attr.ENUM : null;
Attr isExact = yes(node, "exact") ? Attr.EXACT : null;
if (yes(node, "builtin")) {
if (label.equals(UNIV.label)) {
id2sig.put(id, UNIV);
return UNIV;
}
if (label.equals(SIGINT.label)) {
id2sig.put(id, SIGINT);
return SIGINT;
}
if (label.equals(SEQIDX.label)) {
id2sig.put(id, SEQIDX);
return SEQIDX;
}
if (label.equals(STRING.label)) {
id2sig.put(id, STRING);
return STRING;
}
throw new IOException("Unknown builtin sig: " + label + " (id=" + id + ")");
}
if (depth > nmap.size())
throw new IOException("Sig " + label + " (id=" + id + ") is in a cyclic inheritance relationship.");
List<Sig> parents = null;
TupleSet ts = factory.noneOf(1);
for (XMLNode sub : node) {
if (sub.is("atom")) {
ts.add(factory.tuple(sub.getAttribute("label")));
continue;
}
if (!sub.is("type"))
continue;
Sig parent = parseSig(sub.getAttribute("ID"), depth + 1);
if (parents == null)
parents = new ArrayList<Sig>();
parents.add(parent);
}
if (parents == null) {
String parentID = node.getAttribute("parentID");
Sig parent = parseSig(parentID, depth + 1);
if (!(parent instanceof PrimSig))
throw new IOException("Parent of sig " + label + " (id=" + id + ") must not be a subset sig.");
for (Expr choice : choices) if (choice instanceof PrimSig && parent == ((PrimSig) choice).parent && ((Sig) choice).label.equals(label)) {
ans = (Sig) choice;
choices.remove(choice);
break;
}
if (ans == null) {
ans = new PrimSig(label, (PrimSig) parent, isAbstract, isLone, isOne, isSome, isPrivate, isMeta, isEnum);
allsigs.add(ans);
}
} else {
for (Expr choice : choices) if (choice instanceof SubsetSig && ((Sig) choice).label.equals(label) && sameset(parents, ((SubsetSig) choice).parents)) {
ans = (Sig) choice;
choices.remove(choice);
break;
}
if (ans == null) {
ans = new SubsetSig(label, parents, isExact, isLone, isOne, isSome, isPrivate, isMeta);
allsigs.add(ans);
}
}
id2sig.put(id, ans);
expr2ts.put(ans, ts);
if (ans instanceof PrimSig) {
// Add the atoms in this SIG into all parent sigs
for (PrimSig ans2 = ((PrimSig) ans).parent; ans2 != null && !ans2.builtin; ans2 = ans2.parent) {
TupleSet ts2 = expr2ts.get(ans2);
if (ts2 == null)
ts2 = ts.clone();
else {
ts2 = ts2.clone();
ts2.addAll(ts);
}
expr2ts.put(ans2, ts2);
}
}
return ans;
}
Aggregations