Search in sources :

Example 46 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XPoly method coapply.

XCtx<C> coapply(XCtx<D> I) {
    Map types = new HashMap<>();
    Set eqs = new HashSet<>();
    for (D d : dst.allIds()) {
        for (D x : I.terms()) {
            Pair<D, D> t = I.type(x);
            if (!t.first.equals("_1")) {
                throw new RuntimeException();
            }
            if (!t.second.equals(d)) {
                continue;
            }
            for (C k : T(d).terms()) {
                Pair<C, C> u = T(d).type(k);
                if (!u.first.equals("_1")) {
                    throw new RuntimeException();
                }
                C c = u.second;
                Triple<D, D, C> gen = new Triple<>(d, x, k);
                types.put(gen, new Pair("_1", c));
            }
            for (Pair<List<C>, List<C>> eq : T(d).eqs) {
                Function prepend = y -> {
                    if (T(d).terms().contains(y)) {
                        return new Triple(d, x, y);
                    }
                    return y;
                };
                if (eq.first.stream().filter(T(d).terms()::contains).count() > 1) {
                    throw new RuntimeException("Has too many variables: " + eq.first);
                }
                if (eq.second.stream().filter(T(d).terms()::contains).count() > 1) {
                    throw new RuntimeException("Has too many variables: " + eq.second);
                }
                List<C> lhs = (List<C>) eq.first.stream().map(prepend).collect(Collectors.toList());
                List<C> rhs = (List<C>) eq.second.stream().map(prepend).collect(Collectors.toList());
                eqs.add(new Pair(lhs, rhs));
            }
        }
    }
    for (Pair<List<D>, List<D>> eq : I.eqs) {
        D d0 = I.type(eq.first).second;
        // must be nonempty
        Pair<D, List<D>> lhs = trySplit(eq.first, dst, d0);
        Pair<D, List<D>> rhs = trySplit(eq.second, dst, d0);
        D d1 = I.type(lhs.second).first;
        D d2 = I.type(rhs.second).first;
        XMapping<C, C> Tq1 = T(lhs.second);
        XMapping<C, C> Tq2 = T(rhs.second);
        for (C Tjd0 : T(d0).terms()) {
            List<C> a1 = Tq1.em.get(Tjd0);
            List<C> a2 = Tq2.em.get(Tjd0);
            Pair<C, List<C>> a1x = trySplit(a1, src, Tq1.dst.type(a1).second);
            Pair<C, List<C>> a2x = trySplit(a2, src, Tq2.dst.type(a2).second);
            List newlhs;
            List newrhs;
            if (lhs.first == null) {
                newlhs = eq.first;
            } else if (a1x.first == null) {
                newlhs = a1;
            } else {
                newlhs = new LinkedList();
                newlhs.add(new Triple(d1, lhs.first, a1x.first));
                newlhs.addAll(a1x.second);
            }
            if (rhs.first == null) {
                newrhs = eq.second;
            } else if (a2x.first == null) {
                newrhs = a2;
            } else {
                newrhs = new LinkedList();
                newrhs.add(new Triple(d2, rhs.first, a2x.first));
                newrhs.addAll(a2x.second);
            }
            eqs.add(new Pair(newlhs, newrhs));
        }
    }
    return new XCtx<>(new HashSet<>(), types, eqs, src.global, src, "instance");
}
Also used : JComponent(javax.swing.JComponent) CodeTextPanel(catdata.ide.CodeTextPanel) Util(catdata.Util) Set(java.util.Set) HashMap(java.util.HashMap) BorderFactory(javax.swing.BorderFactory) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Map(java.util.Map) JTabbedPane(javax.swing.JTabbedPane) Entry(java.util.Map.Entry) Triple(catdata.Triple) LinkedList(java.util.LinkedList) Pair(catdata.Pair) DefunctGlobalOptions(catdata.ide.DefunctGlobalOptions) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) Triple(catdata.Triple) Function(java.util.function.Function) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) Pair(catdata.Pair)

Example 47 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XSqlToFql method transSQLSchema.

private static String transSQLSchema(List<EExternal> in, int depth) {
    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<>();
    Set<String> seen = new HashSet<>();
    Map<String, List<String>> cols = new HashMap<>();
    Set<String> atoms = new HashSet<>();
    for (EExternal k0 : in) {
        if (k0 instanceof ECreateTable) {
            ECreateTable k = (ECreateTable) k0;
            if (seen.contains(k.name)) {
                throw new RuntimeException("Duplicate name: " + k.name);
            }
            seen.add(k.name);
            nodes.add(k.name);
            inodes.add(new Pair<>(k.name, new LinkedList<>()));
            boolean found = false;
            List<String> lcols = new LinkedList<>();
            for (Pair<String, String> col : k.types) {
                lcols.add(col.first);
                if (col.first.equals("id")) {
                    found = true;
                    continue;
                }
                // if (seen.contains(col.first)) {
                // throw new RuntimeException("Duplicate name: " + k.name);
                // }
                // seen.add(col.first);
                String ref = lookup(col.first, k.fks);
                if (ref == null) {
                    String col_t = col.second.equals("int") ? "int" : "adom";
                    attrs.add(new Triple<>(k.name + "_" + col.first, k.name, col_t));
                    iattrs.add(new Pair<>(k.name + "_" + col.first, new LinkedList<>()));
                } else {
                    if (!nodes.contains(ref)) {
                        throw new RuntimeException("Missing table " + ref + " in " + k + " (or cyclic schema with loop length > 1)");
                    }
                    arrows.add(new Triple<>(k.name + "_" + col.first, k.name, ref));
                    iarrows.add(new Pair<>(k.name + "_" + col.first, new LinkedList<>()));
                    if (ref.equals(k.name)) {
                        List<String> lhs = deep(depth - 1, k.name + "_" + col.first);
                        lhs.add(0, k.name);
                        List<String> rhs = deep(depth, k.name + "_" + col.first);
                        rhs.add(0, k.name);
                        eqs.add(new Pair<>(lhs, rhs));
                    }
                }
            }
            if (!found) {
                throw new RuntimeException("No id column in " + k);
            }
            for (Pair<String, String> fk : k.fks) {
                if (fk.first.equals("id")) {
                    throw new RuntimeException("Primary keys cannot be foreign keys.");
                }
                if (lookup(fk.first, k.types) == null) {
                    throw new RuntimeException("Missing column " + fk.first + " in " + fk);
                }
            }
            cols.put(k.name, lcols);
        }
        // add inst_ prefix below
        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);
                }
                node.add(new Pair<>("v" + tuple.get(0), "v" + tuple.get(0)));
                for (int colNum = 1; colNum < tuple.size(); colNum++) {
                    List<Pair<Object, Object>> xxx = lookup2(k.target + "_" + lcols.get(colNum), iattrs);
                    if (xxx == null) {
                        xxx = lookup2(k.target + "_" + lcols.get(colNum), iarrows);
                        if (xxx == null) {
                            throw new RuntimeException("Anomaly: please report");
                        }
                        xxx.add(new Pair<>("v" + tuple.get(0), "v" + maybeQuote(tuple.get(colNum))));
                    } else {
                        atoms.add(maybeQuote(tuple.get(colNum)));
                        xxx.add(new Pair<>("v" + tuple.get(0), maybeQuote(tuple.get(colNum))));
                    }
                }
            }
        }
    }
    arrows.addAll(attrs);
    XSchema exp = XRaToFpql.doSchema(nodes, arrows, eqs);
    // SigExp.Const exp = new SigExp.Const(nodes, attrs, arrows, eqs);
    iarrows.addAll(iattrs);
    XInst inst = XRaToFpql.doInst(inodes, iarrows, new Var("S"));
    // InstExp.Const inst = new InstExp.Const(inodes, iattrs, iarrows,
    // new SigExp.Var("S"));
    String old = "S = " + exp + "\n\nI = " + inst + " : S";
    return "adom : type\n\n" + Util.sep(atoms.stream().map(x -> x + " : adom").collect(Collectors.toList()), "\n") + "\n\n" + old;
}
Also used : Terminals(org.jparsec.Terminals) JSplitPane(javax.swing.JSplitPane) JDialog(javax.swing.JDialog) JTextField(javax.swing.JTextField) Scanners(org.jparsec.Scanners) Var(catdata.fpql.XExp.Var) Tuple3(org.jparsec.functors.Tuple3) HashMap(java.util.HashMap) SwingConstants(javax.swing.SwingConstants) Tuple4(org.jparsec.functors.Tuple4) Tuple5(org.jparsec.functors.Tuple5) GridLayout(java.awt.GridLayout) HashSet(java.util.HashSet) StringLiteral(org.jparsec.Terminals.StringLiteral) XInst(catdata.fpql.XExp.XInst) Map(java.util.Map) BorderLayout(java.awt.BorderLayout) LinkedList(java.util.LinkedList) JComboBox(javax.swing.JComboBox) JFrame(javax.swing.JFrame) Pair(catdata.Pair) ScrollPaneConstants(javax.swing.ScrollPaneConstants) JButton(javax.swing.JButton) CodeTextPanel(catdata.ide.CodeTextPanel) Util(catdata.Util) Set(java.util.Set) BorderFactory(javax.swing.BorderFactory) JOptionPane(javax.swing.JOptionPane) ActionEvent(java.awt.event.ActionEvent) Collectors(java.util.stream.Collectors) Parser(org.jparsec.Parser) JScrollPane(javax.swing.JScrollPane) Dimension(java.awt.Dimension) List(java.util.List) IntegerLiteral(org.jparsec.Terminals.IntegerLiteral) JLabel(javax.swing.JLabel) Parsers(org.jparsec.Parsers) Identifier(org.jparsec.Terminals.Identifier) Triple(catdata.Triple) XSchema(catdata.fpql.XExp.XSchema) Example(catdata.ide.Example) JTextArea(javax.swing.JTextArea) JPanel(javax.swing.JPanel) XInst(catdata.fpql.XExp.XInst) HashMap(java.util.HashMap) Var(catdata.fpql.XExp.Var) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair) HashSet(java.util.HashSet) LinkedList(java.util.LinkedList) Triple(catdata.Triple) XSchema(catdata.fpql.XExp.XSchema)

Example 48 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class Chase method split.

private static Pair<List<Triple<List<String>, List<Triple<String, String, String>>, List<Triple<String, String, String>>>>, List<Triple<List<String>, List<Triple<String, String, String>>, List<Pair<String, String>>>>> split(List<EmbeddedDependency> l) {
    List<Triple<List<String>, List<Triple<String, String, String>>, List<Triple<String, String, String>>>> ret1 = new LinkedList<>();
    List<Triple<List<String>, List<Triple<String, String, String>>, List<Pair<String, String>>>> ret2 = new LinkedList<>();
    for (EmbeddedDependency e : l) {
        Triple<List<String>, List<Triple<String, String, String>>, List<Triple<String, String, String>>> s = new Triple<>(e.forall, e.where, e.tgd);
        Triple<List<String>, List<Triple<String, String, String>>, List<Pair<String, String>>> t = new Triple<>(e.forall, e.where, e.egd);
        if (!s.third.isEmpty()) {
            ret1.add(s);
        }
        if (!t.third.isEmpty()) {
            ret2.add(t);
        }
    }
    return new Pair<>(ret1, ret2);
}
Also used : Triple(catdata.Triple) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) EmbeddedDependency(catdata.fql.sql.EmbeddedDependency) Pair(catdata.Pair)

Example 49 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class RaToFql method trans.

private static Pair<String, Const> trans(Const src, EFlower fl, String pre) {
    // SigExp src0 = new SigExp.Var("S");
    LinkedList<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
    List<String> nodes1 = new LinkedList<>();
    List<String> nodes2 = new LinkedList<>();
    List<String> nodes3 = new LinkedList<>();
    nodes1.add("adom");
    nodes2.add("adom");
    nodes2.add("guid");
    nodes3.add("adom");
    List<Triple<String, String, String>> attrs = new LinkedList<>();
    attrs.add(new Triple<>("att", "adom", "str"));
    List<Triple<String, String, String>> edges1 = new LinkedList<>();
    List<Triple<String, String, String>> edges2 = new LinkedList<>();
    List<Triple<String, String, String>> edges3 = new LinkedList<>();
    List<Pair<String, String>> inodes1 = new LinkedList<>();
    List<Pair<String, String>> inodes2 = new LinkedList<>();
    List<Pair<String, String>> inodes3 = new LinkedList<>();
    inodes1.add(new Pair<>("adom", "adom"));
    inodes2.add(new Pair<>("adom", "adom"));
    inodes3.add(new Pair<>("adom", "adom"));
    List<Pair<String, String>> iattrs = new LinkedList<>();
    iattrs.add(new Pair<>("att", "att"));
    List<Pair<String, List<String>>> iedges1 = new LinkedList<>();
    List<Pair<String, List<String>>> iedges2 = new LinkedList<>();
    List<Pair<String, List<String>>> iedges3 = new LinkedList<>();
    for (String k : fl.from.keySet()) {
        String v = fl.from.get(k);
        inodes1.add(new Pair<>(k, v));
        nodes1.add(k);
        inodes2.add(new Pair<>(k, "guid"));
        for (Triple<String, String, String> arr : src.arrows) {
            if (arr.second.equals(v)) {
                List<String> l = new LinkedList<>();
                l.add(v);
                l.add(arr.first);
                edges1.add(new Triple<>(k + "_" + arr.first, k, "adom"));
                iedges1.add(new Pair<>(k + "_" + arr.first, l));
                edges2.add(new Triple<>(k + "_" + arr.first, "guid", "adom"));
                List<String> l0 = new LinkedList<>();
                l0.add("guid");
                l0.add(k + "_" + arr.first);
                iedges2.add(new Pair<>(k + "_" + arr.first, l0));
            }
        }
    }
    List<List<Triple<String, String, String>>> eqcs = merge(edges2, fl);
    Iterator<Triple<String, String, String>> it = edges2.iterator();
    while (it.hasNext()) {
        Triple<String, String, String> k = it.next();
        for (List<Triple<String, String, String>> v : eqcs) {
            if (v.contains(k) && !v.get(0).equals(k)) {
                it.remove();
            }
        }
    }
    for (Pair<String, List<String>> kk : iedges2) {
        Triple<String, String, String> k = new Triple<>(kk.second.get(1), "guid", "adom");
        for (List<Triple<String, String, String>> v : eqcs) {
            if (v.contains(k) && !v.get(0).equals(k)) {
                List<String> xxx = new LinkedList<>();
                xxx.add("guid");
                xxx.add(v.get(0).first);
                kk.second = xxx;
                break;
            }
        }
    }
    nodes3.add("guid");
    inodes3.add(new Pair<>("guid", "guid"));
    for (String k : fl.select.keySet()) {
        Pair<String, String> v = fl.select.get(k);
        edges3.add(new Triple<>(k, "guid", "adom"));
        Triple<String, String, String> t = new Triple<>(v.first + "_" + fl.from.get(v.first) + "_" + v.second, "guid", "adom");
        if (fl.from.get(v.first) == null) {
            throw new RuntimeException(v.first + " is not selectable in " + fl);
        }
        for (List<Triple<String, String, String>> eqc : eqcs) {
            if (eqc.contains(t)) {
                List<String> li = new LinkedList<>();
                li.add("guid");
                li.add(eqc.get(0).first);
                iedges3.add(new Pair<>(k, li));
            }
        }
    }
    Const sig1 = new Const(nodes1, attrs, edges1, eqs);
    Const sig2 = new Const(nodes2, attrs, edges2, eqs);
    Const sig3 = new Const(nodes3, attrs, edges3, eqs);
    MapExp.Const map1 = new MapExp.Const(inodes1, iattrs, iedges1, sig1, new Var("S"));
    MapExp.Const map2 = new MapExp.Const(inodes2, iattrs, iedges2, src, sig2);
    MapExp.Const map3 = new MapExp.Const(inodes3, iattrs, iedges3, sig3, sig2);
    String xxx = "";
    xxx += "\n\nschema " + pre + "fromSchema = " + sig1;
    xxx += "\n\nmapping " + pre + "fromMapping = " + map1 + " : " + pre + "fromSchema -> S";
    xxx += "\n\ninstance " + pre + "fromInstance = delta " + pre + "fromMapping I";
    xxx += "\n\nschema " + pre + "whereSchema = " + sig2;
    xxx += "\n\nmapping " + pre + "whereMapping = " + map2 + " : " + pre + "fromSchema -> " + pre + "whereSchema";
    xxx += "\n\ninstance " + pre + "whereInstance = pi " + pre + "whereMapping " + pre + "fromInstance";
    xxx += "\n\nschema " + pre + "Schema = " + sig3;
    xxx += "\n\nmapping " + pre + "selectMapping = " + map3 + " : " + pre + "Schema -> " + pre + "whereSchema";
    if (fl.distinct) {
        xxx += "\n\ninstance " + pre + "selectInstance = delta " + pre + "selectMapping " + pre + "whereInstance";
        xxx += "\n\ninstance " + pre + " = relationalize " + pre + "selectInstance";
    } else {
        xxx += "\n\ninstance " + pre + " = delta " + pre + "selectMapping " + pre + "whereInstance";
    }
    String comment = longSlash + "\n/* " + "Translation of " + pre + "  */\n" + longSlash;
    return new Pair<>(comment + xxx, sig3);
}
Also used : Var(catdata.fql.decl.SigExp.Var) Const(catdata.fql.decl.SigExp.Const) Triple(catdata.Triple) MapExp(catdata.fql.decl.MapExp) Pair(catdata.Pair)

Example 50 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class RaToFql method doED.

private static MapExp.Const doED(Map<String, List<String>> cols, Map<String, String> from, List<Pair<Pair<String, String>, Pair<String, String>>> where, Const target) {
    Set<Triple<String, String, Map<String, String>>> s = new HashSet<>();
    // can process some
    Set<Pair<String, String>> e = new HashSet<>();
    Map<String, String> origin = new HashMap<>();
    Map<String, String> origin2 = new HashMap<>();
    Map<String, String> origin3 = new HashMap<>();
    for (Entry<String, String> x : from.entrySet()) {
        // a AS b
        Map<String, String> map = new HashMap<>();
        if (!cols.containsKey(x.getValue())) {
            throw new RuntimeException("No table " + x.getValue() + " found in " + cols);
        }
        origin.put(x.getKey(), x.getValue());
        for (String col : cols.get(x.getValue())) {
            map.put(col, x.getKey() + "_" + col);
            origin.put(x.getKey() + "_" + col, "adom");
            origin3.put(x.getKey() + "_" + col, x.getValue());
            origin2.put(x.getKey() + "_" + col, col);
        }
        s.add(new Triple<>(x.getValue(), x.getKey(), map));
    }
    for (Pair<Pair<String, String>, Pair<String, String>> x : where) {
        e.add(new Pair<>(x.first.first + "_" + x.first.second, x.second.first + "_" + x.second.second));
    }
    // /////////////////////////////
    Set<Pair<String, Pair<String, String>>> sx = new HashSet<>();
    for (Triple<String, String, Map<String, String>> x : s) {
        // table name, fresh, map
        for (Entry<String, String> y : x.third.entrySet()) {
            sx.add(new Pair<>(x.first, new Pair<>(x.second, y.getValue())));
        }
    }
    // ////////////////////////////
    List<String> wn = new LinkedList<>();
    List<Triple<String, String, String>> wa = new LinkedList<>();
    List<Pair<List<String>, List<String>>> we = new LinkedList<>();
    List<Triple<String, String, String>> wat = new LinkedList<>();
    for (Pair<String, Pair<String, String>> x : sx) {
        if (!wn.contains(x.second.first)) {
            wn.add(x.second.first);
        }
        if (!wn.contains(x.second.second)) {
            wn.add(x.second.second);
        }
        Triple<String, String, String> t = new Triple<>(x.first + "_" + x.second.first + "_" + x.second.second, x.second.first, x.second.second);
        if (!wa.contains(t)) {
            wa.add(t);
        }
    }
    Set<Triple<String, String, String>> eqE = new HashSet<>();
    for (Pair<String, String> eq : e) {
        Triple<String, String, String> t = new Triple<>(eq.first + "_" + eq.second, eq.first, eq.second);
        if (!wa.contains(t)) {
            wa.add(t);
            eqE.add(t);
        }
    }
    Const w = new Const(wn, wat, wa, we);
    List<Pair<String, String>> omx = new LinkedList<>();
    for (String x : wn) {
        omx.add(new Pair<>(x, origin.get(x)));
    }
    List<Pair<String, List<String>>> emx = new LinkedList<>();
    for (Triple<String, String, String> x : wa) {
        String n = origin3.get(x.third);
        String m = origin2.get(x.third);
        List<String> l = new LinkedList<>();
        if (eqE.contains(x)) {
            l.add("adom");
        } else {
            l.add(n);
            l.add(n + "_" + m);
        }
        if (!emx.contains(new Pair<>(x.first, l))) {
            emx.add(new Pair<>(x.first, l));
        }
    }
    MapExp.Const f = new MapExp.Const(omx, new LinkedList<>(), emx, w, target);
    return f;
}
Also used : Pair(catdata.Pair) Const(catdata.fql.decl.SigExp.Const) Triple(catdata.Triple) 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