Search in sources :

Example 6 with ErrorFatal

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

the class A4SolutionWriter method writeField.

/**
 * Write the given Field.
 */
private void writeField(Field x) throws Err {
    try {
        if (sol == null && x.isMeta != null)
            // when writing the metamodel, skip the metamodel
            return;
        // fields!
        if (x.type().hasNoTuple())
            // we do not allow "none" in the XML file's type
            return;
        // declarations
        if (rep != null)
            rep.write(x);
        Util.encodeXMLs(out, "\n<field label=\"", x.label, "\" ID=\"", map(x), "\" parentID=\"", map(x.sig));
        if (x.isPrivate != null)
            out.print("\" private=\"yes");
        if (x.isMeta != null)
            out.print("\" meta=\"yes");
        out.print("\">\n");
        writeExpr("", x);
        out.print("</field>\n");
    } catch (Throwable ex) {
        throw new ErrorFatal("Error evaluating field " + x.sig.label + "." + x.label, ex);
    }
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal)

Example 7 with ErrorFatal

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

the class A4SolutionWriter method writeInstance.

/**
 * If this solution is a satisfiable solution, this method will write it out in
 * XML format.
 */
static void writeInstance(A4Reporter rep, A4Solution sol, PrintWriter out, Iterable<Func> extraSkolems, Map<String, String> sources) throws Err {
    if (!sol.satisfiable())
        throw new ErrorAPI("This solution is unsatisfiable.");
    try {
        Util.encodeXMLs(out, "<alloy builddate=\"", Version.buildDate(), "\">\n\n");
        new A4SolutionWriter(rep, sol, sol.getAllReachableSigs(), sol.getBitwidth(), sol.getMaxSeq(), sol.getOriginalCommand(), sol.getOriginalFilename(), out, extraSkolems);
        if (sources != null)
            for (Map.Entry<String, String> e : sources.entrySet()) {
                Util.encodeXMLs(out, "\n<source filename=\"", e.getKey(), "\" content=\"", e.getValue(), "\"/>\n");
            }
        out.print("\n</alloy>\n");
    } catch (Throwable ex) {
        if (ex instanceof Err)
            throw (Err) ex;
        else
            throw new ErrorFatal("Error writing the solution XML file.", ex);
    }
    if (out.checkError())
        throw new ErrorFatal("Error writing the solution XML file.");
}
Also used : ErrorAPI(edu.mit.csail.sdg.alloy4.ErrorAPI) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Err(edu.mit.csail.sdg.alloy4.Err)

Example 8 with ErrorFatal

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

the class A4SolutionWriter method writeSkolem.

/**
 * Write the given Skolem.
 */
private void writeSkolem(ExprVar x) throws Err {
    try {
        if (sol == null)
            // when writing a metamodel, skip the skolems
            return;
        if (x.type().hasNoTuple())
            // we do not allow "none" in the XML file's type
            return;
        // declarations
        StringBuilder sb = new StringBuilder();
        Util.encodeXMLs(sb, "\n<skolem label=\"", x.label, "\" ID=\"", map(x), "\">\n");
        if (writeExpr(sb.toString(), x)) {
            out.print("</skolem>\n");
        }
    } catch (Throwable ex) {
        throw new ErrorFatal("Error evaluating skolem " + x.label, ex);
    }
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal)

Example 9 with ErrorFatal

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

the class TranslateAlloyToKodkod method execute_commandFromBook.

/**
 * Based on the specified "options", execute one command and return the
 * resulting A4Solution object.
 * <p>
 * Note: it will first test whether the model fits one of the model from the
 * "Software Abstractions" book; if so, it will use the exact instance that was
 * in the book.
 *
 * @param rep - if nonnull, we'll send compilation diagnostic messages to it
 * @param sigs - the list of sigs; this list must be complete
 * @param cmd - the Command to execute
 * @param opt - the set of options guiding the execution of the command
 * @return null if the user chose "save to FILE" as the SAT solver, and nonnull
 *         if the solver finishes the entire solving and is either satisfiable
 *         or unsatisfiable.
 *         <p>
 *         If the return value X is satisfiable, you can call X.next() to get
 *         the next satisfying solution X2; and you can call X2.next() to get
 *         the next satisfying solution X3... until you get an unsatisfying
 *         solution.
 */
public static A4Solution execute_commandFromBook(A4Reporter rep, Iterable<Sig> sigs, Command cmd, A4Options opt) throws Err {
    if (rep == null)
        rep = A4Reporter.NOP;
    TranslateAlloyToKodkod tr = null;
    try {
        if (cmd.parent != null || !cmd.getGrowableSigs().isEmpty())
            return execute_greedyCommand(rep, sigs, cmd, opt);
        tr = new TranslateAlloyToKodkod(rep, opt, sigs, cmd);
        tr.makeFacts(cmd.formula);
        return tr.frame.solve(rep, cmd, new Simplifier(), true);
    } catch (UnsatisfiedLinkError ex) {
        throw new ErrorFatal("The required JNI library cannot be found: " + ex.toString().trim(), ex);
    } 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.");
    } catch (Throwable ex) {
        if (ex instanceof Err)
            throw (Err) ex;
        else
            throw new ErrorFatal("Unknown exception occurred: " + ex, ex);
    }
}
Also used : CapacityExceededException(kodkod.engine.CapacityExceededException) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Err(edu.mit.csail.sdg.alloy4.Err) Pos(edu.mit.csail.sdg.alloy4.Pos) HigherOrderDeclException(kodkod.engine.fol2sat.HigherOrderDeclException)

Example 10 with ErrorFatal

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

the class TranslateAlloyToKodkod method visit.

/* =============================== */
/* Evaluates an ExprBinary node. */
/* =============================== */
/**
 * {@inheritDoc}
 */
@Override
public Object visit(ExprBinary x) throws Err {
    Expr a = x.left, b = x.right;
    Expression s, s2, eL, eR;
    IntExpression i;
    Formula f;
    Object objL, objR;
    switch(x.op) {
        case IMPLIES:
            f = cform(a).not().or(cform(b));
            return k2pos(f, x);
        case IN:
            return k2pos(isIn(cset(a), b), x);
        case NOT_IN:
            return k2pos(isIn(cset(a), b).not(), x);
        case LT:
            i = cint(a);
            f = i.lt(cint(b));
            return k2pos(f, x);
        case LTE:
            i = cint(a);
            f = i.lte(cint(b));
            return k2pos(f, x);
        case GT:
            i = cint(a);
            f = i.gt(cint(b));
            return k2pos(f, x);
        case GTE:
            i = cint(a);
            f = i.gte(cint(b));
            return k2pos(f, x);
        case NOT_LT:
            i = cint(a);
            f = i.lt(cint(b)).not();
            return k2pos(f, x);
        case NOT_LTE:
            i = cint(a);
            f = i.lte(cint(b)).not();
            return k2pos(f, x);
        case NOT_GT:
            i = cint(a);
            f = i.gt(cint(b)).not();
            return k2pos(f, x);
        case NOT_GTE:
            i = cint(a);
            f = i.gte(cint(b)).not();
            return k2pos(f, x);
        case AND:
            f = cform(a);
            f = f.and(cform(b));
            return k2pos(f, x);
        case OR:
            f = cform(a);
            f = f.or(cform(b));
            return k2pos(f, x);
        case IFF:
            f = cform(a);
            f = f.iff(cform(b));
            return k2pos(f, x);
        case PLUSPLUS:
            s = cset(a);
            return s.override(cset(b));
        case MUL:
            i = cint(a);
            return i.multiply(cint(b));
        case DIV:
            i = cint(a);
            return i.divide(cint(b));
        case REM:
            i = cint(a);
            return i.modulo(cint(b));
        case SHL:
            i = cint(a);
            return i.shl(cint(b));
        case SHR:
            i = cint(a);
            return i.shr(cint(b));
        case SHA:
            i = cint(a);
            return i.sha(cint(b));
        case PLUS:
            return cset(a).union(cset(b));
        // s = (Expression)obj; return s.union(cset(b));
        case IPLUS:
            return cint(a).plus(cint(b));
        case MINUS:
            // exception)
            if (a instanceof ExprConstant && ((ExprConstant) a).op == ExprConstant.Op.NUMBER && ((ExprConstant) a).num() == 0)
                if (b instanceof ExprConstant && ((ExprConstant) b).op == ExprConstant.Op.NUMBER && ((ExprConstant) b).num() == max + 1)
                    return IntConstant.constant(min);
            return cset(a).difference(cset(b));
        // s=(Expression)obj; return s.difference(cset(b));
        case IMINUS:
            return cint(a).minus(cint(b));
        case INTERSECT:
            s = cset(a);
            return s.intersection(cset(b));
        case ANY_ARROW_SOME:
        case ANY_ARROW_ONE:
        case ANY_ARROW_LONE:
        case SOME_ARROW_ANY:
        case SOME_ARROW_SOME:
        case SOME_ARROW_ONE:
        case SOME_ARROW_LONE:
        case ONE_ARROW_ANY:
        case ONE_ARROW_SOME:
        case ONE_ARROW_ONE:
        case ONE_ARROW_LONE:
        case LONE_ARROW_ANY:
        case LONE_ARROW_SOME:
        case LONE_ARROW_ONE:
        case LONE_ARROW_LONE:
        case ISSEQ_ARROW_LONE:
        case ARROW:
            s = cset(a);
            return s.product(cset(b));
        case JOIN:
            a = a.deNOP();
            s = cset(a);
            s2 = cset(b);
            if (a instanceof Sig && ((Sig) a).isOne != null && s2 instanceof BinaryExpression) {
                BinaryExpression bin = (BinaryExpression) s2;
                if (bin.op() == ExprOperator.PRODUCT && bin.left() == s)
                    return bin.right();
            }
            return s.join(s2);
        case EQUALS:
            objL = visitThis(a);
            objR = visitThis(b);
            eL = toSet(a, objL);
            eR = toSet(b, objR);
            if (eL instanceof IntToExprCast && eR instanceof IntToExprCast)
                f = ((IntToExprCast) eL).intExpr().eq(((IntToExprCast) eR).intExpr());
            else
                f = eL.eq(eR);
            return k2pos(f, x);
        case NOT_EQUALS:
            objL = visitThis(a);
            objR = visitThis(b);
            eL = toSet(a, objL);
            eR = toSet(b, objR);
            if (eL instanceof IntToExprCast && eR instanceof IntToExprCast)
                f = ((IntToExprCast) eL).intExpr().eq(((IntToExprCast) eR).intExpr()).not();
            else
                f = eL.eq(eR).not();
            return k2pos(f, x);
        case DOMAIN:
            s = cset(a);
            s2 = cset(b);
            for (int j = s2.arity(); j > 1; j--) s = s.product(Expression.UNIV);
            return s.intersection(s2);
        case RANGE:
            s = cset(a);
            s2 = cset(b);
            for (int j = s.arity(); j > 1; j--) s2 = Expression.UNIV.product(s2);
            return s.intersection(s2);
    }
    throw new ErrorFatal(x.pos, "Unsupported operator (" + x.op + ") encountered during ExprBinary.accept()");
}
Also used : Sig(edu.mit.csail.sdg.ast.Sig) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) BinaryExpression(kodkod.ast.BinaryExpression) IntToExprCast(kodkod.ast.IntToExprCast) Expression(kodkod.ast.Expression) BinaryExpression(kodkod.ast.BinaryExpression) IntExpression(kodkod.ast.IntExpression) IntExpression(kodkod.ast.IntExpression) ExprConstant(edu.mit.csail.sdg.ast.ExprConstant)

Aggregations

ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)29 Err (edu.mit.csail.sdg.alloy4.Err)8 Expr (edu.mit.csail.sdg.ast.Expr)6 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)6 Pos (edu.mit.csail.sdg.alloy4.Pos)4 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)3 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)3 ExprVar (edu.mit.csail.sdg.ast.ExprVar)3 Sig (edu.mit.csail.sdg.ast.Sig)3 IOException (java.io.IOException)3 LinkedHashSet (java.util.LinkedHashSet)3 BinaryExpression (kodkod.ast.BinaryExpression)3 CapacityExceededException (kodkod.engine.CapacityExceededException)3 HigherOrderDeclException (kodkod.engine.fol2sat.HigherOrderDeclException)3 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)2 Field (edu.mit.csail.sdg.ast.Sig.Field)2 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)2 A4Tuple (edu.mit.csail.sdg.translator.A4Tuple)2