Search in sources :

Example 91 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XProd method fst.

public static <X> XMapping fst(XCtx<X> I, XCtx<X> J) {
    XCtx<Pair<Triple<X, X, List<X>>, Triple<X, X, List<X>>>> IJ = prod(I, J);
    Map em = new HashMap<>();
    for (Pair<Triple<X, X, List<X>>, Triple<X, X, List<X>>> x : IJ.types.keySet()) {
        List l = new LinkedList<>();
        l.add(x.first.first);
        l.addAll(x.first.third);
        em.put(x, l);
    }
    for (Object x : IJ.allTerms()) {
        if (em.containsKey(x)) {
            continue;
        }
        List l = new LinkedList<>();
        l.add(x);
        em.put(x, l);
    }
    return new XMapping<>(IJ, I, em, "homomorphism");
}
Also used : Triple(catdata.Triple) HashMap(java.util.HashMap) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList) Pair(catdata.Pair)

Example 92 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XProd method uberflower.

// : make sure this is conjunctive otherwise throw an error //duplicate for later
// : on saturated with discrete op will be saturated
// : must add (not query label) (TARGET NODE) EVEN FOR THE CONJUNCTIVE CASE //add label here
// : do pre-filtering based on lhs = const (ground) here //won't help
public static <C, D> XCtx<Pair<Object, Map<Object, Triple<C, C, List<C>>>>> uberflower(XPoly<C, D> poly, XCtx<C> I) {
    // XCtx c = frozen(flower, I.schema);
    Map<Object, Set<Map<Object, Triple<C, C, List<C>>>>> top = new HashMap<>();
    Map<Object, XCtx> frozens = new HashMap();
    for (Object flower_name : poly.blocks.keySet()) {
        Pair<D, Block<C, D>> flowerX = poly.blocks.get(flower_name);
        // D flower_dst = flowerX.first;
        Block<C, D> flower = flowerX.second;
        Set<Map<Object, Triple<C, C, List<C>>>> ret = new HashSet<>();
        ret.add(new HashMap<>());
        for (Object var : flower.from.keySet()) {
            C node = flower.from.get(var);
            Set<Map<Object, Triple<C, C, List<C>>>> ret2 = new HashSet<>();
            for (Map<Object, Triple<C, C, List<C>>> tuple : ret) {
                for (Triple<C, C, List<C>> t : I.cat().hom((C) "_1", node)) {
                    Map<Object, Triple<C, C, List<C>>> merged = new HashMap<>(tuple);
                    merged.put(var, t);
                    String result = eval(convert2(flower.where), merged, flower.from.keySet(), I);
                    if (result.equals("false")) {
                        continue;
                    }
                    ret2.add(merged);
                }
            }
            ret = ret2;
        }
        top.put(flower_name, ret);
        frozens.put(flower_name, frozen(flower, I.schema));
    }
    checkEdges(poly, frozens);
    // instance
    Set<Pair<Object, Map<Object, Triple<C, C, List<C>>>>> ids = new HashSet<>();
    Map<Pair<Object, Map<Object, Triple<C, C, List<C>>>>, Pair<Pair<Object, Map<Object, Triple<C, C, List<C>>>>, Pair<Object, Map<Object, Triple<C, C, List<C>>>>>> types = new HashMap<>();
    Set<Pair<List<Pair<Object, Map<Object, Triple<C, C, List<C>>>>>, List<Pair<Object, Map<Object, Triple<C, C, List<C>>>>>>> eqs = new HashSet<>();
    for (Object flower_name : poly.blocks.keySet()) {
        Set<Map<Object, Triple<C, C, List<C>>>> ret = top.get(flower_name);
        Pair<D, Block<C, D>> flowerX = poly.blocks.get(flower_name);
        D flower_dst = flowerX.first;
        Block<C, D> flower = flowerX.second;
        XCtx c = frozens.get(flower_name);
        for (Map<Object, Triple<C, C, List<C>>> k : ret) {
            types.put(new Pair<>(flower_name, k), new Pair("_1", flower_dst));
            for (D edge : flower.attrs.keySet()) {
                Object tgt = c.type(flower.attrs.get(edge)).second;
                if (!I.global.ids.contains(tgt)) {
                    throw new RuntimeException("Selection path " + edge + " does not target a type");
                }
                List lhs = new LinkedList();
                lhs.add(new Pair<>(flower_name, k));
                lhs.add(edge);
                // must normalize in I
                List<C> rhs0 = subst_new(flower.attrs.get(edge), k, new HashSet(), new HashSet());
                Triple<C, C, List<C>> rhs = I.find_fast(new Triple("_1", tgt, rhs0));
                List rhsX = new LinkedList();
                if (I.schema.cat().hom((C) "_1", (C) tgt).contains(rhs)) {
                    if (rhs.third.isEmpty()) {
                        rhsX.add(rhs.first);
                    } else {
                        rhsX.addAll(rhs.third);
                    }
                } else {
                    rhsX.add(rhs);
                }
                eqs.add(new Pair(lhs, rhsX));
            }
            for (D edge : flower.edges.keySet()) {
                // D tgt = poly.dst.type(edge).second;
                List lhs = new LinkedList();
                lhs.add(new Pair<>(flower_name, k));
                lhs.add(edge);
                // must normalize in I
                Map rhs0Q = new HashMap();
                for (Object str : flower.edges.get(edge).second.keySet()) {
                    List<C> rhs0Z = subst_new(flower.edges.get(edge).second.get(str), k, new HashSet(), new HashSet());
                    rhs0Q.put(str, rhs0Z);
                }
                Map found = null;
                outer: for (Map<Object, Triple<C, C, List<C>>> map : top.get(flower.edges.get(edge).first)) {
                    for (Object str : map.keySet()) {
                        if (!I.getKB().equiv(map.get(str).third, (List<C>) rhs0Q.get(str))) {
                            continue outer;
                        }
                    }
                    if (found != null) {
                        throw new RuntimeException();
                    }
                    found = map;
                }
                if (found == null) {
                    throw new RuntimeException("Cannot find ID " + rhs0Q + " in " + top.get(flower.edges.get(edge)));
                }
                List rhsX = new LinkedList();
                rhsX.add(new Pair<>(flower.edges.get(edge).first, found));
                eqs.add(new Pair(lhs, rhsX));
            }
        }
    }
    Map types0 = types;
    for (C t : I.global.ids) {
        for (Triple<C, C, List<C>> arr : I.cat().hom((C) "_1", t)) {
            if (I.global.cat().hom((C) "_1", t).contains(arr)) {
                continue;
            }
            types0.put(arr, new Pair<>("_1", t));
            for (Entry<C, Pair<C, C>> e : I.global.types.entrySet()) {
                if (!e.getValue().first.equals(t)) {
                    continue;
                }
                List lhs = new LinkedList();
                lhs.add(arr);
                lhs.add(e.getKey());
                List<C> rhs0 = new LinkedList<>();
                // rhs0.add(arr.second);
                rhs0.addAll(arr.third);
                rhs0.add(e.getKey());
                Triple<C, C, List<C>> rhsX = I.find_fast(new Triple<>((C) "_1", e.getValue().second, rhs0));
                List rhs = new LinkedList();
                if (I.schema.cat().hom((C) "_1", e.getValue().second).contains(rhsX)) {
                    if (rhsX.third.isEmpty()) {
                        rhs.add(rhsX.first);
                    } else {
                        rhs.addAll(rhsX.third);
                    }
                } else {
                    rhs.add(rhsX);
                }
                eqs.add(new Pair<>(lhs, rhs));
            }
        }
    }
    XCtx J = new XCtx(ids, types, eqs, I.global, poly.dst, "instance");
    J.saturated = true;
    return J;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) List(java.util.List) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Pair(catdata.Pair) LinkedList(java.util.LinkedList) Triple(catdata.Triple) Block(catdata.fpql.XPoly.Block) HashMap(java.util.HashMap) Map(java.util.Map)

Example 93 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class InstOps method visit.

@Override
public List<PSM> visit(String env, Coreturn e) {
    String xxx = "coreturn_temp_xxx";
    List<PSM> ret = new LinkedList<>();
    InstExp i1 = prog.insts.get(e.inst);
    if (i1 instanceof Sigma) {
        String middle = ((Sigma) i1).I;
        // can't be null
        InstExp i2 = prog.insts.get(middle);
        Mapping f = ((Sigma) i1).F.toMap(prog);
        if (i2 instanceof Delta) {
            for (Node n : f.target.nodes) {
                List<Flower> u = new LinkedList<>();
                for (Node m : f.source.nodes) {
                    if (!f.nm.get(m).equals(n)) {
                        continue;
                    }
                    u.add(new CopyFlower(middle + "_" + m.string + "_subst_inv", "c0", "c1"));
                }
                ret.add(new SimpleCreateTable(xxx, PSM.VARCHAR(), false));
                ret.add(new InsertSQL(xxx, new Union(u), "c0", "c1"));
                ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(e.inst + "_" + n.string + "_subst_inv", xxx), "c0", "c1"));
                ret.add(new DropTable(xxx));
            }
        }
    } else if (i1 instanceof FullSigma) {
        String middle = ((FullSigma) i1).I;
        // can't be null
        InstExp i2 = prog.insts.get(middle);
        Mapping f = ((FullSigma) i1).F.toMap(prog);
        if (i2 instanceof Delta) {
            ret.add(new FullSigmaCounit(f, ((Delta) i2).I, middle, e.inst, env));
        } else {
            throw new RuntimeException();
        }
    } else 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 Pi) {
            Pi input0 = ((Pi) i2);
            String input = input0.I;
            try {
                Map<String, Triple<Node, Node, Arr<Node, Path>>[]> colmap = PSMGen.pi(f, input, middle).second;
                for (Node m : f.source.nodes) {
                    Node n = f.nm.get(m);
                    Triple<Node, Node, Arr<Node, Path>>[] col = colmap.get(n.string);
                    Triple<Node, Node, Arr<Node, Path>> toFind = new Triple<>(n, m, new Arr<>(new Path(f.target, n), n, n));
                    int i = 0;
                    boolean found = false;
                    for (Triple<Node, Node, Arr<Node, Path>> cand : col) {
                        if (cand.equals(toFind)) {
                            found = true;
                            Map<String, String> from = new HashMap<>();
                            from.put("lim", middle + "_" + n + "_limit");
                            LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
                            select.put("c0", new Pair<>("lim", "guid"));
                            select.put("c1", new Pair<>("lim", "c" + i));
                            List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
                            Flower flower = new Flower(select, from, where);
                            ret.add(new SimpleCreateTable(xxx, PSM.VARCHAR(), false));
                            ret.add(new InsertSQL(xxx, flower, "c0", "c1"));
                            ret.add(new InsertSQL(env + "_" + m, PSMGen.compose(e.inst + "_" + m + "_subst_inv", xxx), "c0", "c1"));
                            ret.add(new DropTable(xxx));
                            break;
                        }
                        i++;
                    }
                    if (!found) {
                        throw new RuntimeException();
                    }
                }
            } 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) CopyFlower(catdata.fql.sql.CopyFlower) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FullSigma(catdata.fql.decl.InstExp.FullSigma) Sigma(catdata.fql.decl.InstExp.Sigma) DropTable(catdata.fql.sql.DropTable) Union(catdata.fql.sql.Union) LinkedHashMap(java.util.LinkedHashMap) FQLException(catdata.fql.FQLException) SimpleCreateTable(catdata.fql.sql.SimpleCreateTable) Pi(catdata.fql.decl.InstExp.Pi) Pair(catdata.Pair) FullSigmaCounit(catdata.fql.sql.FullSigmaCounit) 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)

Example 94 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class RaToFql method doAdom.

// always adom and guid
private static String doAdom(Const A, String a) {
    String k = a;
    List<Pair<List<String>, List<String>>> eeqs = new LinkedList<>();
    // List<Triple<String, String, String>> attrs = new LinkedList<>();
    // attrs.add(new Triple<>("att", "adom", "str"));
    List<Triple<String, String, String>> dd_attrs = new LinkedList<>();
    dd_attrs.add(new Triple<>("att", "x", "str"));
    List<Triple<String, String, String>> e_attrs = new LinkedList<>();
    List<String> bn = new LinkedList<>();
    bn.add("r");
    bn.add("d");
    List<Triple<String, String, String>> barrs = new LinkedList<>();
    List<Pair<String, String>> abn = new LinkedList<>();
    abn.add(new Pair<>("r", "guid"));
    abn.add(new Pair<>("d", "adom"));
    List<Pair<String, String>> abatt = new LinkedList<>();
    abatt.add(new Pair<>("att", "att"));
    List<Pair<String, String>> e_abatt = new LinkedList<>();
    List<Pair<String, List<String>>> abarr = new LinkedList<>();
    List<String> cn = new LinkedList<>();
    cn.add("r");
    cn.add("d");
    cn.add("m");
    List<Triple<String, String, String>> carrs = new LinkedList<>();
    carrs.add(new Triple<>("f", "m", "d"));
    List<Pair<String, String>> bcn = new LinkedList<>();
    bcn.add(new Pair<>("r", "r"));
    bcn.add(new Pair<>("d", "d"));
    List<Pair<String, List<String>>> bcarr = new LinkedList<>();
    List<Triple<String, String, String>> bbarrs = new LinkedList<>();
    bbarrs.add(new Triple<>("f", "a", "b"));
    bbarrs.add(new Triple<>("g", "a", "c"));
    bbarrs.add(new Triple<>("h", "b", "d"));
    bbarrs.add(new Triple<>("i", "c", "d"));
    List<String> bbn = new LinkedList<>();
    bbn.add("r");
    bbn.add("a");
    bbn.add("b");
    bbn.add("c");
    bbn.add("d");
    List<Triple<String, String, String>> ccarrs = new LinkedList<>();
    List<String> ccn = new LinkedList<>();
    List<Pair<List<String>, List<String>>> cceqs = new LinkedList<>();
    ccn.add("r");
    ccn.add("a");
    ccn.add("b");
    ccn.add("c");
    ccn.add("d");
    ccn.add("e");
    ccarrs.add(new Triple<>("f", "a", "b"));
    ccarrs.add(new Triple<>("g", "a", "c"));
    ccarrs.add(new Triple<>("h", "b", "d"));
    ccarrs.add(new Triple<>("i", "c", "d"));
    ccarrs.add(new Triple<>("ff", "e", "b"));
    ccarrs.add(new Triple<>("gg", "e", "c"));
    List<String> l1 = new LinkedList<>();
    l1.add("e");
    l1.add("ff");
    l1.add("h");
    List<String> l2 = new LinkedList<>();
    l2.add("e");
    l2.add("gg");
    l2.add("i");
    cceqs.add(new Pair<>(l1, l2));
    List<Triple<String, String, String>> ddarrs = new LinkedList<>();
    List<Pair<List<String>, List<String>>> ddeqs = new LinkedList<>();
    List<String> ddn = new LinkedList<>();
    ddn.add("r");
    ddn.add("v");
    ddn.add("w");
    ddn.add("x");
    ddn.add("y");
    ddarrs.add(new Triple<>("f", "v", "w"));
    ddarrs.add(new Triple<>("g", "w", "x"));
    ddarrs.add(new Triple<>("h", "x", "y"));
    ddarrs.add(new Triple<>("ff", "v", "w"));
    l1 = new LinkedList<>();
    l1.add("v");
    l1.add("f");
    l1.add("g");
    l2 = new LinkedList<>();
    l2.add("v");
    l2.add("ff");
    l2.add("g");
    ddeqs.add(new Pair<>(l1, l2));
    List<Pair<String, String>> ffn = new LinkedList<>();
    List<Pair<String, List<String>>> ffarr = new LinkedList<>();
    ffn.add(new Pair<>("r", "r"));
    ffn.add(new Pair<>("a", "m"));
    ffn.add(new Pair<>("b", "m"));
    ffn.add(new Pair<>("c", "m"));
    ffn.add(new Pair<>("d", "d"));
    l1 = new LinkedList<>();
    l1.add("m");
    ffarr.add(new Pair<>("f", l1));
    l1 = new LinkedList<>();
    l1.add("m");
    ffarr.add(new Pair<>("g", l1));
    l1 = new LinkedList<>();
    l1.add("m");
    l1.add("f");
    ffarr.add(new Pair<>("h", l1));
    l1 = new LinkedList<>();
    l1.add("m");
    l1.add("f");
    ffarr.add(new Pair<>("i", l1));
    List<Pair<String, String>> ggn = new LinkedList<>();
    ggn.add(new Pair<>("r", "r"));
    ggn.add(new Pair<>("a", "a"));
    ggn.add(new Pair<>("b", "b"));
    ggn.add(new Pair<>("c", "c"));
    ggn.add(new Pair<>("d", "d"));
    List<Pair<String, List<String>>> ggarr = new LinkedList<>();
    l1 = new LinkedList<>();
    l1.add("a");
    l1.add("f");
    ggarr.add(new Pair<>("f", l1));
    l1 = new LinkedList<>();
    l1.add("a");
    l1.add("g");
    ggarr.add(new Pair<>("g", l1));
    l1 = new LinkedList<>();
    l1.add("b");
    l1.add("h");
    ggarr.add(new Pair<>("h", l1));
    l1 = new LinkedList<>();
    l1.add("c");
    l1.add("i");
    ggarr.add(new Pair<>("i", l1));
    List<Pair<String, String>> hhn = new LinkedList<>();
    hhn.add(new Pair<>("r", "r"));
    hhn.add(new Pair<>("e", "v"));
    hhn.add(new Pair<>("a", "w"));
    hhn.add(new Pair<>("b", "w"));
    hhn.add(new Pair<>("c", "w"));
    hhn.add(new Pair<>("d", "y"));
    List<Pair<String, List<String>>> hharr = new LinkedList<>();
    l1 = new LinkedList<>();
    l1.add("w");
    hharr.add(new Pair<>("f", l1));
    l1 = new LinkedList<>();
    l1.add("w");
    hharr.add(new Pair<>("g", l1));
    l1 = new LinkedList<>();
    l1.add("w");
    l1.add("g");
    l1.add("h");
    hharr.add(new Pair<>("h", l1));
    l1 = new LinkedList<>();
    l1.add("w");
    l1.add("g");
    l1.add("h");
    hharr.add(new Pair<>("i", l1));
    l1 = new LinkedList<>();
    l1.add("v");
    l1.add("f");
    hharr.add(new Pair<>("ff", l1));
    l1 = new LinkedList<>();
    l1.add("v");
    l1.add("ff");
    hharr.add(new Pair<>("gg", l1));
    List<Pair<String, String>> iin = new LinkedList<>();
    iin.add(new Pair<>("guid", "r"));
    iin.add(new Pair<>("adom", "x"));
    List<Pair<String, List<String>>> iiarr = new LinkedList<>();
    int i = 0;
    for (Triple<String, String, String> m0 : A.arrows) {
        String m = m0.first;
        bn.add("m" + i);
        barrs.add(new Triple<>("i" + i, "r", "m" + i));
        barrs.add(new Triple<>("f" + i, "m" + i, "d"));
        abn.add(new Pair<>("m" + i, "guid"));
        List<String> l = new LinkedList<>();
        l.add("guid");
        abarr.add(new Pair<>("i" + i, l));
        l = new LinkedList<>();
        l.add("guid");
        l.add(m);
        abarr.add(new Pair<>("f" + i, l));
        carrs.add(new Triple<>("i" + i, "r", "m"));
        bcn.add(new Pair<>("m" + i, "m"));
        l = new LinkedList<>();
        l.add("r");
        l.add("i" + i);
        bcarr.add(new Pair<>("i" + i, l));
        l = new LinkedList<>();
        l.add("m");
        l.add("f");
        bcarr.add(new Pair<>("f" + i, l));
        bbarrs.add(new Triple<>("i" + i, "r", "a"));
        ccarrs.add(new Triple<>("i" + i, "r", "a"));
        ddarrs.add(new Triple<>("i" + i, "r", "w"));
        l = new LinkedList<>();
        l.add("r");
        l.add("i" + i);
        ffarr.add(new Pair<>("i" + i, l));
        l = new LinkedList<>();
        l.add("r");
        l.add("i" + i);
        ggarr.add(new Pair<>("i" + i, l));
        l = new LinkedList<>();
        l.add("r");
        l.add("i" + i);
        hharr.add(new Pair<>("i" + i, l));
        l = new LinkedList<>();
        l.add("r");
        l.add("i" + i);
        l.add("g");
        iiarr.add(new Pair<>(m, l));
        i++;
    }
    Const b = new Const(bn, e_attrs, barrs, eeqs);
    // F
    MapExp.Const ab = new MapExp.Const(abn, e_abatt, abarr, b, A);
    Const c = new Const(cn, e_attrs, carrs, eeqs);
    // G
    MapExp.Const bc = new MapExp.Const(bcn, e_abatt, bcarr, b, c);
    Const bb = new Const(bbn, e_attrs, bbarrs, eeqs);
    Const cc = new Const(ccn, e_attrs, ccarrs, cceqs);
    Const dd = new Const(ddn, dd_attrs, ddarrs, ddeqs);
    MapExp.Const ff = new MapExp.Const(ffn, e_abatt, ffarr, bb, c);
    MapExp.Const gg = new MapExp.Const(ggn, e_abatt, ggarr, bb, cc);
    MapExp.Const hh = new MapExp.Const(hhn, e_abatt, hharr, c, dd);
    MapExp.Const ii = new MapExp.Const(iin, abatt, iiarr, A, dd);
    String ret = "///////////////\n";
    ret += "schema " + k + "_B = " + b + "\n\n";
    ret += "mapping " + k + "_F = " + ab + " : " + k + "_B -> " + a + "\n\n";
    ret += "schema " + k + "_C = " + c + "\n\n";
    ret += "mapping " + k + "_G = " + bc + " : " + k + "_B -> " + k + "_C\n\n";
    ret += "schema " + k + "_BB = " + bb + "\n\n";
    ret += "schema " + k + "_CC = " + cc + "\n\n";
    ret += "schema " + k + "_DD = " + dd + "\n\n";
    ret += "mapping " + k + "_FF = " + ff + " : " + k + "_BB -> " + k + "_C\n\n";
    ret += "mapping " + k + "_GG = " + gg + " : " + k + "_BB -> " + k + "_CC\n\n";
    ret += "mapping " + k + "_HH = " + hh + " : " + k + "_CC -> " + k + "_DD\n\n";
    ret += "mapping " + k + "_II = " + ii + " : " + a + " -> " + k + "_DD\n\n";
    ret += "//////////////\n";
    return ret;
// emit as "as_rel"
}
Also used : Const(catdata.fql.decl.SigExp.Const) Triple(catdata.Triple) MapExp(catdata.fql.decl.MapExp) Pair(catdata.Pair)

Example 95 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class RaToFql method transSQLSchema.

private static String transSQLSchema(List<Pair<String, EExternal>> in) {
    List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
    List<Triple<String, String, String>> arrows = new LinkedList<>();
    List<Triple<String, String, String>> attrs = new LinkedList<>();
    List<String> nodes = new LinkedList<>();
    List<Pair<String, List<Pair<Object, Object>>>> inodes = new LinkedList<>();
    List<Pair<String, List<Pair<Object, Object>>>> iattrs = new LinkedList<>();
    List<Pair<String, List<Pair<Object, Object>>>> iarrows = new LinkedList<>();
    String adom = "adom";
    nodes.add(adom);
    List<Pair<Object, Object>> adomT = new LinkedList<>();
    LinkedList<Pair<Object, Object>> attT = new LinkedList<>();
    inodes.add(new Pair<>(adom, adomT));
    iattrs.add(new Pair<>("att", attT));
    attrs.add(new Triple<>("att", adom, "str"));
    Set<Object> enums = new HashSet<>();
    Map<String, Integer> dom1 = new HashMap<>();
    List<Pair<String, EExternal>> queries = new LinkedList<>();
    int count = 0;
    Set<String> seen = new HashSet<>();
    Map<String, List<String>> cols = new HashMap<>();
    for (Pair<String, EExternal> kk0 : in) {
        EExternal k0 = kk0.second;
        // String key = kk0.first;
        if (k0 instanceof ECreateTable) {
            ECreateTable k = (ECreateTable) k0;
            if (seen.contains(k.name)) {
                throw new RuntimeException("Duplicate name: " + k.name);
            }
            if (k.name.equals("adom") || k.name.equals("att")) {
                throw new RuntimeException("The names adom and att cannot be used.");
            }
            seen.add(k.name);
            nodes.add(k.name);
            inodes.add(new Pair<>(k.name, new LinkedList<>()));
            List<String> lcols = new LinkedList<>();
            for (Pair<String, String> col : k.types) {
                lcols.add(col.first);
                if (seen.contains(col.first)) {
                    throw new RuntimeException("Duplicate name: " + col.first);
                }
                seen.add(col.first);
                arrows.add(new Triple<>(k.name + "_" + col.first, k.name, adom));
                iarrows.add(new Pair<>(k.name + "_" + col.first, new LinkedList<>()));
            }
            cols.put(k.name, lcols);
        }
        if (k0 instanceof EInsertValues) {
            EInsertValues k = (EInsertValues) k0;
            List<String> lcols = cols.get(k.target);
            for (List<String> tuple : k.values) {
                if (lcols.size() != tuple.size()) {
                    throw new RuntimeException("Column size mismatch " + tuple + " in " + k.target);
                }
                List<Pair<Object, Object>> node = lookup2(k.target, inodes);
                if (node == null) {
                    throw new RuntimeException("Missing table " + k.target);
                }
                String id = "" + count++;
                node.add(new Pair<>(id, id));
                for (int colNum = 0; colNum < tuple.size(); colNum++) {
                    Integer xxx = dom1.get(tuple.get(colNum));
                    if (xxx == null) {
                        dom1.put(tuple.get(colNum), count);
                        enums.add(tuple.get(colNum));
                        adomT.add(new Pair<>(count, count));
                        attT.add(new Pair<>(count, "\"" + tuple.get(colNum) + "\""));
                        xxx = count;
                        count++;
                    }
                    List<Pair<Object, Object>> yyy = lookup2(k.target + "_" + lcols.get(colNum), iarrows);
                    if (yyy == null) {
                        throw new RuntimeException("Anomaly: please report");
                    }
                    yyy.add(new Pair<>(id, xxx));
                }
            }
        }
        if (k0 instanceof EFlower || k0 instanceof EUnion || k0 instanceof EDiff || k0 instanceof EED) {
            queries.add(kk0);
        }
    }
    Const exp = new Const(nodes, attrs, arrows, eqs);
    InstExp.Const inst = new InstExp.Const(inodes, iattrs, iarrows, new Var("S"));
    // int ctx = 0;
    String xxx = "\n\n";
    Map<String, String> schemas = new HashMap<>();
    Map<String, Const> schemas0 = new HashMap<>();
    Map<String, Boolean> done = new HashMap<>();
    for (Pair<String, EExternal> gh0 : queries) {
        String k = gh0.first;
        EExternal gh = gh0.second;
        if (gh instanceof EFlower) {
            EFlower fl = (EFlower) gh;
            Pair<String, Const> yyy = trans(exp, fl, k);
            xxx += yyy.first + "\n\n";
            schemas.put(k, k + "Schema");
            schemas0.put(k, yyy.second);
        } else if (gh instanceof EUnion) {
            EUnion g = (EUnion) gh;
            String s1 = schemas.get(g.l);
            schemas.put(k, s1);
            schemas0.put(k, schemas0.get(g.l));
            xxx += longSlash + "\n/* Translation of " + k + "  */\n" + longSlash;
            if (g.distinct) {
                xxx += "\n\n" + "instance " + k + "_temp = (" + g.l + " + " + g.r + ")";
                xxx += "\n\n" + "instance " + k + " = relationalize " + k + "_temp";
            } else {
                xxx += "\n\n" + "instance " + k + " = (" + g.l + " + " + g.r + ")";
            }
            xxx += "\n\n";
        } else if (gh instanceof EDiff) {
            String f1x = ((EDiff) gh).l;
            String f2x = ((EDiff) gh).r;
            String s1 = schemas.get(f1x);
            Const s1x = schemas0.get(f1x);
            schemas.put(k, s1);
            String f1y = doAdom(s1x, s1);
            if (!done.containsKey(s1)) {
                xxx += "\n\n" + f1y;
                done.put(s1, true);
            }
            xxx += doAdom2(s1, f1x, k + "_l");
            xxx += doAdom2(s1, f2x, k + "_r");
            String f1 = k + "_l" + "_rel";
            String f2 = k + "_r" + "_rel";
            xxx += longSlash + "\n/* Translation of " + k + "  */\n" + longSlash;
            xxx += "\n\ninstance " + k + "prp = prop " + s1;
            xxx += "\n\ninstance " + k + "one = unit " + s1;
            xxx += "\n\ninstance " + k + "prp2 = (" + k + "prp * " + k + "prp)";
            xxx += "\n\ntransform " + k + f1 + "t = " + k + "one.unit " + f1;
            xxx += "\n\ntransform " + k + f2 + "t = " + k + "one.unit " + f2;
            xxx += "\n\ntransform " + k + f1 + "tchi = " + k + "prp.char " + k + f1 + "t";
            xxx += "\n\ntransform " + k + f2 + "tchi = " + k + "prp.char " + k + f2 + "t";
            xxx += "\n\ntransform " + k + "n = (" + k + f2 + "tchi then " + k + "prp.not)";
            xxx += "\n\ntransform " + k + "j1 = " + k + "prp2.(" + k + f1 + "tchi * " + k + "n)";
            xxx += "\n\ntransform " + k + "j2 = (" + k + "j1 then " + k + "prp2.and)";
            if (((EDiff) gh).distinct) {
                xxx += "\n\ninstance " + k + "temp = kernel " + k + "j2";
                xxx += "\n\ninstance " + k + " = relationalize " + k + "temp";
            } else {
                xxx += "\n\ninstance " + k + " = kernel " + k + "j2";
            }
            xxx += "\n\n";
        } else if (gh instanceof EED) {
            EED c = (EED) gh;
            MapExp.Const f = doED(cols, c.from1, c.where1, exp);
            Const src = (Const) f.src;
            c.from2.putAll(c.from1);
            c.where2.addAll(c.where1);
            MapExp.Const g = doED(cols, c.from2, c.where2, exp);
            List<Pair<String, String>> l = new LinkedList<>();
            for (String x : src.nodes) {
                l.add(new Pair<>(x, x));
            }
            List<Pair<String, List<String>>> em = new LinkedList<>();
            for (Triple<String, String, String> e : src.arrows) {
                List<String> y = new LinkedList<>();
                y.add(e.second);
                y.add(e.first);
                em.add(new Pair<>(e.first, y));
            }
            MapExp.Const i = new MapExp.Const(l, new LinkedList<>(), em, f.src, g.src);
            xxx += longSlash + "\n/* Translation of " + k + " */\n" + longSlash;
            xxx += "\n\nschema " + k + "A = " + f.src;
            xxx += "\n\nschema " + k + "E = " + g.src;
            xxx += "\n\nmapping " + k + "I = " + i + " : " + k + "A -> " + k + "E";
            xxx += "\n\nmapping " + k + " = " + g + " : " + k + "E -> S";
            xxx += "\n\n";
        } else {
            throw new RuntimeException();
        }
    }
    String enum0 = "";
    boolean b = false;
    for (Object o : enums) {
        if (b) {
            enum0 += ", ";
        }
        b = true;
        enum0 += "\"" + o + "\"";
    }
    String comment = "//schema S and instance I represent the entire input database.\n\n";
    String preS = "enum str = { " + enum0 + " }";
    return comment + preS + "\n\nschema S = " + exp + "\n\ninstance I = " + inst + " : S" + xxx;
}
Also used : Var(catdata.fql.decl.SigExp.Var) Pair(catdata.Pair) Const(catdata.fql.decl.SigExp.Const) Triple(catdata.Triple) InstExp(catdata.fql.decl.InstExp) MapExp(catdata.fql.decl.MapExp)

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