kodkod.ast.NaryExpression Java Examples

The following examples show how to use kodkod.ast.NaryExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public Expression visit(NaryExpression expr) { 
	Expression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final ExprOperator op = expr.op();
	
	final List<Expression> children = simplify(op, visitAll(expr));
	final int size = children.size();
	switch(size) { 
	case 0 : return cache(expr, empty(expr.arity()));
	case 1 : return cache(expr, children.get(0));
	default :
		ret = expr.size()==size && allSame(expr,children) ? expr : Expression.compose(op, children);
		return cache(expr,ret);
	}	
}
 
Example #2
Source File: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Calls lookup(expr) and returns the cached value, if any. If a replacement has
 * not been cached, visits the expr's children. If nothing changes, the argument
 * is cached and returned, otherwise a replacement expr is cached and returned.
 *
 * @return { e: Expression | e.op = expr.op && #e.children = #expr.children &&
 *         all i: [0..expr.children) | e.child(i) =
 *         expr.child(i).accept(delegate) }
 */
@Override
public Expression visit(NaryExpression expr) {
    Expression ret = lookup(expr);
    if (ret != null)
        return ret;

    final Expression[] visited = new Expression[expr.size()];
    boolean allSame = true;
    for (int i = 0; i < visited.length; i++) {
        final Expression child = expr.child(i);
        visited[i] = child.accept(delegate);
        allSame = allSame && visited[i] == child;
    }

    ret = allSame ? expr : Expression.compose(expr.op(), visited);
    return cache(expr, ret);
}
 
Example #3
Source File: AbstractReplacer.java    From kodkod with MIT License 6 votes vote down vote up
/** 
 * Calls lookup(expr) and returns the cached value, if any.  
 * If a replacement has not been cached, visits the expr's 
 * children.  If nothing changes, the argument is cached and
 * returned, otherwise a replacement expr is cached and returned.
 * @return { e: Expression | e.op = expr.op && #e.children = #expr.children && all i: [0..expr.children) | e.child(i) = expr.child(i).accept(this) }
 */
public Expression visit(NaryExpression expr) {
	Expression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final Expression[] visited = new Expression[expr.size()];
	boolean allSame = true;
	for(int i = 0 ; i < visited.length; i++) { 
		final Expression child = expr.child(i);
		visited[i] = child.accept(this);
		allSame = allSame && visited[i]==child;
	}
	
	ret = allSame ? expr : Expression.compose(expr.op(), visited);
	return cache(expr,ret);
}
 
Example #4
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Calls lookup(expr) and returns the cached value, if any.  
 * If a translation has not been cached, translates the expression,
 * calls cache(...) on it and returns it.
 * @return let t = lookup(expr) | some t => t, 
 *      let op = (expr.op).(UNION->or + INTERSECTION->and + DIFFERENCE->difference + OVERRIDE->override + JOIN->dot + PRODUCT->cross) | 
 *       cache(expr, op(expr.left.accept(this), expr.right.accept(this)))
 */
public BooleanMatrix visit(NaryExpression expr) {
	BooleanMatrix ret = lookup(expr);
	if (ret!=null) return ret;

	final ExprOperator op = expr.op();
	final BooleanMatrix first = expr.child(0).accept(this);		
	final BooleanMatrix[] rest = new BooleanMatrix[expr.size()-1];
	for(int i = 0; i < rest.length; i++) { 	rest[i] = expr.child(i+1).accept(this); }
	
	switch(op) {
	case UNION        	: ret = first.or(rest); break;
	case INTERSECTION	: ret = first.and(rest); break;
	case OVERRIDE 		: ret = first.override(rest); break;
	case PRODUCT		: ret = first.cross(rest); break;
	default : 
		throw new IllegalArgumentException("Unknown associative operator: " + op);
	}

	return cache(expr, ret);
}
 
Example #5
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
/** @return true if e is (a product of) Expression.NONE*/
private boolean isEmpty(Expression e) { 
	if (e==Expression.NONE) return true;
	else if (e instanceof BinaryExpression) { 
		final BinaryExpression b = (BinaryExpression) e;
		return b.op()==ExprOperator.PRODUCT && isEmpty(b.left()) && isEmpty(b.right());
	} else if (e instanceof NaryExpression) { 
		final NaryExpression n = (NaryExpression) e;
		if (n.op()==ExprOperator.PRODUCT) { 
			for(int i = 0, max = n.size(); i < max; i++) { 
				if (!isEmpty(n.child(i))) return false;
			}
			return true;
		}
	}
	return false;
}
 
Example #6
Source File: AbstractDetector.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(expr) and returns the cached value, if any.  
 * If no cached value exists, visits each child, caches the
 * disjunction of the children's return values and returns it. 
 * @return let x = lookup(expr) | 
 *          x != null => x,  
 *          cache(expr, expr.child(0).accept(this) || ... || expr.child(expr.size()-1).accept(this)) 
 */
public Boolean visit(NaryExpression expr) {
	final Boolean ret = lookup(expr);
	if (ret!=null) return ret;
	for(Expression child : expr) { 
		if (child.accept(this))
			return cache(expr, true);
	}
	return cache(expr, false);
}
 
Example #7
Source File: AbstractCollector.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(expr) and returns the cached value, if any.  
 * If no cached value exists, visits each child, caches the
 * union of the children's return values and returns it. 
 * @return let x = lookup(expr) | 
 *          x != null => x,  
 *          cache(expr, expr.child(0).accept(this) + .. + expr.child(expr.size()-1).accept(this)) 
 */
public Set<T> visit(NaryExpression expr) {
	Set<T> ret = lookup(expr);
	if (ret!=null) return ret;		
	ret = newSet();
	for(Expression child : expr) { 
		ret.addAll(child.accept(this));
	}
	return cache(expr, ret);
}
 
Example #8
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @return true if the given  expression needs to be parenthesized if a 
 * child of a binary  expression with the given operator */
private boolean parenthesize(ExprOperator op, Expression child) { 
	return display.containsKey(child.toString()) ? false :
		   child instanceof IfExpression ||
		   (child instanceof NaryExpression && ((NaryExpression)child).op()!=op) ||
	       (child instanceof BinaryExpression && 
	        (/*op!=ExprOperator.JOIN && */
	         ((BinaryExpression)child).op()!=op));
}
 
Example #9
Source File: AbstractVoidVisitor.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Visits the children if this.visited(expr) returns false.  Otherwise does nothing.
 * @ensures all i: [0..#expr.children) | expr.child(i).accept(this)
 */
public void visit(NaryExpression expr) { 
	if (visited(expr)) return;
	for(Expression child : expr) { 
		child.accept(this);
	}
}
 
Example #10
Source File: TranslateKodkodToJava.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visit(NaryExpression x) {
    String newname = makename(x);
    if (newname == null)
        return;
    String[] list = new String[x.size()];
    for (int i = 0; i < list.length; i++)
        list[i] = make(x.child(i));
    file.printf("Expression %s=Expression.compose(ExprOperator.", newname);
    switch (x.op()) {
        case INTERSECTION :
            file.print("INTERSECTION");
            break;
        case OVERRIDE :
            file.print("OVERRIDE");
            break;
        case PRODUCT :
            file.print("PRODUCT");
            break;
        case UNION :
            file.print("UNION");
            break;
        default :
            throw new RuntimeException("Unknown kodkod operator \"" + x.op() + "\" encountered");
    }
    for (int i = 0; i < list.length; i++)
        file.printf(", %s", list[i]);
    file.printf(");%n");
}
 
Example #11
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @return true if the given  expression needs to be parenthesized if a 
 * child of a binary  expression with the given operator */
private boolean parenthesize(ExprOperator op, Expression child) { 
	return child instanceof IfExpression ||
		   child instanceof NaryExpression ||
	       (child instanceof BinaryExpression && 
	        (op==ExprOperator.JOIN || 
	         ((BinaryExpression)child).op()!=op));
}
 
Example #12
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @ensures appends the tokenization of the given node to this.tokens */
public void visit(NaryExpression node) {
	final ExprOperator op = node.op();
	visitChild(node.child(0), parenthesize(op, node.child(0)));
	for(int i = 1, size = node.size(); i < size; i++) {
		infix(op);
		visitChild(node.child(i), parenthesize(op, node.child(i)));
	}
}
 
Example #13
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * @ensures appends the tokenization of the given node to this.tokens
 */
@Override
public void visit(NaryExpression node) {
    final ExprOperator op = node.op();
    visitChild(node.child(0), parenthesize(op, node.child(0)));
    for (int i = 1, size = node.size(); i < size; i++) {
        infix(op);
        visitChild(node.child(i), parenthesize(op, node.child(i)));
    }
}
 
Example #14
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(expr) and returns the cached value, if any. If a translation has
 * not been cached, translates the expression, calls cache(...) on it and
 * returns it.
 *
 * @return let t = lookup(expr) | some t => t, let op = (expr.op).(UNION->or +
 *         INTERSECTION->and + DIFFERENCE->difference + OVERRIDE->override +
 *         JOIN->dot + PRODUCT->cross) | cache(expr, op(expr.left.accept(this),
 *         expr.right.accept(this)))
 */
@Override
public BooleanMatrix visit(NaryExpression expr) {
    BooleanMatrix ret = lookup(expr);
    if (ret != null)
        return ret;

    final ExprOperator op = expr.op();
    final BooleanMatrix first = expr.child(0).accept(this);
    final BooleanMatrix[] rest = new BooleanMatrix[expr.size() - 1];
    for (int i = 0; i < rest.length; i++) {
        rest[i] = expr.child(i + 1).accept(this);
    }

    switch (op) {
        case UNION :
            ret = first.or(rest);
            break;
        case INTERSECTION :
            ret = first.and(rest);
            break;
        case OVERRIDE :
            ret = first.override(rest);
            break;
        case PRODUCT :
            ret = first.cross(rest);
            break;
        default :
            throw new IllegalArgumentException("Unknown associative operator: " + op);
    }

    return cache(expr, ret);
}
 
Example #15
Source File: AbstractDetector.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(expr) and returns the cached value, if any. If no cached value
 * exists, visits each child, caches the disjunction of the children's return
 * values and returns it.
 *
 * @return let x = lookup(expr) | x != null => x, cache(expr,
 *         expr.child(0).accept(this) || ... ||
 *         expr.child(expr.size()-1).accept(this))
 */
@Override
public Boolean visit(NaryExpression expr) {
    final Boolean ret = lookup(expr);
    if (ret != null)
        return ret;
    for (Expression child : expr) {
        if (child.accept(this))
            return cache(expr, true);
    }
    return cache(expr, false);
}
 
Example #16
Source File: AbstractCollector.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(expr) and returns the cached value, if any. If no cached value
 * exists, visits each child, caches the union of the children's return values
 * and returns it.
 *
 * @return let x = lookup(expr) | x != null => x, cache(expr,
 *         expr.child(0).accept(this) + .. +
 *         expr.child(expr.size()-1).accept(this))
 */
@Override
public Set<T> visit(NaryExpression expr) {
    Set<T> ret = lookup(expr);
    if (ret != null)
        return ret;
    ret = newSet();
    for (Expression child : expr) {
        ret.addAll(child.accept(this));
    }
    return cache(expr, ret);
}
 
Example #17
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @effects appends the tokenization of the given node to this.tokens */
public void visit(NaryExpression node) {
	if (displayed(node)) return;
	final boolean oldTop = notTop();
	final ExprOperator op = node.op();
	visitChild(node.child(0), parenthesize(op, node.child(0)));
	for(int i = 1, size = node.size(); i < size; i++) {
		infix(op);
		visitChild(node.child(i), parenthesize(op, node.child(i)));
	}
	top = oldTop;
}
 
Example #18
Source File: AbstractVoidVisitor.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Visits the children if this.visited(expr) returns false. Otherwise does
 * nothing.
 *
 * @ensures all i: [0..#expr.children) | expr.child(i).accept(this)
 */
@Override
public void visit(NaryExpression expr) {
    if (visited(expr))
        return;
    for (Expression child : expr) {
        child.accept(this);
    }
}
 
Example #19
Source File: PrettyPrinter.java    From kodkod with MIT License 4 votes vote down vote up
public void visit(NaryExpression expr) { 
	visit(expr, expr.op(), expr.iterator()); 
}
 
Example #20
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
/** @return true if the given expression should be parenthesized when a 
 * child of a compound parent */
private boolean parenthesize(Expression child) { 
	return (display.isEmpty() || !display.containsKey(child.toString())) &&
	       (child instanceof BinaryExpression || child instanceof NaryExpression || child instanceof IfExpression); 
}
 
Example #21
Source File: AspectReturnVisitor.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public E visit(NaryExpression expr) {
    start(expr);
    return end(expr, visitor.visit(expr));
}
 
Example #22
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public Boolean visit(NaryExpression expr) {
    return checkVisitedThenAccum(expr, Boolean.FALSE, expr);
}
 
Example #23
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(NaryExpression expr) {
    visit(expr, expr.op(), expr.iterator());
}
 
Example #24
Source File: HOLTranslator.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public Expression visit(NaryExpression expr) {
    return expr;
}
 
Example #25
Source File: VoidVisitor.java    From kodkod with MIT License 2 votes vote down vote up
/** 
* Visits the given nary expression.
**/
  public void visit(NaryExpression expr);
 
Example #26
Source File: ReturnVisitor.java    From kodkod with MIT License 2 votes vote down vote up
/** 
* Visits the given nary expression and returns the result.
* @return the result of visiting <code>expr</code> 
**/
  public E visit(NaryExpression expr);
 
Example #27
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * @return true if the given expression needs to be parenthesized if a child of
 *         a binary expression with the given operator
 */
private boolean parenthesize(ExprOperator op, Expression child) {
    return child instanceof IfExpression || child instanceof NaryExpression || (child instanceof BinaryExpression && (op == ExprOperator.JOIN || ((BinaryExpression) child).op() != op));
}
 
Example #28
Source File: ReturnVisitor.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Visits the given nary expression and returns the result.
 *
 * @return the result of visiting <code>expr</code>
 **/
public E visit(NaryExpression expr);
 
Example #29
Source File: VoidVisitor.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Visits the given nary expression.
 **/
public void visit(NaryExpression expr);