Java Code Examples for kodkod.ast.IntExpression#accept()

The following examples show how to use kodkod.ast.IntExpression#accept() . 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(intExpr) and returns the cached value, if any. If a replacement
 * has not been cached, visits the intExpr's children. If nothing changes, the
 * argument is cached and returned, otherwise a replacement intExpr is cached
 * and returned.
 *
 * @return { e: IntExpression | e.op = intExpr.op && #e.children =
 *         #intExpr.children && all i: [0..intExpr.children) | e.child(i) =
 *         intExpr.child(i).accept(delegate) }
 */
@Override
public IntExpression visit(NaryIntExpression intExpr) {
    IntExpression ret = lookup(intExpr);
    if (ret != null)
        return ret;

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

    ret = allSame ? intExpr : IntExpression.compose(intExpr.op(), visited);
    return cache(intExpr, ret);
}
 
Example 2
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Translates the given sum expression as follows (where A_0...A_|A| stand for
 * boolean variables that represent the tuples of the expression A, etc.): let
 * sum = "sum a: A, b: B, ..., x: X | IE(a, b, ..., x) " | sum a: A, b: B, ...,
 * x: X | if (a in A && b in B && ... && x in X) then IE(a, b, ..., x) else 0 }.
 *
 * @param decls intexpr declarations
 * @param formula the formula body
 * @param currentDecl currently processed declaration; should be 0 initially
 * @param declConstraints the constraints implied by the declarations; should be
 *            Boolean.TRUE intially
 * @param values integer values computed so far
 */
private final void sum(Decls decls, IntExpression expr, int currentDecl, BooleanValue declConstraints, List<Int> values) {
    final BooleanFactory factory = interpreter.factory();
    if (decls.size() == currentDecl) {
        Int intExpr = expr.accept(this);
        Int newInt = intExpr.choice(declConstraints, factory.integer(0));
        values.add(newInt);
        return;
    }

    final Decl decl = decls.get(currentDecl);
    final BooleanMatrix declTransl = visit(decl);
    final BooleanMatrix groundValue = factory.matrix(declTransl.dimensions());
    env = env.extend(decl.variable(), decl.expression(), groundValue);
    for (IndexedEntry<BooleanValue> entry : declTransl) {
        groundValue.set(entry.index(), BooleanConstant.TRUE);
        sum(decls, expr, currentDecl + 1, factory.and(entry.value(), declConstraints), values);
        groundValue.set(entry.index(), BooleanConstant.FALSE);
    }
    env = env.parent();
}
 
Example 3
Source File: AbstractReplacer.java    From kodkod with MIT License 6 votes vote down vote up
/** 
* Calls lookup(intExpr) and returns the cached value, if any.  
* If a replacement has not been cached, visits the intExpr's 
* children.  If nothing changes, the argument is cached and
* returned, otherwise a replacement intExpr is cached and returned.
* @return { e: IntExpression | e.op = intExpr.op && #e.children = #intExpr.children && all i: [0..intExpr.children) | e.child(i) = intExpr.child(i).accept(this) }
*/
  public IntExpression visit(NaryIntExpression intExpr) {
IntExpression ret = lookup(intExpr);
if (ret!=null) return ret;
	
final IntExpression[] visited = new IntExpression[intExpr.size()];
boolean allSame = true;
for(int i = 0 ; i < visited.length; i++) { 
	final IntExpression child = intExpr.child(i);
	visited[i] = child.accept(this);
	allSame = allSame && visited[i]==child;
}

ret = allSame ? intExpr : IntExpression.compose(intExpr.op(), visited);
return cache(intExpr,ret);
  }
 
Example 4
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(intExpr) returns false. Otherwise does
 * nothing.
 *
 * @ensures all i: [0..#intExpr.children) | intExpr.child(i).accept(this)
 */
@Override
public void visit(NaryIntExpression intExpr) {
    if (visited(intExpr))
        return;
    for (IntExpression child : intExpr) {
        child.accept(this);
    }
}
 
Example 5
Source File: AbstractDetector.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(intExpr) 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(intExpr) | x != null => x, cache(intExpr,
 *         intExpr.child(0).accept(this) || ... ||
 *         intExpr.child(intExpr.size()-1).accept(this))
 */
@Override
public Boolean visit(NaryIntExpression intExpr) {
    final Boolean ret = lookup(intExpr);
    if (ret != null)
        return ret;
    for (IntExpression child : intExpr) {
        if (child.accept(this))
            return cache(intExpr, true);
    }
    return cache(intExpr, false);
}
 
Example 6
Source File: AbstractVoidVisitor.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Visits the children if this.visited(intExpr) returns false.  Otherwise does nothing.
 * @ensures all i: [0..#intExpr.children) | intExpr.child(i).accept(this)
 */
public void visit(NaryIntExpression intExpr) {
	if (visited(intExpr)) return;
	for(IntExpression child : intExpr) { 
		child.accept(this);
	}
}
 
Example 7
Source File: AbstractDetector.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(intExpr) 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(intExpr) | 
 *          x != null => x,  
 *          cache(intExpr, intExpr.child(0).accept(this) || ... || intExpr.child(intExpr.size()-1).accept(this)) 
 */
public Boolean visit(NaryIntExpression intExpr) {
	final Boolean ret = lookup(intExpr);
	if (ret!=null) return ret;
	for(IntExpression child : intExpr) { 
		if (child.accept(this))
			return cache(intExpr, true);
	}
	return cache(intExpr, false);
}