Java Code Examples for kodkod.instance.TupleSet#isEmpty()

The following examples show how to use kodkod.instance.TupleSet#isEmpty() . 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: SymmetryDetector.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Returns an array that contains unique non-empty tuplesets in the given bounds, 
 * sorted in the order of increasing size.
 * @return unique non-empty tuplesets in the given bounds, 
 * sorted in the order of increasing size.
 */
private static TupleSet[] sort(Bounds bounds) { 
	final List<TupleSet> sets = new ArrayList<TupleSet>(bounds.relations().size());
	for(Relation r : bounds.relations()) { 
		final TupleSet lower = bounds.lowerBound(r);
		final TupleSet upper = bounds.upperBound(r);
		if (!lower.isEmpty() && lower.size()<upper.size()) { sets.add(lower); }
		if (!upper.isEmpty()) {	sets.add(upper); }
	}
	final TupleSet[] sorted = sets.toArray(new TupleSet[sets.size()]);
	Arrays.sort(sorted, new Comparator<TupleSet>(){
		public int compare(TupleSet o1, TupleSet o2) {
			return o1.size() - o2.size();
		}
	});
	return sorted;
}
 
Example 2
Source File: SymmetryDetector.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an array that contains unique non-empty tuplesets in the given
 * bounds, sorted in the order of increasing size.
 *
 * @return unique non-empty tuplesets in the given bounds, sorted in the order
 *         of increasing size.
 */
private TupleSet[] sort(Bounds bounds) {
    final List<TupleSet> sets = new ArrayList<TupleSet>(bounds.relations().size());
    for (Relation r : bounds.relations()) {
        if (r.isAtom() && ignoreAllAtomRelsExcept != null && !ignoreAllAtomRelsExcept.contains(r))
            continue;
        if (ignoreRels != null && ignoreRels.contains(r))
            continue;
        final TupleSet lower = bounds.lowerBound(r);
        final TupleSet upper = bounds.upperBound(r);
        if (!lower.isEmpty() && lower.size() < upper.size()) {
            sets.add(lower);
        }
        if (!upper.isEmpty()) {
            sets.add(upper);
        }
    }
    final TupleSet[] sorted = sets.toArray(new TupleSet[sets.size()]);
    Arrays.sort(sorted, new Comparator<TupleSet>() {

        @Override
        public int compare(TupleSet o1, TupleSet o2) {
            return o1.size() - o2.size();
        }
    });
    return sorted;
}
 
Example 3
Source File: ConfigAssure.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Displays an instance obtained with the given options.
 *
 * @requires inst != null and opt != null
 */
private final void display(Instance inst, Options opt) {
    final Universe univ = inst.universe();
    final Evaluator eval = new Evaluator(inst, opt);
    final TupleFactory factory = univ.factory();
    final List<TupleSet> subnets = new ArrayList<TupleSet>();

    System.out.println("address\t\tnetwork id\tmask\tdevice-interface");
    for (int i = 0, ports = univ.size() - 32; i < ports; i++) {
        final Object atom = univ.atom(i);
        final Relation p = Relation.unary(atom.toString());
        inst.add(p, factory.setOf(atom));

        System.out.print(toQuad(eval.evaluate(addr(p))) + "\t");
        System.out.print(toQuad(eval.evaluate(netid(p))) + "\t");
        System.out.print(eval.evaluate(implicitMask(p)) + "\t");
        System.out.println(p);

        final TupleSet members = eval.evaluate(subnet(p));
        if (!members.isEmpty())
            subnets.add(members);
    }

    System.out.println("\nsubnets:");
    for (TupleSet sub : subnets) {
        System.out.println(sub);
    }

}
 
Example 4
Source File: SolutionIterator.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the trivial solution corresponding to the trivial translation stored
 * in {@code this.translation}, and if {@code this.translation.cnf.solve()} is
 * true, sets {@code this.translation} to a new translation that eliminates the
 * current trivial solution from the set of possible solutions. The latter has
 * the effect of forcing either the translator or the solver to come up with the
 * next solution or return UNSAT. If {@code this.translation.cnf.solve()} is
 * false, sets {@code this.translation} to null.
 *
 * @requires this.translation != null
 * @ensures this.translation is modified to eliminate the current trivial
 *          solution from the set of possible solutions
 * @return current solution
 */
private Solution nextTrivialSolution() {
    final Translation.Whole transl = this.translation;

    final Solution sol = Solver.trivial(transl, translTime); // this also
                                                            // frees up
                                                            // solver
                                                            // resources,
                                                            // if unsat

    if (sol.instance() == null) {
        translation = null; // unsat, no more solutions
    } else {
        trivial++;

        final Bounds bounds = transl.bounds();
        final Bounds newBounds = bounds.clone();
        final List<Formula> changes = new ArrayList<Formula>();

        for (Relation r : bounds.relations()) {
            final TupleSet lower = bounds.lowerBound(r);

            if (lower != bounds.upperBound(r)) { // r may change
                if (lower.isEmpty()) {
                    changes.add(r.some());
                } else {
                    final Relation rmodel = Relation.nary(r.name() + "_" + trivial, r.arity());
                    newBounds.boundExactly(rmodel, lower);
                    changes.add(r.eq(rmodel).not());
                }
            }
        }

        // nothing can change => there can be no more solutions (besides the
        // current trivial one).
        // note that transl.formula simplifies to the constant true with
        // respect to
        // transl.bounds, and that newBounds is a superset of transl.bounds.
        // as a result, finding the next instance, if any, for
        // transl.formula.and(Formula.or(changes))
        // with respect to newBounds is equivalent to finding the next
        // instance of Formula.or(changes) alone.
        final Formula formula = changes.isEmpty() ? Formula.FALSE : Formula.or(changes);

        final long startTransl = System.currentTimeMillis();
        translation = Translator.translate(formula, newBounds, transl.options());
        translTime += System.currentTimeMillis() - startTransl;
    }
    return sol;
}
 
Example 5
Source File: Solver.java    From kodkod with MIT License 4 votes vote down vote up
/**
 * Returns the trivial solution corresponding to the trivial translation stored in {@code this.translation},
 * and if {@code this.translation.cnf.solve()} is true, sets {@code this.translation} to a new translation 
 * that eliminates the current trivial solution from the set of possible solutions.  The latter has the effect 
 * of forcing either the translator or the solver to come up with the next solution or return UNSAT.
 * If {@code this.translation.cnf.solve()} is false, sets {@code this.translation} to null.
 * @requires this.translation != null
 * @ensures this.translation is modified to eliminate the current trivial solution from the set of possible solutions
 * @return current solution
 */
private Solution nextTrivialSolution() {
	final Translation.Whole transl = this.translation;
	
	final Solution sol = trivial(transl, translTime); // this also frees up solver resources, if unsat
	
	if (sol.instance()==null) {
		translation = null; // unsat, no more solutions
	} else {
		trivial++;
		
		final Bounds bounds = transl.bounds();
		final Bounds newBounds = bounds.clone();
		final List<Formula> changes = new ArrayList<Formula>();

		for(Relation r : bounds.relations()) {
			final TupleSet lower = bounds.lowerBound(r); 
			
			if (lower != bounds.upperBound(r)) { // r may change
				if (lower.isEmpty()) { 
					changes.add(r.some());
				} else {
					final Relation rmodel = Relation.nary(r.name()+"_"+trivial, r.arity());
					newBounds.boundExactly(rmodel, lower);	
					changes.add(r.eq(rmodel).not());
				}
			}
		}
		
		// nothing can change => there can be no more solutions (besides the current trivial one).
		// note that transl.formula simplifies to the constant true with respect to 
		// transl.bounds, and that newBounds is a superset of transl.bounds.
		// as a result, finding the next instance, if any, for transl.formula.and(Formula.or(changes)) 
		// with respect to newBounds is equivalent to finding the next instance of Formula.or(changes) alone.
		final Formula formula = changes.isEmpty() ? Formula.FALSE : Formula.or(changes);
		
		final long startTransl = System.currentTimeMillis();
		translation = Translator.translate(formula, newBounds, transl.options());
		translTime += System.currentTimeMillis() - startTransl;
	} 
	return sol;
}