Commit bff8c851 authored by Ryan Wisnesky's avatar Ryan Wisnesky
Browse files

fixes and improvements to sigma_chase and better hashcodes for a few types

parent fcba3c4f
Loading
Loading
Loading
Loading
+1 −1
Changes for doc/aqlmanual/aqlmanual.tex: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -1072,7 +1072,7 @@ Defines the SQL quasi-type to be used with CONVERT statements when emitting SQL

\subsection{{\tt chase\_style}}

Either {\tt parallel} (faster, but uses more space) or {\tt leftkan} (slower, but uses less space).
Either {\tt parallel} (faster, but uses more space) or {\tt leftkan} (slower, but uses less space).  Note that parallel will not be compatible with sigma operations such as sigma on transforms, the co-unit, etc.  Parallel is recommended only when data volume is too large for the other algorithms.

\end{document}

+1 −1
Changes for src/catdata/BinRelMap.java: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -94,7 +94,7 @@ public class BinRelMap<X, Y> implements Iterable<Pair<X, Y>> {
	}

	public BinRelMap() {
		R = new LinkedHashMap<>();
		R = new LinkedHashMap<>(1000000);
	}

	public boolean add(X x, Y y) {
+16 −1
Changes for src/catdata/Pair.java: 16 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -11,10 +11,21 @@ public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>, Serializable {
	public  T1 first; //TODO aql make these final.  Same for Triple
	public  T2 second;
	
	public void setFirst(T1 x) {
		first = x;
		hashCode2();
	}
	public void setSecond(T2 x) {
		second = x;
		hashCode2();
	}

	public Pair(T1 value, T2 value2) {
		first = value;
		second = value2;
		hashCode2();
	}
	private int hashCode;
	
	@Override
	public String toString() {
@@ -24,11 +35,15 @@ public class Pair<T1, T2> implements Comparable<Pair<T1, T2>>, Serializable {

	@Override
	public int hashCode() {
		return hashCode;
	}
	
	public void hashCode2() {
		int prime = 31;
		int result = 1;
		result = prime * result + ((first == null) ? 0 : first.hashCode());
		result = prime * result + ((second == null) ? 0 : second.hashCode());
		return result;
		hashCode = result;
	}
	
	@Override
+72 −51
Changes for src/catdata/aql/Chase.java: 72 added lines, 51 removed lines.
Original line number Diff line number Diff line
@@ -34,8 +34,8 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
					.iterator(), b = r.args.iterator();
			List<Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> l = new LinkedList<>();
			for (int i = 0; i < t.args.size(); i++) {
				Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>> c = a
						.next(), d = b.next();
				Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>> c = a.next(),
						d = b.next();
				Optional<Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> z = mergeable(
						c, d);
				if (!z.isPresent()) {
@@ -61,8 +61,7 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
		public final Ctx<Ty, BinRelMap<Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>, Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>>> tys;
		public final Ctx<Att2, BinRelMap<Lineage<Void, En2, Void, Fk2, Void, Gen, Void>, Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>>> atts;

		// public final Ctx<Ty, Ctx<Y, Term<Ty, Void, Sym, Void, Void, Void, Chc<Y,
		// Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>>> vs;
		public final Ctx<Ty, Ctx<Y, Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>>> vs;

		public Content() {
			ens = new Ctx<>();
@@ -80,7 +79,7 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {

			tys = new Ctx<>();
			atts = new Ctx<>();
			// vs = new Ctx<>();
			vs = new Ctx<>();
			for (Ty ty : F.dst.typeSide.tys) {
				tys.put(ty, new BinRelMap<>());
			}
@@ -88,7 +87,7 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
				atts.put(att, new BinRelMap<>());
			}
			for (Ty ty : F.src.typeSide.tys) {
				// vs.put(ty, new Ctx<>());
				vs.put(ty, new Ctx<>());
			}
		}

@@ -120,6 +119,9 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
			for (En1 en1 : us.keySet()) {
				us.get(en1).putAll(c.us.get(en1).map);
			}
			for (Ty t : vs.keySet()) {
				vs.get(t).putAll(c.vs.get(t).map);
			}
		}

		public Content(Instance<Ty, En1, Sym, Fk1, Att1, Gen, Sk, X, Y> I) {
@@ -145,22 +147,23 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
			}

			this.tys = new Ctx<>();
			this.vs = new Ctx<>();

			for (Ty ty : F.dst.typeSide.tys) {
				tys.put(ty, new BinRelMap<>());
				vs.put(ty, new Ctx<>());
			}
			this.atts = new Ctx<>();
			for (Att2 att : F.dst.atts.keySet()) {
				atts.put(att, new BinRelMap<>());
			}

			// this.vs = new Ctx<>();
		
			/*
			 * for (Ty ty : I.schema().typeSide.tys) { Ctx<Y, Term<Ty, Void, Sym, Void,
			 * Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> ctx = new
			 * Ctx<>(); for (Y y : I.algebra().talg().sks.keySet()) {
			 * ctx.put(y,Term.Sk(Chc.inLeft(y))); } vs.put(ty, ctx); }
			 */
			for (Y y : I.algebra().talg().sks.keySet()) {
				Ty ty = I.algebra().talg().sks.get(y);
				vs.get(ty).put(y, Term.Sk(Chc.inLeft(y)));
			}

		}

		public Content merge(Ctx<En2, UnionFind<Lineage<Void, En2, Void, Fk2, Void, Gen, Void>>> ufs,
@@ -199,13 +202,12 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
				}
			}

			
			// partitions to their constants, if any
			Ctx<Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>, Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> pars = new Ctx<>();
			for (Ty ty : F.dst.typeSide.tys) {

				for (Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>> x 
						: ufs2.get(ty).values()) {
				for (Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>> x : ufs2
						.get(ty).values()) {

					Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>> par = ufs2
							.get(ty).find(x);
@@ -221,15 +223,16 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
									+ print(parConst) + " in input instance.");
						} else {
							pars.map.put(par, me.get());
							//System.out.println("merge " + x + " and " + parConst + " to get " + me.get());
							// System.out.println("merge " + x + " and " + parConst + " to get " +
							// me.get());
						}
					} else {
						Optional<Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> me = mergeable(
								x, par);
						// System.out.println("1 " + x + " and " + par);
						if (!me.isPresent()) {
							throw new RuntimeException("Collision: " + print(x) + " not mergeable with "
									+ print(par) + " in input instance.");
							throw new RuntimeException("Collision: " + print(x) + " not mergeable with " + print(par)
									+ " in input instance.");
						} else {

							pars.put(par, x);
@@ -276,11 +279,14 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
			}
			// System.out.println("xxx2 " + ret);

			/*
			 * for (Ty ty : vs.keySet()) { for (Y x : vs.get(ty).keySet()) { Term<Ty, Void,
			 * Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>> n =
			 * vs.get(ty).get(x); ret.vs.get(ty).put(x, nf.apply(ty, n)); } }
			 */
			for (Ty ty : vs.keySet()) {
				for (Y x : vs.get(ty).keySet()) {
					Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>> n = vs
							.get(ty).get(x);
					ret.vs.get(ty).put(x, nf.apply(ty, n));
				}
			}

			//// System.out.println("merged " + ret);
			// ret.iso = iso.map((k,x)->new Pair<>(k,x.map(y->ufs.get(k).find(y))));
			return ret;
@@ -297,8 +303,6 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
			return t.mapGenSk(Function.identity(), f).toString();
		}

		

		public String sizes() {
			String s = "";
			for (En2 en2 : ens.keySet()) {
@@ -312,7 +316,8 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {

		@Override
		public String toString() {
			return "Content [ens=" + ens + ", fks=" + fks + ", us=" + us + ", tys=" + tys + ", atts=" + atts + "]";
			return "Content [ens=" + ens + ", fks=" + fks + ", us=" + us + ", tys=" + tys + ", atts=" + atts + ", vs="
					+ vs + "]";
		}
	}

@@ -343,19 +348,17 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
		T = new Content(I);
		// System.out.println(T);
		for (;;) {
			boolean changed = step();
			boolean changed = step(i == 0);
			if (!changed) {
				return;
			}
			// System.out.println(T);
			if (i++ > 2) {
				throw new RuntimeException();
			}
			i++;
		}

	}

	private boolean step() {
	private boolean step(boolean isFirstTime) {

		Content toAdd = new Content();
		Ctx<En2, UnionFind<Lineage<Void, En2, Void, Fk2, Void, Gen, Void>>> ufs = new Ctx<>();
@@ -375,8 +378,10 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
		makeObjectsTotal(toAdd, changed);
		// System.out.println("C " + changed[0]);

	//	if (isFirstTime) {
			moveObjects(toAdd, changed);
			// System.out.println("D " + changed[0]);
		//}

		doEqs(toAdd, ufs, ufs2, changed);

@@ -525,8 +530,8 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
				Lineage<Void, En2, Void, Fk2, Void, Gen, Void> initial = T.us.get(v).get(x);
				Set<Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> N = evalT(
						F.atts.get(a).third, Util.singSet(initial));
				//System.out.println("eval on " + x + " at " + a + " gives " + N + " with a lhs of " + lhs);
				
				// System.out.println("eval on " + x + " at " + a + " gives " + N + " with a lhs
				// of " + lhs);

				if (!N.contains(lhs)) {
					Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>> m = populateT(
@@ -559,7 +564,11 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
			}
			return Term.Sym(t.sym, l);
		} else if (t.sk != null) {
			return Term.Sk(Chc.inLeft(t.sk));
			Ty ty = I.algebra().talg().sks.get(t.sk);
			Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>> l = T.vs.get(ty)
					.get(t.sk);
			return l;
//			return Term.Sk(Chc.inLeft(t.sk));
		}
		return Util.anomaly();
	}
@@ -699,7 +708,8 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
			Set<Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> ret = new HashSet<>();
			Set<Lineage<Void, En2, Void, Fk2, Void, Gen, Void>> x = eval(t.arg.toFkList(), N);
			for (Lineage<Void, En2, Void, Fk2, Void, Gen, Void> l : x) {
				Collection<Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> w = T.atts.get(t.att).get(l);
				Collection<Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> w = T.atts
						.get(t.att).get(l);
				if (w == null) {
					w = new HashSet<>();
				}
@@ -712,7 +722,7 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {

	public void moveObjects(Content toAdd, Boolean[] changed) {
		// a : a -> F(a)

		System.out.println("started");
		for (En1 a : F.src.ens) {
			// T_v(x) -> Ey. T_a(x,y)
			// this is the 'loading' step in the Content constructor
@@ -729,20 +739,31 @@ public class Chase<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, Sk, X, Y> {
				}
			}
		}
		/*
		 * for (Ty a : F.src.typeSide.tys) { // T_v(x) -> Ey. T_a(x,y) // this is the
		 * 'loading' step in the Content constructor
		 * 
		 * // T_a(x,y) -> T_v(x) /\ T_w(y) Ty w = a; Ctx<Y, Term<Ty, Void, Sym, Void,
		 * Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> T_a =
		 * T.vs.get(a); BinRelMap<Term<Ty, Void, Sym, Void, Void, Void, Chc<Y,
		 * Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>, Term<Ty, Void, Sym, Void, Void,
		 * Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> T_w = T.tys.get(w);
		 * for (Y xy : T_a.keySet()) { Term<Ty, Void, Sym, Void, Void, Void, Chc<Y,
		 * Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>> x = T_a.get(xy); if
		 * (!T_w.containsKey(x)) { changed[0] = changed[0] | toAdd.tys.get(w).add(x, x);
		 * } } }
		 */
		System.out.println("started2");

		for (Ty a : F.src.typeSide.tys) {
			// T_v(x) -> Ey. T_a(x,y) // this is the 'loading' step in the Content
			// T_a(x,y) -> T_v(x) /\ T_w(y)

			Ty w = a;

			Ctx<Y, Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> T_a = T.vs
					.get(a);
			BinRelMap<Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>, Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>>> T_w = T.tys
					.get(w);
			//int i = 0;
			for (Y xy : T_a.keySet()) {
				Term<Ty, Void, Sym, Void, Void, Void, Chc<Y, Lineage<Ty, En2, Sym, Fk2, Att2, Gen, Sk>>> x = T_a
						.get(xy);
				if (!T_w.containsKey(x)) {
					changed[0] = changed[0] | toAdd.tys.get(w).add(x, x);
				}
				// System.out.println(xy + " " + i++);
			}
		}

//		System.out.println("ended");

	}

	public void makeObjectsTotal(Content toAdd, Boolean[] changed) {
+1 −1
Changes for src/catdata/aql/SigmaLeftKanAlgebra.java: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -162,7 +162,7 @@ public class SigmaLeftKanAlgebra<Ty, En1, Sym, Fk1, Att1, En2, Fk2, Att2, Gen, S
		for (List<Pair<X, Lineage<Void, En2, Void, Fk2, Void, Gen, Void>>> a : ua.map.values()) {
			for (Pair<X, Lineage<Void, En2, Void, Fk2, Void, Gen, Void>> s : a) {
				if (s.second.equals(y)) {
					s.second = x;
					s.setSecond(x);
				}
			}
		}
Loading