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

The following examples show how to use kodkod.ast.NaryFormula#op() . 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: FormulaFlattener.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Visits the formula's children with appropriate settings for the negated flag
 * if bf has not been visited before.
 *
 * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.NaryFormula)
 */
@Override
public final void visit(NaryFormula nf) {
    if (visited(nf))
        return;
    final FormulaOperator op = nf.op();
    if ((negated && op == AND) || (!negated && op == OR)) { // can't break
                                                           // down further
                                                           // in these
                                                           // cases
        addConjunct(nf);
    } else { // will break down further
        for (Formula f : nf) {
            f.accept(this);
        }
    }
}
 
Example 2
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 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: Nodes.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
public static List<Formula> allConjuncts(Formula formula, List<Formula> acc) {
    List<Formula> ans = acc != null ? acc : new ArrayList<Formula>();
    if (formula instanceof BinaryFormula) {
        final BinaryFormula bin = (BinaryFormula) formula;
        if (bin.op() == FormulaOperator.AND) {
            allConjuncts(bin.left(), ans);
            allConjuncts(bin.right(), ans);
            return ans;
        }
    }
    if (formula instanceof NaryFormula) {
        final NaryFormula nf = (NaryFormula) formula;
        if (nf.op() == FormulaOperator.AND) {
            for (Formula child : nf) {
                allConjuncts(child, ans);
            }
            return ans;
        }
    }
    ans.add(formula);
    return ans;
}
 
Example 5
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 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: FOL2BoolTranslator.java    From kodkod with MIT License 6 votes vote down vote up
/** 
 * Calls lookup(formula) and returns the cached value, if any.  
 * If a translation has not been cached, translates the formula,
 * calls cache(...) on it and returns it.
 * @return let t = lookup(formula) | some t => t, 
 * 	cache(formula, formula.op(formula.left.accept(this), formula.right.accept(this))
 */
public final BooleanValue visit(NaryFormula formula) {
	final BooleanValue ret = lookup(formula);
	if (ret!=null) return ret;

	final FormulaOperator op = formula.op();		
	final Operator.Nary boolOp;
	
	switch(op) { 
	case AND : boolOp = Operator.AND; break;
	case OR  : boolOp = Operator.OR;  break;
	default	 : throw new IllegalArgumentException("Unknown nary operator: " + op);
	}
	
	final BooleanAccumulator acc = BooleanAccumulator.treeGate(boolOp);
	final BooleanValue shortCircuit = boolOp.shortCircuit();
	for(Formula child : formula) { 
		if (acc.add(child.accept(this))==shortCircuit)
			break;
	}
	
	return cache(formula, interpreter.factory().accumulate(acc));
}
 
Example 8
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 9
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 10
Source File: AnnotatedNode.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Visits the children of the given formula if it has not been visited already with
 * the given value of the negated flag and if formula.op==OR && negated or
 * formula.op==AND && !negated. Otherwise does nothing.
 * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.NaryFormula)
 */
public void visit(NaryFormula formula) { 
	if (visited(formula)) return;
	final FormulaOperator op = formula.op();
	if ((!negated && op==AND) || (negated && op==OR)) { // op==AND || op==OR
		for(Formula child : formula) { 
			child.accept(this);
		}
	}
}
 
Example 11
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 12
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 13
Source File: FormulaFlattener.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Visits the formula's children with appropriate settings
 * for the negated flag if bf  has not been visited before.
 * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.NaryFormula)
 */
public final void visit(NaryFormula nf) { 
	if (visited(nf)) return;
	final FormulaOperator op = nf.op();
	if ((negated && op==AND) || (!negated && op==OR)) { // can't break down further in these cases
		addConjunct(nf);
	} else { // will break down further
		for(Formula f : nf) { 
			f.accept(this);
		}
	}
}
 
Example 14
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 15
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Visits the children of the given formula if it has not been visited already
 * with the given value of the negated flag and if formula.op==OR && negated or
 * formula.op==AND && !negated. Otherwise does nothing.
 *
 * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.NaryFormula)
 */
@Override
public void visit(NaryFormula formula) {
    if (visited(formula))
        return;
    final FormulaOperator op = formula.op();
    if ((!negated && op == AND) || (negated && op == OR)) { // op==AND
                                                           // || op==OR
        for (Formula child : formula) {
            child.accept(this);
        }
    }
}
 
Example 16
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 17
Source File: HOLTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Override
public Proc visit(NaryFormula formula) {
    if (annotated.isFOLNode(formula))
        return new Proc.FOL(bounds, formula);
    Proc ans = null;
    for (Formula f : formula) {
        Proc p = formula.op() == FormulaOperator.OR ? toProc(f) : f.accept(this);
        ans = ans == null ? p : ans.compose(formula.op(), p);
    }
    return ans;
}
 
Example 18
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(formula) and returns the cached value, if any. If a translation
 * has not been cached, translates the formula, calls cache(...) on it and
 * returns it.
 *
 * @return let t = lookup(formula) | some t => t, cache(formula,
 *         formula.op(formula.left.accept(this), formula.right.accept(this))
 */
@Override
public final BooleanValue visit(NaryFormula formula) {
    final BooleanValue ret = lookup(formula);
    if (ret != null)
        return ret;

    final FormulaOperator op = formula.op();
    final Operator.Nary boolOp;

    switch (op) {
        case AND :
            boolOp = Operator.AND;
            break;
        case OR :
            boolOp = Operator.OR;
            break;
        default :
            throw new IllegalArgumentException("Unknown nary operator: " + op);
    }

    final BooleanAccumulator acc = BooleanAccumulator.treeGate(boolOp);
    final BooleanValue shortCircuit = boolOp.shortCircuit();
    for (Formula child : formula) {
        if (acc.add(child.accept(this)) == shortCircuit)
            break;
    }

    return cache(formula, interpreter.factory().accumulate(acc));
}
 
Example 19
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);

}