Search in sources :

Example 21 with Expr

use of edu.mit.csail.sdg.ast.Expr 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 22 with Expr

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

the class EvaluatorExample method main.

public static void main(String[] args) throws Exception {
    A4Reporter rep = new A4Reporter();
    File tmpAls = CompUtil.flushModelToFile(model, null);
    {
        Module world = CompUtil.parseEverything_fromString(rep, model);
        A4Options opt = new A4Options();
        opt.originalFilename = tmpAls.getAbsolutePath();
        opt.solver = A4Options.SatSolver.SAT4J;
        Command cmd = world.getAllCommands().get(0);
        A4Solution sol = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), cmd, opt);
        assert sol.satisfiable();
        sol.writeXML(outputfilename);
        // eval with existing A4Solution
        Expr e = CompUtil.parseOneExpression_fromString(world, "univ");
        System.out.println(sol.eval(e));
        e = CompUtil.parseOneExpression_fromString(world, "Point");
        System.out.println(sol.eval(e));
    }
    // reload everything from files
    {
        XMLNode xmlNode = new XMLNode(new File(outputfilename));
        String alloySourceFilename = xmlNode.iterator().next().getAttribute("filename");
        Module ansWorld = CompUtil.parseEverything_fromFile(rep, null, alloySourceFilename);
        A4Solution ans = A4SolutionReader.read(ansWorld.getAllReachableSigs(), xmlNode);
        Expr e = CompUtil.parseOneExpression_fromString(ansWorld, "univ");
        System.out.println(ans.eval(e));
        e = CompUtil.parseOneExpression_fromString(ansWorld, "Point");
        System.out.println(ans.eval(e));
    }
}
Also used : Expr(edu.mit.csail.sdg.ast.Expr) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Command(edu.mit.csail.sdg.ast.Command) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) A4Options(edu.mit.csail.sdg.translator.A4Options) Module(edu.mit.csail.sdg.ast.Module) File(java.io.File) A4Solution(edu.mit.csail.sdg.translator.A4Solution)

Example 23 with Expr

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

the class StaticInstanceReader method atoms.

/**
 * Constructs the atoms corresponding to the given sig.
 */
private void atoms(A4Solution sol, PrimSig s) throws Err {
    Expr sum = Sig.NONE;
    for (PrimSig c : s.children()) {
        sum = sum.plus(c);
        atoms(sol, c);
    }
    // This ensures
    A4TupleSet ts = (A4TupleSet) (sol.eval(s.minus(sum)));
    // specific sig
    for (A4Tuple z : ts) {
        String atom = z.atom(0);
        int i, dollar = atom.lastIndexOf('$');
        try {
            i = Integer.parseInt(dollar >= 0 ? atom.substring(dollar + 1) : atom);
        } catch (NumberFormatException ex) {
            i = Integer.MAX_VALUE;
        }
        AlloyAtom at = new AlloyAtom(sig(s), ts.size() == 1 ? Integer.MAX_VALUE : i, atom);
        atom2sets.put(at, new LinkedHashSet<AlloySet>());
        string2atom.put(atom, at);
    }
}
Also used : A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) Expr(edu.mit.csail.sdg.ast.Expr) A4TupleSet(edu.mit.csail.sdg.translator.A4TupleSet) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 24 with Expr

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

the class StaticInstanceReader method setOrRel.

/**
 * Construct an AlloySet or AlloyRelation corresponding to the given expression.
 */
private void setOrRel(A4Solution sol, String label, Expr expr, boolean isPrivate, boolean isMeta) throws Err {
    for (List<PrimSig> ps : expr.type().fold()) {
        if (ps.size() == 1) {
            PrimSig t = ps.get(0);
            AlloySet set = makeSet(label, isPrivate, isMeta, sig(t));
            sets.add(set);
            for (A4Tuple tp : (A4TupleSet) (sol.eval(expr.intersect(t)))) {
                atom2sets.get(string2atom.get(tp.atom(0))).add(set);
            }
        } else {
            Expr mask = null;
            List<AlloyType> types = new ArrayList<AlloyType>(ps.size());
            for (int i = 0; i < ps.size(); i++) {
                types.add(sig(ps.get(i)));
                if (mask == null)
                    mask = ps.get(i);
                else
                    mask = mask.product(ps.get(i));
            }
            AlloyRelation rel = makeRel(label, isPrivate, isMeta, types);
            Set<AlloyTuple> ts = new LinkedHashSet<AlloyTuple>();
            for (A4Tuple tp : (A4TupleSet) (sol.eval(expr.intersect(mask)))) {
                AlloyAtom[] atoms = new AlloyAtom[tp.arity()];
                for (int i = 0; i < tp.arity(); i++) {
                    atoms[i] = string2atom.get(tp.atom(i));
                    if (atoms[i] == null)
                        throw new ErrorFatal("Unexpected XML inconsistency: cannot resolve atom " + tp.atom(i));
                }
                ts.add(new AlloyTuple(atoms));
            }
            rels.put(rel, ts);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) A4TupleSet(edu.mit.csail.sdg.translator.A4TupleSet) A4Tuple(edu.mit.csail.sdg.translator.A4Tuple) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 25 with Expr

use of edu.mit.csail.sdg.ast.Expr 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

Expr (edu.mit.csail.sdg.ast.Expr)46 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)17 Sig (edu.mit.csail.sdg.ast.Sig)15 Func (edu.mit.csail.sdg.ast.Func)12 ExprVar (edu.mit.csail.sdg.ast.ExprVar)11 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)11 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)9 Decl (edu.mit.csail.sdg.ast.Decl)8 Field (edu.mit.csail.sdg.ast.Sig.Field)8 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)7 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)7 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)6 Pos (edu.mit.csail.sdg.alloy4.Pos)6 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)6 Command (edu.mit.csail.sdg.ast.Command)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 BinaryExpression (kodkod.ast.BinaryExpression)5