Search in sources :

Example 31 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XMapping method pi.

@SuppressWarnings({ "rawtypes", "unchecked" })
public XCtx<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>> pi(XCtx<C> I) {
    Pair<Map<D, List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>>>, Map<D, List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>>>> zzz = makeThetas2(I);
    Map<D, List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>>> thetas_d = zzz.first;
    Map<D, List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>>> bad_thetas = zzz.second;
    Map types = new HashMap<>();
    for (D d : dst.allIds()) {
        for (Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>> theta : thetas_d.get(d)) {
            types.put(theta, new Pair<>("_1", d));
        }
    }
    Set<Pair<List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>>, List<Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>>>>> eqs = new HashSet<>();
    for (D h : dst.allTerms()) {
        Pair<D, D> t = dst.type(h);
        // what if t.second is a type? have a theta but won't build a theta
        for (Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>> theta : thetas_d.get(t.first)) {
            Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>> theta0 = new HashMap<>();
            for (C c : src.allIds()) {
                for (Triple<D, D, List<D>> f : dst.cat().hom(t.second, em.get(c).get(0))) {
                    List<D> f0 = new LinkedList<>();
                    f0.add(h);
                    f0.addAll(f.third);
                    Triple<D, D, List<D>> key = dst.find_fast(new Triple<>(t.first, f.second, f0));
                    if (key == null) {
                        throw new RuntimeException("Cannot find null key in " + dst.cat().arrows());
                    }
                    Triple<C, C, List<C>> toStore = getWrapper(I, theta, new Pair<>(c, key));
                    if (toStore == null) {
                        throw new RuntimeException("Cannot find " + new Pair<>(c, key) + " in " + theta);
                    }
                    theta0.put(new Pair<>(c, f), toStore);
                }
            }
            List lhs = new LinkedList<>();
            lhs.add(theta);
            lhs.add(h);
            if (I.global.allIds().contains(t.second)) {
                List rhs = new LinkedList<>();
                rhs.add(theta0);
                if (bad_thetas.get(t.second).contains(theta0)) {
                    Object o1 = t.second;
                    Object o2 = new Triple<>(t.second, t.second, new LinkedList<>());
                    Pair key = new Pair(o1, o2);
                    // theta0.get(key);
                    Triple<C, C, List<C>> val = getWrapper(I, theta0, key);
                    if (val == null) {
                        throw new RuntimeException();
                    }
                    List lll = new LinkedList();
                    if (val.third.isEmpty()) {
                        lll.add(val.first);
                    } else {
                        lll.addAll((val.third));
                    }
                    eqs.add(new Pair(lhs, lll));
                } else {
                    eqs.add(new Pair<>(lhs, rhs));
                }
            } else {
                boolean found = false;
                for (Map<Pair<C, Triple<D, D, List<D>>>, Triple<C, C, List<C>>> thetaX : thetas_d.get(t.second)) {
                    if (theta0.equals(thetaX)) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    throw new RuntimeException("At h=" + h + ": " + t + ", Constructed theta^prime " + theta0 + " not found in\n\n" + Util.sep(thetas_d.get(t.second), "\n"));
                }
                List rhs = new LinkedList<>();
                rhs.add(theta0);
                eqs.add(new Pair<>(lhs, rhs));
            }
        }
    }
    XCtx ret = new XCtx(new HashSet<>(), types, eqs, src.global, dst, "instance");
    ret.saturated = true;
    return ret;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair) HashSet(java.util.HashSet) LinkedList(java.util.LinkedList) Triple(catdata.Triple) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 32 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XMapping method counit.

@SuppressWarnings({ "rawtypes", "unchecked" })
public XMapping<Pair<Triple<D, D, List<D>>, C>, D> counit(XCtx<D> I) {
    XCtx<Pair<Triple<D, D, List<D>>, C>> FI = delta(I);
    XMapping<Pair<Triple<D, D, List<D>>, C>, Pair<Triple<D, D, List<D>>, C>> f = (XMapping<Pair<Triple<D, D, List<D>>, C>, Pair<Triple<D, D, List<D>>, C>>) this;
    XCtx<Pair<Triple<D, D, List<D>>, C>> FFI = f.apply0(FI);
    Map m = new HashMap<>();
    for (Pair<Triple<D, D, List<D>>, C> c : FFI.terms()) {
        List<D> l = new LinkedList<>(c.first.third);
        l.add(0, c.first.first);
        m.put(c, l);
    }
    for (Object o : FFI.allTerms()) {
        if (m.containsKey(o)) {
            continue;
        }
        List l = new LinkedList();
        l.add(o);
        m.put(o, l);
    }
    return new XMapping<>(FFI, I, m, "homomorphism");
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) Triple(catdata.Triple) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(catdata.Pair)

Example 33 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XMapping method rel.

@SuppressWarnings({ "rawtypes", "unchecked" })
public XMapping<C, D> rel() {
    Map m = new HashMap<>();
    for (Triple<C, C, List<C>> arr : src.cat().arrows()) {
        if (!arr.first.equals("_1")) {
            continue;
        }
        Map<Triple<C, C, List<C>>, Triple<C, C, List<C>>> lx;
        if (src.schema.ids.contains(arr.second)) {
            lx = src.obs(arr);
        } else {
            if (src.global.cat().hom((C) "_1", arr.second).contains(arr)) {
                continue;
            } else {
                lx = new HashMap<>();
                lx.put(new Triple<>(arr.second, arr.second, new LinkedList<>()), arr);
            }
        }
        List<C> j = new LinkedList<>();
        j.add(arr.first);
        j.addAll(arr.third);
        List<D> x = apply(j);
        Triple<D, D, List<D>> y = dst.find_fast(new Triple<>((D) arr.first, (D) arr.second, x));
        Map<Triple<D, D, List<D>>, Triple<D, D, List<D>>> rx;
        if (dst.schema.ids.contains(y.second)) {
            rx = dst.obs(y);
        } else {
            if (dst.global.cat().hom((D) "_1", y.second).contains(y)) {
                List<D> y2 = new LinkedList<>();
                y2.add(y.first);
                y2.addAll(y.third);
                m.put(lx, y2);
                continue;
            } else {
                rx = new HashMap<>();
                rx.put(new Triple<>(y.second, y.second, new LinkedList<>()), y);
            }
        }
        if (m.containsKey(lx)) {
            if (!m.get(lx).equals(Util.singList(rx))) {
                throw new RuntimeException();
            }
        }
        m.put(lx, Util.singList(rx));
    }
    for (Object o : src.schema.allTerms()) {
        if (m.containsKey(o)) {
            continue;
        }
        List l = new LinkedList();
        l.add(o);
        m.put(o, l);
    }
    return new XMapping<>(src.rel(), dst.rel(), m, "homomorphism");
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) Triple(catdata.Triple) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 34 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XNeo4jToFQL method toSchema.

private static XSchema toSchema(Map<String, Set<String>> propsForLabels, Map<String, Pair<String, String>> sortsForEdges) {
    List<String> labels = new LinkedList<>(propsForLabels.keySet());
    List<Triple<String, String, String>> arrows = new LinkedList<>();
    for (String l : labels) {
        for (String p : propsForLabels.get(l)) {
            arrows.add(new Triple<>(p, l, "dom"));
        }
    }
    for (String e : sortsForEdges.keySet()) {
        Pair<String, String> p = sortsForEdges.get(e);
        arrows.add(new Triple<>(e, p.first, p.second));
    }
    XSchema ret = new XSchema(labels, arrows, new LinkedList<>());
    return ret;
}
Also used : Triple(catdata.Triple) XSchema(catdata.fpql.XExp.XSchema) LinkedList(java.util.LinkedList)

Example 35 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XOps method visit.

@Override
public XObject visit(XProgram env, XSOED e) {
    XExp src0 = env.exps.get(e.src);
    if (src0 == null) {
        throw new RuntimeException("Missing: " + e.src);
    }
    if (!(src0 instanceof XSchema)) {
        throw new RuntimeException("Not a schema: " + e.src);
    }
    XSchema src = (XSchema) src0;
    XCtx src1 = (XCtx) ENV.objs.get(e.src);
    XExp dst0 = env.exps.get(e.dst);
    if (dst0 == null) {
        throw new RuntimeException("Missing: " + e.dst);
    }
    if (!(dst0 instanceof XSchema)) {
        throw new RuntimeException("Not a schema: " + e.dst);
    }
    XSchema dst = (XSchema) dst0;
    XCtx dst1 = (XCtx) ENV.objs.get(e.dst);
    XObject I0 = ENV.objs.get(e.I);
    if (I0 == null) {
        throw new RuntimeException("Missing: " + e.I);
    }
    if (!(I0 instanceof XCtx)) {
        throw new RuntimeException("Not an instance: " + e.I);
    }
    XCtx I = (XCtx) I0;
    if (!src1.equals(I.schema)) {
        throw new RuntimeException("Instance schema does not match source");
    }
    List<String> nodes = new LinkedList<>();
    List<Triple<String, String, String>> arrows = new LinkedList<>();
    List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
    Map em_s = new HashMap();
    Map em_t = new HashMap();
    nodes.addAll(src.nodes);
    nodes.addAll(dst.nodes);
    arrows.addAll(src.arrows);
    arrows.addAll(dst.arrows);
    arrows.addAll(e.es);
    eqs.addAll(src.eqs);
    eqs.addAll(dst.eqs);
    for (FOED k : e.as) {
        for (Pair<List<String>, List<String>> v : k.eqs) {
            List<String> l = new LinkedList<>(v.first);
            List<String> r = new LinkedList<>(v.second);
            l.removeAll(Util.singList(k.a));
            r.removeAll(Util.singList(k.a));
            eqs.add(new Pair<>(l, r));
        }
    }
    for (String n : src.nodes) {
        em_s.put(n, Util.singList(n));
    }
    for (String n : dst.nodes) {
        em_t.put(n, Util.singList(n));
    }
    for (Triple<String, String, String> n : src.arrows) {
        em_s.put(n.first, Util.singList(n.first));
    }
    for (Triple<String, String, String> n : dst.arrows) {
        em_t.put(n.first, Util.singList(n.first));
    }
    for (Object n : src1.allTerms()) {
        if (em_s.containsKey(n)) {
            continue;
        }
        em_s.put(n, Util.singList(n));
    }
    for (Object n : dst1.allTerms()) {
        if (em_t.containsKey(n)) {
            continue;
        }
        em_t.put(n, Util.singList(n));
    }
    XSchema X = new XSchema(nodes, arrows, eqs);
    XCtx Y = (XCtx) X.accept(env, this);
    XMapping F = new XMapping(src1, Y, em_s, "mapping");
    XMapping G = new XMapping(dst1, Y, em_t, "mapping");
    XCtx J = F.apply0(I);
    return G.delta(J);
}
Also used : HashMap(java.util.HashMap) FOED(catdata.fpql.XExp.XSOED.FOED) LinkedList(java.util.LinkedList) Triple(catdata.Triple) XSchema(catdata.fpql.XExp.XSchema) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap) Pair(catdata.Pair) XPair(catdata.fpql.XExp.XPair)

Aggregations

Triple (catdata.Triple)116 Pair (catdata.Pair)93 LinkedList (java.util.LinkedList)84 List (java.util.List)75 HashMap (java.util.HashMap)65 Map (java.util.Map)49 HashSet (java.util.HashSet)47 LinkedHashMap (java.util.LinkedHashMap)36 Set (java.util.Set)28 Chc (catdata.Chc)22 Util (catdata.Util)18 En (catdata.aql.exp.SchExpRaw.En)18 Ty (catdata.aql.exp.TyExpRaw.Ty)18 Ctx (catdata.Ctx)17 Sym (catdata.aql.exp.TyExpRaw.Sym)17 Collectors (java.util.stream.Collectors)17 Att (catdata.aql.exp.SchExpRaw.Att)16 Fk (catdata.aql.exp.SchExpRaw.Fk)16 Tuple3 (org.jparsec.functors.Tuple3)16 Quad (catdata.Quad)13