Search in sources :

Example 16 with ExprVar

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

the class SimInstance method visit.

/**
 * {@inheritDoc}
 */
@Override
public SimTupleset visit(Field x) throws Err {
    if (x.defined) {
        final ExprVar v = (ExprVar) (x.sig.decl.get());
        final Expr b = x.decl().expr;
        final Env<ExprVar, Object> oldenv = env;
        env = new Env<ExprVar, Object>();
        if (!b.hasVar(v)) {
            SimTupleset ans = cset(x.sig).product(cset(b));
            env = oldenv;
            return ans;
        }
        SimTupleset ans = SimTupleset.EMPTY;
        for (SimTuple a : visit(x.sig)) {
            SimTupleset left = SimTupleset.make(a);
            env.put(v, left);
            SimTupleset right = cset(b);
            env.remove(v);
            ans = left.product(right).union(ans);
        }
        env = oldenv;
        return ans;
    }
    Object ans = sfs.get(x);
    if (ans instanceof SimTupleset)
        return (SimTupleset) ans;
    else
        throw new ErrorFatal("Unknown field " + x + " encountered during evaluation.");
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr)

Example 17 with ExprVar

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

the class SimInstance method visit.

/**
 * {@inheritDoc}
 */
@Override
public SimTupleset visit(Sig x) throws Err {
    if (x.isSame(Sig.NONE))
        return SimTupleset.EMPTY;
    if (x.isSame(Sig.SEQIDX))
        return SimTupleset.make(0, maxseq - 1);
    if (x.isSame(Sig.SIGINT))
        return SimTupleset.make(min, max);
    if (x.isSame(Sig.STRING)) {
        if (cacheSTRING == null) {
            cacheSTRING = SimTupleset.EMPTY;
            for (Map.Entry<Expr, SimTupleset> e : sfs.entrySet()) if (e.getKey() instanceof Field || e.getKey() instanceof ExprVar) {
                for (SimTuple t : e.getValue()) for (int i = t.arity() - 1; i >= 0; i--) {
                    String a = t.get(i).toString();
                    if (a.length() > 0 && a.charAt(0) == '"')
                        cacheSTRING = cacheSTRING.union(SimTuple.make(t.get(i)));
                }
            }
        }
        return cacheSTRING;
    }
    if (x == Sig.UNIV) {
        if (cacheUNIV == null) {
            cacheUNIV = SimTupleset.make(min, max);
            for (Map.Entry<Expr, SimTupleset> e : sfs.entrySet()) if (e.getKey() instanceof PrimSig && ((PrimSig) (e.getKey())).isTopLevel())
                cacheUNIV = cacheUNIV.union(e.getValue());
            cacheUNIV = cacheUNIV.union(visit(Sig.STRING));
        }
        return cacheUNIV;
    }
    Object ans = sfs.get(x);
    if (ans instanceof SimTupleset)
        return (SimTupleset) ans;
    else
        throw new ErrorFatal("Unknown sig " + x + " encountered during evaluation.");
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) Field(edu.mit.csail.sdg.ast.Sig.Field) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 18 with ExprVar

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

the class A4Solution method toString.

/**
 * Dumps the Kodkod solution into String.
 */
@Override
public String toString() {
    if (!solved)
        return "---OUTCOME---\nUnknown.\n";
    if (eval == null)
        return "---OUTCOME---\nUnsatisfiable.\n";
    String answer = toStringCache;
    if (answer != null)
        return answer;
    Instance sol = eval.instance();
    StringBuilder sb = new StringBuilder();
    sb.append("---INSTANCE---\n" + "integers={");
    boolean firstTuple = true;
    for (IndexedEntry<TupleSet> e : sol.intTuples()) {
        if (firstTuple)
            firstTuple = false;
        else
            sb.append(", ");
        // No need to print e.index() since we've ensured the Int atom's
        // String representation is always equal to ""+e.index()
        Object atom = e.value().iterator().next().atom(0);
        sb.append(atom2name(atom));
    }
    sb.append("}\n");
    try {
        for (Sig s : sigs) {
            sb.append(s.label).append("=").append(eval(s)).append("\n");
            for (Field f : s.getFields()) sb.append(s.label).append("<:").append(f.label).append("=").append(eval(f)).append("\n");
        }
        for (ExprVar v : skolems) {
            sb.append("skolem ").append(v.label).append("=").append(eval(v)).append("\n");
        }
        return toStringCache = sb.toString();
    } catch (Err er) {
        return toStringCache = ("<Evaluator error occurred: " + er + ">");
    }
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) ExprVar(edu.mit.csail.sdg.ast.ExprVar) TupleSet(kodkod.instance.TupleSet) Field(edu.mit.csail.sdg.ast.Sig.Field) Err(edu.mit.csail.sdg.alloy4.Err) Instance(kodkod.instance.Instance)

Example 19 with ExprVar

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

the class CompModule method addSig.

Sig addSig(String name, ExprVar par, List<ExprVar> parents, List<Decl> fields, Expr fact, Attr... attributes) throws Err {
    Sig obj;
    Pos pos = Pos.UNKNOWN.merge(WHERE.find(attributes));
    status = 3;
    dup(pos, name, true);
    String full = (path.length() == 0) ? "this/" + name : path + "/" + name;
    Pos subset = null, subsig = null;
    boolean exact = false;
    if (par != null) {
        if (par.label.equals("extends")) {
            subsig = par.span().merge(parents.get(0).span());
        } else {
            exact = !par.label.equals("in");
            subset = par.span();
            for (ExprVar p : parents) subset = p.span().merge(subset);
        }
    }
    attributes = Util.append(attributes, exact ? Attr.EXACT : null);
    if (subset != null) {
        attributes = Util.append(attributes, SUBSET.makenull(subset));
        List<Sig> newParents = new ArrayList<Sig>(parents == null ? 0 : parents.size());
        if (parents != null)
            for (ExprVar p : parents) newParents.add(new PrimSig(p.label, WHERE.make(p.pos)));
        obj = new SubsetSig(full, newParents, attributes);
    } else {
        attributes = Util.append(attributes, SUBSIG.makenull(subsig));
        PrimSig newParent = (parents != null && parents.size() > 0) ? (new PrimSig(parents.get(0).label, WHERE.make(parents.get(0).pos))) : UNIV;
        obj = new PrimSig(full, newParent, attributes);
    }
    sigs.put(name, obj);
    old2fields.put(obj, fields);
    old2appendedfacts.put(obj, fact);
    return obj;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) ExprVar(edu.mit.csail.sdg.ast.ExprVar) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Pos(edu.mit.csail.sdg.alloy4.Pos) ArrayList(java.util.ArrayList) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 20 with ExprVar

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

the class TranslateAlloyToKodkod method visit.

/**
 * {@inheritDoc}
 */
@Override
public Object visit(ExprCall x) throws Err {
    final Func f = x.fun;
    final Object candidate = f.count() == 0 ? cacheForConstants.get(f) : null;
    if (candidate != null)
        return candidate;
    final Expr body = f.getBody();
    if (body.type().arity() < 0 || body.type().arity() != f.returnDecl.type().arity())
        throw new ErrorType(body.span(), "Function return value not fully resolved.");
    final int n = f.count();
    int maxRecursion = unrolls;
    for (Func ff : current_function) if (ff == f) {
        if (maxRecursion < 0) {
            throw new ErrorSyntax(x.span(), "" + f + " cannot call itself recursively!");
        }
        if (maxRecursion == 0) {
            Type t = f.returnDecl.type();
            if (t.is_bool)
                return Formula.FALSE;
            if (t.is_int())
                return IntConstant.constant(0);
            int i = t.arity();
            Expression ans = Expression.NONE;
            while (i > 1) {
                ans = ans.product(Expression.NONE);
                i--;
            }
            return ans;
        }
        maxRecursion--;
    }
    Env<ExprVar, Object> newenv = new Env<ExprVar, Object>();
    for (int i = 0; i < n; i++) newenv.put(f.get(i), cset(x.args.get(i)));
    Env<ExprVar, Object> oldenv = env;
    env = newenv;
    current_function.add(f);
    Object ans = visitThis(body);
    env = oldenv;
    current_function.remove(current_function.size() - 1);
    if (ans instanceof Formula)
        k2pos((Formula) ans, x);
    if (f.count() == 0)
        cacheForConstants.put(f, ans);
    return ans;
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) Type(edu.mit.csail.sdg.ast.Type) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Expr(edu.mit.csail.sdg.ast.Expr) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Func(edu.mit.csail.sdg.ast.Func) Expression(kodkod.ast.Expression) BinaryExpression(kodkod.ast.BinaryExpression) IntExpression(kodkod.ast.IntExpression) Env(edu.mit.csail.sdg.alloy4.Env)

Aggregations

ExprVar (edu.mit.csail.sdg.ast.ExprVar)22 Expr (edu.mit.csail.sdg.ast.Expr)11 Sig (edu.mit.csail.sdg.ast.Sig)11 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)8 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)7 Field (edu.mit.csail.sdg.ast.Sig.Field)7 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)5 Func (edu.mit.csail.sdg.ast.Func)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)3 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)3 SafeList (edu.mit.csail.sdg.alloy4.SafeList)3 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)3 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)3 A4Solution (edu.mit.csail.sdg.translator.A4Solution)3 IOException (java.io.IOException)3 IdentityHashMap (java.util.IdentityHashMap)3 Map (java.util.Map)3