Search in sources :

Example 1 with XTransConst

use of catdata.fpql.XExp.XTransConst in project fql by CategoricalData.

the class XParser method toTrans.

private static XTransConst toTrans(Object decl) {
    Tuple5 y = (Tuple5) decl;
    // org.jparsec.functors.Pair x = (org.jparsec.functors.Pair) y.a;
    Tuple3 nodes = (Tuple3) y.a;
    // Tuple3 arrows = (Tuple3) x.b;
    List nodes0 = (List) nodes.b;
    // List arrows0 = (List) arrows.b;
    List<Pair<Pair<String, String>, List<String>>> eqsX = new LinkedList<>();
    for (Object o : nodes0) {
        Tuple3 u = (Tuple3) o;
        List<String> m = (List<String>) u.c;
        if (u.a instanceof Tuple3) {
            Tuple3 n = (Tuple3) u.a;
            eqsX.add(new Pair<>(new Pair<>(n.a.toString(), n.c.toString()), m));
        } else {
            String n = (String) u.a;
            eqsX.add(new Pair<>(new Pair<>(n, null), m));
        }
    }
    XTransConst ret = new XTransConst(toExp(y.c), toExp(y.e), eqsX);
    return ret;
}
Also used : Tuple5(org.jparsec.functors.Tuple5) XTransConst(catdata.fpql.XExp.XTransConst) Tuple3(org.jparsec.functors.Tuple3) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) Pair(catdata.Pair) XPair(catdata.fpql.XExp.XPair)

Example 2 with XTransConst

use of catdata.fpql.XExp.XTransConst in project fql by CategoricalData.

the class XRaToFpql 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<String, List<Pair<Object, Object>>>(adom, adomT));
    // iattrs.add(new Pair<String, List<Pair<Object, Object>>>("att", attT));
    // attrs.add(new Triple<>("att", adom, "adom"));
    Set<Object> enums = new HashSet<>();
    Map<String, Object> 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);
            if (lcols == null) {
                throw new RuntimeException("Missing: " + 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 = "v" + count++;
                node.add(new Pair<>(id, id));
                for (int colNum = 0; colNum < tuple.size(); colNum++) {
                    Object xxx = dom1.get(tuple.get(colNum));
                    if (xxx == null) {
                        // was 2nd=count
                        dom1.put(tuple.get(colNum), tuple.get(colNum));
                        enums.add(tuple.get(colNum));
                        // adomT.add(new Pair<Object, Object>(count, count));
                        // adomT.add(new Pair<Object, Object>(count, tuple.get(colNum)
                        // ));
                        xxx = dom1.get(tuple.get(colNum));
                    // 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);
        }
    }
    XSchema exp = doSchema(nodes, /* attrs, */
    arrows, eqs);
    XInst inst = doInst(inodes, /* iattrs, */
    iarrows, new Var("S"));
    // int ctx = 0;
    String xxx = "\n\n";
    Map<String, String> schemas = new HashMap<>();
    Map<String, XSchema> 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, XSchema> yyy = trans(exp, fl, k, enums);
            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" + k + "_temp = (" + g.l + " + " + g.r + ")";
                xxx += "\n\n" + k + " = relationalize " + k + "_temp";
            } else {
                xxx += "\n\n" + k + " = (" + g.l + " + " + g.r + ")";
            }
            xxx += "\n\n";
        } else if (gh instanceof EED) {
            EED c = (EED) gh;
            XInst f = doED(/*cols, */
            c.from1, c.where1, exp);
            c.from2.putAll(c.from1);
            c.where2.addAll(c.where1);
            XInst g = doED(/* cols, */
            c.from2, c.where2, exp);
            List<Pair<Pair<String, String>, List<String>>> vm = new LinkedList<>();
            for (Pair<String, String> x : f.nodes) {
                List<String> l = new LinkedList<>();
                l.add(x.first);
                vm.add(new Pair<>(new Pair<>(x.first, null), l));
            }
            XTransConst i = new XTransConst(f, g, vm);
            xxx += longSlash + "\n/* Translation of " + k + " */\n" + longSlash;
            xxx += "\n\n" + k + "A = " + f + " : S";
            xxx += "\n\n" + k + "E = " + g + " : S";
            xxx += "\n\n" + k + "I = " + i + " : " + k + "A -> " + k + "E";
            xxx += "\n\n";
        } else {
            throw new RuntimeException();
        }
    }
    String comment = "//schema S and instance I represent the entire input database.\n\n";
    String preS = "adom : type\n";
    String senums0 = preS + Util.sep(enums.stream().map(x -> x + " : adom").collect(Collectors.toList()), "\n");
    return comment + senums0 + "\n\nS = " + exp + "\n\nI = " + inst + " : S" + xxx;
}
Also used : Terminals(org.jparsec.Terminals) java.util(java.util) JSplitPane(javax.swing.JSplitPane) JDialog(javax.swing.JDialog) Scanners(org.jparsec.Scanners) Var(catdata.fpql.XExp.Var) XMapConst(catdata.fpql.XExp.XMapConst) Tuple3(org.jparsec.functors.Tuple3) SwingConstants(javax.swing.SwingConstants) Tuple4(org.jparsec.functors.Tuple4) Tuple5(org.jparsec.functors.Tuple5) GridLayout(java.awt.GridLayout) StringLiteral(org.jparsec.Terminals.StringLiteral) XInst(catdata.fpql.XExp.XInst) BorderLayout(java.awt.BorderLayout) JComboBox(javax.swing.JComboBox) JFrame(javax.swing.JFrame) Pair(catdata.Pair) ScrollPaneConstants(javax.swing.ScrollPaneConstants) JButton(javax.swing.JButton) CodeTextPanel(catdata.ide.CodeTextPanel) XTransConst(catdata.fpql.XExp.XTransConst) Util(catdata.Util) BorderFactory(javax.swing.BorderFactory) JOptionPane(javax.swing.JOptionPane) ActionEvent(java.awt.event.ActionEvent) Collectors(java.util.stream.Collectors) Parser(org.jparsec.Parser) Language(catdata.ide.Language) JScrollPane(javax.swing.JScrollPane) Dimension(java.awt.Dimension) 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) XTransConst(catdata.fpql.XExp.XTransConst) XInst(catdata.fpql.XExp.XInst) Var(catdata.fpql.XExp.Var) Pair(catdata.Pair) Triple(catdata.Triple) XSchema(catdata.fpql.XExp.XSchema)

Aggregations

Pair (catdata.Pair)2 XTransConst (catdata.fpql.XExp.XTransConst)2 Tuple3 (org.jparsec.functors.Tuple3)2 Tuple5 (org.jparsec.functors.Tuple5)2 Triple (catdata.Triple)1 Util (catdata.Util)1 Var (catdata.fpql.XExp.Var)1 XInst (catdata.fpql.XExp.XInst)1 XMapConst (catdata.fpql.XExp.XMapConst)1 XPair (catdata.fpql.XExp.XPair)1 XSchema (catdata.fpql.XExp.XSchema)1 CodeTextPanel (catdata.ide.CodeTextPanel)1 Example (catdata.ide.Example)1 Language (catdata.ide.Language)1 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 GridLayout (java.awt.GridLayout)1 ActionEvent (java.awt.event.ActionEvent)1 java.util (java.util)1 LinkedList (java.util.LinkedList)1