kodkod.ast.operator.ExprOperator Java Examples

The following examples show how to use kodkod.ast.operator.ExprOperator. 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: NaryExpression.java    From kodkod with MIT License 6 votes vote down vote up
/**  
 * Constructs a new associative expression: op(children)
 * @requires children array is not modified while in use by this associative expression
 * @requires some op.op[children]
 * @ensures this.children' = children && this.op' = op
 */
NaryExpression(ExprOperator op, Expression[] children) {
	assert children.length>2;
	if (!op.nary()) 
		throw new IllegalArgumentException("Cannot construct an nary expression using the non-nary operator " + op);
	
	this.op = op;
	this.children = children;
	
	switch(op) { 
	case UNION : case INTERSECTION : case OVERRIDE :		
		this.arity = children[0].arity();
		for(int i = 1; i < children.length; i++) { 
			if (children[i].arity()!=arity)
				throw new IllegalArgumentException("Incompatible arities: " + children[0] + " and " + children[i]);
		}
		break;
	case PRODUCT : 		
		int sum = 0; 
		for(Expression child : children) { sum += child.arity(); }
		this.arity = sum; 
		break;
	default :  			
		throw new IllegalArgumentException("Unknown associative operator: " + op);
	}
}
 
Example #3
Source File: BinaryExpression.java    From kodkod with MIT License 6 votes vote down vote up
/**  
 * Constructs a new binary expression: left op right
 * 
 * @ensures this.left' = left && this.right' = right && this.op' = op
 * @throws NullPointerException  left = null || right = null || op = null
 * @throws IllegalArgumentException  left and right cannot be combined with the specified operator.
 */
BinaryExpression(final Expression left, final ExprOperator op, final Expression right) {
	switch(op) { 
	case UNION : case INTERSECTION : case DIFFERENCE : case OVERRIDE :
		this.arity = left.arity();
		if (arity!=right.arity())
			throw new IllegalArgumentException("Incompatible arities: " + left + " and " + right);
		break;
	case JOIN : 
		this.arity = left.arity() + right.arity() - 2;
		if (arity < 1)
			throw new IllegalArgumentException("Incompatible arities: " + left + " and " + right);
		break;
	case PRODUCT : 
		this.arity = left.arity() + right.arity();
		break;
	default : 
		throw new IllegalArgumentException("Not a binary operator: " + op);
	}
	
	this.op = op;
	this.left = left;
	this.right = right;
	
}
 
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: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
/** @effects appends the tokenization of the given node to this.tokens */
public void visit(BinaryExpression node) {
	if (displayed(node)) return;
	final boolean oldTop = notTop();
	final ExprOperator op = node.op();
	final Expression left = node.left(), right = node.right();
	if (op==ExprOperator.JOIN && left.arity()==1 && right.arity()==2 && right instanceof Relation) { 
		append(right);
		append("[");
		visitChild(left, false);
		append("]");
	} else {
		visitChild(left, parenthesize(op, left));
		infix(op);
		visitChild(right, parenthesize(op, right));
	}
	top = oldTop;
}
 
Example #6
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Calls lookup(binExpr) 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(binExpr) | some t => t, 
 *      let op = (binExpr.op).(UNION->or + INTERSECTION->and + DIFFERENCE->difference + OVERRIDE->override + JOIN->dot + PRODUCT->cross) | 
 *       cache(binExpr, op(binExpr.left.accept(this), binExpr.right.accept(this)))
 */
public BooleanMatrix visit(BinaryExpression binExpr) {
	BooleanMatrix ret = lookup(binExpr);
	if (ret!=null) return ret;

	final BooleanMatrix left = binExpr.left().accept(this);
	final BooleanMatrix right = binExpr.right().accept(this);
	final ExprOperator op = binExpr.op();

	switch(op) {
	case UNION        	: ret = left.or(right); break;
	case INTERSECTION	: ret = left.and(right); break;
	case DIFFERENCE 	: ret = left.difference(right); break;
	case OVERRIDE 		: ret = left.override(right); break;
	case JOIN 			: ret = left.dot(right); break;
	case PRODUCT		: ret = left.cross(right); break;
	default : 
		throw new IllegalArgumentException("Unknown operator: " + op);
	}

	return cache(binExpr, ret);
}
 
Example #7
Source File: PartialCannonicalizer.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public Expression visit(UnaryExpression expr) { 
	Expression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final ExprOperator op = expr.op();
	final Expression child = expr.expression().accept(this);
	
	if (isEmpty(child)) return cache(expr, child);
	final int hash = hash(op, child);
	for(Iterator<PartialCannonicalizer.Holder<Expression>> itr = exprs.get(hash); itr.hasNext(); ) {
		final Expression next = itr.next().obj;
		if (next.getClass()==UnaryExpression.class) { 
			if (((UnaryExpression)next).expression()==child)
				return cache(expr, next);
		}
	}
	ret = child==expr.expression() ? expr : child.apply(op);
	exprs.add(new PartialCannonicalizer.Holder<Expression>(ret, hash));
	return cache(expr,ret);
}
 
Example #8
Source File: TranslatorTest.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
private final void testNary(ExprOperator op) {
    bounds.bound(r1[0], factory.range(factory.tuple(1, 0), factory.tuple(1, 3)));
    bounds.bound(r1[1], factory.range(factory.tuple(1, 2), factory.tuple(1, 5)));
    bounds.bound(r1[3], factory.range(factory.tuple(1, 3), factory.tuple(1, 6)));

    for (int i = 2; i <= 5; i++) {
        final Expression[] exprs = new Expression[i];
        exprs[0] = r1[0];
        Expression binExpr = r1[0];
        for (int j = 1; j < i; j++) {
            binExpr = binExpr.compose(op, r1[j % 4]);
            exprs[j] = r1[j % 4];
        }
        Expression nExpr = Expression.compose(op, exprs);
        final Solution sol = solver.solve(binExpr.eq(nExpr).not(), bounds);
        assertNull(sol.instance());
    }

}
 
Example #9
Source File: TranslatorTest.java    From kodkod with MIT License 6 votes vote down vote up
private final void testNary(ExprOperator op) { 
	bounds.bound(r1[0], factory.range(factory.tuple(1, 0), factory.tuple(1, 3)));
	bounds.bound(r1[1], factory.range(factory.tuple(1, 2), factory.tuple(1, 5)));
	bounds.bound(r1[3], factory.range(factory.tuple(1, 3), factory.tuple(1, 6)));
	

	for(int i = 2; i <= 5; i++) { 
		final Expression[] exprs = new Expression[i];
		exprs[0] = r1[0];
		Expression binExpr = r1[0];
		for(int j = 1; j < i; j++) { 
			binExpr = binExpr.compose(op, r1[j%4]);
			exprs[j] = r1[j%4];
		}
		Expression nExpr = Expression.compose(op, exprs);
		final Solution sol = solver.solve(binExpr.eq(nExpr).not(), bounds);
		assertNull(sol.instance());	
	}
	
	
}
 
Example #10
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 #11
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Calls lookup(unaryExpr) 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(unaryExpr) | some t => t, let op =
 *         (unaryExpr.op).(TRANSPOSE->transpose + CLOSURE->closure +
 *         REFLEXIVE_CLOSURE->(lambda(m)(m.closure().or(iden))) |
 *         cache(unaryExpr, op(unaryExpr.child))
 */
@Override
public final BooleanMatrix visit(UnaryExpression unaryExpr) {
    BooleanMatrix ret = lookup(unaryExpr);
    if (ret != null)
        return ret;

    final BooleanMatrix child = unaryExpr.expression().accept(this);
    final ExprOperator op = unaryExpr.op();

    switch (op) {
        case TRANSPOSE :
            ret = child.transpose();
            break;
        case CLOSURE :
            ret = child.closure();
            break;
        case REFLEXIVE_CLOSURE :
            ret = child.closure().or(visit((ConstantExpression) Expression.IDEN));
            break;
        default :
            throw new IllegalArgumentException("Unknown operator: " + op);
    }
    return cache(unaryExpr, ret);
}
 
Example #12
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Calls lookup(unaryExpr) 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(unaryExpr) | some t => t, 
 *      let op = (unaryExpr.op).(TRANSPOSE->transpose + CLOSURE->closure + REFLEXIVE_CLOSURE->(lambda(m)(m.closure().or(iden))) | 
 *       cache(unaryExpr, op(unaryExpr.child))
 */
public final BooleanMatrix visit(UnaryExpression unaryExpr) {
	BooleanMatrix ret = lookup(unaryExpr);
	if (ret!=null) return ret;

	final BooleanMatrix child = unaryExpr.expression().accept(this);
	final ExprOperator op = unaryExpr.op();

	switch(op) {
	case TRANSPOSE         	: ret = child.transpose(); break;
	case CLOSURE           	: ret = child.closure(); break;
	case REFLEXIVE_CLOSURE	: ret = child.closure().or(visit((ConstantExpression)Expression.IDEN)); break;
	default : 
		throw new IllegalArgumentException("Unknown operator: " + op);
	}
	return cache(unaryExpr,ret);
}
 
Example #13
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 #14
Source File: UnaryExpression.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new unary expression: op expression
 *
 * @ensures this.expression' = expression && this.op' = op
 * @throws NullPointerException expression = null || op = null
 * @throws IllegalArgumentException op in {TRANSPOSE, CLOSURE,
 *             REFLEXIVE_CLOSURE} && child.arity != 2
 */
UnaryExpression(ExprOperator op, Expression child) {
    if (!op.unary()) {
        throw new IllegalArgumentException("Not a unary operator: " + op);
    }
    if (child.arity() != 2 && op != ExprOperator.PRE) {
        throw new IllegalArgumentException("Invalid arity: " + child + "::" + child.arity());
    }
    this.expression = child;
    this.op = op;
    this.arity = child.arity();
}
 
Example #15
Source File: Expression.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the composition of the given expressions using the given operator. 
 * @requires exprs.size() = 2 => op.binary(), exprs.size() > 2 => op.nary()
 * @return exprs.size()=1 => exprs.iterator().next() else {e: Expression | e.children = exprs.toArray() and e.op = this }
 */
public static Expression compose(ExprOperator op, Collection<? extends Expression> exprs) { 
	switch(exprs.size()) { 
	case 0 : 	throw new IllegalArgumentException("Expected at least one argument: " + exprs);
	case 1 : 	return exprs.iterator().next();
	case 2 :
		final Iterator<? extends Expression> itr = exprs.iterator();
		return new BinaryExpression(itr.next(), op, itr.next());
	default : 			
		return new NaryExpression(op, exprs.toArray(new Expression[exprs.size()]));
	}
}
 
Example #16
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 #17
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(BinaryExpression node) {
	final ExprOperator op = node.op();
	visitChild(node.left(), parenthesize(op, node.left()));
	infix(op);
	visitChild(node.right(), parenthesize(op, node.right()));
}
 
Example #18
Source File: Expression.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the composition of the given expressions using the given operator. 
 * @requires exprs.length = 2 => op.binary(), exprs.length > 2 => op.nary()
 * @return exprs.length=1 => exprs[0] else {e: Expression | e.children = exprs and e.op = this }
 */
public static Expression compose(ExprOperator op, Expression...exprs) { 
	switch(exprs.length) { 
	case 0 : 	throw new IllegalArgumentException("Expected at least one argument: " + Arrays.toString(exprs));
	case 1 : 	return exprs[0];
	case 2 : 	return new BinaryExpression(exprs[0], op, exprs[1]);
	default : 	return new NaryExpression(op, Containers.copy(exprs, new Expression[exprs.length]));
	}
}
 
Example #19
Source File: PartialCannonicalizer.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Expression visit(BinaryExpression expr) { 
	Expression ret = lookup(expr);
	if (ret!=null) return ret;
	final ExprOperator op = expr.op();
	final Expression left = expr.left().accept(this);
	final Expression right = expr.right().accept(this);
	
	ret = simplify(op, left, right);
	
	if (ret==null) {
	
		final int hash = hash(op, left, right);
		for(Iterator<PartialCannonicalizer.Holder<Expression>> itr = exprs.get(hash); itr.hasNext(); ) {
			final Expression next = itr.next().obj;
			if (next instanceof BinaryExpression) { 
				final BinaryExpression hit = (BinaryExpression) next;
				if (hit.op()==op && hit.left()==left && hit.right()==right) { 
					return cache(expr, hit);
				}
			}
		}

		ret = left==expr.left()&&right==expr.right() ? expr : left.compose(op, right);
		exprs.add(new PartialCannonicalizer.Holder<Expression>(ret, hash));
	}
	
	return cache(expr,ret);
}
 
Example #20
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 #21
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 #22
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @return a simplification of left op right, if possible, or null otherwise. */
final Expression simplify(ExprOperator op, Expression left, Expression right) { 
	switch(op) { 
	case UNION : case OVERRIDE : 
		if (left==right) 			{ return left; }
		else if (isEmpty(left))		{ return right; }
		else if (isEmpty(right))	{ return left; }
		break;
	case JOIN :
		if (isEmpty(left) || isEmpty(right)) 	{ return empty(left.arity()+right.arity()-2); }
		break;
	case PRODUCT : 
		if (isEmpty(left) || isEmpty(right)) 	{ return empty(left.arity()+right.arity()); }
		break;
	case DIFFERENCE :
		if (left==right)						{ return empty(left.arity()); }
		else if (isEmpty(left)||isEmpty(right)) { return left; }
		break;
	case INTERSECTION :
		if (left==right)			{ return left; }
		else if (isEmpty(left))		{ return left; }
		else if (isEmpty(right))	{ return right; }
		break;
	default :
		Assertions.UNREACHABLE();
	}
	return null;
}
 
Example #23
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Expression visit(BinaryExpression expr) { 
	Expression ret = lookup(expr);
	if (ret!=null) return ret;
	final ExprOperator op = expr.op();
	final Expression left = expr.left().accept(this);
	final Expression right = expr.right().accept(this);
	
	ret = simplify(op, left, right);
	
	if (ret==null) {
		ret = left==expr.left()&&right==expr.right() ? expr : left.compose(op, right);
	}
	
	return cache(expr,ret);
}
 
Example #24
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Expression visit(UnaryExpression expr) { 
	Expression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final ExprOperator op = expr.op();
	final Expression child = expr.expression().accept(this);
	
	if (isEmpty(child)) return cache(expr, child);
	ret = child==expr.expression() ? expr : child.apply(op);
	return cache(expr,ret);
}
 
Example #25
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 #26
Source File: BinaryExpression.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new binary expression: left op right
 *
 * @ensures this.left' = left && this.right' = right && this.op' = op
 * @throws NullPointerException left = null || right = null || op = null
 * @throws IllegalArgumentException left and right cannot be combined with the
 *             specified operator.
 */
BinaryExpression(final Expression left, final ExprOperator op, final Expression right) {
    switch (op) {
        case UNION :
        case INTERSECTION :
        case DIFFERENCE :
        case OVERRIDE :
            this.arity = left.arity();
            if (arity != right.arity())
                throw new IllegalArgumentException("Incompatible arities: " + left + " and " + right);
            break;
        case JOIN :
            this.arity = left.arity() + right.arity() - 2;
            if (arity < 1)
                throw new IllegalArgumentException("Incompatible arities: " + left + " and " + right);
            break;
        case PRODUCT :
            this.arity = left.arity() + right.arity();
            break;
        default :
            throw new IllegalArgumentException("Not a binary operator: " + op);
    }

    this.op = op;
    this.left = left;
    this.right = right;

}
 
Example #27
Source File: NaryExpression.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new associative expression: op(children)
 *
 * @requires children array is not modified while in use by this associative
 *           expression
 * @requires some op.op[children]
 * @ensures this.children' = children && this.op' = op
 */
NaryExpression(ExprOperator op, Expression[] children) {
    assert children.length > 2;
    if (!op.nary())
        throw new IllegalArgumentException("Cannot construct an nary expression using the non-nary operator " + op);

    this.op = op;
    this.children = children;

    switch (op) {
        case UNION :
        case INTERSECTION :
        case OVERRIDE :
            this.arity = children[0].arity();
            for (int i = 1; i < children.length; i++) {
                if (children[i].arity() != arity)
                    throw new IllegalArgumentException("Incompatible arities: " + children[0] + " and " + children[i]);
            }
            break;
        case PRODUCT :
            int sum = 0;
            for (Expression child : children) {
                sum += child.arity();
            }
            this.arity = sum;
            break;
        default :
            throw new IllegalArgumentException("Unknown associative operator: " + op);
    }
}
 
Example #28
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(binExpr) 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(binExpr) | some t => t, let op =
 *         (binExpr.op).(UNION->or + INTERSECTION->and + DIFFERENCE->difference
 *         + OVERRIDE->override + JOIN->dot + PRODUCT->cross) | cache(binExpr,
 *         op(binExpr.left.accept(this), binExpr.right.accept(this)))
 */
@Override
public BooleanMatrix visit(BinaryExpression binExpr) {
    BooleanMatrix ret = lookup(binExpr);
    if (ret != null)
        return ret;

    final BooleanMatrix left = binExpr.left().accept(this);
    final BooleanMatrix right = binExpr.right().accept(this);
    final ExprOperator op = binExpr.op();

    switch (op) {
        case UNION :
            ret = left.or(right);
            break;
        case INTERSECTION :
            ret = left.and(right);
            break;
        case DIFFERENCE :
            ret = left.difference(right);
            break;
        case OVERRIDE :
            ret = left.override(right);
            break;
        case JOIN :
            ret = left.dot(right);
            break;
        case PRODUCT :
            ret = left.cross(right);
            break;
        default :
            throw new IllegalArgumentException("Unknown operator: " + op);
    }

    return cache(binExpr, ret);
}
 
Example #29
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 #30
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(BinaryExpression node) {
    final ExprOperator op = node.op();
    visitChild(node.left(), parenthesize(op, node.left()));
    infix(op);
    visitChild(node.right(), parenthesize(op, node.right()));
}