Search in sources :

Example 61 with Err

use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.

the class SimInstance method init.

/**
 * Initializes the given field 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(Field field, SimTupleset value) throws Err {
    if (value == null) {
        sfs.remove(field);
        return;
    }
    if (!value.empty() && value.arity() != field.type().arity())
        throw new ErrorType("Evaluator encountered an error: field " + field.label + " arity must not be " + value.arity());
    if (field.defined)
        throw new ErrorAPI("Evaluator cannot prebind the value of a defined field.");
    sfs.put(field, value);
    cacheUNIV = null;
    cacheSTRING = null;
    cacheForConstants.clear();
}
Also used : ErrorAPI(edu.mit.csail.sdg.alloy4.ErrorAPI) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType)

Example 62 with Err

use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.

the class A4Solution method eval.

/**
 * Return the A4TupleSet for the given field (if solution not yet solved, or
 * unsatisfiable, or field not found, then return an empty tupleset)
 */
public A4TupleSet eval(Field field) {
    try {
        if (!solved || eval == null)
            return new A4TupleSet(factory.noneOf(field.type().arity()), this);
        A4TupleSet ans = evalCache.get(field);
        if (ans != null)
            return ans;
        TupleSet ts = eval.evaluate((Expression) TranslateAlloyToKodkod.alloy2kodkod(this, field));
        ans = new A4TupleSet(ts, this);
        evalCache.put(field, ans);
        return ans;
    } catch (Err er) {
        return new A4TupleSet(factory.noneOf(field.type().arity()), this);
    }
}
Also used : TupleSet(kodkod.instance.TupleSet) Err(edu.mit.csail.sdg.alloy4.Err)

Example 63 with Err

use of edu.mit.csail.sdg.alloy4.Err 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 64 with Err

use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.

the class A4Solution method eval.

/**
 * Return the A4TupleSet for the given sig (if solution not yet solved, or
 * unsatisfiable, or sig not found, then return an empty tupleset)
 */
public A4TupleSet eval(Sig sig) {
    try {
        if (!solved || eval == null)
            return new A4TupleSet(factory.noneOf(1), this);
        A4TupleSet ans = evalCache.get(sig);
        if (ans != null)
            return ans;
        TupleSet ts = eval.evaluate((Expression) TranslateAlloyToKodkod.alloy2kodkod(this, sig));
        ans = new A4TupleSet(ts, this);
        evalCache.put(sig, ans);
        return ans;
    } catch (Err er) {
        return new A4TupleSet(factory.noneOf(1), this);
    }
}
Also used : TupleSet(kodkod.instance.TupleSet) Err(edu.mit.csail.sdg.alloy4.Err)

Example 65 with Err

use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.

the class A4SolutionWriter method writeExpr.

/**
 * Write the given Expr and its Type.
 */
private boolean writeExpr(String prefix, Expr expr) throws Err {
    Type type = expr.type();
    if (!type.hasTuple())
        return false;
    if (sol != null) {
        // Check to see if the tupleset is *really* fully contained inside
        // "type".
        // If not, then grow "type" until the tupleset is fully contained
        // inside "type"
        Expr sum = type.toExpr();
        int lastSize = (-1);
        while (true) {
            A4TupleSet ts = (A4TupleSet) (sol.eval(expr.minus(sum)));
            int n = ts.size();
            if (n <= 0)
                break;
            if (lastSize > 0 && lastSize <= n)
                throw new ErrorFatal("An internal error occurred in the evaluator.");
            lastSize = n;
            Type extra = ts.iterator().next().type();
            type = type.merge(extra);
            sum = sum.plus(extra.toExpr());
        }
        // Now, write out the tupleset
        A4TupleSet ts = (A4TupleSet) (sol.eval(expr));
        for (A4Tuple t : ts) {
            if (prefix.length() > 0) {
                out.print(prefix);
                prefix = "";
            }
            out.print("   <tuple>");
            for (int i = 0; i < t.arity(); i++) Util.encodeXMLs(out, " <atom label=\"", t.atom(i), "\"/>");
            out.print(" </tuple>\n");
        }
    }
    // Now, write out the type
    if (prefix.length() > 0)
        return false;
    for (List<PrimSig> ps : type.fold()) {
        out.print("   <types>");
        for (PrimSig sig : ps) Util.encodeXMLs(out, " <type ID=\"", map(sig), "\"/>");
        out.print(" </types>\n");
    }
    return true;
}
Also used : Type(edu.mit.csail.sdg.ast.Type) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Aggregations

ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)32 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)28 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)27 Err (edu.mit.csail.sdg.alloy4.Err)25 Expr (edu.mit.csail.sdg.ast.Expr)25 Sig (edu.mit.csail.sdg.ast.Sig)24 Pos (edu.mit.csail.sdg.alloy4.Pos)16 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)15 ExprVar (edu.mit.csail.sdg.ast.ExprVar)15 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)15 ArrayList (java.util.ArrayList)15 Field (edu.mit.csail.sdg.ast.Sig.Field)12 LinkedHashMap (java.util.LinkedHashMap)10 IOException (java.io.IOException)9 TempList (edu.mit.csail.sdg.alloy4.ConstList.TempList)8 Func (edu.mit.csail.sdg.ast.Func)8 TupleSet (kodkod.instance.TupleSet)8 Command (edu.mit.csail.sdg.ast.Command)7 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)6 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)5