use of edu.mit.csail.sdg.alloy4.ErrorType in project org.alloytools.alloy by AlloyTools.
the class TranslateAlloyToKodkod method execute_greedyCommand.
private static A4Solution execute_greedyCommand(A4Reporter rep, Iterable<Sig> sigs, Command usercommand, A4Options opt) throws Exception {
// FIXTHIS: if the next command has a "smaller scope" than the last
// command, we would get a Kodkod exception...
// FIXTHIS: if the solver is "toCNF" or "toKodkod" then this method will
// throw an Exception...
// FIXTHIS: does solution enumeration still work when we're doing a
// greedy solve?
TranslateAlloyToKodkod tr = null;
try {
long start = System.currentTimeMillis();
GreedySimulator sim = new GreedySimulator();
sim.allSigs = sigs;
sim.partial = null;
A4Reporter rep2 = new A4Reporter(rep) {
private boolean first = true;
@Override
public void translate(String solver, int bitwidth, int maxseq, int skolemDepth, int symmetry) {
if (first)
super.translate(solver, bitwidth, maxseq, skolemDepth, symmetry);
first = false;
}
@Override
public void resultSAT(Object command, long solvingTime, Object solution) {
}
@Override
public void resultUNSAT(Object command, long solvingTime, Object solution) {
}
};
// Form the list of commands
List<Command> commands = new ArrayList<Command>();
while (usercommand != null) {
commands.add(usercommand);
usercommand = usercommand.parent;
}
// For each command...
A4Solution sol = null;
for (int i = commands.size() - 1; i >= 0; i--) {
Command cmd = commands.get(i);
sim.growableSigs = cmd.getGrowableSigs();
while (cmd != null) {
rep.debug(cmd.scope.toString());
usercommand = cmd;
tr = new TranslateAlloyToKodkod(rep2, opt, sigs, cmd);
tr.makeFacts(cmd.formula);
sim.totalOrderPredicates = tr.totalOrderPredicates;
sol = tr.frame.solve(rep2, cmd, sim.partial == null || cmd.check ? new Simplifier() : sim, false);
if (!sol.satisfiable() && !cmd.check) {
start = System.currentTimeMillis() - start;
if (sim.partial == null) {
rep.resultUNSAT(cmd, start, sol);
return sol;
} else {
rep.resultSAT(cmd, start, sim.partial);
return sim.partial;
}
}
if (sol.satisfiable() && cmd.check) {
start = System.currentTimeMillis() - start;
rep.resultSAT(cmd, start, sol);
return sol;
}
sim.partial = sol;
if (sim.growableSigs.isEmpty())
break;
for (Sig s : sim.growableSigs) {
CommandScope sc = cmd.getScope(s);
if (sc.increment > sc.endingScope - sc.startingScope) {
cmd = null;
break;
}
cmd = cmd.change(s, sc.isExact, sc.startingScope + sc.increment, sc.endingScope, sc.increment);
}
}
}
if (sol.satisfiable())
rep.resultSAT(usercommand, System.currentTimeMillis() - start, sol);
else
rep.resultUNSAT(usercommand, System.currentTimeMillis() - start, sol);
return sol;
} 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.");
}
}
use of edu.mit.csail.sdg.alloy4.ErrorType in project org.alloytools.alloy by AlloyTools.
the class TranslateAlloyToKodkod method alloy2kodkod.
/**
* Translate the Alloy expression into an equivalent Kodkod Expression or
* IntExpression or Formula object.
*
* @param sol - an existing satisfiable A4Solution object
* @param expr - this is the Alloy expression we want to translate
*/
public static Object alloy2kodkod(A4Solution sol, Expr expr) throws Err {
if (expr.ambiguous && !expr.errors.isEmpty())
expr = expr.resolve(expr.type(), null);
if (!expr.errors.isEmpty())
throw expr.errors.pick();
TranslateAlloyToKodkod tr = new TranslateAlloyToKodkod(sol.getBitwidth(), sol.unrolls(), sol.a2k(), sol.s2k());
Object ans;
try {
ans = tr.visitThis(expr);
} catch (UnsatisfiedLinkError ex) {
throw new ErrorFatal("The required JNI library cannot be found: " + ex.toString().trim());
} catch (CapacityExceededException ex) {
throw rethrow(ex);
} catch (HigherOrderDeclException ex) {
throw new ErrorType("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;
throw new ErrorFatal("Unknown exception occurred: " + ex, ex);
}
if ((ans instanceof IntExpression) || (ans instanceof Formula) || (ans instanceof Expression))
return ans;
throw new ErrorFatal("Unknown internal error encountered in the evaluator.");
}
use of edu.mit.csail.sdg.alloy4.ErrorType 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;
}
use of edu.mit.csail.sdg.alloy4.ErrorType in project org.alloytools.alloy by AlloyTools.
the class ExprITE method make.
/**
* Constructs a ExprITE expression.
*
* @param cond - the condition formula
* @param left - the then-clause
* @param right - the else-clause
*/
public static Expr make(Pos pos, Expr cond, Expr left, Expr right) {
JoinableList<Err> errs = emptyListOfErrors;
if (cond.mult != 0)
errs = errs.make(new ErrorSyntax(cond.span(), "Multiplicity expression not allowed here."));
if (left.mult != 0)
errs = errs.make(new ErrorSyntax(left.span(), "Multiplicity expression not allowed here."));
if (right.mult != 0)
errs = errs.make(new ErrorSyntax(right.span(), "Multiplicity expression not allowed here."));
Type c = EMPTY;
while (left.errors.isEmpty() && right.errors.isEmpty()) {
Type a = left.type, b = right.type;
c = a.unionWithCommonArity(b);
// if (a.is_int && b.is_int) c=Type.makeInt(c);
if (a.is_bool && b.is_bool)
c = Type.makeBool(c);
if (c == EMPTY) {
// [AM]
// if (Type.SIGINT2INT) {
// if (a.is_int && b.intersects(SIGINT.type)) {
// right=right.cast2int(); continue; }
// if (b.is_int && a.intersects(SIGINT.type)) {
// left=left.cast2int(); continue; }
// }
// if (Type.INT2SIGINT) {
// if (a.is_int && b.hasArity(1)) { left=left.cast2sigint();
// continue; }
// if (b.is_int && a.hasArity(1)) { right=right.cast2sigint();
// continue; }
// }
errs = errs.make(new ErrorType(cond.span().merge(right.span()).merge(left.span()), "The then-clause and the else-clause must match.\nThe then-clause has type: " + a + "\nand the else-clause has type: " + b));
}
break;
}
cond = cond.typecheck_as_formula();
return new ExprITE(pos, cond, left, right, c, errs.make(cond.errors).make(left.errors).make(right.errors));
}
use of edu.mit.csail.sdg.alloy4.ErrorType in project openflowplugin by opendaylight.
the class OnfExperimenterErrorFactory method deserialize.
@Override
public ErrorMessage deserialize(ByteBuf message) {
ErrorMessageBuilder builder = new ErrorMessageBuilder();
builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
builder.setXid(message.readUnsignedInt());
int type = message.readUnsignedShort();
ErrorType errorType = ErrorType.forValue(type);
if (errorType != null && errorType.equals(ErrorType.EXPERIMENTER)) {
builder.setType(errorType.getIntValue());
builder.setTypeString(errorType.getName());
} else {
LOG.warn("Deserializing other than {} error message with {}", ErrorType.EXPERIMENTER.getName(), this.getClass().getCanonicalName());
builder.setType(type);
builder.setTypeString(UNKNOWN_TYPE);
}
int code = message.readUnsignedShort();
OnfExperimenterErrorCode errorCode = OnfExperimenterErrorCode.forValue(code);
if (errorCode != null) {
builder.setCode(errorCode.getIntValue());
builder.setCodeString(errorCode.getName());
} else {
builder.setCode(code);
builder.setCodeString(UNKNOWN_CODE);
}
builder.addAugmentation(ExperimenterIdError.class, new ExperimenterIdErrorBuilder().setExperimenter(new ExperimenterId(message.readUnsignedInt())).build());
if (message.readableBytes() > 0) {
byte[] data = new byte[message.readableBytes()];
message.readBytes(data);
builder.setData(data);
}
return builder.build();
}
Aggregations