Search in sources :

Example 6 with Ty

use of catdata.aql.exp.TyExpRaw.Ty in project fql by CategoricalData.

the class ColimSchExpModify method eval.

// TODO aql add options
@Override
public ColimitSchema<N> eval(AqlEnv env) {
    boolean checkJava = !(Boolean) env.defaults.getOrDefault(options, AqlOption.allow_java_eqs_unsafe);
    ColimitSchema<N> colim0 = colim.eval(env);
    // colim0.schemaStr.co
    for (Pair<String, String> k : ens) {
        colim0 = colim0.renameEntity(new En(k.first), new En(k.second), checkJava);
    }
    for (Pair<Pair<String, String>, String> k : fks0) {
        colim0 = colim0.renameFk(new Fk(new En(k.first.first), k.first.second), new Fk(new En(k.first.first), k.second), checkJava);
    }
    for (Pair<Pair<String, String>, String> k : atts0) {
        colim0 = colim0.renameAtt(new Att(new En(k.first.first), k.first.second), new Att(new En(k.first.first), k.second), checkJava);
    }
    for (Pair<Pair<String, String>, List<String>> k : fks) {
        if (!colim0.schemaStr.fks.containsKey(new Fk(new En(k.first.first), k.first.second))) {
            throw new RuntimeException("Not an fk: " + k.first + " in\n\n" + colim0.schemaStr);
        }
        String pre = "In processing " + k.first + " -> " + k.second + ", ";
        Collage<Ty, En, Sym, Fk, Att, Void, Void> xxx = colim0.schemaStr.collage();
        RawTerm term = RawTerm.fold(k.second, "v");
        En tr = colim0.schemaStr.fks.get(new Fk(new En(k.first.first), k.first.second)).second;
        Ctx<String, Chc<Ty, En>> ctx = new Ctx<>("v", Chc.inRight(new En(k.first.first)));
        Term<Ty, En, Sym, Fk, Att, Gen, Sk> t = RawTerm.infer1x(ctx.map, term, null, Chc.inRight(tr), xxx.convert(), pre, colim0.schemaStr.typeSide.js).second;
        // colim0 = colim0.removeAtt(new Att(k.first), new Var(k.second.first), t.convert(), checkJava);
        colim0 = colim0.removeFk(new Fk(new En(k.first.first), k.first.second), t.toFkList(), checkJava);
    }
    for (Pair<Pair<String, String>, Triple<String, String, RawTerm>> k : atts) {
        if (!colim0.schemaStr.atts.containsKey(new Att(new En(k.first.first), k.first.second))) {
            throw new RuntimeException("Not an attribute: " + k.first + " in\n\n" + colim0.schemaStr);
        }
        String pre = "In processing " + k.first + " -> lambda " + k.second.first + "." + k.second.third + ", ";
        Pair<En, Ty> r = colim0.schemaStr.atts.get(new Att(new En(k.first.first), k.first.second));
        if (k.second.second != null && !k.second.second.equals(r.first)) {
            throw new RuntimeException(pre + " given type is " + k.second.second + " but expected " + r.first);
        }
        Collage<Ty, En, Sym, Fk, Att, Void, Void> xxx = colim0.schemaStr.collage();
        Ctx<String, Chc<Ty, En>> ctx = new Ctx<>(k.second.first, Chc.inRight(r.first));
        Term<Ty, En, Sym, Fk, Att, Gen, Sk> t = RawTerm.infer1x(ctx.map, k.second.third, null, Chc.inLeft(r.second), xxx.convert(), pre, colim0.schemaStr.typeSide.js).second;
        colim0 = colim0.removeAtt(new Att(new En(k.first.first), k.first.second), new Var(k.second.first), t.convert(), checkJava);
    }
    return colim0;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) Var(catdata.aql.Var) En(catdata.aql.exp.SchExpRaw.En) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair) Fk(catdata.aql.exp.SchExpRaw.Fk) Ty(catdata.aql.exp.TyExpRaw.Ty) Sym(catdata.aql.exp.TyExpRaw.Sym) RawTerm(catdata.aql.RawTerm) Triple(catdata.Triple) Gen(catdata.aql.exp.InstExpRaw.Gen) Sk(catdata.aql.exp.InstExpRaw.Sk) Chc(catdata.Chc)

Example 7 with Ty

use of catdata.aql.exp.TyExpRaw.Ty in project fql by CategoricalData.

the class EasikAql method translate1.

private static Pair<SchExp<?, ?, ?, ?, ?>, List<Pair<String, EdsExpRaw>>> translate1(Node sketch, Set<String> used, Set<String> warnings, String sname) {
    List<String> ens = new LinkedList<>();
    List<Pair<String, Pair<String, Ty>>> atts = new LinkedList<>();
    List<Pair<String, Pair<String, String>>> fks = new LinkedList<>();
    List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
    // there shouldn't be observation equations in easik
    // List<Quad<String, String, RawTerm, RawTerm>> eqs2 = new
    // LinkedList<>();
    List<Pair<String, EdsExpRaw>> edsExps = new LinkedList<>();
    NodeList l = sketch.getChildNodes();
    for (int temp = 0; temp < l.getLength(); temp++) {
        Node n = l.item(temp);
        NodeList j = n.getChildNodes();
        for (int temp2 = 0; temp2 < j.getLength(); temp2++) {
            Node m = j.item(temp2);
            if (m.getNodeName().equals("entity")) {
                String nodeName = safe(m.getAttributes().getNamedItem("name").getTextContent());
                ens.add(nodeName);
                NodeList k = m.getChildNodes();
                for (int temp3 = 0; temp3 < k.getLength(); temp3++) {
                    Node w = k.item(temp3);
                    if (w.getNodeName().equals("attribute")) {
                        String attName = safe(w.getAttributes().getNamedItem("name").getTextContent());
                        String tyName = w.getAttributes().getNamedItem("attributeTypeClass").getTextContent();
                        used.add(tyName);
                        atts.add(new Pair<>(nodeName + "_" + attName.replace(" ", "_"), new Pair<>(nodeName, new Ty(easikTypeToString(tyName)))));
                    }
                }
            } else if (m.getNodeName().equals("edge")) {
                String ename = safe(m.getAttributes().getNamedItem("id").getTextContent());
                String esrc = safe(m.getAttributes().getNamedItem("source").getTextContent());
                fks.add(new Pair<>(ename, new Pair<>(esrc, safe(m.getAttributes().getNamedItem("target").getTextContent()))));
                if (m.getAttributes().getNamedItem("type").getTextContent().equals("injective")) {
                    List<EdExpRaw> eds = new LinkedList<>();
                    List<Pair<String, String>> As = new LinkedList<>();
                    As.add(new Pair<>("x", esrc));
                    As.add(new Pair<>("y", esrc));
                    List<Pair<RawTerm, RawTerm>> Aeqs = new LinkedList<>();
                    Aeqs.add(new Pair<>(new RawTerm(ename, Util.singList(new RawTerm("x"))), new RawTerm(ename, Util.singList(new RawTerm("y")))));
                    List<Pair<String, String>> Es = new LinkedList<>();
                    List<Pair<RawTerm, RawTerm>> Eeqs = new LinkedList<>();
                    Eeqs.add(new Pair<>(new RawTerm("x"), new RawTerm("y")));
                    EdExpRaw ed = new EdExpRaw(As, Aeqs, Es, Eeqs, false, null);
                    eds.add(ed);
                    edsExps.add(new Pair<>("injective", new EdsExpRaw(new SchExpVar(sname), new LinkedList<>(), eds, null)));
                }
                if (m.getAttributes().getNamedItem("type").getTextContent().equals("partial")) {
                    warnings.add("Not exported - partial edges.  Their AQL semantics is unclear");
                }
            } else if (m.getNodeName().equals("uniqueKey")) {
                String esrc = safe(m.getAttributes().getNamedItem("noderef").getTextContent());
                List<String> atts0 = new LinkedList<>();
                for (int w = 0; w < m.getChildNodes().getLength(); w++) {
                    Node node = m.getChildNodes().item(w);
                    if (!node.getNodeName().equals("attref")) {
                        continue;
                    }
                    String att = safe(node.getAttributes().getNamedItem("name").getTextContent());
                    atts0.add(att);
                }
                List<EdExpRaw> eds = new LinkedList<>();
                List<Pair<String, String>> As = new LinkedList<>();
                As.add(new Pair<>("x", esrc));
                As.add(new Pair<>("y", esrc));
                List<Pair<RawTerm, RawTerm>> Aeqs = new LinkedList<>();
                for (String att : atts0) {
                    Aeqs.add(new Pair<>(new RawTerm(esrc + "_" + att, Util.singList(new RawTerm("x"))), new RawTerm(esrc + "_" + att, Util.singList(new RawTerm("y")))));
                }
                List<Pair<String, String>> Es = new LinkedList<>();
                List<Pair<RawTerm, RawTerm>> Eeqs = new LinkedList<>();
                Eeqs.add(new Pair<>(new RawTerm("x"), new RawTerm("y")));
                EdExpRaw ed = new EdExpRaw(As, Aeqs, Es, Eeqs, false, null);
                eds.add(ed);
                edsExps.add(new Pair<>("key", new EdsExpRaw(new SchExpVar(sname), new LinkedList<>(), eds, null)));
            } else if (m.getNodeName().equals("commutativediagram")) {
                NodeList k = m.getChildNodes();
                Node w1 = null;
                Node w2 = null;
                for (int temp4 = 0; temp4 < k.getLength(); temp4++) {
                    Node wX = k.item(temp4);
                    if (wX.getNodeName().equals("path") && w1 == null) {
                        w1 = wX;
                    } else if (wX.getNodeName().equals("path") && w2 == null) {
                        w2 = wX;
                    }
                }
                if (w1 == null || w2 == null) {
                    throw new RuntimeException("Easik to AQL internal error");
                }
                String cod1 = safe(w1.getAttributes().getNamedItem("domain").getTextContent());
                String cod2 = safe(w2.getAttributes().getNamedItem("domain").getTextContent());
                List<String> lhs = new LinkedList<>();
                List<String> rhs = new LinkedList<>();
                lhs.add(cod1);
                rhs.add(cod2);
                NodeList lhsX = w1.getChildNodes();
                for (int temp3 = 0; temp3 < lhsX.getLength(); temp3++) {
                    if (!lhsX.item(temp3).getNodeName().equals("edgeref")) {
                        continue;
                    }
                    String toAdd = safe(lhsX.item(temp3).getAttributes().getNamedItem("id").getTextContent());
                    lhs.add(toAdd);
                }
                NodeList rhsX = w2.getChildNodes();
                for (int temp3 = 0; temp3 < rhsX.getLength(); temp3++) {
                    if (!rhsX.item(temp3).getNodeName().equals("edgeref")) {
                        continue;
                    }
                    String toAdd = safe(rhsX.item(temp3).getAttributes().getNamedItem("id").getTextContent());
                    rhs.add(toAdd);
                }
                eqs.add(new Pair<>(lhs, rhs));
            }
        }
    }
    SchExp<?, ?, ?, ?, ?> schExp = new SchExpRaw(new TyExpVar<>("sql"), new LinkedList<>(), ens, fks, eqs, atts, new LinkedList<>(), new LinkedList<>(), null);
    return new Pair<>(schExp, edsExps);
}
Also used : Node(org.w3c.dom.Node) EdExpRaw(catdata.aql.exp.EdsExpRaw.EdExpRaw) NodeList(org.w3c.dom.NodeList) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) Ty(catdata.aql.exp.TyExpRaw.Ty) NodeList(org.w3c.dom.NodeList) RawTerm(catdata.aql.RawTerm) LinkedList(java.util.LinkedList) SchExpVar(catdata.aql.exp.SchExp.SchExpVar)

Example 8 with Ty

use of catdata.aql.exp.TyExpRaw.Ty in project fql by CategoricalData.

the class EdsExpRaw method eval.

@Override
public Constraints<Ty, En, Sym, Fk, Att> eval(AqlEnv env) {
    Schema<Ty, En, Sym, Fk, Att> sch = schema.eval(env);
    Collection<ED<Ty, En, Sym, Fk, Att>> l = new LinkedList<>();
    for (String k : imports) {
        @SuppressWarnings("unchecked") Constraints<Ty, En, Sym, Fk, Att> v = env.defs.eds.get(k);
        l.addAll(v.eds);
    }
    for (EdExpRaw e : eds) {
        l.add(e.eval(sch, new AqlOptions(options, null, env.defaults)));
    }
    return new Constraints<>(sch, l, new AqlOptions(options, null, env.defaults));
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) En(catdata.aql.exp.SchExpRaw.En) LinkedList(java.util.LinkedList) Constraints(catdata.aql.Constraints) AqlOptions(catdata.aql.AqlOptions) ED(catdata.aql.ED)

Example 9 with Ty

use of catdata.aql.exp.TyExpRaw.Ty in project fql by CategoricalData.

the class InstExpCsvQuotient method eval.

@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> eval(AqlEnv env) {
    Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> J = I.eval(env);
    Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col = new Collage<>(J.collage());
    AqlOptions strat = new AqlOptions(options, col, env.defaults);
    Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>>> eqs0 = new HashSet<>(J.eqs());
    Map<String, String> map = new HashMap<>();
    for (String q : queries) {
        map.put(q, q);
    }
    try {
        Map<En, List<String[]>> ret = InstExpCsv.start2(map, strat, J.schema(), true);
        for (En q : ret.keySet()) {
            for (String[] row : ret.get(q)) {
                if (row.length != 2) {
                    throw new RuntimeException("On " + q + ", encountered a row of length != 2: " + Arrays.toString(row));
                }
                Gen gen1 = (Gen) row[0];
                Gen gen2 = (Gen) row[1];
                if (gen1 == null) {
                    throw new RuntimeException("Encountered a NULL generator in column 1 of " + q);
                }
                if (gen2 == null) {
                    throw new RuntimeException("Encountered a NULL generator in column 2 of " + q);
                }
                if (!J.gens().containsKey(gen1)) {
                    throw new RuntimeException("Cannot import record linkage: " + gen1 + " is not a generator in the input instance");
                } else if (!J.gens().containsKey(gen2)) {
                    throw new RuntimeException("Cannot import record linkage: " + gen2 + " is not a generator in the input instance");
                }
                Term<Ty, En, Sym, Fk, Att, Gen, Sk> l = Term.Gen(gen1);
                Term<Ty, En, Sym, Fk, Att, Gen, Sk> r = Term.Gen(gen2);
                eqs0.add(new Pair<>(l, r));
                col.eqs.add(new Eq<>(new Ctx<>(), l, r));
            }
        }
        InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Sk, ID> initial0 = new InitialAlgebra<>(strat, J.schema(), col, new It(), Object::toString, Object::toString);
        return new LiteralInstance<>(J.schema(), col.gens.map, col.sks.map, eqs0, initial0.dp(), initial0, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe));
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage());
    }
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) HashMap(java.util.HashMap) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) It(catdata.aql.It) LiteralInstance(catdata.aql.fdm.LiteralInstance) InitialAlgebra(catdata.aql.fdm.InitialAlgebra) List(java.util.List) ID(catdata.aql.It.ID) Pair(catdata.Pair) HashSet(java.util.HashSet) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage)

Example 10 with Ty

use of catdata.aql.exp.TyExpRaw.Ty in project fql by CategoricalData.

the class InstExpCsv method joinedEn.

@Override
protected void joinedEn(Map<En, List<String[]>> rows, En en0, Pair<List<Pair<String, String>>, List<Pair<String, String>>> s, Schema<Ty, En, Sym, Fk, Att> sch) throws Exception {
    String en = en0.str;
    Map<String, String> inner;
    if (s == null) {
        inner = new HashMap<>();
    } else {
        inner = Util.toMapSafely(s.second);
    }
    boolean autoGenIds = (Boolean) op.getOrDefault(inner, AqlOption.csv_generate_ids);
    for (En en2 : rows.keySet()) {
        if (rows.get(en2).size() == 0) {
            throw new RuntimeException("No header in CSV file for " + en2);
        }
    }
    // index of each column name
    Ctx<String, Integer> m = new Ctx<>();
    for (int i = 0; i < rows.get(en0).get(0).length; i++) {
        m.put(rows.get(en0).get(0)[i], i);
    }
    boolean prepend = (boolean) op.getOrDefault(inner, AqlOption.csv_prepend_entity);
    String sep = (String) op.getOrDefault(inner, AqlOption.import_col_seperator);
    String pre = (String) op.getOrDefault(inner, AqlOption.csv_import_prefix);
    // System.out.println("prefix is " + pre);
    Map<String, String> map;
    if (s != null) {
        map = new Ctx<>(Util.toMapSafely(s.first)).map;
    } else {
        map = new HashMap<>();
    }
    Function<String, String> mediate = x -> {
        if (map.containsKey(x)) {
            return map.get(x);
        }
        String z = map.containsKey(x) ? map.get(x) : x;
        if (prepend) {
            int i = x.indexOf(en + sep);
            if (i != 0) {
                return pre + z;
            }
            String temp = x.substring((en + sep).length());
            return pre + temp;
        }
        return pre + z;
    };
    int startId = 0;
    for (String[] row : rows.get(en0).subList(1, rows.get(en0).size())) {
        Gen l0;
        String idCol = map.containsKey(en) ? map.get(en) : (String) op.getOrDefault(inner, AqlOption.id_column_name);
        if (autoGenIds && !m.containsKey(idCol)) {
            l0 = toGen(en0, "" + startId++);
        } else if (!autoGenIds && !m.containsKey(idCol)) {
            throw new RuntimeException("On " + en + ", ID column " + idCol + " not found in headers " + m.keySet() + ". \n\nPossible solution: provide a mapping.\n\nPossible solution: set csv_generate_ids=true to auto-generate IDs.\n\nPossible solution: rename the headers in the CSV file.\n\nPossible solution: add an ID column to the CSV file.");
        } else {
            l0 = toGen(en0, row[m.get(idCol)]);
        }
        ens0.get(en0).add(l0);
        for (Fk fk : sch.fksFrom(en0)) {
            if (!fks0.containsKey(l0)) {
                fks0.put(l0, new Ctx<>());
            }
            Gen g = toGen(sch.fks.get(fk).second, row[m.get(mediate.apply(fk.str))]);
            fks0.get(l0).put(fk, g);
        }
        for (Att att : sch.attsFrom(en0)) {
            if (!atts0.containsKey(l0)) {
                atts0.put(l0, new Ctx<>());
            }
            String zz = mediate.apply(att.str);
            if (!m.containsKey(zz)) {
                throw new RuntimeException("No column " + att + " in file for " + en + " nor explicit mapping for " + att + " given. Tried " + zz + " and options are " + m.keySet());
            }
            int z = m.get(zz);
            if (z >= row.length) {
                throw new RuntimeException("Cannot get index " + z + " from " + Arrays.toString(row));
            }
            String o = row[z];
            Term<Ty, Void, Sym, Void, Void, Void, Null<?>> r = objectToSk(sch, o, l0, att, tys0, extraRepr, true, nullOnErr);
            atts0.get(l0).put(att, r);
        }
    }
}
Also used : AqlOptions(catdata.aql.AqlOptions) Ctx(catdata.Ctx) Arrays(java.util.Arrays) En(catdata.aql.exp.SchExpRaw.En) CSVReader(com.opencsv.CSVReader) Sym(catdata.aql.exp.TyExpRaw.Sym) HashMap(java.util.HashMap) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOption(catdata.aql.AqlOptions.AqlOption) Function(java.util.function.Function) CSVParser(com.opencsv.CSVParser) Map(java.util.Map) HashCodeBuilder(org.apache.commons.lang3.builder.HashCodeBuilder) Term(catdata.aql.Term) LinkedList(java.util.LinkedList) Pair(catdata.Pair) EqualsBuilder(org.apache.commons.lang3.builder.EqualsBuilder) Null(catdata.Null) Fk(catdata.aql.exp.SchExpRaw.Fk) Util(catdata.Util) CSVParserBuilder(com.opencsv.CSVParserBuilder) Field(java.lang.reflect.Field) Att(catdata.aql.exp.SchExpRaw.Att) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Schema(catdata.aql.Schema) FileReader(java.io.FileReader) CSVReaderBuilder(com.opencsv.CSVReaderBuilder) CSVReaderNullFieldIndicator(com.opencsv.enums.CSVReaderNullFieldIndicator) Ty(catdata.aql.exp.TyExpRaw.Ty) Att(catdata.aql.exp.SchExpRaw.Att) Null(catdata.Null) Fk(catdata.aql.exp.SchExpRaw.Fk) Ty(catdata.aql.exp.TyExpRaw.Ty) Ctx(catdata.Ctx) Sym(catdata.aql.exp.TyExpRaw.Sym) En(catdata.aql.exp.SchExpRaw.En) Gen(catdata.aql.exp.InstExpRaw.Gen)

Aggregations

Ty (catdata.aql.exp.TyExpRaw.Ty)43 En (catdata.aql.exp.SchExpRaw.En)36 Sym (catdata.aql.exp.TyExpRaw.Sym)35 Att (catdata.aql.exp.SchExpRaw.Att)33 Fk (catdata.aql.exp.SchExpRaw.Fk)33 Ctx (catdata.Ctx)29 Pair (catdata.Pair)27 HashSet (java.util.HashSet)21 Gen (catdata.aql.exp.InstExpRaw.Gen)20 Chc (catdata.Chc)19 List (java.util.List)19 Triple (catdata.Triple)18 AqlOptions (catdata.aql.AqlOptions)18 HashMap (java.util.HashMap)17 Term (catdata.aql.Term)15 Sk (catdata.aql.exp.InstExpRaw.Sk)15 LinkedList (java.util.LinkedList)15 Collage (catdata.aql.Collage)14 RawTerm (catdata.aql.RawTerm)14 Set (java.util.Set)12