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

The following examples show how to use kodkod.ast.Relation#binary() . 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: Transpose4x4UnaryLR.java    From kodkod with MIT License 6 votes vote down vote up
public Transpose4x4UnaryLR() {
	for(int i = 0; i < 4; i++) {
		sl[i] = Relation.unary("sl[" + i + "]");	
		tl[i] = Relation.unary("tl[" + i + "]");
		
		mx1[i] = Relation.unary("mx1[" + i + "]");
		mx2[i] = Relation.unary("mx2[" + i + "]");		
				
		sx1[i] = Relation.unary("sx1[" + i + "]");
		sx2[i] = Relation.unary("sx2[" + i + "]");
			
		for(int j = 0; j < 4; j++) {
			mi[i][j] = Relation.unary("mi[" + i + ", " + j + "]");
			si[i][j] = Relation.unary("si[" + i + ", " + j + "]");
		}
	}
	this.idx = Relation.unary("idx");
	this.succ = Relation.binary("succ");	
	for(int i = 0; i <= 16; i++) {
		ints[i] = IntConstant.constant(i).toExpression();
	}
	
}
 
Example 2
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
public final void testEmina_01232006() {
    final List<String> atoms = new ArrayList<String>(5);
    for (int i = 0; i < 5; i++)
        atoms.add("a" + i);
    final Universe u = new Universe(atoms);
    final TupleFactory tf = u.factory();

    final Relation r1 = Relation.unary("r1"), r2 = Relation.binary("r2"), r3 = Relation.ternary("r3");
    final Bounds b = new Bounds(u);
    final TupleSet r2Bound = tf.noneOf(2);
    for (int i = 0; i < 4; i++)
        r2Bound.add(tf.tuple(atoms.get(i), atoms.get(i)));
    r2Bound.add(tf.tuple("a4", "a1"));
    r2Bound.add(tf.tuple("a4", "a2"));
    b.bound(r2, r2Bound);
    b.bound(r1, tf.setOf("a0", "a3"), tf.setOf("a0", "a3", "a4"));
    b.bound(r3, tf.setOf(tf.tuple("a0", "a0", "a0"), tf.tuple("a3", "a3", "a3")));
    final Formula f = r1.product(r2).in(r3);

    final Instance instance = solver.solve(f, b).instance();
    assertTrue((new Evaluator(instance)).evaluate(f));
    // System.out.println(instance);
    // System.out.println((new Evaluator(instance)).evaluate(f ));

}
 
Example 3
Source File: Transpose4x4UnaryL.java    From kodkod with MIT License 6 votes vote down vote up
public Transpose4x4UnaryL() {
	for(int i = 0; i < 4; i++) {
		mx1[i] = Relation.unary("mx1[" + i + "]");
		mx2[i] = Relation.unary("mx2[" + i + "]");			
		sx1[i] = Relation.unary("sx1[" + i + "]");
		sx2[i] = Relation.unary("sx2[" + i + "]");
		for(int j = 0; j < 4; j++) {
			mi[i][j] = Relation.unary("mi[" + i + ", " + j + "]");
			si[i][j] = Relation.unary("si[" + i + ", " + j + "]");
		}
	}
	this.succ = Relation.binary("succ");	
	for(int i = 0; i < 16; i++) {
		ints[i] = IntConstant.constant(i).toExpression();
	}
	
}
 
Example 4
Source File: ConfigAssure.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new instance of ConfigAssure.
 */
public ConfigAssure() {
    this.port = Relation.unary("port");
    this.unknown = Relation.unary("unknown");
    this.addr = Relation.binary("addr");
    this.groupMask = Relation.binary("groupMask");
    this.subnet = Relation.binary("subnet");
    this.group = Relation.binary("group");
    this.mask = group.join(groupMask);
}
 
Example 5
Source File: DiffEg.java    From kodkod with MIT License 5 votes vote down vote up
public DiffEg() {
	Subject = Relation.unary("Subject");
	Resource = Relation.unary("Resource");
	Action = Relation.unary("Action");
	Request = Relation.unary("Request");
	Conflicted = Relation.unary("Conflicted");
	sRequest = Relation.binary("s");
	rRequest = Relation.binary("r");
	action = Relation.binary("a");
	sConflicted = Relation.binary("s");
	rConflicted = Relation.binary("r");
}
 
Example 6
Source File: SkolemizationTest.java    From kodkod with MIT License 5 votes vote down vote up
public SkolemizationTest() {
	this.solver = new Solver();
	List<String> atoms = new ArrayList<String>(USIZE);
	for (int i = 0; i < USIZE; i++) {
		atoms.add(""+i);
	}
	final Universe universe = new Universe(atoms);
	this.factory = universe.factory();
	this.r1a = Relation.unary("r1a");
	this.r1b = Relation.unary("r1b");
	this.r2a = Relation.binary("r2a");
	this.r2b = Relation.binary("r2b");
	this.bounds = new Bounds(universe);
}
 
Example 7
Source File: Dijkstra.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an instance of Dijkstra example.
 */
public Dijkstra() {
    Process = Relation.unary("Process");
    Mutex = Relation.unary("Mutex");
    State = Relation.unary("State");
    holds = Relation.ternary("holds");
    waits = Relation.ternary("waits");
    sfirst = Relation.unary("sfirst");
    slast = Relation.unary("slast");
    sord = Relation.binary("sord");
    mfirst = Relation.unary("mfirst");
    mlast = Relation.unary("mlast");
    mord = Relation.binary("mord");
}
 
Example 8
Source File: RegressionTests.java    From kodkod with MIT License 5 votes vote down vote up
@Test
public final void testGreg_03032006() {
	Relation r = Relation.binary("r");
	Universe univ = new Universe(Collections.singleton("o"));
	Bounds bounds = new Bounds(univ);
	bounds.bound(r,  univ.factory().allOf(2));

	assertEquals(Solution.Outcome.SATISFIABLE, solver.solve(r.some(), bounds).outcome());

}
 
Example 9
Source File: LAT258.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs a new instance of LAT258.
 */
public LAT258() {
	goal = Relation.unary("goal");
	p = Relation.unary("p");
	t = Relation.unary("t");
	u = Relation.unary("u");
	v = Relation.unary("v");
	w = Relation.unary("w");
	x = Relation.unary("x");
	y = Relation.unary("y");
	z = Relation.unary("z");
	lessThan = Relation.binary("lessThan");
	meet = Relation.ternary("meet");
	join = Relation.ternary("join");
}
 
Example 10
Source File: LAT258.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new instance of LAT258.
 */
public LAT258() {
    goal = Relation.unary("goal");
    p = Relation.unary("p");
    t = Relation.unary("t");
    u = Relation.unary("u");
    v = Relation.unary("v");
    w = Relation.unary("w");
    x = Relation.unary("x");
    y = Relation.unary("y");
    z = Relation.unary("z");
    lessThan = Relation.binary("lessThan");
    meet = Relation.ternary("meet");
    join = Relation.ternary("join");
}
 
Example 11
Source File: GEO158.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new instance of GEO0040.
 */
public GEO158() {
    super();
    partOf = Relation.binary("partOf");
    incident = Relation.binary("incident");
    sum = Relation.ternary("sum");
    endPoint = Relation.binary("endPoint");
    closed = Relation.unary("Closed");
    open = Relation.unary("Open");
    curve = Relation.unary("Curve");
    point = Relation.unary("Point");
    meet = Relation.ternary("meet");
    innerPoint = Relation.binary("innerPoint");
}
 
Example 12
Source File: CeilingsAndFloors.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an instance of the ceilings and floors class.
 */
public CeilingsAndFloors() {
    Platform = Relation.unary("Platform");
    Man = Relation.unary("Man");
    ceiling = Relation.binary("ceiling");
    floor = Relation.binary("floor");
}
 
Example 13
Source File: SET948.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs a new instance of SET948.
 */
public SET948() {
	empty = Relation.unary("empty");
	subset = Relation.binary("subset");
	in = Relation.binary("in");
	disjoint = Relation.binary("disjoint");
	union = Relation.binary("union");
	intersect2 = Relation.ternary("set_intersection2");
	union2 = Relation.ternary("set_union2");
}
 
Example 14
Source File: COM008.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs a new instance of COM008.
 */
public COM008() {
	equalish = Relation.binary("equalish");
	rewrite = Relation.binary("rewrite");
	trr = Relation.binary("trr");
	a = Relation.unary("a");
	b = Relation.unary("b");
	c = Relation.unary("c");
	goal = Relation.unary("goal");
	Atom = Relation.unary("Atom");
}
 
Example 15
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
public final void testGreg_03032006() {
    Relation r = Relation.binary("r");
    Universe univ = new Universe(Collections.singleton("o"));
    Bounds bounds = new Bounds(univ);
    bounds.bound(r, univ.factory().allOf(2));

    assertEquals(Solution.Outcome.SATISFIABLE, solver.solve(r.some(), bounds).outcome());

}
 
Example 16
Source File: ToyLists.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs a new instance of the toylists problem.
 */
public ToyLists() {
	list = Relation.unary("List");
	nonEmptyList = Relation.unary("NonEmptyList");
	emptyList  = Relation.unary("EmptyList");
	thing = Relation.unary("Thing");
	equivTo = Relation.binary("equivTo");
	prefixes = Relation.binary("prefixes");
	car = Relation.binary("car");
	cdr = Relation.binary("cdr");
}
 
Example 17
Source File: NUM378.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs a new instance of NUM378.
 */
public NUM378() {
    succ = Relation.binary("succ");
    sum = Relation.ternary("sum");
}
 
Example 18
Source File: RegressionTests.java    From kodkod with MIT License 4 votes vote down vote up
@Test
public final void testGreg_01192006() {
	//		 circular linked list
	Relation Entry = Relation.unary("Entry");
	Relation head = Relation.unary("head");
	Relation next = Relation.binary("next");
	Formula nextFun = next.function(Entry, Entry);

	//		 bijection between indices and entries in linked list
	Relation Index = Relation.unary("Index");
	Relation index2Entry = Relation.binary("index2Entry");
	Expression entries = head.join(next.closure());
	Variable e = Variable.unary("e");
	Expression preImage = index2Entry.join(e);
	Formula index2EntryBij = e.in(entries).implies(preImage.one()).and(
			e.in(entries).not().implies(preImage.no())).forAll(e.oneOf(Entry));

	//		 try to force list to have three distinct entries
	Variable e1 = Variable.unary("e1");
	Variable e2 = Variable.unary("e2");
	Variable e3 = Variable.unary("e3");
	Formula threeEntries =
		e1.eq(e2).not().and(e1.eq(e3).not()).and(e2.eq(e3).not()).
		forSome(e1.oneOf(entries).and(e2.oneOf(entries).and(e3.oneOf(entries))));
	Formula simulate = head.one().and(nextFun).and(index2EntryBij).and(threeEntries);

	Object Entry0 = "Entry0";
	Object Entry1 = "Entry1";
	Object Entry2 = "Entry2";
	Object Entry3 = "Entry3";
	Object Index0 = "Index0";
	Object Index1 = "Index1";
	Object Index2 = "Index2";
	Object Index3 = "Index3";

	Universe univ = new Universe(
			Arrays.asList(Entry0, Entry1, Entry2, Entry3,
					Index0, Index1, Index2, Index3));
	TupleFactory factory = univ.factory();
	TupleSet entryTuples = factory.setOf(Entry0, Entry1, Entry2, Entry3);
	TupleSet indexTuples = factory.setOf(Index0, Index1, Index2, Index3);

	Instance instance = new Instance(univ);
	instance.add(Entry, entryTuples);
	instance.add(head, factory.setOf(Entry0));
	instance.add(Index, indexTuples);

	Tuple next0 = factory.tuple(Entry0, Entry1);
	Tuple next1 = factory.tuple(Entry1, Entry2);
	Tuple next2 = factory.tuple(Entry2, Entry3);
	Tuple next3 = factory.tuple(Entry3, Entry0);
	instance.add(next, factory.setOf(next0, next1, next2, next3));

	Tuple i2e0 = factory.tuple(Index0, Entry0);
	Tuple i2e1 = factory.tuple(Index1, Entry1);
	Tuple i2e2 = factory.tuple(Index2, Entry2);
	Tuple i2e3 = factory.tuple(Index3, Entry3);
	instance.add(index2Entry, factory.setOf(i2e0, i2e1, i2e2, i2e3));

	Evaluator eval = new Evaluator(instance);
	assertTrue(eval.evaluate(simulate));

	Bounds bounds = new Bounds(univ);
	bounds.boundExactly(Entry, entryTuples);
	bounds.bound(head, entryTuples);
	bounds.bound(next, entryTuples.product(entryTuples));
	bounds.bound(Index, indexTuples);
	bounds.bound(index2Entry, indexTuples.product(entryTuples));
	//		Solver solver = new Solver(SATSolverName.Default);

	//			System.out.println(simulate);
	//			System.out.println(bounds);
	//			System.out.println(instance);
	instance = solver.solve(simulate, bounds).instance();
	//			System.out.println(instance);
	assertNotNull(instance);

}
 
Example 19
Source File: TranslatorTest.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testFlattening() {
    final List<String> atoms = new ArrayList<String>(9);
    atoms.add("-1");
    atoms.add("0");
    atoms.add("1");
    atoms.add("null");
    for (int i = 0; i < 5; i++) {
        atoms.add("m" + i);
    }
    final Universe u = new Universe(atoms);
    final TupleFactory t = u.factory();

    final Relation[] m = new Relation[5];
    for (int i = 0; i < 5; i++) {
        m[i] = Relation.unary("m" + i);
    }

    final Relation univ = Relation.unary("univ"), none = Relation.unary("none"), iden = Relation.binary("inden"),
                    mutant = Relation.unary("mutant"), inc = Relation.binary("inc"), add = Relation.ternary("add"),
                    i0 = Relation.unary("i0"), zero = Relation.unary("0"), one = Relation.unary("1"),
                    ints = Relation.unary("int");

    final Bounds b = new Bounds(u);

    b.boundExactly(univ, t.allOf(1));
    b.boundExactly(none, t.noneOf(1));
    TupleSet idenSet = t.noneOf(2);
    for (String atom : atoms) {
        idenSet.add(t.tuple(atom, atom));
    }
    b.boundExactly(iden, idenSet);

    b.bound(mutant, t.range(t.tuple("m0"), t.tuple("m4")));
    for (int i = 0; i < 5; i++) {
        b.boundExactly(m[i], t.setOf("m" + i));
    }

    b.bound(i0, t.range(t.tuple("-1"), t.tuple("1")));
    b.boundExactly(zero, t.setOf(t.tuple("0")));
    b.boundExactly(one, t.setOf(t.tuple("1")));
    b.boundExactly(ints, t.allOf(1));
    b.boundExactly(inc, t.setOf(t.tuple("-1", "0"), t.tuple("0", "1")));

    // [1, 1, -1], [1, -1, 0], [1, 0, 1], [-1, 1, 0], [-1, -1, 1],
    // [-1, 0, -1], [0, 1, 1], [0, -1, -1], [0, 0, 0]]
    b.boundExactly(add, t.setOf(t.tuple("1", "1", "-1"), t.tuple("1", "-1", "0"), t.tuple("1", "0", "1"), t.tuple("-1", "1", "0"), t.tuple("-1", "-1", "1"), t.tuple("-1", "0", "-1"), t.tuple("0", "1", "1"), t.tuple("0", "-1", "-1"), t.tuple("0", "0", "0")));

    /*
     * ((one i0 && one mutant) && !((if (i0 in (0 . ^~inc)) then ((add . 0) . i0)
     * else i0) = (if !((if (mutant in m4) then (if (i0 in 0) then 1 else 0) else
     * (if (mutant in m3) then (if ((i0 = 0) || (i0 in (0 . ^~inc))) then 1 else 0)
     * else (if (mutant in m2) then (if (i0 in (0 . ^inc)) then 1 else 0) else (if
     * (mutant in m1) then (if ((i0 = 0) || (i0 in (0 . ^inc))) then 1 else 0) else
     * (if (mutant in m0) then (if !(i0 in 0) then 1 else 0) else (if (i0 in (0 .
     * ^~inc)) then 1 else 0)))))) in 0) then ((add . 0) . i0) else i0)))
     */

    final Formula[] cm = new Formula[5];
    for (int i = 0; i < 5; i++) {
        cm[i] = mutant.in(m[i]);
    }

    // (i0 in (0 . ^~inc))
    final Formula fs0 = i0.in(zero.join(inc.transpose().closure()));
    // (i0 in (0 . ^inc))
    final Formula fs1 = i0.in(zero.join(inc.closure()));
    // (i0 = 0)
    final Formula fs2 = i0.eq(zero);

    final Expression em0 = cm[0].thenElse(i0.in(zero).not().thenElse(one, zero), fs0.thenElse(one, zero));
    final Expression em1 = cm[1].thenElse((fs2.or(fs1)).thenElse(one, zero), em0);
    final Expression em2 = cm[2].thenElse(fs1.thenElse(one, zero), em1);
    final Expression em3 = cm[3].thenElse(fs2.or(fs0).thenElse(one, zero), em2);
    final Expression em4 = cm[4].thenElse(i0.in(zero).thenElse(one, zero), em3);

    final Expression es1 = add.join(zero).join(i0);
    final Expression expr2 = em4.in(zero).not().thenElse(es1, i0);
    final Expression expr1 = fs0.thenElse(es1, i0);

    final Formula f = i0.one().and(mutant.one()).and(expr1.eq(expr2).not());

    final Instance instance = solver.solve(f, b).instance();
    assertNotNull(instance);

    // System.out.println(instance);

}
 
Example 20
Source File: SymmetryBreaker.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
/**
 * If possible, breaks symmetry on the given total ordering predicate and
 * returns a formula f such that the meaning of total with respect to
 * this.bounds is equivalent to the meaning of f with respect to this.bounds'.
 * If symmetry cannot be broken on the given predicate, returns null.
 * <p>
 * We break symmetry on the relation constrained by the given predicate iff
 * total.first, total.last, and total.ordered have the same upper bound, which,
 * when cross-multiplied with itself gives the upper bound of total.relation.
 * Assuming that this is the case, we then break symmetry on total.relation,
 * total.first, total.last, and total.ordered using one of the methods described
 * in {@linkplain #breakMatrixSymmetries(Map, boolean)}; the method used depends
 * on the value of the "agressive" flag. The partition that formed the upper
 * bound of total.ordered is removed from this.symmetries.
 * </p>
 *
 * @return null if symmetry cannot be broken on total; otherwise returns a
 *         formula f such that the meaning of total with respect to this.bounds
 *         is equivalent to the meaning of f with respect to this.bounds'
 * @ensures this.symmetries and this.bounds are modified as desribed in
 *          {@linkplain #breakMatrixSymmetries(Map, boolean)} iff total.first,
 *          total.last, and total.ordered have the same upper bound, which, when
 *          cross-multiplied with itself gives the upper bound of total.relation
 * @see #breakMatrixSymmetries(Map,boolean)
 */
private final Formula breakTotalOrder(RelationPredicate.TotalOrdering total, boolean aggressive) {
    final Relation first = total.first(), last = total.last(), ordered = total.ordered(),
                    relation = total.relation();
    final IntSet domain = bounds.upperBound(ordered).indexView();

    if (symmetricColumnPartitions(ordered) != null && bounds.upperBound(first).indexView().contains(domain.min()) && bounds.upperBound(last).indexView().contains(domain.max())) {

        // construct the natural ordering that corresponds to the ordering
        // of the atoms in the universe
        final IntSet ordering = Ints.bestSet(usize * usize);
        int prev = domain.min();
        for (IntIterator atoms = domain.iterator(prev + 1, usize); atoms.hasNext();) {
            int next = atoms.next();
            ordering.add(prev * usize + next);
            prev = next;
        }

        if (ordering.containsAll(bounds.lowerBound(relation).indexView()) && bounds.upperBound(relation).indexView().containsAll(ordering)) {

            // remove the ordered partition from the set of symmetric
            // partitions
            removePartition(domain.min());

            final TupleFactory f = bounds.universe().factory();

            if (aggressive) {
                bounds.boundExactly(first, f.setOf(f.tuple(1, domain.min())));
                bounds.boundExactly(last, f.setOf(f.tuple(1, domain.max())));
                bounds.boundExactly(ordered, bounds.upperBound(total.ordered()));
                bounds.boundExactly(relation, f.setOf(2, ordering));

                return Formula.TRUE;

            } else {
                final Relation firstConst = Relation.unary("SYM_BREAK_CONST_" + first.name());
                final Relation lastConst = Relation.unary("SYM_BREAK_CONST_" + last.name());
                final Relation ordConst = Relation.unary("SYM_BREAK_CONST_" + ordered.name());
                final Relation relConst = Relation.binary("SYM_BREAK_CONST_" + relation.name());
                bounds.boundExactly(firstConst, f.setOf(f.tuple(1, domain.min())));
                bounds.boundExactly(lastConst, f.setOf(f.tuple(1, domain.max())));
                bounds.boundExactly(ordConst, bounds.upperBound(total.ordered()));
                bounds.boundExactly(relConst, f.setOf(2, ordering));

                return Formula.and(first.eq(firstConst), last.eq(lastConst), ordered.eq(ordConst), relation.eq(relConst));
                // return first.eq(firstConst).and(last.eq(lastConst)).and(
                // ordered.eq(ordConst)).and( relation.eq(relConst));
            }

        }
    }

    return null;
}