Search in sources :

Example 1 with XInst

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

the class XNeo4jToFQL method trans0.

private static String trans0(Map<String, Map<String, Object>> properties, Map<String, Set<Pair<String, String>>> edges) {
    labelForProperty(properties);
    Map<String, Set<String>> pfl = propsForLabels(properties);
    Map<String, Pair<String, String>> sfe = sortsForEges(edges, properties);
    XSchema xxx = toSchema(pfl, sfe);
    XInst yyy = toInst(properties, edges);
    return "dom : type\n" + Util.sep(adom(properties), " ") + " : dom\n\nS = " + xxx + "\n\nI = " + yyy + " : S\n";
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) XSchema(catdata.fpql.XExp.XSchema) XInst(catdata.fpql.XExp.XInst) Pair(catdata.Pair)

Example 2 with XInst

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

the class XParser method toInstConst.

/* public static final Parser<?> instance(Reference ref) {
		Parser<?> node = Parsers.tuple(ident(), term(":"), ident());
		Parser<?> p3 = Parsers.tuple(path(), term("="), path());
		Parser<?> xxx = Parsers.tuple(section("variables", node), 
				section("equations", p3));
		Parser kkk = ((Parser)term("INSTANCE")).or((Parser) term("instance"));
		Parser<?> constant = Parsers
				.tuple(kkk, xxx.between(term("{"), term("}")), term(":"),
						ref.lazy());
		return constant;  */
private static XInst toInstConst(Object decl) {
    Tuple4 y = (Tuple4) decl;
    org.jparsec.functors.Pair x = (org.jparsec.functors.Pair) y.b;
    Tuple3 nodes = (Tuple3) x.a;
    Tuple3 arrows = (Tuple3) x.b;
    List nodes0 = (List) nodes.b;
    List arrows0 = (List) arrows.b;
    List<Pair<String, String>> nodesX = new LinkedList<>();
    for (Object o : nodes0) {
        Tuple3 u = (Tuple3) o;
        List<String> n2 = (List) u.a;
        String l = (String) u.c;
        for (String n : n2) {
            // String n = (String) u.a;
            nodesX.add(new Pair<>(n, l));
        }
    }
    List<Pair<List<String>, List<String>>> eqsX = new LinkedList<>();
    for (Object o : arrows0) {
        Tuple3 u = (Tuple3) o;
        List<String> n = (List<String>) u.a;
        List<String> m = (List<String>) u.c;
        eqsX.add(new Pair<>(n, m));
    }
    XInst ret = new XInst(toExp(y.d), nodesX, eqsX);
    if (y.a.toString().equals("INSTANCE")) {
        ret.saturated = true;
    }
    return ret;
}
Also used : XInst(catdata.fpql.XExp.XInst) LinkedList(java.util.LinkedList) Tuple4(org.jparsec.functors.Tuple4) Tuple3(org.jparsec.functors.Tuple3) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) XPair(catdata.fpql.XExp.XPair)

Example 3 with XInst

use of catdata.fpql.XExp.XInst 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 4 with XInst

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

the class XNeo4jToFQL method toInst.

private static XInst toInst(Map<String, Map<String, Object>> properties, Map<String, Set<Pair<String, String>>> edges) {
    List<Pair<String, String>> data = new LinkedList<>();
    List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
    for (String n : properties.keySet()) {
        Map<String, Object> props = properties.get(n);
        data.add(new Pair<>(n, (String) props.get("label")));
        for (String p : props.keySet()) {
            if (p.equals("label")) {
                continue;
            }
            List<String> l = new LinkedList<>();
            List<String> r = new LinkedList<>();
            l.add(n);
            l.add(p);
            r.add((String) props.get(p));
            eqs.add(new Pair<>(l, r));
        }
    }
    for (String e : edges.keySet()) {
        for (Pair<String, String> p : edges.get(e)) {
            List<String> l = new LinkedList<>();
            List<String> r = new LinkedList<>();
            l.add(p.first);
            l.add(e);
            r.add(p.second);
            eqs.add(new Pair<>(l, r));
        }
    }
    XInst ret = new XInst(new Var("S"), data, eqs);
    return ret;
}
Also used : XInst(catdata.fpql.XExp.XInst) Var(catdata.fpql.XExp.Var) LinkedList(java.util.LinkedList) Pair(catdata.Pair)

Example 5 with XInst

use of catdata.fpql.XExp.XInst 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)5 XInst (catdata.fpql.XExp.XInst)5 Var (catdata.fpql.XExp.Var)3 XSchema (catdata.fpql.XExp.XSchema)3 LinkedList (java.util.LinkedList)3 Tuple3 (org.jparsec.functors.Tuple3)3 Tuple4 (org.jparsec.functors.Tuple4)3 Triple (catdata.Triple)2 Util (catdata.Util)2 CodeTextPanel (catdata.ide.CodeTextPanel)2 Example (catdata.ide.Example)2 BorderLayout (java.awt.BorderLayout)2 Dimension (java.awt.Dimension)2 GridLayout (java.awt.GridLayout)2 ActionEvent (java.awt.event.ActionEvent)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 BorderFactory (javax.swing.BorderFactory)2