Search in sources :

Example 76 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class SqlToOpl method convert.

// : happy medium between CNF and non-CNF? ignore as many columns as possible?
// ////////////////////////////////////////////////////////////////////////////////////////////////////
private static Pair<OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String>> convert(SqlSchema info, SqlInstance inst, String S0, String I0) {
    Set<Chc<SqlType, SqlTable>> sorts = new HashSet<>();
    Set<Chc<SqlType, SqlTable>> entities = new HashSet<>();
    Map<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, Pair<List<Chc<SqlType, SqlTable>>, Chc<SqlType, SqlTable>>> symbols = new HashMap<>();
    List<Triple<OplCtx<Chc<SqlType, SqlTable>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>>> equations = new LinkedList<>();
    for (SqlType type : info.types) {
        sorts.add(Chc.inLeft(type));
    }
    for (SqlTable table : info.tables) {
        sorts.add(Chc.inRight(table));
        entities.add(Chc.inRight(table));
        for (SqlColumn col : table.columns) {
            symbols.put(Chc.inRight(Chc.inLeft(col)), new Pair<>(Util.singList(Chc.inRight(table)), Chc.inLeft(col.type)));
        }
    }
    for (SqlForeignKey fk : info.fks) {
        symbols.put(Chc.inRight(Chc.inRight(fk)), new Pair<>(Util.singList(Chc.inRight(fk.source)), Chc.inRight(fk.target)));
        OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> head = new OplTerm<>("x");
        OplCtx<Chc<SqlType, SqlTable>, String> ctx = new OplCtx<>(Util.singList(new Pair<>("x", Chc.inRight(fk.source))));
        OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> rhs0 = new OplTerm<>(Chc.inRight(Chc.inRight(fk)), Util.singList(head));
        for (SqlColumn tcol : fk.map.keySet()) {
            SqlColumn scol = fk.map.get(tcol);
            Chc<SqlColumn, SqlForeignKey> l = Chc.inLeft(scol);
            Chc<SqlColumn, SqlForeignKey> r = Chc.inLeft(tcol);
            OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> lhs = new OplTerm<>(Chc.inRight(l), Util.singList(head));
            OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> rhs = new OplTerm<>(Chc.inRight(r), Util.singList(rhs0));
            equations.add(new Triple<>(ctx, lhs, rhs));
        }
    }
    OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sch = new OplSchema<>(S0, entities);
    OplSig<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sig = new OplSig<>(VIt.vit, new HashMap<>(), sorts, symbols, equations);
    sch.validate(sig);
    OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> I = new OplInst<>(S0, I0, "none");
    Map<String, Chc<SqlType, SqlTable>> gens = new HashMap<>();
    List<Pair<OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>, OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>>> eqs = new LinkedList<>();
    int fr = 0;
    if (inst != null) {
        Map<SqlTable, Map<Map<SqlColumn, Optional<Object>>, String>> iso1 = new HashMap<>();
        for (SqlTable table : info.tables) {
            Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
            Map<Map<SqlColumn, Optional<Object>>, String> i1 = new HashMap<>();
            // Map<String, Map<SqlColumn, Optional<Object>>> i2 = new HashMap<>();
            for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
                String i = "v" + (fr++);
                i1.put(tuple, i);
                // i2.put(i, tuple);
                gens.put(i, Chc.inRight(table));
                for (SqlColumn col : table.columns) {
                    SqlType ty = col.type;
                    Optional<Object> val = tuple.get(col);
                    if (!val.isPresent()) {
                        continue;
                    }
                    symbols.put(Chc.inLeft(val.get()), new Pair<>(new LinkedList<>(), Chc.inLeft(ty)));
                    OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inLeft(Chc.inLeft(val.get())), new LinkedList<>());
                    OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inLeft(col))), Util.singList(new OplTerm<>(Chc.inRight(i), new LinkedList<>())));
                    eqs.add(new Pair<>(lhs, rhs));
                }
            }
            iso1.put(table, i1);
        // iso2.put(table, i2);
        }
        for (SqlForeignKey fk : info.fks) {
            for (Map<SqlColumn, Optional<Object>> in : inst.get(fk.source)) {
                Map<SqlColumn, Optional<Object>> out = inst.follow(in, fk);
                String tgen = iso1.get(fk.target).get(out);
                String sgen = iso1.get(fk.source).get(in);
                OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inRight(tgen), new LinkedList<>());
                OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inRight(fk))), Util.singList(new OplTerm<>(Chc.inRight(sgen), new LinkedList<>())));
                eqs.add(new Pair<>(lhs, rhs));
            }
        }
    }
    OplPres<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> P = new OplPres<>(new HashMap<>(), S0, sig, gens, eqs);
    P.toSig();
    I.validate(sch, P, null);
    return new Pair<>(sch, I);
}
Also used : HashMap(java.util.HashMap) OplSchema(catdata.opl.OplExp.OplSchema) OplInst(catdata.opl.OplExp.OplInst) OplPres(catdata.opl.OplExp.OplPres) SqlType(catdata.sql.SqlType) HashSet(java.util.HashSet) Pair(catdata.Pair) Optional(java.util.Optional) OplSig(catdata.opl.OplExp.OplSig) LinkedList(java.util.LinkedList) Triple(catdata.Triple) SqlForeignKey(catdata.sql.SqlForeignKey) SqlTable(catdata.sql.SqlTable) SqlColumn(catdata.sql.SqlColumn) HashMap(java.util.HashMap) Map(java.util.Map) Chc(catdata.Chc)

Example 77 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class SqlToOpl method convertCnf.

// : code formatter should not wrap lines ever
private static Pair<OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String>> convertCnf(SqlSchema info, SqlInstance inst, String S0, String I0) {
    if (!info.isCnf()) {
        throw new RuntimeException("Schema not in categorical normal form");
    }
    Set<Chc<SqlType, SqlTable>> sorts = new HashSet<>();
    Set<Chc<SqlType, SqlTable>> entities = new HashSet<>();
    Map<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, Pair<List<Chc<SqlType, SqlTable>>, Chc<SqlType, SqlTable>>> symbols = new HashMap<>();
    List<Triple<OplCtx<Chc<SqlType, SqlTable>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>>> equations = new LinkedList<>();
    for (SqlType type : info.types) {
        sorts.add(Chc.inLeft(type));
    }
    for (SqlTable table : info.tables) {
        sorts.add(Chc.inRight(table));
        entities.add(Chc.inRight(table));
        for (SqlColumn col : table.columns) {
            if (col.equals(table.getCnfId())) {
                continue;
            }
            if (isFk(info, table, col)) {
                continue;
            }
            symbols.put(Chc.inRight(Chc.inLeft(col)), new Pair<>(Util.singList(Chc.inRight(table)), Chc.inLeft(col.type)));
        }
    }
    for (SqlForeignKey fk : info.fks) {
        symbols.put(Chc.inRight(Chc.inRight(fk)), new Pair<>(Util.singList(Chc.inRight(fk.source)), Chc.inRight(fk.target)));
    }
    OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sch = new OplSchema<>(S0, entities);
    OplSig<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sig = new OplSig<>(VIt.vit, new HashMap<>(), sorts, symbols, equations);
    sch.validate(sig);
    OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> I = new OplInst<>(S0, I0, "none");
    Map<String, Chc<SqlType, SqlTable>> gens = new HashMap<>();
    List<Pair<OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>, OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>>> eqs = new LinkedList<>();
    int fr = 0;
    if (inst != null) {
        Map<SqlTable, Map<Object, String>> iso1 = new HashMap<>();
        for (SqlTable table : info.tables) {
            Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
            Map<Object, String> i1 = new HashMap<>();
            // Map<String, Object> i2 = new HashMap<>();
            for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
                String i = "v" + (fr++);
                if (!tuple.get(table.getCnfId()).isPresent()) {
                    throw new RuntimeException("Anomly: please report");
                }
                i1.put(tuple.get(table.getCnfId()).get(), i);
                // i2.put(i, tuple.get(table.getCnfId()).get());
                gens.put(i, Chc.inRight(table));
                for (SqlColumn col : table.columns) {
                    if (col.equals(table.getCnfId())) {
                        continue;
                    }
                    if (isFk(info, table, col)) {
                        continue;
                    }
                    SqlType ty = col.type;
                    Optional<Object> val = tuple.get(col);
                    if (!val.isPresent()) {
                        continue;
                    }
                    symbols.put(Chc.inLeft(val.get()), new Pair<>(new LinkedList<>(), Chc.inLeft(ty)));
                    OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inLeft(Chc.inLeft(val.get())), new LinkedList<>());
                    OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inLeft(col))), Util.singList(new OplTerm<>(Chc.inRight(i), new LinkedList<>())));
                    eqs.add(new Pair<>(lhs, rhs));
                }
            }
            iso1.put(table, i1);
        // iso2.put(table, i2);
        }
        for (SqlForeignKey fk : info.fks) {
            for (Map<SqlColumn, Optional<Object>> in : inst.get(fk.source)) {
                Map<SqlColumn, Optional<Object>> out = inst.follow(in, fk);
                if (!out.get(fk.target.getCnfId()).isPresent() || !in.get(fk.source.getCnfId()).isPresent()) {
                    throw new RuntimeException("Anomaly: please report");
                }
                String tgen = iso1.get(fk.target).get(out.get(fk.target.getCnfId()).get());
                String sgen = iso1.get(fk.source).get(in.get(fk.source.getCnfId()).get());
                OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inRight(tgen), new LinkedList<>());
                OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inRight(fk))), Util.singList(new OplTerm<>(Chc.inRight(sgen), new LinkedList<>())));
                eqs.add(new Pair<>(lhs, rhs));
            }
        }
    }
    OplPres<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> P = new OplPres<>(new HashMap<>(), S0, sig, gens, eqs);
    P.toSig();
    I.validate(sch, P, null);
    return new Pair<>(sch, I);
}
Also used : HashMap(java.util.HashMap) OplSchema(catdata.opl.OplExp.OplSchema) OplInst(catdata.opl.OplExp.OplInst) OplPres(catdata.opl.OplExp.OplPres) SqlType(catdata.sql.SqlType) HashSet(java.util.HashSet) Pair(catdata.Pair) Optional(java.util.Optional) OplSig(catdata.opl.OplExp.OplSig) LinkedList(java.util.LinkedList) Triple(catdata.Triple) SqlForeignKey(catdata.sql.SqlForeignKey) SqlTable(catdata.sql.SqlTable) SqlColumn(catdata.sql.SqlColumn) HashMap(java.util.HashMap) Map(java.util.Map) Chc(catdata.Chc)

Example 78 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class MplParser method program.

public static Program<MplExp<String, String>> program(String s) {
    List<Triple<String, Integer, MplExp<String, String>>> ret = new LinkedList<>();
    List decls = (List) program.parse(s);
    for (Object d : decls) {
        org.jparsec.functors.Pair pr = (org.jparsec.functors.Pair) d;
        Tuple3 decl = (Tuple3) pr.b;
        toProgHelper(pr.a.toString(), s, ret, decl);
    }
    return new Program<>(ret, null);
}
Also used : Program(catdata.Program) Triple(catdata.Triple) Tuple3(org.jparsec.functors.Tuple3) MplPair(catdata.mpl.Mpl.MplTerm.MplPair) Pair(catdata.Pair)

Example 79 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class Pi method pi.

public static <O1, A1, O2, A2> Triple<Functor<O2, A2, Set, Fn>, Map<O2, Set<Map>>, Map<O2, Triple<O2, O1, A2>[]>> pi(Functor<O1, A1, O2, A2> F, Functor<O1, A1, Set, Fn> inst) {
    Category<O2, A2> D = F.target;
    Category<O1, A1> C = F.source;
    Map<O2, Set> ret1 = new HashMap<>();
    Map<A2, Map> ret2 = new HashMap<>();
    Map<O2, Triple<O2, O1, A2>[]> nodecats = new HashMap<>();
    Map<O2, Set<Map>> nodetables = new HashMap<>();
    for (O2 d0 : D.objects()) {
        Triple<Category<Triple<O2, O1, A2>, Quad<A2, A1, A2, A2>>, Functor<Triple<O2, O1, A2>, Quad<A2, A1, A2, A2>, O2, A2>, Functor<Triple<O2, O1, A2>, Quad<A2, A1, A2, A2>, O1, A1>> B = doComma2(D, C, F, d0);
        Set<Map> r = lim2(Functor.compose(B.third, inst));
        if (r != null) {
            ret1.put(d0, squish(r));
            nodetables.put(d0, r);
        }
        nodecats.put(d0, cnames(B.first));
    }
    for (A2 s : D.arrows()) {
        O2 dA = D.source(s);
        Set<Map> q1 = nodetables.get(dA);
        Triple<O2, O1, A2>[] cnames1 = nodecats.get(dA);
        O2 dB = D.target(s);
        Set<Map> q2 = nodetables.get(dB);
        Triple<O2, O1, A2>[] cnames2 = nodecats.get(dB);
        Set<Map> raw = product(q2, q1);
        Set<Map> rax = subset2(D, s, cnames2, cnames1, raw);
        Map<Object, Object> ray = project(rax, cnames2.length + 1, 0);
        ret2.put(s, ray);
    }
    Functor<O2, A2, Set, Fn> ret = new Functor<>(F.target, FinSet.FinSet, ret1::get, a -> new Fn<>(ret1.get(F.target.source(a)), ret1.get(F.target.target(a)), ret2.get(a)::get));
    Triple xxx = new Triple<>(ret, nodetables, nodecats);
    return xxx;
}
Also used : Fn(catdata.fqlpp.cat.FinSet.Fn) Triple(catdata.Triple)

Example 80 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class Pi method lim2.

private static <OA, AA, OB, AB, AC> Set<Map> lim2(Functor<Triple<OA, OB, AC>, Quad<AA, AB, AC, AC>, Set, Fn> I) {
    Category<Triple<OA, OB, AC>, Quad<AA, AB, AC, AC>> B = I.source;
    if (B.objects().isEmpty()) {
        Set<Map> ret = new HashSet<>();
        Map m = new HashMap();
        m.put(0, new Unit());
        ret.add(m);
        return ret;
    }
    Triple<OA, OB, AC>[] cnames = cnames(I.source);
    int temp = 0;
    List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
    // store objects of B directly
    Map<String, Object> from = new HashMap<>();
    LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
    Map<Object, Set<Map<Object, Object>>> state = new HashMap<>();
    for (Triple<OA, OB, AC> n : B.objects()) {
        from.put("t" + temp, n);
        select.put("c" + temp, new Pair<>("t" + temp, "c0"));
        temp++;
        state.put(n, shred(I.applyO(n)));
    }
    for (Quad<AA, AB, AC, AC> e : B.arrows()) {
        if (B.isId(e)) {
            continue;
        }
        from.put("t" + temp, e);
        where.add(new Pair<>(new Pair<>("t" + temp, "c0"), new Pair<>("t" + cnamelkp(cnames, B.source(e)), "c0")));
        where.add(new Pair<>(new Pair<>("t" + temp, "c1"), new Pair<>("t" + cnamelkp(cnames, B.target(e)), "c0")));
        temp++;
        state.put(e, shred(I.applyA(e)));
    }
    Flower f = new Flower(select, from, where);
    Set<Map<Object, Object>> uuu = f.eval(state);
    Set<Map> x0 = unshred(B.objects().size(), uuu);
    x0 = keygen(x0);
    return x0;
}
Also used : Quad(catdata.Quad) Unit(catdata.Unit) Pair(catdata.Pair) Triple(catdata.Triple)

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