Java Code Examples for kodkod.ast.operator.FormulaOperator#AND

The following examples show how to use kodkod.ast.operator.FormulaOperator#AND . 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: 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 2
Source File: A4Solution.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Associates the Kodkod formula to a particular Alloy Expr (if the Kodkod
 * formula is not already associated with an Alloy Expr or Alloy Pos)
 */
Formula k2pos(Formula formula, Expr expr) throws Err {
    if (solved)
        throw new ErrorFatal("Cannot alter the k->pos mapping since solve() has completed.");
    if (formula == null || expr == null || k2pos.containsKey(formula))
        return formula;
    k2pos.put(formula, expr);
    if (formula instanceof BinaryFormula) {
        BinaryFormula b = (BinaryFormula) formula;
        if (b.op() == FormulaOperator.AND) {
            k2pos(b.left(), expr);
            k2pos(b.right(), expr);
        }
    }
    return formula;
}
 
Example 3
Source File: A4Solution.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Associates the Kodkod formula to a particular Alloy Pos (if the Kodkod
 * formula is not already associated with an Alloy Expr or Alloy Pos)
 */
Formula k2pos(Formula formula, Pos pos) throws Err {
    if (solved)
        throw new ErrorFatal("Cannot alter the k->pos mapping since solve() has completed.");
    if (formula == null || pos == null || pos == Pos.UNKNOWN || k2pos.containsKey(formula))
        return formula;
    k2pos.put(formula, pos);
    if (formula instanceof BinaryFormula) {
        BinaryFormula b = (BinaryFormula) formula;
        if (b.op() == FormulaOperator.AND) {
            k2pos(b.left(), pos);
            k2pos(b.right(), pos);
        }
    }
    return formula;
}
 
Example 4
Source File: HOL2ProcTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Override
protected void start(Node n) {
    stack.push(n);
    // ***NOTE*** assumes the formula is already in NNF !!!
    boolean skolemizableSoFar = skolemizable.empty() ? true : skolemizable.lastElement();
    if (!skolemizableSoFar) {
        skolemizable.push(false);
    } else {
        if ((n instanceof BinaryFormula && ((BinaryFormula) n).op() == FormulaOperator.AND) || (n instanceof NaryFormula && ((NaryFormula) n).op() == FormulaOperator.AND) || (n instanceof QuantifiedFormula && ((QuantifiedFormula) n).quantifier() == Quantifier.SOME))
            skolemizable.push(true);
        else
            skolemizable.push(false);
    }
}
 
Example 5
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 6
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 7
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 8
Source File: PartialCannonicalizer.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Formula visit(BinaryFormula formula) { 
	Formula ret = lookup(formula);
	if (ret!=null) return ret;
	final FormulaOperator op = formula.op();
	if (op==FormulaOperator.AND) {
		final Set<Formula> conjuncts = kodkod.util.nodes.Nodes.roots(formula);
		if (conjuncts.size()>2) { 
			return cache(formula, Formula.and(conjuncts).accept(this));
		}
	}
	
	final Formula left = formula.left().accept(this);
	final Formula right = formula.right().accept(this);
	
	ret = simplify(op, left, right);
	
	if (ret==null) {
	
		final int hash = hash(op, left, right);
		for(Iterator<PartialCannonicalizer.Holder<Formula>> itr = formulas.get(hash); itr.hasNext(); ) {
			final Formula next = itr.next().obj;
			if (next instanceof BinaryFormula) { 
				final BinaryFormula hit = (BinaryFormula) next;
				if (hit.op()==op && hit.left()==left && hit.right()==right) { 
					return cache(formula, hit);
				}
			}
		}

		ret = left==formula.left()&&right==formula.right() ? formula : left.compose(op, right);
		formulas.add(new PartialCannonicalizer.Holder<Formula>(ret, hash));
	}
	
	return cache(formula,ret);
}
 
Example 9
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);

}