Search in sources :

Example 11 with FQLException

use of catdata.fql.FQLException in project fql by CategoricalData.

the class LeftKan method beta2.

// beta, delta, gamma
private boolean beta2() {
    boolean ret = false;
    try {
        for (Edge e : A.edges) {
            Path g = F.appy(B, new Path(A, e));
            Set<Pair<Object, Integer>> lhs = Instance.compose(X.data.get(e.name), ua.get(e.target));
            Set<Pair<Object, Integer>> rhs = Instance.compose(ua.get(e.source), eval(g));
            // System.out.println(lhs);
            // System.out.println(rhs);
            Node n = g.target;
            ret = ret || addCoincidences(lhs, rhs, n);
        }
    } catch (FQLException fe) {
        fe.printStackTrace();
        throw new RuntimeException(fe.getMessage());
    }
    return ret;
}
Also used : Path(catdata.fql.decl.Path) FQLException(catdata.fql.FQLException) Node(catdata.fql.decl.Node) Edge(catdata.fql.decl.Edge) Pair(catdata.Pair)

Example 12 with FQLException

use of catdata.fql.FQLException in project fql by CategoricalData.

the class InstOps method visit.

@Override
public List<PSM> visit(String env, Return e) {
    // String xxx = "return_temp_xxx";
    List<PSM> ret = new LinkedList<>();
    InstExp i1 = prog.insts.get(e.inst);
    if (i1 instanceof Delta) {
        String middle = ((Delta) i1).I;
        // can't be null
        InstExp i2 = prog.insts.get(middle);
        Mapping f = ((Delta) i1).F.toMap(prog);
        if (i2 instanceof Sigma) {
            Sigma input0 = ((Sigma) i2);
            String input = input0.I;
            for (Node n : f.source.nodes) {
                ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(input + "_" + n.string, middle + "_" + f.nm.get(n) + "_subst", e.inst + "_" + n.string + "_subst"), "c0", "c1"));
            }
        } else if (i2 instanceof FullSigma) {
            FullSigma input0 = ((FullSigma) i2);
            String input = input0.I;
            for (Node n : f.source.nodes) {
                ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(input + "_" + n.string, middle + "_" + n.string + "_e", e.inst + "_" + n.string + "_subst"), "c0", "c1"));
            }
        } else {
            throw new RuntimeException();
        }
    } else if (i1 instanceof Pi) {
        String middle = ((Pi) i1).I;
        // can't be null
        InstExp i2 = prog.insts.get(middle);
        Mapping f = ((Pi) i1).F.toMap(prog);
        if (i2 instanceof Delta) {
            Delta input0 = ((Delta) i2);
            String input = input0.I;
            for (Node n : f.target.nodes) {
                try {
                    Map<String, Triple<Node, Node, Arr<Node, Path>>[]> colmap = PSMGen.pi(f, middle, e.inst).second;
                    Triple<Node, Node, Arr<Node, Path>>[] col = colmap.get(n.string);
                    if (col.length == 0) {
                        LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
                        Map<String, String> from = new HashMap<>();
                        List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
                        from.put("lim", e.inst + "_" + n.string + "_limit");
                        from.put("middle", input + "_" + n.string);
                        select.put("c0", new Pair<>("middle", "c0"));
                        select.put("c1", new Pair<>("lim", "guid"));
                        Flower flower = new Flower(select, from, where);
                        ret.add(new InsertSQL(env + "_" + n.string, flower, "c0", "c1"));
                        return ret;
                    }
                    // LinkedHashMap<String, String> attrs = new LinkedHashMap<>();
                    // attrs.put("guid", PSM.VARCHAR());
                    LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
                    Map<String, String> from = new HashMap<>();
                    from.put("lim", e.inst + "_" + n.string + "_limit");
                    List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
                    int i = 0;
                    for (Triple<Node, Node, Arr<Node, Path>> col0 : col) {
                        from.put("c" + i + "_subst_inv", middle + "_" + col0.second.string + "_subst_inv");
                        where.add(new Pair<>(new Pair<>("lim", "c" + i), new Pair<>("c" + i + "_subst_inv", "c0")));
                        // attrs.put("c" + i, PSM.VARCHAR());
                        i++;
                    }
                    // if (col.length > 1) {
                    for (int j = 0; j < col.length; j++) {
                        if (col[j].third.arr.equals(f.target.toCategory2().second.of(new Path(f.target, n)).arr)) {
                            where.add(new Pair<>(new Pair<>("c" + 0 + "_subst_inv", "c1"), new Pair<>("c" + j + "_subst_inv", "c1")));
                        }
                    }
                    select.put("c" + 0, new Pair<>("c" + 0 + "_subst_inv", "c1"));
                    select.put("c1", new Pair<>("lim", "guid"));
                    // }
                    // ret.add(new CreateTable(xxx, attrs, false));
                    Flower flower = new Flower(select, from, where);
                    ret.add(new InsertSQL(env + "_" + n.string, flower, "c0", "c1"));
                } catch (FQLException fe) {
                    fe.printStackTrace();
                    throw new RuntimeException(fe.getMessage());
                }
            }
        } else {
            throw new RuntimeException();
        }
    } else {
        throw new RuntimeException();
    }
    return ret;
}
Also used : Arr(catdata.fql.cat.Arr) FullSigma(catdata.fql.decl.InstExp.FullSigma) Sigma(catdata.fql.decl.InstExp.Sigma) LinkedHashMap(java.util.LinkedHashMap) FQLException(catdata.fql.FQLException) Pi(catdata.fql.decl.InstExp.Pi) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) CopyFlower(catdata.fql.sql.CopyFlower) Flower(catdata.fql.sql.Flower) PropPSM(catdata.fql.sql.PropPSM) ExpPSM(catdata.fql.sql.ExpPSM) PSM(catdata.fql.sql.PSM) LinkedList(java.util.LinkedList) Triple(catdata.Triple) InsertSQL(catdata.fql.sql.InsertSQL) Delta(catdata.fql.decl.InstExp.Delta) FullSigma(catdata.fql.decl.InstExp.FullSigma) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with FQLException

use of catdata.fql.FQLException in project fql by CategoricalData.

the class InstOps method visit.

@Override
public List<PSM> visit(String dst, TT e) {
    try {
        List<PSM> ret = new LinkedList<>();
        Signature s = prog.insts.get(e.obj).type(prog).toSig(prog);
        String temp1 = next();
        ret.addAll(PSMGen.makeTables(temp1, s, false));
        String temp2 = next();
        ret.addAll(PSMGen.makeTables(temp2, s, false));
        Pair<Map<Node, List<Pair<Path, Attribute<Node>>>>, List<PSM>> xxx = Relationalizer.observations(s, temp1, e.tgt, false);
        Pair<Map<Node, List<Pair<Path, Attribute<Node>>>>, List<PSM>> yyy = Relationalizer.observations(s, temp2, e.obj, false);
        if (!xxx.first.equals(yyy.first)) {
            throw new RuntimeException("not equal: " + xxx + " and " + yyy);
        }
        ret.addAll(xxx.second);
        ret.addAll(yyy.second);
        for (Node n : s.nodes) {
            List<?> cols = xxx.first.get(n);
            Map<String, String> from = new HashMap<>();
            from.put("t1", e.tgt + "_" + n);
            from.put("t1_obs", temp1 + "_" + n + "_" + "observables");
            from.put("t2", e.obj + "_" + n);
            from.put("t2_obs", temp2 + "_" + n + "_" + "observables");
            List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
            where.add(new Pair<>(new Pair<>("t1", "c0"), new Pair<>("t1_obs", "id")));
            where.add(new Pair<>(new Pair<>("t2", "c0"), new Pair<>("t2_obs", "id")));
            for (int i = 0; i < cols.size(); i++) {
                where.add(new Pair<>(new Pair<>("t1_obs", "c" + i), new Pair<>("t2_obs", "c" + i)));
            }
            LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
            select.put("c0", new Pair<>("t1", "c0"));
            select.put("c1", new Pair<>("t2", "c0"));
            Flower f = new Flower(select, from, where);
            ret.add(new InsertSQL(dst + "_" + n, f, "c0", "c1"));
        }
        ret.addAll(PSMGen.dropTables(temp1, s));
        ret.addAll(PSMGen.dropTables(temp2, s));
        return ret;
    } catch (FQLException fe) {
        fe.printStackTrace();
        throw new RuntimeException(fe.getLocalizedMessage());
    }
}
Also used : CopyFlower(catdata.fql.sql.CopyFlower) Flower(catdata.fql.sql.Flower) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PropPSM(catdata.fql.sql.PropPSM) ExpPSM(catdata.fql.sql.ExpPSM) PSM(catdata.fql.sql.PSM) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) FQLException(catdata.fql.FQLException) InsertSQL(catdata.fql.sql.InsertSQL) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Pair(catdata.Pair)

Example 14 with FQLException

use of catdata.fql.FQLException in project fql by CategoricalData.

the class Inst method stuff.

// base^exp
public static FinCat<Mapping, Map<Node, Path>> stuff(Signature base, Signature exp) throws FQLException {
    Pair<FinCat<Node, Path>, Fn<Path, Arr<Node, Path>>> xxx = base.toCategory2();
    FinCat<Node, Path> base0 = xxx.first;
    Pair<FinCat<Node, Path>, Fn<Path, Arr<Node, Path>>> yyy = exp.toCategory2();
    FinCat<Node, Path> exp0 = yyy.first;
    List<LinkedHashMap<Node, Node>> nms = homomorphs(exp.nodes, base.nodes);
    List<Mapping> mappings = new LinkedList<>();
    for (LinkedHashMap<Node, Node> nm : nms) {
        LinkedHashMap<Attribute<Node>, List<Attribute<Node>>> ams = new LinkedHashMap<>();
        for (Attribute<Node> a : exp.attrs) {
            ams.put(a, base.attrsFor(nm.get(a.source)));
        }
        LinkedHashMap<Edge, List<Path>> ems = new LinkedHashMap<>();
        for (Edge e : exp.edges) {
            Set<Arr<Node, Path>> s = base0.hom(nm.get(e.source), nm.get(e.target));
            List<Path> p = new LinkedList<>();
            for (Arr<Node, Path> sx : s) {
                p.add(sx.arr);
            }
            ems.put(e, p);
        }
        List<LinkedHashMap<Attribute<Node>, Attribute<Node>>> ams0 = homomorphs(ams);
        List<LinkedHashMap<Edge, Path>> ems0 = homomorphs(ems);
        for (LinkedHashMap<Attribute<Node>, Attribute<Node>> am : ams0) {
            for (LinkedHashMap<Edge, Path> em : ems0) {
                try {
                    Mapping m = new Mapping(true, exp, base, nm, em, am);
                    mappings.add(m);
                } catch (Exception e) {
                }
            }
        }
    }
    List<Arr<Mapping, Map<Node, Path>>> arrows = new LinkedList<>();
    for (Mapping s : mappings) {
        for (Mapping t : mappings) {
            Map<Node, List<Path>> map = new HashMap<>();
            for (Node n : exp.nodes) {
                List<Path> p = new LinkedList<>();
                for (Arr<Node, Path> k : base0.hom(s.nm.get(n), t.nm.get(n))) {
                    p.add(k.arr);
                }
                map.put(n, p);
            }
            List<LinkedHashMap<Node, Path>> map0 = homomorphs(map);
            outer: for (Map<Node, Path> k : map0) {
                for (Node x : k.keySet()) {
                    for (Node y : k.keySet()) {
                        for (Arr<Node, Path> f : exp0.hom(x, y)) {
                            Path lhs = Path.append(base, k.get(x), t.appy(base, f.arr));
                            Path rhs = Path.append(base, s.appy(base, f.arr), k.get(y));
                            if (!xxx.second.of(lhs).equals(xxx.second.of(rhs))) {
                                continue outer;
                            }
                        }
                    }
                }
                arrows.add(new Arr<>(k, s, t));
            }
        }
    }
    Map<Mapping, Arr<Mapping, Map<Node, Path>>> identities = new HashMap<>();
    for (Mapping m : mappings) {
        Map<Node, Path> map = new HashMap<>();
        for (Node n : m.source.nodes) {
            map.put(n, new Path(m.target, m.nm.get(n)));
        }
        Arr<Mapping, Map<Node, Path>> uuu = new Arr<>(map, m, m);
        identities.put(m, new Arr<>(map, m, m));
        if (!arrows.contains(uuu)) {
            arrows.add(uuu);
        }
    }
    Map<Pair<Arr<Mapping, Map<Node, Path>>, Arr<Mapping, Map<Node, Path>>>, Arr<Mapping, Map<Node, Path>>> composition = new HashMap<>();
    for (Arr<Mapping, Map<Node, Path>> a1 : arrows) {
        for (Arr<Mapping, Map<Node, Path>> a2 : arrows) {
            if (!a1.dst.equals(a2.src)) {
                continue;
            }
            Map<Node, Path> m = new HashMap<>();
            for (Node n : exp.nodes) {
                m.put(n, xxx.second.of(Path.append(base, a1.arr.get(n), a2.arr.get(n))).arr);
            }
            composition.put(new Pair<>(a1, a2), new Arr<>(m, a1.src, a2.dst));
        }
    }
    return new FinCat<>(mappings, arrows, composition, identities);
}
Also used : Attribute(catdata.fql.decl.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(catdata.fql.decl.Node) Mapping(catdata.fql.decl.Mapping) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) Path(catdata.fql.decl.Path) Fn(catdata.fql.Fn) LinkedList(java.util.LinkedList) FQLException(catdata.fql.FQLException) Edge(catdata.fql.decl.Edge) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 15 with FQLException

use of catdata.fql.FQLException in project fql by CategoricalData.

the class Inst method hom.

/*
	private static List<Mapping> bistuff(Signature base, Signature exp)
			throws FQLException {

		if (base.nodes.size() != exp.nodes.size()) {
			return null;
		}
		if (base.attrs.size() != exp.attrs.size()) {
			return null;
		}

		Pair<FinCat<Node, Path>, Fn<Path, Arr<Node, Path>>> xxx = base
				.toCategory2();
		FinCat<Node, Path> base0 = xxx.first;
		Pair<FinCat<Node, Path>, Fn<Path, Arr<Node, Path>>> yyy = exp
				.toCategory2();
		FinCat<Node, Path> exp0 = yyy.first;

		if (xxx.first.arrows.size() != yyy.first.arrows.size()) {
			return null;
		}

		List<LinkedHashMap<Node, Node>> nms = bijections(exp.nodes, base.nodes);

		List<Mapping> ret = new LinkedList<>();
		outmost: for (LinkedHashMap<Node, Node> nm : nms) {
			Map<Pair<Node, Node>, List<LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>>> bijm = new HashMap<>();
			Map<Node, List<LinkedHashMap<Attribute<Node>, Attribute<Node>>>> atm = new HashMap<>();
			for (Node s : nm.keySet()) {
				List<Attribute<Node>> sa = exp.attrsFor(s);
				List<Attribute<Node>> ta = base.attrsFor(nm.get(s));
				if (sa.size() != ta.size()) {
					continue outmost;
				}
				List<LinkedHashMap<Attribute<Node>, Attribute<Node>>> am = bijections(
						sa, ta);
				Iterator<LinkedHashMap<Attribute<Node>, Attribute<Node>>> it = am
						.iterator();
				outer: while (it.hasNext()) {
					Map<Attribute<Node>, Attribute<Node>> k = it.next();
					for (Entry<Attribute<Node>, Attribute<Node>> v : k
							.entrySet()) {
						if (!v.getKey().target.equals(v.getValue().target)) {
							it.remove();
							continue outer;
						}
					}
				}
				if (am.isEmpty() && !sa.isEmpty()) {
					continue outmost;
				}
				atm.put(s, am);
				Node sx = nm.get(s);
				for (Node t : nm.keySet()) {
					Set<Arr<Node, Path>> h1 = exp0.hom(s, t);
					Node st = nm.get(t);
					Set<Arr<Node, Path>> h2 = base0.hom(sx, st);
					if (h1.size() != h2.size()) {
						continue outmost;
					}
					List<LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>> bij = bijections(
							new LinkedList<>(h1), new LinkedList<>(h2));
					if (!bij.isEmpty()) {
						bijm.put(new Pair<>(s, t), bij);
					}
				}
			}

			List<LinkedHashMap<Pair<Node, Node>, LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>>> bijmX = homomorphs(bijm);
			List<LinkedHashMap<Node, LinkedHashMap<Attribute<Node>, Attribute<Node>>>> atmX = homomorphs(atm);
	
			List<LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>> bijmZ = new LinkedList<>();
			for (LinkedHashMap<Pair<Node, Node>, LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>> k : bijmX) {
				LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>> bijmY = new LinkedHashMap<>();
				for (Entry<Pair<Node, Node>, LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>> v : k
						.entrySet()) {
					bijmY.putAll(v.getValue());
				}
				bijmZ.add(bijmY);
			}

			List<LinkedHashMap<Attribute<Node>, Attribute<Node>>> atmZ = new LinkedList<>();
			for (LinkedHashMap<Node, LinkedHashMap<Attribute<Node>, Attribute<Node>>> k : atmX) {
				LinkedHashMap<Attribute<Node>, Attribute<Node>> atmY = new LinkedHashMap<>();
				for (Entry<Node, LinkedHashMap<Attribute<Node>, Attribute<Node>>> v : k
						.entrySet()) {
					atmY.putAll(v.getValue());
				}
				atmZ.add(atmY);
			}
			if (exp.attrs.isEmpty()) {
				atmZ.add(new LinkedHashMap<>());
			}
			if (exp.edges.isEmpty()) {
				bijmZ.add(new LinkedHashMap<>());
			}

			for (Map<Arr<Node, Path>, Arr<Node, Path>> f : bijmZ) {
				LinkedHashMap<Edge, Path> em = new LinkedHashMap<>();
				for (Edge e : exp.edges) {
					em.put(e, f.get(yyy.second.of(new Path(exp, e))).arr);
				}
				for (LinkedHashMap<Attribute<Node>, Attribute<Node>> g : atmZ) {
					try {
						Mapping m = new Mapping(true, exp, base, nm, em, g);
						ret.add(m);
					} catch (FQLException fe) {
						// fe.printStackTrace();
					}
				}
			}
			// break;
		}

		return ret;
	}
*/
// I => J
public static List<Transform> hom(Instance I, Instance J) {
    if (!I.thesig.equals(J.thesig)) {
        throw new RuntimeException();
    }
    List<Transform> ret = new LinkedList<>();
    Map<Node, List<LinkedHashMap<Pair<Object, Object>, Pair<Object, Object>>>> m = new HashMap<>();
    for (Node n : I.thesig.nodes) {
        List<Pair<Object, Object>> src = new LinkedList<>(I.data.get(n.string));
        List<Pair<Object, Object>> dst = new LinkedList<>(J.data.get(n.string));
        List<LinkedHashMap<Pair<Object, Object>, Pair<Object, Object>>> h = homomorphs(src, dst);
        m.put(n, h);
    }
    List<LinkedHashMap<Node, LinkedHashMap<Pair<Object, Object>, Pair<Object, Object>>>> map = homomorphs(m);
    for (LinkedHashMap<Node, LinkedHashMap<Pair<Object, Object>, Pair<Object, Object>>> t : map) {
        try {
            List<Pair<String, List<Pair<Object, Object>>>> l = new LinkedList<>();
            for (Entry<Node, LinkedHashMap<Pair<Object, Object>, Pair<Object, Object>>> u : t.entrySet()) {
                l.add(new Pair<>(u.getKey().string, conv(u.getValue())));
            }
            Transform tr = new Transform(I, J, l);
            ret.add(tr);
        } catch (Exception e) {
        }
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(catdata.fql.decl.Node) LinkedList(java.util.LinkedList) FQLException(catdata.fql.FQLException) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) LinkedList(java.util.LinkedList) Transform(catdata.fql.decl.Transform) Pair(catdata.Pair)

Aggregations

FQLException (catdata.fql.FQLException)37 Pair (catdata.Pair)28 LinkedList (java.util.LinkedList)23 HashMap (java.util.HashMap)21 Node (catdata.fql.decl.Node)20 LinkedHashMap (java.util.LinkedHashMap)20 Arr (catdata.fql.cat.Arr)15 Map (java.util.Map)15 Instance (catdata.fql.decl.Instance)14 Path (catdata.fql.decl.Path)14 Transform (catdata.fql.decl.Transform)12 Attribute (catdata.fql.decl.Attribute)11 Edge (catdata.fql.decl.Edge)11 Signature (catdata.fql.decl.Signature)11 List (java.util.List)11 Triple (catdata.Triple)9 HashSet (java.util.HashSet)6 Fn (catdata.fql.Fn)5 CopyFlower (catdata.fql.sql.CopyFlower)5 ExpPSM (catdata.fql.sql.ExpPSM)5