Java Code Examples for kodkod.ast.NaryExpression#size()

The following examples show how to use kodkod.ast.NaryExpression#size() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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;
}