Search in sources :

Example 1 with ExprUnary

use of edu.mit.csail.sdg.ast.ExprUnary 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();
    }
}
Also used : Err(edu.mit.csail.sdg.alloy4.Err) Decl(edu.mit.csail.sdg.ast.Decl) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Field(edu.mit.csail.sdg.ast.Sig.Field) ExprBinary(edu.mit.csail.sdg.ast.ExprBinary) Expr(edu.mit.csail.sdg.ast.Expr) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) ExprUnary(edu.mit.csail.sdg.ast.ExprUnary) Module(edu.mit.csail.sdg.ast.Module) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Pair(edu.mit.csail.sdg.alloy4.Pair)

Example 2 with ExprUnary

use of edu.mit.csail.sdg.ast.ExprUnary in project org.alloytools.alloy by AlloyTools.

the class BoundsComputer method sim.

// ==============================================================================================================//
/**
 * If ex is a simple combination of Relations, then return that combination,
 * else return null.
 */
private Expression sim(Expr ex) {
    while (ex instanceof ExprUnary) {
        ExprUnary u = (ExprUnary) ex;
        if (u.op != ExprUnary.Op.NOOP && u.op != ExprUnary.Op.EXACTLYOF)
            break;
        ex = u.sub;
    }
    if (ex instanceof ExprBinary) {
        ExprBinary b = (ExprBinary) ex;
        if (b.op == ExprBinary.Op.ARROW || b.op == ExprBinary.Op.PLUS || b.op == ExprBinary.Op.JOIN) {
            Expression left = sim(b.left);
            if (left == null)
                return null;
            Expression right = sim(b.right);
            if (right == null)
                return null;
            if (b.op == ExprBinary.Op.ARROW)
                return left.product(right);
            if (b.op == ExprBinary.Op.PLUS)
                return left.union(right);
            else
                return left.join(right);
        }
    }
    if (ex instanceof ExprConstant) {
        switch(((ExprConstant) ex).op) {
            case EMPTYNESS:
                return Expression.NONE;
        }
    }
    if (ex == Sig.NONE)
        return Expression.NONE;
    if (ex == Sig.SIGINT)
        return Expression.INTS;
    if (ex instanceof Sig)
        return sol.a2k((Sig) ex);
    if (ex instanceof Field)
        return sol.a2k((Field) ex);
    return null;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Sig(edu.mit.csail.sdg.ast.Sig) Field(edu.mit.csail.sdg.ast.Sig.Field) ExprBinary(edu.mit.csail.sdg.ast.ExprBinary) Expression(kodkod.ast.Expression) ExprUnary(edu.mit.csail.sdg.ast.ExprUnary) ExprConstant(edu.mit.csail.sdg.ast.ExprConstant)

Example 3 with ExprUnary

use of edu.mit.csail.sdg.ast.ExprUnary 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;
}
Also used : Sig(edu.mit.csail.sdg.ast.Sig) VisitQueryOnce(edu.mit.csail.sdg.ast.VisitQueryOnce) Field(edu.mit.csail.sdg.ast.Sig.Field) ExprCall(edu.mit.csail.sdg.ast.ExprCall) Err(edu.mit.csail.sdg.alloy4.Err) ProductType(edu.mit.csail.sdg.ast.Type.ProductType) ExprUnary(edu.mit.csail.sdg.ast.ExprUnary)

Aggregations

ExprUnary (edu.mit.csail.sdg.ast.ExprUnary)3 Sig (edu.mit.csail.sdg.ast.Sig)3 Field (edu.mit.csail.sdg.ast.Sig.Field)3 Err (edu.mit.csail.sdg.alloy4.Err)2 ExprBinary (edu.mit.csail.sdg.ast.ExprBinary)2 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)2 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)2 Pair (edu.mit.csail.sdg.alloy4.Pair)1 Decl (edu.mit.csail.sdg.ast.Decl)1 Expr (edu.mit.csail.sdg.ast.Expr)1 ExprCall (edu.mit.csail.sdg.ast.ExprCall)1 ExprConstant (edu.mit.csail.sdg.ast.ExprConstant)1 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)1 Module (edu.mit.csail.sdg.ast.Module)1 ProductType (edu.mit.csail.sdg.ast.Type.ProductType)1 VisitQueryOnce (edu.mit.csail.sdg.ast.VisitQueryOnce)1 Expression (kodkod.ast.Expression)1