kodkod.ast.IfExpression Java Examples

The following examples show how to use kodkod.ast.IfExpression. 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: 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(IfExpression node) {
		if (displayed(node)) return;
		final boolean oldTop = notTop();
		if (isEmpty(node.elseExpr())) {
			append("guard(");
			visitChild(node.condition(), false);
			append(",");
			space();
			visitChild(node.thenExpr(), false);
			append(")");
		} else {
			append("if");
			space();
			visitChild(node.condition(), parenthesize(node.condition()));
			infix("then");
//			indent++;
//			newline();
			visitChild(node.thenExpr(), parenthesize(node.thenExpr()));
			infix("else");
//			newline();
			visitChild(node.elseExpr(), parenthesize(node.elseExpr()));
//			indent--;
		}
		top = oldTop;
	}
 
Example #2
Source File: PartialCannonicalizer.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public Expression visit(IfExpression expr) { 
	Expression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final Formula cond = expr.condition().accept(this);
	final Expression thenExpr = expr.thenExpr().accept(this);
	final Expression elseExpr = expr.elseExpr().accept(this);
	
	ret = simplify(cond,thenExpr,elseExpr);
	
	if (ret==null) { 
		final int hash = hash(IfExpression.class, cond, thenExpr, elseExpr);
		for(Iterator<PartialCannonicalizer.Holder<Expression>> itr = exprs.get(hash); itr.hasNext(); ) {
			final Expression next = itr.next().obj;
			if (next.getClass()==IfExpression.class) { 
				final IfExpression hit = (IfExpression) next;
				if (hit.condition()==cond && hit.thenExpr()==thenExpr && hit.elseExpr()==elseExpr) { 
					return cache(expr, hit);
				}
			}
		}
		ret = cond==expr.condition()&&thenExpr==expr.thenExpr()&&elseExpr==expr.elseExpr() ? expr : cond.thenElse(thenExpr, elseExpr);
		exprs.add(new PartialCannonicalizer.Holder<Expression>(ret, hash));
	}
	return cache(expr,ret);
}
 
Example #3
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Expression visit(IfExpression expr) { 
	Expression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final Formula cond = expr.condition().accept(this);
	final Expression thenExpr = expr.thenExpr().accept(this);
	final Expression elseExpr = expr.elseExpr().accept(this);
	
	ret = simplify(cond,thenExpr,elseExpr);
	
	if (ret==null) { 
		ret = cond==expr.condition()&&thenExpr==expr.thenExpr()&&elseExpr==expr.elseExpr() ? expr : cond.thenElse(thenExpr, elseExpr);
	}
	return cache(expr,ret);
}
 
Example #4
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(IfExpression node) {
	visitChild(node.condition(), parenthesize(node.condition()));
	infix("=>");
	indent++;
	newline();
	visitChild(node.thenExpr(), parenthesize(node.thenExpr()));
	infix("else");
	newline();
	visitChild(node.elseExpr(), parenthesize(node.elseExpr()));
	indent--;
}
 
Example #5
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 #6
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Calls lookup(ifExpr) 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(ifExpr) | some t => t, 
 * 			cache(ifExpr, ifExpr.condition.accept(this).choice(ifExpr.then.accept(this), ifExpr.else.accept(this)))
 */
public BooleanMatrix visit(IfExpression ifExpr) {
	BooleanMatrix ret = lookup(ifExpr);
	if (ret!=null) return ret;

	final BooleanValue condition = ifExpr.condition().accept(this);
	final BooleanMatrix thenExpr = ifExpr.thenExpr().accept(this);
	final BooleanMatrix elseExpr = ifExpr.elseExpr().accept(this);
	ret = thenExpr.choice(condition, elseExpr);

	return cache(ifExpr,ret);
}
 
Example #7
Source File: AbstractCollector.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(ifExpr) 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(ifExpr) | 
 *          x != null => x,  
 *          cache(ifExpr, ifExpr.condition.accept(this) + ifExpr.thenExpr.accept(this) + ifExpr.elseExpr.accept(this)) 
 */
public Set<T> visit(IfExpression ifExpr) {
	Set<T> ret = lookup(ifExpr);
	if (ret!=null) return ret;
	ret = newSet();
	ret.addAll(ifExpr.condition().accept(this));
	ret.addAll(ifExpr.thenExpr().accept(this));
	ret.addAll(ifExpr.elseExpr().accept(this));
	return cache(ifExpr, ret);
}
 
Example #8
Source File: AbstractVoidVisitor.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Visits the if-condition, the then-expression, and the else-expression  if
 * this.visited(ifExpr) returns false.  Otherwise does nothing.
 * @ensures ifExpr.condition.accept(this) && ifExpr.thenExpr.accept(this) &&
 *          ifExpr.elseExpr.accept(this)
 */
public void visit(IfExpression ifExpr) {
	if (visited(ifExpr)) return;
	ifExpr.condition().accept(this);
	ifExpr.thenExpr().accept(this);
	ifExpr.elseExpr().accept(this);
}
 
Example #9
Source File: AbstractReplacer.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(ifExpr) and returns the cached value, if any.  
 * If a replacement has not been cached, visits the expression's 
 * children.  If nothing changes, the argument is cached and
 * returned, otherwise a replacement expression is cached and returned.
 * @return { i: IfExpression | i.condition = ifExpr.condition.accept(this) &&
 *                             i.thenExpr = ifExpr.thenExpr.accept(this) &&
 *                             i.elseExpr = ifExpr.elseExpr.accept(this) }
 */
public Expression visit(IfExpression ifExpr) {
	Expression ret = lookup(ifExpr);
	if (ret!=null) return ret;

	final Formula condition = ifExpr.condition().accept(this);
	final Expression thenExpr = ifExpr.thenExpr().accept(this);
	final Expression elseExpr = ifExpr.elseExpr().accept(this);
	ret = (condition==ifExpr.condition() && thenExpr==ifExpr.thenExpr() &&
		   elseExpr==ifExpr.elseExpr()) ? 
	      ifExpr : condition.thenElse(thenExpr, elseExpr);
	return cache(ifExpr,ret);
}
 
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(IfExpression x) {
    String newname = makename(x);
    if (newname == null)
        return;
    String a = make(x.condition());
    String b = make(x.thenExpr());
    String c = make(x.elseExpr());
    file.printf("Expression %s=%s.thenElse(%s,%s);%n", newname, a, b, c);
}
 
Example #11
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(IfExpression node) {
    visitChild(node.condition(), parenthesize(node.condition()));
    infix("=>");
    indent++;
    newline();
    visitChild(node.thenExpr(), parenthesize(node.thenExpr()));
    infix("else");
    newline();
    visitChild(node.elseExpr(), parenthesize(node.elseExpr()));
    indent--;
}
 
Example #12
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(ifExpr) 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(ifExpr) | some t => t, cache(ifExpr,
 *         ifExpr.condition.accept(this).choice(ifExpr.then.accept(this),
 *         ifExpr.else.accept(this)))
 */
@Override
public BooleanMatrix visit(IfExpression ifExpr) {
    BooleanMatrix ret = lookup(ifExpr);
    if (ret != null)
        return ret;

    final BooleanValue condition = ifExpr.condition().accept(this);
    final BooleanMatrix thenExpr = ifExpr.thenExpr().accept(this);
    final BooleanMatrix elseExpr = ifExpr.elseExpr().accept(this);
    ret = thenExpr.choice(condition, elseExpr);

    return cache(ifExpr, ret);
}
 
Example #13
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 #14
Source File: AbstractCollector.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(ifExpr) 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(ifExpr) | x != null => x, cache(ifExpr,
 *         ifExpr.condition.accept(this) + ifExpr.thenExpr.accept(this) +
 *         ifExpr.elseExpr.accept(this))
 */
@Override
public Set<T> visit(IfExpression ifExpr) {
    Set<T> ret = lookup(ifExpr);
    if (ret != null)
        return ret;
    ret = newSet();
    ret.addAll(ifExpr.condition().accept(this));
    ret.addAll(ifExpr.thenExpr().accept(this));
    ret.addAll(ifExpr.elseExpr().accept(this));
    return cache(ifExpr, ret);
}
 
Example #15
Source File: AbstractVoidVisitor.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Visits the if-condition, the then-expression, and the else-expression if
 * this.visited(ifExpr) returns false. Otherwise does nothing.
 *
 * @ensures ifExpr.condition.accept(this) && ifExpr.thenExpr.accept(this) &&
 *          ifExpr.elseExpr.accept(this)
 */
@Override
public void visit(IfExpression ifExpr) {
    if (visited(ifExpr))
        return;
    ifExpr.condition().accept(this);
    ifExpr.thenExpr().accept(this);
    ifExpr.elseExpr().accept(this);
}
 
Example #16
Source File: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(ifExpr) and returns the cached value, if any. If a replacement
 * has not been cached, visits the expression's children. If nothing changes,
 * the argument is cached and returned, otherwise a replacement expression is
 * cached and returned.
 *
 * @return { i: IfExpression | i.condition = ifExpr.condition.accept(delegate)
 *         && i.thenExpr = ifExpr.thenExpr.accept(delegate) && i.elseExpr =
 *         ifExpr.elseExpr.accept(delegate) }
 */
@Override
public Expression visit(IfExpression ifExpr) {
    Expression ret = lookup(ifExpr);
    if (ret != null)
        return ret;

    final Formula condition = ifExpr.condition().accept(delegate);
    final Expression thenExpr = ifExpr.thenExpr().accept(delegate);
    final Expression elseExpr = ifExpr.elseExpr().accept(delegate);
    ret = (condition == ifExpr.condition() && thenExpr == ifExpr.thenExpr() && elseExpr == ifExpr.elseExpr()) ? ifExpr : condition.thenElse(thenExpr, elseExpr);
    return cache(ifExpr, ret);
}
 
Example #17
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 #18
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(IfExpression ifExpr) {
    visit(ifExpr, "ite", ifExpr.condition(), ifExpr.thenExpr(), ifExpr.elseExpr());
}
 
Example #19
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
/**
 * Calls visited(ifexpr); ifexpr's children are not top-level formulas so they
 * are not visited.
 */
@Override
public void visit(IfExpression ifexpr) {
    visited(ifexpr);
}
 
Example #20
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public Boolean visit(IfExpression ife) {
    return checkVisitedThenAccumA(ife, Boolean.FALSE, ife.condition(), ife.thenExpr(), ife.elseExpr());
}
 
Example #21
Source File: HOLTranslator.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public Expression visit(IfExpression ifExpr) {
    return ifExpr;
}
 
Example #22
Source File: AspectReturnVisitor.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public E visit(IfExpression ifExpr) {
    start(ifExpr);
    return end(ifExpr, visitor.visit(ifExpr));
}
 
Example #23
Source File: PrettyPrinter.java    From kodkod with MIT License 4 votes vote down vote up
public void visit(IfExpression ifExpr) {
	visit(ifExpr, "ite", ifExpr.condition(), ifExpr.thenExpr(), ifExpr.elseExpr());
}
 
Example #24
Source File: PrettyPrinter.java    From kodkod with MIT License 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 child instanceof BinaryExpression || child instanceof IfExpression; 
}
 
Example #25
Source File: AnnotatedNode.java    From kodkod with MIT License 2 votes vote down vote up
/**
 * Calls visited(ifexpr); ifexpr's children are not top-level formulas
 * so they are not visited.
 */
public void visit(IfExpression ifexpr) {
	visited(ifexpr);
}
 
Example #26
Source File: VoidVisitor.java    From kodkod with MIT License 2 votes vote down vote up
/** 
* Visits the given if-then expression.
**/
  public void visit(IfExpression ifExpr);
 
Example #27
Source File: AbstractDetector.java    From kodkod with MIT License 2 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.condition.accept(this) || expr.thenExpr.accept(this) || expr.elseExpr.accept(this)) 
 */
public Boolean visit(IfExpression expr) {
	final Boolean ret = lookup(expr);
	return (ret!=null) ? ret : cache(expr, expr.condition().accept(this) || expr.thenExpr().accept(this) || expr.elseExpr().accept(this));
}
 
Example #28
Source File: ReturnVisitor.java    From kodkod with MIT License 2 votes vote down vote up
/** 
* Visits the given if-then expression and returns the result.
* @return the result of visiting <code>ifExpr</code> 
**/
  public E visit(IfExpression ifExpr);
 
Example #29
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 #30
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 should be parenthesized when a child of
 *         a compound parent
 */
private boolean parenthesize(Expression child) {
    return child instanceof BinaryExpression || child instanceof IfExpression;
}