Java Code Examples for kodkod.ast.Relation#totalOrder()

The following examples show how to use kodkod.ast.Relation#totalOrder() . 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 5 votes vote down vote up
/**
 * Calls lookup(pred) 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 { p: RelationPredicate | p.name = pred.name && p.relation =
 *         pred.relation.accept(delegate) && p.name = FUNCTION => p.targetMult =
 *         pred.targetMult && p.domain = pred.domain.accept(delegate) && p.range
 *         = pred.range.accept(delegate), p.name = TOTAL_ORDERING => p.ordered =
 *         pred.ordered.accept(delegate) && p.first =
 *         pred.first.accept(delegate) && p.last = pred.last.accept(delegate) }
 */
@Override
public Formula visit(RelationPredicate pred) {
    Formula ret = lookup(pred);
    if (ret != null)
        return ret;

    final Relation r = (Relation) pred.relation().accept(delegate);
    switch (pred.name()) {
        case ACYCLIC :
            ret = (r == pred.relation()) ? pred : r.acyclic();
            break;
        case FUNCTION :
            final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
            final Expression domain = fp.domain().accept(delegate);
            final Expression range = fp.range().accept(delegate);
            ret = (r == fp.relation() && domain == fp.domain() && range == fp.range()) ? fp : (fp.targetMult() == Multiplicity.ONE ? r.function(domain, range) : r.partialFunction(domain, range));
            break;
        case TOTAL_ORDERING :
            final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
            final Relation ordered = (Relation) tp.ordered().accept(delegate);
            final Relation first = (Relation) tp.first().accept(delegate);
            final Relation last = (Relation) tp.last().accept(delegate);
            ret = (r == tp.relation() && ordered == tp.ordered() && first == tp.first() && last == tp.last()) ? tp : r.totalOrder(ordered, first, last);
            break;
        default :
            throw new IllegalArgumentException("unknown relation predicate: " + pred.name());
    }
    return cache(pred, ret);
}
 
Example 2
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
public final void testMana_01312007() {
    final Relation A = Relation.unary("A");
    final Relation first1 = Relation.unary("first1");
    final Relation first2 = Relation.unary("first2");
    final Relation last1 = Relation.unary("last1");
    final Relation last2 = Relation.unary("last2");
    final Relation next1 = Relation.binary("next1");
    final Relation next2 = Relation.binary("next2");

    final Formula f0 = next1.totalOrder(A, first1, last1);
    final Formula f1 = next2.totalOrder(A, first2, last2);
    final Formula f2 = first1.eq(last2);

    final Formula f3 = f0.and(f1).and(f2);

    final Universe u = new Universe(Arrays.asList("a0", "a1", "a2"));
    final TupleFactory f = u.factory();
    final Bounds b = new Bounds(u);

    b.bound(A, f.allOf(1));
    b.bound(first1, f.allOf(1));
    b.bound(first2, f.allOf(1));
    b.bound(last1, f.allOf(1));
    b.bound(last2, f.allOf(1));
    b.bound(next1, f.allOf(2));
    b.bound(next2, f.allOf(2));

    final Solver solver = new Solver();
    final Solution sol = solver.solve(f3, b);

    assertEquals(Solution.Outcome.SATISFIABLE, sol.outcome());
}
 
Example 3
Source File: AbstractReplacer.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(pred) 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 { p: RelationPredicate | p.name = pred.name && p.relation = pred.relation.accept(this) &&
 *                                  p.name = FUNCTION => p.targetMult = pred.targetMult && 
 *                                                       p.domain = pred.domain.accept(this) &&
 *                                                       p.range = pred.range.accept(this),
 *                                  p.name = TOTAL_ORDERING => p.ordered = pred.ordered.accept(this) &&
 *                                                             p.first = pred.first.accept(this) &&
 *                                                             p.last = pred.last.accept(this) }
 */
public Formula visit(RelationPredicate pred) {
	Formula ret = lookup(pred);
	if (ret!=null) return ret;

	final Relation r = (Relation)pred.relation().accept(this);
	switch(pred.name()) {
	case ACYCLIC :  
		ret = (r==pred.relation()) ? pred : r.acyclic(); 
		break;
	case FUNCTION :
		final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
		final Expression domain = fp.domain().accept(this);
		final Expression range = fp.range().accept(this);
		ret = (r==fp.relation() && domain==fp.domain() && range==fp.range()) ?
				fp : 
				(fp.targetMult()==Multiplicity.ONE ? r.function(domain, range) : r.partialFunction(domain,range));
		break;
	case TOTAL_ORDERING : 
		final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
		final Relation ordered = (Relation) tp.ordered().accept(this);
		final Relation first = (Relation)tp.first().accept(this);
		final Relation last = (Relation)tp.last().accept(this);
		ret = (r==tp.relation() && ordered==tp.ordered() && first==tp.first() && last==tp.last()) ? 
				tp : r.totalOrder(ordered, first, last);
		break;
	default :
		throw new IllegalArgumentException("unknown relation predicate: " + pred.name());
	}
	return cache(pred,ret);
}
 
Example 4
Source File: RegressionTests.java    From kodkod with MIT License 5 votes vote down vote up
@Test
public final void testMana_01312007() {
	final Relation A = Relation.unary("A");
	final Relation first1 = Relation.unary("first1");
	final Relation first2 = Relation.unary("first2");
	final Relation last1 = Relation.unary("last1");
	final Relation last2 = Relation.unary("last2");
	final Relation next1 = Relation.binary("next1");
	final Relation next2 = Relation.binary("next2");

	final Formula f0 = next1.totalOrder(A, first1, last1);
	final Formula f1 = next2.totalOrder(A, first2, last2);
	final Formula f2 = first1.eq(last2);

	final Formula f3 = f0.and(f1).and(f2);

	final Universe u = new Universe(Arrays.asList("a0","a1","a2"));
	final TupleFactory f = u.factory();
	final Bounds b = new Bounds(u);

	b.bound(A, f.allOf(1));
	b.bound(first1, f.allOf(1));
	b.bound(first2, f.allOf(1));
	b.bound(last1, f.allOf(1));
	b.bound(last2, f.allOf(1));
	b.bound(next1, f.allOf(2));
	b.bound(next2, f.allOf(2));

	final Solver solver = new Solver();
	final Solution sol = solver.solve(f3, b);

	assertEquals(Solution.Outcome.SATISFIABLE, sol.outcome());
}