Search in sources :

Example 11 with Decl

use of kodkod.ast.Decl in project org.alloytools.alloy by AlloyTools.

the class AbstractReplacer method visit.

/**
 * Calls lookup(decl) and returns the cached value, if any. If a replacement has
 * not been cached, visits the declaration's variable and expression. If nothing
 * changes, the argument is cached and returned, otherwise a replacement Decl
 * object is cached and returned.
 *
 * @return { d: Declaration | d.variable = declaration.variable.accept(delegate)
 *         && d.multiplicity = decl.multiplicity && d.expression =
 *         declaration.expression.accept(delegate)
 */
@Override
public Decl visit(Decl decl) {
    Decl ret = lookup(decl);
    if (ret != null)
        return ret;
    final Variable variable = (Variable) decl.variable().accept(delegate);
    final Expression expression = decl.expression().accept(delegate);
    ret = (variable == decl.variable() && expression == decl.expression()) ? decl : variable.declare(decl.multiplicity(), expression);
    return cache(decl, ret);
}
Also used : Variable(kodkod.ast.Variable) ProjectExpression(kodkod.ast.ProjectExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) SumExpression(kodkod.ast.SumExpression) IfIntExpression(kodkod.ast.IfIntExpression) BinaryExpression(kodkod.ast.BinaryExpression) ConstantExpression(kodkod.ast.ConstantExpression) UnaryIntExpression(kodkod.ast.UnaryIntExpression) NaryIntExpression(kodkod.ast.NaryIntExpression) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) UnaryExpression(kodkod.ast.UnaryExpression) NaryExpression(kodkod.ast.NaryExpression) IfExpression(kodkod.ast.IfExpression) Decl(kodkod.ast.Decl)

Example 12 with Decl

use of kodkod.ast.Decl in project org.alloytools.alloy by AlloyTools.

the class TranslateKodkodToJava method countHeight.

/**
 * Count the height of the given Kodkod AST tree.
 */
public static int countHeight(Node node) {
    ReturnVisitor<Integer, Integer, Integer, Integer> vis = new ReturnVisitor<Integer, Integer, Integer, Integer>() {

        private int max(int a, int b) {
            return (a >= b) ? a : b;
        }

        private int max(int a, int b, int c) {
            return (a >= b) ? (a >= c ? a : c) : (b >= c ? b : c);
        }

        @Override
        public Integer visit(Relation x) {
            return 1;
        }

        @Override
        public Integer visit(IntConstant x) {
            return 1;
        }

        @Override
        public Integer visit(ConstantFormula x) {
            return 1;
        }

        @Override
        public Integer visit(Variable x) {
            return 1;
        }

        @Override
        public Integer visit(ConstantExpression x) {
            return 1;
        }

        @Override
        public Integer visit(NotFormula x) {
            return 1 + x.formula().accept(this);
        }

        @Override
        public Integer visit(IntToExprCast x) {
            return 1 + x.intExpr().accept(this);
        }

        @Override
        public Integer visit(Decl x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(ExprToIntCast x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(UnaryExpression x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(UnaryIntExpression x) {
            return 1 + x.intExpr().accept(this);
        }

        @Override
        public Integer visit(MultiplicityFormula x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(BinaryExpression x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(ComparisonFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(BinaryFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(BinaryIntExpression x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(IntComparisonFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(IfExpression x) {
            return 1 + max(x.condition().accept(this), x.thenExpr().accept(this), x.elseExpr().accept(this));
        }

        @Override
        public Integer visit(IfIntExpression x) {
            return 1 + max(x.condition().accept(this), x.thenExpr().accept(this), x.elseExpr().accept(this));
        }

        @Override
        public Integer visit(SumExpression x) {
            return 1 + max(x.decls().accept(this), x.intExpr().accept(this));
        }

        @Override
        public Integer visit(QuantifiedFormula x) {
            return 1 + max(x.decls().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(FixFormula x) {
            return 1 + max(x.condition().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(Comprehension x) {
            return 1 + max(x.decls().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(Decls x) {
            int max = 0, n = x.size();
            for (int i = 0; i < n; i++) max = max(max, x.get(i).accept(this));
            return max;
        }

        @Override
        public Integer visit(ProjectExpression x) {
            int max = x.expression().accept(this);
            for (Iterator<IntExpression> t = x.columns(); t.hasNext(); ) {
                max = max(max, t.next().accept(this));
            }
            return max;
        }

        @Override
        public Integer visit(RelationPredicate x) {
            if (x instanceof Function) {
                Function f = ((Function) x);
                return max(f.domain().accept(this), f.range().accept(this));
            }
            return 1;
        }

        @Override
        public Integer visit(NaryExpression x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }

        @Override
        public Integer visit(NaryIntExpression x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }

        @Override
        public Integer visit(NaryFormula x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }
    };
    Object ans = node.accept(vis);
    if (ans instanceof Integer)
        return ((Integer) ans).intValue();
    else
        return 0;
}
Also used : Variable(kodkod.ast.Variable) UnaryIntExpression(kodkod.ast.UnaryIntExpression) Decls(kodkod.ast.Decls) ConstantExpression(kodkod.ast.ConstantExpression) ComparisonFormula(kodkod.ast.ComparisonFormula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) UnaryExpression(kodkod.ast.UnaryExpression) Function(kodkod.ast.RelationPredicate.Function) Relation(kodkod.ast.Relation) BinaryExpression(kodkod.ast.BinaryExpression) IntConstant(kodkod.ast.IntConstant) NaryIntExpression(kodkod.ast.NaryIntExpression) NotFormula(kodkod.ast.NotFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) IfExpression(kodkod.ast.IfExpression) IntToExprCast(kodkod.ast.IntToExprCast) ProjectExpression(kodkod.ast.ProjectExpression) IfIntExpression(kodkod.ast.IfIntExpression) IfIntExpression(kodkod.ast.IfIntExpression) UnaryIntExpression(kodkod.ast.UnaryIntExpression) NaryIntExpression(kodkod.ast.NaryIntExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) IntExpression(kodkod.ast.IntExpression) RelationPredicate(kodkod.ast.RelationPredicate) NaryExpression(kodkod.ast.NaryExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) Decl(kodkod.ast.Decl) ConstantFormula(kodkod.ast.ConstantFormula) Comprehension(kodkod.ast.Comprehension) NaryFormula(kodkod.ast.NaryFormula) ExprToIntCast(kodkod.ast.ExprToIntCast) MultiplicityFormula(kodkod.ast.MultiplicityFormula) BinaryFormula(kodkod.ast.BinaryFormula) SumExpression(kodkod.ast.SumExpression) FixFormula(kodkod.ast.FixFormula) ReturnVisitor(kodkod.ast.visitor.ReturnVisitor) IntComparisonFormula(kodkod.ast.IntComparisonFormula)

Example 13 with Decl

use of kodkod.ast.Decl in project org.alloytools.alloy by AlloyTools.

the class TranslateKodkodToJava method visit.

/**
 * {@inheritDoc}
 */
@Override
public void visit(Decls x) {
    String newname = makename(x);
    if (newname == null)
        return;
    for (Decl y : x) {
        y.accept(this);
    }
    boolean first = true;
    file.printf("Decls %s=", newname);
    for (Decl y : x) {
        String z = map.get(y);
        if (first) {
            file.printf("%s", z);
            first = false;
        } else
            file.printf(".and(%s)", z);
    }
    file.printf(";%n");
}
Also used : Decl(kodkod.ast.Decl)

Example 14 with Decl

use of kodkod.ast.Decl in project org.alloytools.alloy by AlloyTools.

the class FreeVariableCollector method visit.

/**
 * Visits the given comprehension, quantified formula, or sum expression. The
 * method returns a set that contains all the free variables in the declarations
 * and the body, minus the variables that are actually bound in the
 * declarations.
 */
@SuppressWarnings("unchecked")
private Set<Variable> visit(Node creator, Decls decls, Node body) {
    Set<Variable> ret = lookup(creator);
    if (ret != null)
        return ret;
    ret = newSet();
    final Set<Variable> boundVars = newSet();
    // computed set and previously bound variables to ret
    for (Decl decl : decls) {
        for (Variable v : visit(decl)) {
            if (!boundVars.contains(v))
                ret.add(v);
        }
        varsInScope.push(decl.variable());
        boundVars.add(decl.variable());
    }
    // add to ret the free variables in the body, minus the bound variables
    for (Variable v : (Set<Variable>) body.accept(this)) {
        if (!boundVars.contains(v))
            ret.add(v);
    }
    // remove the declared variables from the in-scope stack
    for (int i = decls.size(); i > 0; i--) {
        varsInScope.pop();
    }
    return cache(creator, ret);
}
Also used : Variable(kodkod.ast.Variable) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) Decl(kodkod.ast.Decl)

Example 15 with Decl

use of kodkod.ast.Decl in project org.alloytools.alloy by AlloyTools.

the class EvaluatorTest method testComprehension.

public final void testComprehension() {
    final Variable[] vars = new Variable[3];
    final Decl[] decls = new Decl[3];
    for (int i = 0; i < 3; i++) {
        Variable v = Variable.unary("v" + i);
        Decl d = v.oneOf(person);
        vars[i] = v;
        decls[i] = d;
    }
    // {v0: Person | no v0.shaken} = univ - shaken.Person
    assertEquals(eval(vars[0].join(shaken).no().comprehension(decls[0])), eval(univ.difference(shaken.join(person))));
    // {v0, v1: Person | v1 in v0.shaken} = shaken
    assertEquals(eval(vars[1].in(vars[0].join(shaken)).comprehension(decls[0].and(decls[1]))), value(shaken));
    // {v0, v1, v2: Person | no v1.shaken} = Person->(univ -
    // shaken.Person)->Person
    assertEquals(eval(vars[1].join(shaken).no().comprehension(decls[0].and(decls[1]).and(decls[2]))), eval(person.product(univ.difference(shaken.join(person))).product(person)));
}
Also used : Variable(kodkod.ast.Variable) Decl(kodkod.ast.Decl)

Aggregations

Decl (kodkod.ast.Decl)32 Formula (kodkod.ast.Formula)12 Variable (kodkod.ast.Variable)12 QuantifiedFormula (kodkod.ast.QuantifiedFormula)9 Decls (kodkod.ast.Decls)7 Expression (kodkod.ast.Expression)6 IntExpression (kodkod.ast.IntExpression)6 Relation (kodkod.ast.Relation)6 SumExpression (kodkod.ast.SumExpression)5 Solution (kodkod.engine.Solution)5 Test (org.junit.Test)5 BinaryFormula (kodkod.ast.BinaryFormula)4 BooleanFactory (kodkod.engine.bool.BooleanFactory)4 BooleanMatrix (kodkod.engine.bool.BooleanMatrix)4 BooleanValue (kodkod.engine.bool.BooleanValue)4 Instance (kodkod.instance.Instance)4 HashSet (java.util.HashSet)3 ComparisonFormula (kodkod.ast.ComparisonFormula)3 IntComparisonFormula (kodkod.ast.IntComparisonFormula)3 MultiplicityFormula (kodkod.ast.MultiplicityFormula)3