Search in sources :

Example 16 with Sig

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

the class A4SolutionReader method parseType.

/**
 * Parse type.
 */
private Expr parseType(XMLNode node) throws IOException, Err {
    Expr expr = null;
    if (!node.is("types"))
        throw new IOException("<types>...</type> expected");
    for (XMLNode n : node) if (n.is("type")) {
        Sig sig = parseSig(n.getAttribute("ID"), 0);
        if (expr == null)
            expr = sig;
        else
            expr = expr.product(sig);
    }
    if (expr == null)
        throw new IOException("<type ID=../> expected");
    return expr;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Sig(edu.mit.csail.sdg.ast.Sig) Expr(edu.mit.csail.sdg.ast.Expr) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) IOException(java.io.IOException)

Example 17 with Sig

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

the class A4SolutionReader method parseField.

/**
 * Parse field.
 */
private Field parseField(String id) throws IOException, Err {
    final XMLNode node = nmap.get(id);
    if (node == null)
        throw new IOException("Unknown FieldID " + id + " encountered.");
    if (!node.is("field"))
        throw new IOException("ID " + id + " is not a field.");
    String label = label(node);
    Pos isPrivate = yes(node, "private") ? Pos.UNKNOWN : null;
    Pos isMeta = yes(node, "meta") ? Pos.UNKNOWN : null;
    Expr type = null;
    for (XMLNode sub : node) if (sub.is("types")) {
        Expr t = parseType(sub);
        if (type == null)
            type = t;
        else
            type = type.plus(t);
    }
    int arity;
    if (type == null || (arity = type.type().arity()) < 2)
        throw new IOException("Field " + label + " is maltyped.");
    String parentID = node.getAttribute("parentID");
    Sig parent = id2sig.get(parentID);
    if (parent == null)
        throw new IOException("ID " + parentID + " is not a sig.");
    Field field = null;
    for (Field f : parent.getFields()) if (f.label.equals(label) && f.type().arity() == arity && choices.contains(f)) {
        field = f;
        choices.remove(f);
        break;
    }
    if (field == null)
        field = parent.addTrickyField(Pos.UNKNOWN, isPrivate, null, null, isMeta, new String[] { label }, UNIV.join(type))[0];
    TupleSet ts = parseTuples(node, arity);
    expr2ts.put(field, ts);
    return field;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Sig(edu.mit.csail.sdg.ast.Sig) Field(edu.mit.csail.sdg.ast.Sig.Field) TupleSet(kodkod.instance.TupleSet) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Expr(edu.mit.csail.sdg.ast.Expr) Pos(edu.mit.csail.sdg.alloy4.Pos) IOException(java.io.IOException)

Example 18 with Sig

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

the class TableView method toTable.

/**
 * Format a solution to a string
 *
 * @param solution
 * @param instance
 * @param sigs
 * @return
 */
public static Map<String, Table> toTable(A4Solution solution, Instance instance, SafeList<Sig> sigs) {
    Map<String, Table> map = new HashMap<String, Table>();
    for (Sig s : sigs) {
        if (!s.label.startsWith("this/"))
            continue;
        TupleSet instanceTuples = instance.tuples(s.label);
        if (instanceTuples != null) {
            SimTupleset sigInstances = toSimTupleset(instanceTuples);
            Table table = new Table(sigInstances.size() + 1, s.getFields().size() + 1, 1);
            table.set(0, 0, s.label);
            if (s.getFields().size() == 0 && sigInstances.size() <= 1)
                continue;
            int c = 1;
            for (Field f : s.getFields()) {
                table.set(0, c++, f.label);
            }
            map.put(s.label, table);
            int r = 1;
            for (SimTuple sigInstance : sigInstances) {
                assert sigInstance.arity() == 1;
                SimTupleset leftJoin = SimTupleset.make(sigInstance);
                table.set(r, 0, sigInstance.get(0));
                c = 1;
                for (Field f : s.getFields()) {
                    SimTupleset relations = toSimTupleset(solution.eval(f));
                    SimTupleset joined = leftJoin.join(relations);
                    Table relationTable = toTable(joined);
                    table.set(r, c++, relationTable);
                }
                r++;
            }
        }
    }
    return map;
}
Also used : Sig(edu.mit.csail.sdg.ast.Sig) A4TupleSet(edu.mit.csail.sdg.translator.A4TupleSet) TupleSet(kodkod.instance.TupleSet) Field(edu.mit.csail.sdg.ast.Sig.Field) Table(org.alloytools.util.table.Table) HashMap(java.util.HashMap) SimTupleset(edu.mit.csail.sdg.sim.SimTupleset) SimTuple(edu.mit.csail.sdg.sim.SimTuple)

Example 19 with Sig

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

the class StaticInstanceReader method sigMETA.

/**
 * Returns the AlloyType corresponding to the given sig; create an AlloyType for
 * it if none existed before.
 */
private void sigMETA(SubsetSig s) throws Err {
    AlloyAtom atom;
    AlloyType type = sig2type.get(s);
    if (type != null)
        return;
    type = makeType(s.label, s.isOne != null, s.isAbstract != null, false, s.isPrivate != null, s.isMeta != null, s.isEnum != null);
    atom = new AlloyAtom(type, Integer.MAX_VALUE, s.label);
    atom2sets.put(atom, new LinkedHashSet<AlloySet>());
    sig2atom.put(s, atom);
    sig2type.put(s, type);
    ts.put(type, AlloyType.SET);
    for (Sig p : s.parents) {
        if (p instanceof SubsetSig)
            sigMETA((SubsetSig) p);
        else
            sigMETA((PrimSig) p);
        ins.add(new AlloyTuple(atom, sig2atom.get(p)));
    }
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 20 with Sig

use of edu.mit.csail.sdg.ast.Sig 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();
    }
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) Expr(edu.mit.csail.sdg.ast.Expr) Func(edu.mit.csail.sdg.ast.Func) Command(edu.mit.csail.sdg.ast.Command) A4Options(edu.mit.csail.sdg.translator.A4Options) Decl(edu.mit.csail.sdg.ast.Decl) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) A4Solution(edu.mit.csail.sdg.translator.A4Solution)

Aggregations

Sig (edu.mit.csail.sdg.ast.Sig)45 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)33 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)25 Field (edu.mit.csail.sdg.ast.Sig.Field)17 Expr (edu.mit.csail.sdg.ast.Expr)15 ExprVar (edu.mit.csail.sdg.ast.ExprVar)11 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)9 ArrayList (java.util.ArrayList)8 Func (edu.mit.csail.sdg.ast.Func)7 HashMap (java.util.HashMap)7 TupleSet (kodkod.instance.TupleSet)7 Pos (edu.mit.csail.sdg.alloy4.Pos)6 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)6 Command (edu.mit.csail.sdg.ast.Command)6 A4Solution (edu.mit.csail.sdg.translator.A4Solution)6 LinkedHashMap (java.util.LinkedHashMap)6 Err (edu.mit.csail.sdg.alloy4.Err)5 SafeList (edu.mit.csail.sdg.alloy4.SafeList)4 Decl (edu.mit.csail.sdg.ast.Decl)4 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)4