kodkod.instance.Tuple Java Examples
The following examples show how to use
kodkod.instance.Tuple.
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: FloatTestBase.java From quetzal with Eclipse Public License 2.0 | 6 votes |
private Instance instance(Formula f, Set<Relation> vars) { int msb = bitWidth - 1; Universe u = new Universe(atoms); TupleFactory tf = u.factory(); Bounds b = new Bounds(u); for(int i = 0; i < msb; i++) { Integer v = Integer.valueOf(1<<i); b.boundExactly(v.intValue(), tf.setOf(v)); } b.boundExactly(-(1<<msb), tf.setOf(Integer.valueOf(-(1<<msb)))); Set<Tuple> digits = HashSetFactory.make(); for(Object o : atoms) { digits.add(tf.tuple(o)); } for(Relation r : vars) { b.bound(r, tf.setOf(digits)); } Solver solver = new Solver(options); return solver.solve(f, b).instance(); }
Example #2
Source File: SudokuParser.java From kodkod with MIT License | 6 votes |
/** * Returns a pretty-printed string of the given sudoku solution. * @requires solution is a valid sudoku solution * @requires some r: int | solution.universe = { i: Integer | 1 <= i.intValue() <= r*r } * @return a pretty-printed string of the given sudoku solution */ public static final String prettyPrint(TupleSet solution) { final StringBuilder str = new StringBuilder(); final int n = solution.universe().size(); final int r = (int)Math.sqrt(n); appendDivider(str, r); final Iterator<Tuple> psol = solution.iterator(); for(int i = 1; i <= n; i++) { str.append("| "); for(int j = 0; j < r; j++) { for(int k = 0; k < r; k++) { final int atom = (Integer)psol.next().atom(2); if (atom<10&&r>3) str.append(" "); str.append(atom); str.append(" "); } str.append("| "); } str.append("\n"); if (i%r==0) appendDivider(str, r); } return str.toString(); }
Example #3
Source File: EvaluatorTest.java From kodkod with MIT License | 6 votes |
@Test public final void testEvalProduct() { // Hilary->Jocelyn = Hilary->Jocelyn final Set<Tuple> hilaryAndJocelyn = eval(hilary.product(jocelyn)); final Tuple hj = hilaryAndJocelyn.iterator().next(); assertEquals(hilaryAndJocelyn.size(), 1); assertEquals(hj.atom(0), value(hilary).iterator().next().atom(0)); assertEquals(hj.atom(1), value(jocelyn).iterator().next().atom(0)); // Person->(spouse->shaken) = (Person->spouse)->shaken assertEquals(eval(person.product(spouse.product(shaken))), eval(person.product(spouse).product(shaken))); // Person->(spouse + shaken) = Person->spouse + Person->shaken assertEquals(eval(person.product(spouse.union(shaken))), eval(person.product(spouse).union(person.product(shaken)))); // arity(spouse->shaken) = 4 assertEquals(spouse.product(shaken).arity(), 4); }
Example #4
Source File: EvaluatorTest.java From kodkod with MIT License | 6 votes |
@Test public final void testEvalUnion() { // Hilary + Hilary = Hilary assertEquals(eval(hilary.union(hilary)), value(hilary)); // Hilary + Jocelyn + Person = Person assertEquals(eval(hilary.union(jocelyn).union(person)), value(person)); // spouse + shaken = spouse + shaken Set<Tuple> spousePlusShaken = new HashSet<Tuple>(); spousePlusShaken.addAll(value(spouse)); spousePlusShaken.addAll(value(shaken)); assertEquals(eval(spouse.union(shaken)), spousePlusShaken); // shaken + spouse = spouse + shaken assertEquals(eval(shaken.union(spouse)), spousePlusShaken); // spouse + Person = arity mismatch try { eval(spouse.union(person)); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException iae) {} }
Example #5
Source File: TranslateKodkodToJava.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** Print the tupleset using the name n. */ private void printTupleset(String n, TupleSet ts, Map<Object,String> atomMap) { file.printf("TupleSet %s = factory.noneOf(%d);%n", n, ts.arity()); for (Tuple t : ts) { file.printf("%s.add(", n); for (int i = 0; i < ts.arity(); i++) { if (i != 0) file.printf(".product("); Object a = t.atom(i); String b = atomMap == null ? null : atomMap.get(a); file.printf("factory.tuple(\"%s\")", (b == null ? a.toString() : b)); if (i != 0) file.printf(")"); } file.printf(");%n"); } }
Example #6
Source File: BoundsComputer.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * Computes the lowerbound from bottom-up; it will also set a suitable initial * value for each sig's upperbound. Precondition: sig is not a builtin sig */ private TupleSet computeLowerBound(List<Tuple> atoms, final PrimSig sig) throws Err { int n = sc.sig2scope(sig); TupleSet lower = factory.noneOf(1); for (PrimSig c : sig.children()) lower.addAll(computeLowerBound(atoms, c)); TupleSet upper = lower.clone(); boolean isExact = sc.isExact(sig); if (isExact || sig.isTopLevel()) for (n = n - upper.size(); n > 0; n--) { Tuple atom = atoms.remove(atoms.size() - 1); // If MUST<SCOPE and s is exact, then add fresh atoms to both // LOWERBOUND and UPPERBOUND. // If MUST<SCOPE and s is inexact but toplevel, then add fresh // atoms to the UPPERBOUND. if (isExact) lower.add(atom); upper.add(atom); } lb.put(sig, lower); ub.put(sig, upper); return lower; }
Example #7
Source File: EvaluatorTest.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
public final void testEvalUnion() { // Hilary + Hilary = Hilary assertEquals(eval(hilary.union(hilary)), value(hilary)); // Hilary + Jocelyn + Person = Person assertEquals(eval(hilary.union(jocelyn).union(person)), value(person)); // spouse + shaken = spouse + shaken Set<Tuple> spousePlusShaken = new HashSet<Tuple>(); spousePlusShaken.addAll(value(spouse)); spousePlusShaken.addAll(value(shaken)); assertEquals(eval(spouse.union(shaken)), spousePlusShaken); // shaken + spouse = spouse + shaken assertEquals(eval(shaken.union(spouse)), spousePlusShaken); // spouse + Person = arity mismatch try { eval(spouse.union(person)); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException iae) {} }
Example #8
Source File: A4SolutionReader.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** Parse tuple. */ private Tuple parseTuple(XMLNode tuple, int arity) throws Err { Tuple ans = null; try { for (XMLNode sub : tuple) if (sub.is("atom")) { Tuple x = factory.tuple(sub.getAttribute("label")); if (ans == null) ans = x; else ans = ans.product(x); } if (ans == null) throw new ErrorFatal("Expecting: <tuple> <atom label=\"..\"/> .. </tuple>"); if (ans.arity() != arity) throw new ErrorFatal("Expecting: tuple of arity " + arity + " but got tuple of arity " + ans.arity()); return ans; } catch (Throwable ex) { throw new ErrorFatal("Expecting: <tuple> <atom label=\"..\"/> .. </tuple>", ex); } }
Example #9
Source File: A4TupleSet.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * Returns a read-only iterator that iterates over each tuple in this TupleSet. */ @Override public Iterator<A4Tuple> iterator() { return new Iterator<A4Tuple>() { private final Iterator<Tuple> it = tuples.iterator(); @Override public final boolean hasNext() { return it.hasNext(); } @Override public final A4Tuple next() { if (!it.hasNext()) throw new NoSuchElementException(); return new A4Tuple(it.next(), sol); } @Override public final void remove() { throw new UnsupportedOperationException(); } }; }
Example #10
Source File: SudokuParser.java From kodkod with MIT License | 5 votes |
/** * Returns a string representation of the given puzzle. * @requires some r: int | puzzle.universe.atoms[int] = { i: Integer | 1 <= i.intValue() <= r*r } * @requires puzzle.arity = 3 * @return a string representation of the given puzzle */ public static final String toString(TupleSet puzzle) { final StringBuilder str = new StringBuilder(); final int n = puzzle.universe().size(); final String sep = (n>9) ? " " : ""; Iterator<Tuple> itr = puzzle.iterator(); if (!itr.hasNext()) { str.append(0); for(int i = 1, max = n*n; i < max; i++) { str.append(sep+0); } return str.toString(); } int last = 0; Tuple tuple = itr.next(); if ((Integer)tuple.atom(0)==1 && (Integer)tuple.atom(1)==1) { str.append(tuple.atom(2)); } else { str.append(0); itr = puzzle.iterator(); } while(itr.hasNext()) { tuple = itr.next(); final int current = n*((Integer)tuple.atom(0)-1) + ((Integer)tuple.atom(1)-1); for(int i = last+1; i < current; i++) { str.append(sep+0); } str.append(sep+tuple.atom(2)); last = current; } for(int i = last+1, max = n*n; i < max; i++) { str.append(sep+0); } return str.toString(); }
Example #11
Source File: SudokuParser.java From kodkod with MIT License | 5 votes |
/** * Returns the representation of the clues encoded in the given array as * an NxNxN tuple set drawn from the given universe. N is assumed * to be a perfect square, and the universe is assumed to consist of Integer objects in * the range [1..N]. The array is assumed to consist of N*N numbers, drawn * from the set 0..N, inclusive, where a consecutive sequence of N * numbers represents one row of an NxN Sudoku grid. * Zeros stand for blank entries. * @requires some r: int | puzzle.length = r * r * r * r * @requires universe.atoms[int] = {i: Integer | 1 <= i.intValue() <= puzzle.length} * @return a tupleset representation of the clues in the puzzle specified by the given array. */ public static final TupleSet parse(String[] puzzle, Universe univ) { final int n = (int)StrictMath.sqrt(puzzle.length); final int r = (int)StrictMath.sqrt(n); if (puzzle.length!=r*r*r*r) throw new IllegalArgumentException("Not a valid puzzle spec: expected " + (r*r*r*r) + " numbers but found " + puzzle.length); final TupleFactory f = univ.factory(); final TupleSet givens = f.noneOf(3); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { try { final int digit = Integer.parseInt(puzzle[i*n+j]); if (digit>0 && digit<=n) { final Tuple t = f.tuple(i+1, j+1, digit); givens.add(t); } else if (digit>n) { throw new IllegalArgumentException("Not a valid puzzle spec: expected numbers from [0, " + n+"] but found "+digit); } } catch (NumberFormatException nfe) { throw new IllegalArgumentException("Not a valid puzzle spec: expected numbers from [0, " + n+"] but found "+puzzle[i*n+j]); } } } return givens; }
Example #12
Source File: ALG195_1.java From kodkod with MIT License | 5 votes |
private static void displayOp(Instance instance, Relation op) { System.out.println("\n"+op+":"); final Iterator<Tuple> iter = instance.tuples(op).iterator(); for(int i = 0; i < 7; i++) { for(int j = 0; j < 7; j++) { System.out.print(iter.next().atom(2)); System.out.print("\t"); } System.out.println(); } }
Example #13
Source File: ALG197.java From kodkod with MIT License | 5 votes |
/** * Returns the bounds the problem (axioms 1, 4, 9-11, last formula of 14-15, and first formula of 16-22). * @return the bounds for the problem */ public final Bounds bounds() { final Bounds b = super.bounds(); final TupleFactory f = b.universe().factory(); final TupleSet op1h = b.upperBound(op1).clone(); final TupleSet op2h = b.upperBound(op2).clone(); final TupleSet op1l = f.setOf(f.tuple("e16", "e16", "e15")); // axiom 14, line 6 final TupleSet op2l = f.setOf(f.tuple("e26", "e26", "e25")); // axiom 15, line 6 op1h.removeAll(f.area(f.tuple("e16", "e16", "e10"), f.tuple("e16", "e16", "e16"))); op1h.addAll(op1l); op2h.removeAll(f.area(f.tuple("e26", "e26", "e20"), f.tuple("e26", "e26", "e26"))); op2h.addAll(op2l); b.bound(op1, op1l, op1h); b.bound(op2, op2l, op2h); final TupleSet high = f.area(f.tuple("e10", "e20"), f.tuple("e15", "e26")); // first line of axioms 16-22 for(int i = 0; i < 7; i++) { Tuple t = f.tuple("e16", "e2"+i); high.add(t); b.bound(h[i], f.setOf(t), high); high.remove(t); } return b; }
Example #14
Source File: ALG195.java From kodkod with MIT License | 5 votes |
/** * Returns the bounds the problem (axioms 1, 4, 9-13, second formula of 14-15, and first formula of 16-22). * @return the bounds for the problem */ public final Bounds bounds() { final Bounds b = super.bounds(); final TupleFactory f = b.universe().factory(); final TupleSet op1h = b.upperBound(op1).clone(); final TupleSet op2h = b.upperBound(op2).clone(); for(int i = 0; i < 7; i++) { op1h.remove(f.tuple("e1"+i, "e1"+i, "e1"+i)); // axiom 12 op2h.remove(f.tuple("e2"+i, "e2"+i, "e2"+i)); // axiom 13 } final TupleSet op1l = f.setOf(f.tuple("e15", "e15", "e11")); // axiom 14, line 2 final TupleSet op2l = f.setOf(f.tuple("e25", "e25", "e21")); // axiom 15, line 2 op1h.removeAll(f.area(f.tuple("e15", "e15", "e10"), f.tuple("e15", "e15", "e16"))); op1h.addAll(op1l); op2h.removeAll(f.area(f.tuple("e25", "e25", "e20"), f.tuple("e25", "e25", "e26"))); op2h.addAll(op2l); b.bound(op1, op1l, op1h); b.bound(op2, op2l, op2h); final TupleSet high = f.area(f.tuple("e10", "e20"), f.tuple("e14", "e26")); high.addAll(f.area(f.tuple("e16", "e20"), f.tuple("e16", "e26"))); // first line of axioms 16-22 for(int i = 0; i < 7; i++) { Tuple t = f.tuple("e15", "e2"+i); high.add(t); b.bound(h[i], f.setOf(t), high); high.remove(t); } return b; }
Example #15
Source File: Quasigroups7.java From kodkod with MIT License | 5 votes |
private static void displayOp(Instance instance, Relation op) { System.out.println("\n"+op+":"); final Iterator<Tuple> iter = instance.tuples(op).iterator(); for(int i = 0; i < 7; i++) { for(int j = 0; j < 7; j++) { System.out.print(iter.next().atom(2)); System.out.print("\t"); } System.out.println(); } }
Example #16
Source File: ListEncoding.java From kodkod with MIT License | 5 votes |
TupleSet copyFrom(TupleFactory tf, TupleSet other) { final int arity = other.arity(); final TupleSet out = tf.noneOf(arity); final Object[] tmp = new Object[arity]; for(Tuple t : other) { for(int i = 0; i < arity; i++) { tmp[i] = t.atom(i); } out.add(tf.tuple((Object[])tmp)); } return out; }
Example #17
Source File: SocialGolfer.java From kodkod with MIT License | 5 votes |
private void print(Solution sol, Solver s) { if (sol.instance()==null) System.out.println(sol); else { System.out.println(sol.outcome()); System.out.println(sol.stats()); System.out.println("Schedule:"); for(Tuple t : sol.instance().tuples(plays)) { System.out.print(t.atom(0)+"->"+t.atom(1)+"->"+t.atom(2) + "; "); } System.out.println(); } }
Example #18
Source File: LatinSquare.java From kodkod with MIT License | 5 votes |
/** * Usage: java examples.LatinSquare [order] */ public static void main(String[] args) { if (args.length < 1) usage(); try { final int n = Integer.parseInt(args[0]); if (n < 1) usage(); final LatinSquare model = new LatinSquare(); final Solver solver = new Solver(); solver.options().setSolver(SATFactory.MiniSat); solver.options().setSymmetryBreaking(n*n*n); final Formula f = model.latin().and(model.qg5()).and(model.idempotent()); final Bounds b = model.bounds(n); System.out.println(f); final Solution sol = solver.solve(f, b); if (sol.instance()==null) { System.out.println(sol); } else { System.out.println(sol.stats()); final Iterator<Tuple> iter = sol.instance().tuples(model.square).iterator(); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { System.out.print(iter.next().atom(2)); System.out.print("\t"); } System.out.println(); } } } catch (NumberFormatException nfe) { usage(); } }
Example #19
Source File: TranslatorTest.java From kodkod with MIT License | 5 votes |
private final void testIntersectionMultiplicity(Multiplicity mult, Relation p, Relation q, Tuple intersection) { final Instance m = solve(p.intersection(q).apply(mult)); assertNotNull(m); final Set<Tuple> ps = m.tuples(p), qs = m.tuples(q); assertFalse(ps.isEmpty()); assertFalse(qs.isEmpty()); assertTrue(ps.contains(intersection)); assertTrue(qs.contains(intersection)); }
Example #20
Source File: BasicUniverse.java From quetzal with Eclipse Public License 2.0 | 5 votes |
private void collectAtoms(Set<Object> liveAtoms, TupleSet bound) { for(Tuple t : bound) { for(int i = 0; i < t.arity(); i++) { Object atom = t.atom(i); liveAtoms.add(atom); } } }
Example #21
Source File: JenaUtil.java From quetzal with Eclipse Public License 2.0 | 5 votes |
public static void addTupleSet(Dataset dataset, TupleSet tt, BasicUniverse u, Instance t2) { if (tt != null) { for(Tuple t : tt) { Object graph = t.atom(0); Model m = QuadTableRelations.defaultGraph.equals(graph)? dataset.getDefaultModel(): dataset.getNamedModel(graph.toString()); m.add(fromTuple(m, t, u, t2)); } } }
Example #22
Source File: TableView.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
private static SimTuple toSimTuple(Tuple tuple) { List<SimAtom> atoms = new ArrayList<>(); for (int i = 0; i < tuple.arity(); i++) { SimAtom atom = SimAtom.make(tuple.atom(i).toString()); atoms.add(atom); } return SimTuple.make(atoms); }
Example #23
Source File: ALG195_1.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
private static void displayOp(Instance instance, Relation op) { System.out.println("\n" + op + ":"); final Iterator<Tuple> iter = instance.tuples(op).iterator(); for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { System.out.print(iter.next().atom(2)); System.out.print("\t"); } System.out.println(); } }
Example #24
Source File: ALG197.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * Returns the bounds the problem (axioms 1, 4, 9-11, last formula of 14-15, and * first formula of 16-22). * * @return the bounds for the problem */ @Override public final Bounds bounds() { final Bounds b = super.bounds(); final TupleFactory f = b.universe().factory(); final TupleSet op1h = b.upperBound(op1).clone(); final TupleSet op2h = b.upperBound(op2).clone(); final TupleSet op1l = f.setOf(f.tuple("e16", "e16", "e15")); // axiom // 14, // line // 6 final TupleSet op2l = f.setOf(f.tuple("e26", "e26", "e25")); // axiom // 15, // line // 6 op1h.removeAll(f.area(f.tuple("e16", "e16", "e10"), f.tuple("e16", "e16", "e16"))); op1h.addAll(op1l); op2h.removeAll(f.area(f.tuple("e26", "e26", "e20"), f.tuple("e26", "e26", "e26"))); op2h.addAll(op2l); b.bound(op1, op1l, op1h); b.bound(op2, op2l, op2h); final TupleSet high = f.area(f.tuple("e10", "e20"), f.tuple("e15", "e26")); // first line of axioms 16-22 for (int i = 0; i < 7; i++) { Tuple t = f.tuple("e16", "e2" + i); high.add(t); b.bound(h[i], f.setOf(t), high); high.remove(t); } return b; }
Example #25
Source File: Quasigroups7.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
private static void displayOp(Instance instance, Relation op) { System.out.println("\n" + op + ":"); final Iterator<Tuple> iter = instance.tuples(op).iterator(); for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { System.out.print(iter.next().atom(2)); System.out.print("\t"); } System.out.println(); } }
Example #26
Source File: TranslatorTest.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
private final void testIntersectionMultiplicity(Multiplicity mult, Relation p, Relation q, Tuple intersection) { final Instance m = solve(p.intersection(q).apply(mult)); assertNotNull(m); final Set<Tuple> ps = m.tuples(p), qs = m.tuples(q); assertFalse(ps.isEmpty()); assertFalse(qs.isEmpty()); assertTrue(ps.contains(intersection)); assertTrue(qs.contains(intersection)); }
Example #27
Source File: EvaluatorTest.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
public final void testEvalProduct() { // Hilary->Jocelyn = Hilary->Jocelyn final Set<Tuple> hilaryAndJocelyn = eval(hilary.product(jocelyn)); final Tuple hj = hilaryAndJocelyn.iterator().next(); assertEquals(hilaryAndJocelyn.size(), 1); assertEquals(hj.atom(0), value(hilary).iterator().next().atom(0)); assertEquals(hj.atom(1), value(jocelyn).iterator().next().atom(0)); // Person->(spouse->shaken) = (Person->spouse)->shaken assertEquals(eval(person.product(spouse.product(shaken))), eval(person.product(spouse).product(shaken))); // Person->(spouse + shaken) = Person->spouse + Person->shaken assertEquals(eval(person.product(spouse.union(shaken))), eval(person.product(spouse).union(person.product(shaken)))); // arity(spouse->shaken) = 4 assertEquals(spouse.product(shaken).arity(), 4); }
Example #28
Source File: BookExamples.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * This constructs a Kodkod Tuple from the list of atoms, and returns null if no * such Tuple can be constructed. */ private static Tuple t_tuple(TupleFactory factory, Object... atoms) { try { if (atoms.length <= 0) return null; return factory.tuple(atoms); } catch (Throwable ex) { return null; } }
Example #29
Source File: TranslateAlloyToKodkod.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
private TupleSet convert(TupleFactory factory, Expr f) throws Err { TupleSet old = ((A4TupleSet) (partial.eval(f))).debugGetKodkodTupleset(); TupleSet ans = factory.noneOf(old.arity()); for (Tuple oldT : old) { Tuple newT = null; for (int i = 0; i < oldT.arity(); i++) { if (newT == null) newT = factory.tuple(oldT.atom(i)); else newT = newT.product(factory.tuple(oldT.atom(i))); } ans.add(newT); } return ans; }
Example #30
Source File: TableView.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
private static SimTupleset toSimTupleset(TupleSet tupleSet) { List<SimTuple> l = new ArrayList<>(); for (Tuple tuple : tupleSet) { l.add(toSimTuple(tuple)); } return SimTupleset.make(l); }