use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class CompModule method addCommand.
/**
* Add a COMMAND declaration.
*/
void addCommand(boolean followUp, Pos pos, Expr e, boolean check, int overall, int bitwidth, int seq, int expects, List<CommandScope> scopes, ExprVar label) throws Err {
if (followUp && !Version.experimental)
throw new ErrorSyntax(pos, "Syntax error encountering => symbol.");
if (label != null)
pos = Pos.UNKNOWN.merge(pos).merge(label.pos);
status = 3;
String n;
if (check)
n = addAssertion(pos, "check$" + (1 + commands.size()), e);
else
addFunc(e.span().merge(pos), Pos.UNKNOWN, n = "run$" + (1 + commands.size()), null, new ArrayList<Decl>(), null, e);
String labelName = (label == null || label.label.length() == 0) ? n : label.label;
Command parent = followUp ? commands.get(commands.size() - 1) : null;
Command newcommand = new Command(e.span().merge(pos), e, labelName, check, overall, bitwidth, seq, expects, scopes, null, ExprVar.make(null, n), parent);
if (parent != null)
commands.set(commands.size() - 1, newcommand);
else
commands.add(newcommand);
}
use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class CompModule method resolveCommand.
/**
* Resolve a particular command.
*/
private Command resolveCommand(Command cmd, ConstList<Sig> exactSigs, Expr globalFacts) throws Err {
Command parent = cmd.parent == null ? null : resolveCommand(cmd.parent, exactSigs, globalFacts);
String cname = ((ExprVar) (cmd.formula)).label;
Expr e;
Clause declaringClause = null;
if (cmd.check) {
// We prefer assertion in the
List<Object> m = getRawQS(2, cname);
// topmost module
if (m.size() == 0 && cname.indexOf('/') < 0)
m = getRawNQS(this, 2, cname);
if (m.size() > 1)
unique(cmd.pos, cname, m);
if (m.size() < 1)
throw new ErrorSyntax(cmd.pos, "The assertion \"" + cname + "\" cannot be found.");
Expr expr = (Expr) (m.get(0));
e = expr.not();
} else {
// We prefer fun/pred in the
List<Object> m = getRawQS(4, cname);
// topmost module
if (m.size() == 0 && cname.indexOf('/') < 0)
m = getRawNQS(this, 4, cname);
if (m.size() > 1)
unique(cmd.pos, cname, m);
if (m.size() < 1)
throw new ErrorSyntax(cmd.pos, "The predicate/function \"" + cname + "\" cannot be found.");
Func f = (Func) (m.get(0));
declaringClause = f;
e = f.getBody();
if (!f.isPred)
e = e.in(f.returnDecl);
if (f.decls.size() > 0)
e = ExprQt.Op.SOME.make(null, null, f.decls, e);
}
if (e == null)
e = ExprConstant.TRUE;
TempList<CommandScope> sc = new TempList<CommandScope>(cmd.scope.size());
for (CommandScope et : cmd.scope) {
Sig s = getRawSIG(et.sig.pos, et.sig.label);
if (s == null)
throw new ErrorSyntax(et.sig.pos, "The sig \"" + et.sig.label + "\" cannot be found.");
sc.add(new CommandScope(null, s, et.isExact, et.startingScope, et.endingScope, et.increment));
}
if (cmd.nameExpr != null) {
cmd.nameExpr.setReferenced(declaringClause);
}
return new Command(cmd.pos, cmd.nameExpr, cmd.label, cmd.check, cmd.overall, cmd.bitwidth, cmd.maxseq, cmd.expects, sc.makeConst(), exactSigs, globalFacts.and(e), parent);
}
use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class SimInstance method makeAtom.
/**
* Create a fresh atom for the given sig, then return the newly created atom.
*
* @throws ErrorAPI if attempting to add an atom to an abstract sig with
* children, or a builtin sig, or a subset sig.
*/
public SimAtom makeAtom(Sig sig) throws Err {
if (sig.builtin)
throw new ErrorAPI("Cannot add an atom to a builtin sig.");
if (!(sig instanceof PrimSig))
throw new ErrorAPI("Cannot add an atom to a subset sig.");
PrimSig s = (PrimSig) sig;
if (s.isAbstract != null && !s.children().isEmpty())
throw new ErrorAPI("Cannot add an atom to an abstract parent sig.");
String label = sig.label + "$";
if (label.startsWith("this/"))
label = label.substring(5);
for (int i = 0; ; i++) {
SimAtom atom = SimAtom.make(label + i);
if (hasAtom(atom))
continue;
SimTupleset add = SimTupleset.make(SimTuple.make(atom));
if (cacheUNIV != null)
cacheUNIV = cacheUNIV.union(add);
for (; s != null; s = s.parent) if (!s.builtin) {
SimTupleset old = sfs.get(s);
if (old == null || old.empty())
sfs.put(s, add);
else if (!add.in(old))
sfs.put(s, old.union(add));
else
break;
}
return atom;
}
}
use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class SimInstance method init.
/**
* Initializes the given sig to be associated with the given unary value; should
* only be called at the beginning.
* <p>
* The resulting instance may or may not satisfy all facts, and should be
* checked for consistency.
*/
public void init(Sig sig, SimTupleset value) throws Err {
if (value == null) {
sfs.remove(sig);
return;
}
if (value.arity() > 1)
throw new ErrorType("Evaluator encountered an error: sig " + sig.label + " arity must not be " + value.arity());
if (sig.builtin)
throw new ErrorAPI("Evaluator cannot prebind the builtin sig \"" + sig.label + "\"");
sfs.put(sig, value);
cacheUNIV = null;
cacheSTRING = null;
cacheForConstants.clear();
}
use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class SimInstance method validate.
/**
* Checks whether this instance satisfies every fact defined in the given model.
*
* @param world - this must be the root of the Alloy model
* @return an empty String if yes, nonempty String if no
*/
public String validate(Module world) {
try {
for (Sig s : world.getAllReachableSigs()) if (!s.builtin) {
if (s.isLone != null && !(visit(s).longsize() <= 1))
return "There can be at most one " + s;
if (s.isOne != null && !(visit(s).longsize() == 1))
return "There must be exactly one " + s;
if (s.isSome != null && !(visit(s).longsize() >= 1))
return "There must be at least one " + s;
if (s instanceof SubsetSig) {
SubsetSig p = (SubsetSig) s;
Expr sum = null;
for (Sig par : p.parents) sum = par.plus(sum);
if (p.exact) {
if (!equal(s, sum))
return "Sig " + s + " must be equal to the union of its parents " + p.parents;
} else {
if (!isIn(s, sum))
return "Sig " + s + " must be equal or subset of its parents " + p.parents;
}
} else if (s != Sig.UNIV && s != Sig.NONE) {
PrimSig p = (PrimSig) s;
if (!isIn(s, p.parent))
return "Sig " + s + " must be equal or subset of its parent " + p.parent;
}
if (s.isAbstract != null) {
Expr sum = null;
for (Sig x : ((PrimSig) s).children()) sum = x.plus(sum);
if (sum != null && !equal(s, sum))
return "Abstract sig " + s + " must be equal to the union of its subsigs";
}
for (Decl d : s.getFieldDecls()) for (ExprHasName f : d.names) if (!((Field) f).defined) {
if (!cform(s.decl.get().join(f).in(d.expr).forAll(s.decl))) {
return "Field " + f + " violated its bound: " + visit((Field) f) + "\n" + d.expr;
}
SimTupleset setS = visit(s);
SimTupleset setF = visit((Field) f);
for (SimAtom x : setF.getAllAtoms(0)) if (!setS.has(x))
return "Field " + f + " first column has extra atom: " + setF + " not in " + setS;
}
for (Decl d : s.getFieldDecls()) {
if (d.disjoint != null && d.names.size() > 0) {
if (!cform(ExprList.makeDISJOINT(null, null, d.names)))
return "Fields must be disjoint.";
}
if (d.disjoint2 != null)
for (ExprHasName f : d.names) {
Decl that = s.oneOf("that");
Expr formula = s.decl.get().equal(that.get()).not().implies(s.decl.get().join(f).intersect(that.get().join(f)).no());
if (!cform(formula.forAll(that).forAll(s.decl)))
return "Fields must be disjoint.";
}
}
for (Expr f : s.getFacts()) {
if (!cform(f.forAll(s.decl))) {
f = f.deNOP();
if (f instanceof ExprUnary) {
ExprUnary u = (ExprUnary) f;
f = u.sub.deNOP();
if (f instanceof ExprBinary) {
ExprBinary b = (ExprBinary) f;
if (b.op == ExprBinary.Op.JOIN && b.left.isSame(s.decl.get()) && b.right.deNOP() instanceof Field) {
String n = ((Field) (b.right.deNOP())).label;
if (u.op == ExprUnary.Op.SOME)
return "The " + n + " cannot be empty.";
if (u.op == ExprUnary.Op.LONE)
return "The " + n + " cannot have more than one value.";
if (u.op == ExprUnary.Op.ONE)
return "The " + n + " must have exactly one value.";
if (u.op == ExprUnary.Op.NO)
return "The " + n + " must be empty.";
}
}
}
return "Cannot violate a consistency constraint";
}
}
}
for (Module m : world.getAllReachableModules()) for (Pair<String, Expr> f : m.getAllFacts()) if (!cform(f.b)) {
String err = f.a;
if (err.matches("^fact\\$[0-9][0-9]*"))
err = f.b.toString();
if (err.length() >= 2 && err.startsWith("\"") && err.endsWith("\""))
err = err.substring(1, err.length() - 1);
return "Violation: " + err;
}
return "";
} catch (Err ex) {
return "An internal error has occured:\n" + ex.dump();
}
}
Aggregations