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

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

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

    ret = allSame ? formula : Formula.compose(formula.op(), visited);
    return cache(formula, ret);
}
 
Example 2
Source File: PrenexNFConverter.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
@Override
public Formula visit(NaryFormula formula) {
    final ArrayList<Formula> children = new ArrayList<Formula>(formula.size());
    boolean allSame = true;
    boolean noQuant = true;
    for (Formula ch : formula) {
        Formula ch2 = ch.accept(this);
        allSame = allSame && ch == ch2;
        noQuant = noQuant && !(ch2 instanceof QuantifiedFormula);
        children.add(ch2);
    }
    Formula ans;
    if (allSame && noQuant) {
        ans = formula;
    } else {
        ans = children.get(0);
        for (int i = 1; i < children.size(); i++)
            ans = new Pair(ans, children.get(i)).compose(formula.op());
    }
    return saveMapping(ans, formula);
}
 
Example 3
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * @ensures appends the tokenization of the given node to this.tokens
 */
@Override
public void visit(NaryFormula node) {
    final FormulaOperator op = node.op();
    boolean parens = parenthesize(op, node.child(0));
    if (parens)
        indent++;
    visitChild(node.child(0), parens);
    if (parens)
        indent--;
    for (int i = 1, size = node.size(); i < size; i++) {
        infix(op);
        newline();
        parens = parenthesize(op, node.child(i));
        if (parens)
            indent++;
        visitChild(node.child(i), parens);
        if (parens)
            indent--;
    }
}
 
Example 4
Source File: TranslateKodkodToJava.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visit(NaryFormula 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("Formula %s=Formula.compose(FormulaOperator.", newname);
    switch (x.op()) {
        case AND :
            file.print("AND");
            break;
        case OR :
            file.print("OR");
            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 5
Source File: AbstractReplacer.java    From kodkod with MIT License 6 votes vote down vote up
/** 
 * Calls lookup(formula) and returns the cached value, if any.  
 * If a replacement has not been cached, visits the formula's 
 * children.  If nothing changes, the argument is cached and
 * returned, otherwise a replacement formula is cached and returned.
 * @return { e: Expression | e.op = formula.op && #e.children = #formula.children && all i: [0..formula.children) | e.child(i) = formula.child(i).accept(this) }
 */
public Formula visit(NaryFormula formula) {
	Formula ret = lookup(formula);
	if (ret!=null) return ret;
	
	final Formula[] visited = new Formula[formula.size()];
	boolean allSame = true;
	for(int i = 0 ; i < visited.length; i++) { 
		final Formula child = formula.child(i);
		visited[i] = child.accept(this);
		allSame = allSame && visited[i]==child;
	}
	
	ret = allSame ? formula : Formula.compose(formula.op(), visited);
	return cache(formula,ret);
}
 
Example 6
Source File: PrettyPrinter.java    From kodkod with MIT License 6 votes vote down vote up
/** @ensures appends the tokenization of the given node to this.tokens */
public void visit(NaryFormula node) {
	final FormulaOperator op = node.op();
	boolean parens = parenthesize(op, node.child(0));
	if (parens) indent++;
	visitChild(node.child(0), parens);
	if (parens) indent--;
	for(int i = 1, size = node.size(); i < size; i++) { 
		infix(op);
		newline();
		parens = parenthesize(op, node.child(i));
		if (parens) indent++;
		visitChild(node.child(i), parens);
		if (parens) indent--;
	}
}
 
Example 7
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(NaryFormula node) {
	if (displayed(node)) return;
	final boolean oldTop = top;
	final FormulaOperator op = node.op();
	if ((negated && op==AND) || (!negated && op==OR)) { // not top in these cases
		top = false;
	}
	boolean parens = parenthesize(op, node.child(0));
	negated = negated ^ (op==OR);
	if (parens) indent++;
	visitChild(node.child(0), parens);
	if (parens) indent--;
	for(int i = 1, size = node.size(); i < size; i++) { 
		infix(op);
		if (top) newline();
		parens = parenthesize(op, node.child(i));
		if (parens) indent++;
		visitChild(node.child(i), parens);
		if (parens) indent--;
	}
	negated = negated ^ (op==OR);
	top = oldTop;
}
 
Example 8
Source File: Skolemizer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * If not cached, visits the formula's children with appropriate settings for
 * the negated flag and the skolemDepth parameter.
 *
 * @see kodkod.ast.visitor.AbstractReplacer#visit(kodkod.ast.NaryFormula)
 */
@Override
public final Formula visit(NaryFormula bf) {
    Formula ret = lookup(bf);
    if (ret != null)
        return ret;

    final int oldDepth = skolemDepth;
    final FormulaOperator op = bf.op();

    switch (op) {
        case AND :
            if (negated)
                skolemDepth = -1;
            break;
        case OR :
            if (!negated)
                skolemDepth = -1;
            break;
        default :
            throw new IllegalArgumentException("Unknown nary operator: " + op);
    }

    final Formula[] visited = new Formula[bf.size()];
    boolean allSame = true;
    for (int i = 0; i < visited.length; i++) {
        final Formula child = bf.child(i);
        visited[i] = child.accept(this);
        allSame = allSame && (child == visited[i]);
    }
    ret = allSame ? bf : Formula.compose(op, visited);

    skolemDepth = oldDepth;

    return source(cache(bf, ret), bf);
}
 
Example 9
Source File: Nodes.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the roots of the given formula. In other words, breaks up the given
 * formula into its conjunctive components, {f0, ..., fk}, such that, for all
 * 0<=i<=k, f<sub>i</sub> is not a conjunction and [[f0 && ... && fk]] <=>
 * [[formula]].
 *
 * @return subformulas, {f0, ..., fk}, of the given formula such that, for all
 *         0<=i<=k, f<sub>i</sub> is not a conjuction and [[f0 && ... && fk]]
 *         <=> [[formula]].
 */
public static Set<Formula> roots(Formula formula) {

    final List<Formula> formulas = new LinkedList<Formula>();
    formulas.add(formula);

    final ListIterator<Formula> itr = formulas.listIterator();
    while (itr.hasNext()) {
        final Formula f = itr.next();
        if (f instanceof BinaryFormula) {
            final BinaryFormula bin = (BinaryFormula) f;
            if (bin.op() == FormulaOperator.AND) {
                itr.remove();
                itr.add(bin.left());
                itr.add(bin.right());
                itr.previous();
                itr.previous();
            }
        } else if (f instanceof NaryFormula) {
            final NaryFormula nf = (NaryFormula) f;
            if (nf.op() == FormulaOperator.AND) {
                itr.remove();
                for (Formula child : nf) {
                    itr.add(child);
                }
                for (int i = nf.size(); i > 0; i--) {
                    itr.previous();
                }
            }
        }
    }

    return new LinkedHashSet<Formula>(formulas);
}
 
Example 10
Source File: Skolemizer.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * If not cached, visits the formula's children with appropriate settings
 * for the negated flag and the skolemDepth parameter.
 * @see kodkod.ast.visitor.AbstractReplacer#visit(kodkod.ast.NaryFormula)
 */
public final Formula visit(NaryFormula bf) {
	Formula ret = lookup(bf);
	if (ret!=null) return ret;			
	
	final int oldDepth = skolemDepth;
	final FormulaOperator op = bf.op();
	
	switch(op) { 
	case AND : if (negated)  skolemDepth = -1; break;
	case OR  : if (!negated) skolemDepth = -1; break;
	default  : throw new IllegalArgumentException("Unknown nary operator: " + op);
	}
	
	final Formula[] visited = new Formula[bf.size()];
	boolean allSame = true;
	for(int i = 0; i < visited.length; i++) { 
		final Formula child = bf.child(i);
		visited[i] = child.accept(this);
		allSame = allSame && (child==visited[i]);
	}
	ret = allSame ? bf : Formula.compose(op, visited);
	
	skolemDepth = oldDepth;
	
	return source(cache(bf,ret),bf);
}
 
Example 11
Source File: Nodes.java    From kodkod with MIT License 5 votes vote down vote up
/**
    * Returns the roots of the given formula.
    * In other words, breaks up the given formula into its conjunctive 
    * components, {f0, ..., fk}, 
    * such that, for all 0<=i<=k, f<sub>i</sub> is not a conjunction  and
    * [[f0 && ... && fk]] <=> [[formula]].  
    * @return subformulas, {f0, ..., fk}, of the given formula such that, for all 0<=i<=k, 
    * f<sub>i</sub> is not a conjunction and [[f0 && ... && fk]] <=> [[formula]].
    */
public static Set<Formula> roots(Formula formula) {

   	final List<Formula> formulas = new LinkedList<Formula>();
	formulas.add(formula);
	
	final ListIterator<Formula> itr = formulas.listIterator();
	while(itr.hasNext()) {
		final Formula f = itr.next();
		if (f instanceof BinaryFormula) {
			final BinaryFormula bin = (BinaryFormula) f;
			if (bin.op()==FormulaOperator.AND) {
				itr.remove();
				itr.add(bin.left());
				itr.add(bin.right());
				itr.previous();
				itr.previous();
			}
		} else if (f instanceof NaryFormula) { 
			final NaryFormula nf = (NaryFormula) f;
			if (nf.op()==FormulaOperator.AND) { 
				itr.remove();
				for(Formula child : nf) { 
					itr.add(child);
				}
				for(int i = nf.size(); i>0; i--) { 
					itr.previous();
				}
			}
		}
	}
	
	return new LinkedHashSet<Formula>(formulas);
}
 
Example 12
Source File: Nodes.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns an unmodifiable set consisting of the children of the given formula, if the formula is a binary or an nary conjunction.  Otherwise
 * returns a singleton set containing the formula itself.
 * @return  an unmodifiable set consisting of children of the given formula, if the formula is a binary or an nary conjunction.  Otherwise
 * returns a singleton set containing the formula itself.
 */
public static Set<Formula> conjuncts(Formula formula) { 
	if (formula instanceof BinaryFormula) { 
		final BinaryFormula bin = (BinaryFormula) formula;
		if (bin.op()==FormulaOperator.AND) {
			final Formula left = bin.left(), right = bin.right();
			if (left==right) return Collections.singleton(left);
			else return new AbstractSet<Formula>() {
				@Override
				public boolean contains(Object o) { return left==o || right==o; }
				@Override
				public Iterator<Formula> iterator() { return Containers.iterate(left, right); }
				@Override
				public int size() { return 2;	}
				
			};
		}
	} else if (formula instanceof NaryFormula) { 
		final NaryFormula nf = (NaryFormula) formula;
		if (nf.op()==FormulaOperator.AND) { 
			final LinkedHashSet<Formula> children = new LinkedHashSet<Formula>(1+(nf.size()*4)/3);
			for(Formula child : nf) { children.add(child); }
			return Collections.unmodifiableSet(children);
		}
	} 
	
	return Collections.singleton(formula);
	
}
 
Example 13
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Formula visit(NaryFormula formula) { 
	Formula ret = lookup(formula);
	if (ret!=null) return ret;
	
	final FormulaOperator op = formula.op();
	
	final List<Formula> children = simplify(op, visitAll(formula));
	final int size = children.size();
	if (size<2) { 
		return cache(formula, Formula.compose(op, children));
	} else {
		ret = formula.size()==size && allSame(formula,children) ? formula : Formula.compose(op, children);	
		return cache(formula,ret);
	}	
}
 
Example 14
Source File: Nodes.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an unmodifiable set consisting of the children of the given formula,
 * if the formula is a binary or an nary conjunction. Otherwise returns a
 * singleton set containing the formula itself.
 *
 * @return an unmodifiable set consisting of children of the given formula, if
 *         the formula is a binary or an nary conjunction. Otherwise returns a
 *         singleton set containing the formula itself.
 */
public static Set<Formula> conjuncts(Formula formula) {
    if (formula instanceof BinaryFormula) {
        final BinaryFormula bin = (BinaryFormula) formula;
        if (bin.op() == FormulaOperator.AND) {
            final Formula left = bin.left(), right = bin.right();
            if (left == right)
                return Collections.singleton(left);
            else
                return new AbstractSet<Formula>() {

                    @Override
                    public boolean contains(Object o) {
                        return left == o || right == o;
                    }

                    @Override
                    public Iterator<Formula> iterator() {
                        return Containers.iterate(left, right);
                    }

                    @Override
                    public int size() {
                        return 2;
                    }

                };
        }
    } else if (formula instanceof NaryFormula) {
        final NaryFormula nf = (NaryFormula) formula;
        if (nf.op() == FormulaOperator.AND) {
            final LinkedHashSet<Formula> children = new LinkedHashSet<Formula>(1 + (nf.size() * 4) / 3);
            for (Formula child : nf) {
                children.add(child);
            }
            return Collections.unmodifiableSet(children);
        }
    }

    return Collections.singleton(formula);

}