use of edu.mit.csail.sdg.ast.Sig.PrimSig in project org.alloytools.alloy by AlloyTools.
the class ExampleUsingTheAPI method main.
public static void main(String[] args) throws Err {
// Chooses the Alloy4 options
A4Options opt = new A4Options();
opt.solver = A4Options.SatSolver.SAT4J;
// abstract sig A {}
PrimSig A = new PrimSig("A", Attr.ABSTRACT);
// sig B {}
PrimSig B = new PrimSig("B");
// one sig A1 extends A {}
PrimSig A1 = new PrimSig("A1", A, Attr.ONE);
// one sig A2 extends A {}
PrimSig A2 = new PrimSig("A2", A, Attr.ONE);
// A { f: B lone->lone B }
Expr f = A.addField("f", B.lone_arrow_lone(B));
// Since (B lone->lone B) is not unary, the default is "setOf", meaning
// "f:set (B lone->lone B)"
// A { g: B }
Expr g = A.addField("g", B);
// The line above is the same as: A.addField(null, "g", B.oneOf()) since
// B is unary.
// If you want "setOf", you need: A.addField(null, "g", B.setOf())
// pred someG { some g }
Func someG = new Func(null, "SomeG", null, null, g.some());
// pred atMostThree[x:univ, y:univ] { #(x+y) >= 3 }
Decl x = UNIV.oneOf("x");
Decl y = UNIV.oneOf("y");
Expr body = x.get().plus(y.get()).cardinality().lte(ExprConstant.makeNUMBER(3));
Func atMost3 = new Func(null, "atMost3", Util.asList(x, y), null, body);
List<Sig> sigs = Arrays.asList(new Sig[] { A, B, A1, A2 });
// run { some A && atMostThree[B,B] } for 3 but 3 int, 3 seq
Expr expr1 = A.some().and(atMost3.call(B, B));
Command cmd1 = new Command(false, 3, 3, 3, expr1);
A4Solution sol1 = TranslateAlloyToKodkod.execute_command(NOP, sigs, cmd1, opt);
System.out.println("[Solution1]:");
System.out.println(sol1.toString());
// run { some f && SomeG[] } for 3 but 2 int, 1 seq, 5 A, exactly 6 B
Expr expr2 = f.some().and(someG.call());
Command cmd2 = new Command(false, 3, 2, 1, expr2);
cmd2 = cmd2.change(A, false, 1);
cmd2 = cmd2.change(B, true, 1);
A4Solution sol2 = TranslateAlloyToKodkod.execute_command(NOP, sigs, cmd2, opt);
while (sol2.satisfiable()) {
System.out.println("[Solution2]:");
System.out.println(sol2.toString());
sol2 = sol2.next();
}
}
use of edu.mit.csail.sdg.ast.Sig.PrimSig in project org.alloytools.alloy by AlloyTools.
the class DemoFileSystem method makeInstance1.
/* Here is instance number 1. */
void makeInstance1() throws Err {
// file = F1, F2, F3
// dir = Root, D1, D2
// F1.parent = D1
// F2.parent = D2
// F3.parent = D2
// D2.parent = D1
// D1.parent = Root
PrimSig file1 = makeSig(file, "F1", false, true);
PrimSig file2 = makeSig(file, "F2", false, true);
PrimSig file3 = makeSig(file, "F3", false, true);
PrimSig dir1 = makeSig(dir, "D1", false, true);
PrimSig dir2 = makeSig(dir, "D2", false, true);
fact = file1.join(parent).equal(dir1).and(fact);
fact = file2.join(parent).equal(dir2).and(fact);
fact = file3.join(parent).equal(dir2).and(fact);
fact = dir2.join(parent).equal(dir1).and(fact);
fact = dir1.join(parent).equal(root).and(fact);
}
use of edu.mit.csail.sdg.ast.Sig.PrimSig in project org.alloytools.alloy by AlloyTools.
the class DemoFileSystem method makeSig.
PrimSig makeSig(String name, boolean isAbstract, boolean isOne) throws Err {
PrimSig ans = new PrimSig(name, (isAbstract ? Attr.ABSTRACT : null), (isOne ? Attr.ONE : null));
sigs.add(ans);
return ans;
}
use of edu.mit.csail.sdg.ast.Sig.PrimSig in project org.alloytools.alloy by AlloyTools.
the class CompModule method populate.
/**
* Resolve the name based on the current context and this module.
*/
private Expr populate(TempList<Expr> ch, TempList<String> re, Decl rootfield, Sig rootsig, boolean rootfunparam, Func rootfunbody, Pos pos, String fullname, Expr THIS) {
// Return object can be Func(with > 0 arguments) or Expr
final String name = (fullname.charAt(0) == '@') ? fullname.substring(1) : fullname;
boolean fun = (rootsig != null && (rootfield == null || rootfield.expr.mult() == ExprUnary.Op.EXACTLYOF)) || (rootsig == null && !rootfunparam);
if (name.equals("univ"))
return ExprUnary.Op.NOOP.make(pos, UNIV);
if (name.equals("Int"))
return ExprUnary.Op.NOOP.make(pos, SIGINT);
if (name.equals("seq/Int"))
return ExprUnary.Op.NOOP.make(pos, SEQIDX);
if (name.equals("String"))
return ExprUnary.Op.NOOP.make(pos, STRING);
if (name.equals("none"))
return ExprUnary.Op.NOOP.make(pos, NONE);
if (name.equals("iden"))
return ExprConstant.Op.IDEN.make(pos, 0);
if (name.equals("sig$") || name.equals("field$"))
if (world != null) {
Sig s = world.sigs.get(name);
if (s != null)
return ExprUnary.Op.NOOP.make(pos, s);
}
final List<Object> ans = name.indexOf('/') >= 0 ? getRawQS(fun ? 5 : 1, name) : getRawNQS(this, fun ? 5 : 1, name);
final Sig param = params.get(name);
if (param != null && !ans.contains(param))
ans.add(param);
for (Object x : ans) {
if (x instanceof Sig) {
Sig y = (Sig) x;
ch.add(ExprUnary.Op.NOOP.make(pos, y, null, 0));
re.add("sig " + y.label);
} else if (x instanceof Func) {
Func f = (Func) x;
int fn = f.count();
int penalty = 0;
if (resolution == 1 && fn > 0 && rootsig != null && THIS != null && THIS.type().hasArity(1) && f.get(0).type().intersects(THIS.type())) {
// If we're inside a sig, and there is a unary variable
// bound to "this",
// we should consider it as a possible FIRST ARGUMENT of a
// fun/pred call
ConstList<Expr> t = Util.asList(THIS);
// penalty
ch.add(fn == 1 ? ExprCall.make(pos, null, f, t, 1 + penalty) : ExprBadCall.make(pos, null, f, t, 1 + penalty));
// of
// 1
re.add((f.isPred ? "pred this." : "fun this.") + f.label);
}
if (resolution == 1) {
ch.add(fn == 0 ? ExprCall.make(pos, null, f, null, penalty) : ExprBadCall.make(pos, null, f, null, penalty));
re.add((f.isPred ? "pred " : "fun ") + f.label);
}
if (resolution == 2 && f != rootfunbody && THIS != null && fullname.charAt(0) != '@' && fn > 0 && f.get(0).type().intersects(THIS.type())) {
// If there is some value bound to "this", we should
// consider it as a possible FIRST ARGUMENT of a fun/pred
// call
ConstList<Expr> t = Util.asList(THIS);
ch.add(fn == 1 ? ExprCall.make(pos, null, f, t, 0) : ExprBadCall.make(pos, null, f, t, 0));
re.add((f.isPred ? "pred this." : "fun this.") + f.label);
}
if (resolution != 1) {
ch.add(fn == 0 ? ExprCall.make(pos, null, f, null, 0) : ExprBadCall.make(pos, null, f, null, 0));
re.add((f.isPred ? "pred " : "fun ") + f.label);
}
}
}
// All else: we can call, and can refer to anything visible.
for (CompModule m : getAllNameableModules()) for (Sig s : m.sigs.values()) if (m == this || s.isPrivate == null)
for (Field f : s.getFields()) if (f.isMeta == null && (m == this || f.isPrivate == null) && f.label.equals(name))
if (resolution == 1) {
Expr x = null;
if (rootsig == null) {
x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
} else if (rootsig.isSameOrDescendentOf(f.sig)) {
x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
if (fullname.charAt(0) != '@')
x = THIS.join(x);
} else if (rootfield == null || rootfield.expr.mult() == ExprUnary.Op.EXACTLYOF) {
x = ExprUnary.Op.NOOP.make(pos, f, null, 1);
}
// penalty of 1
if (x != null) {
ch.add(x);
re.add("field " + f.sig.label + " <: " + f.label);
}
} else if (rootfield == null || rootsig.isSameOrDescendentOf(f.sig)) {
Expr x0 = ExprUnary.Op.NOOP.make(pos, f, null, 0);
if (resolution == 2 && THIS != null && fullname.charAt(0) != '@' && f.type().firstColumnOverlaps(THIS.type())) {
ch.add(THIS.join(x0));
re.add("field " + f.sig.label + " <: this." + f.label);
if (rootsig != null)
continue;
}
ch.add(x0);
re.add("field " + f.sig.label + " <: " + f.label);
}
if (metaSig() != null && (rootsig == null || rootfield == null)) {
SafeList<PrimSig> children = null;
try {
children = metaSig().children();
} catch (Err err) {
return null;
}
// exception NOT possible
for (PrimSig s : children) for (Field f : s.getFields()) if (f.label.equals(name)) {
Expr x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
ch.add(x);
re.add("field " + f.sig.label + " <: " + f.label);
}
}
if (metaField() != null && (rootsig == null || rootfield == null)) {
SafeList<PrimSig> children = null;
try {
children = metaField().children();
} catch (Err err) {
return null;
}
// exception NOT possible
for (PrimSig s : children) for (Field f : s.getFields()) if (f.label.equals(name)) {
Expr x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
ch.add(x);
re.add("field " + f.sig.label + " <: " + f.label);
}
}
return null;
}
use of edu.mit.csail.sdg.ast.Sig.PrimSig 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.");
}
Aggregations