Search in sources :

Example 16 with En

use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.

the class ColimitSchema method renameAtt.

public ColimitSchema<N> renameAtt(Att src, Att dst, boolean checkJava) {
    if (!schemaStr.atts.containsKey(src)) {
        throw new RuntimeException(src + " is not an attribute of " + src.en + " in \n" + schemaStr);
    }
    if (schemaStr.atts.containsKey(dst)) {
        throw new RuntimeException(dst + " is already an attribute in \n" + schemaStr);
    }
    Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> isoToUser = Mapping.id(schemaStr);
    Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> isoFromUser = Mapping.id(schemaStr);
    Function<Att, Att> fun = x -> x.equals(src) ? dst : x;
    Function<Att, Att> fun2 = x -> x.equals(dst) ? src : x;
    Map<Att, Pair<En, Ty>> atts = new HashMap<>();
    for (Att k : schemaStr.atts.keySet()) {
        atts.put(fun.apply(k), schemaStr.atts.get(k));
    }
    Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs = new HashSet<>();
    for (Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> eq : schemaStr.eqs) {
        eqs.add(new Triple<>(eq.first, eq.second.mapAtt(fun), eq.third.mapAtt(fun)));
    }
    DP<Ty, En, Sym, Fk, Att, Void, Void> dp = new DP<Ty, En, Sym, Fk, Att, Void, Void>() {

        @Override
        public String toStringProver() {
            return "rename attribute of " + schemaStr.dp.toStringProver();
        }

        @Override
        public boolean eq(Ctx<Var, Chc<Ty, En>> ctx, Term<Ty, En, Sym, Fk, Att, Void, Void> lhs, Term<Ty, En, Sym, Fk, Att, Void, Void> rhs) {
            return schemaStr.dp.eq(ctx, lhs.mapAtt(fun2), rhs.mapAtt(fun2));
        }
    };
    Schema<Ty, En, Sym, Fk, Att> schemaStr2 = new Schema<>(ty, schemaStr.ens, atts, schemaStr.fks.map, eqs, dp, checkJava);
    Map<Att, Triple<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>>> attsM = new HashMap<>();
    for (Att k : schemaStr.atts.keySet()) {
        attsM.put(k, new Triple<>(isoToUser.atts.get(k).first, isoToUser.atts.get(k).second, isoToUser.atts.get(k).third.mapAtt(fun)));
    }
    isoToUser = new Mapping<>(isoToUser.ens.map, attsM, isoToUser.fks.map, schemaStr, schemaStr2, checkJava);
    Map<Att, Triple<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>>> attsM2 = new HashMap<>();
    for (Att k : schemaStr2.atts.keySet()) {
        Var v = new Var("v");
        attsM2.put(k, new Triple<>(v, schemaStr2.atts.get(k).first, Term.Att(fun2.apply(k), Term.Var(v))));
    }
    isoFromUser = new Mapping<>(isoFromUser.ens.map, attsM2, isoFromUser.fks.map, schemaStr2, schemaStr, checkJava);
    return wrap(isoToUser, isoFromUser);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) Sym(catdata.aql.exp.TyExpRaw.Sym) Chc(catdata.Chc) HashMap(java.util.HashMap) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOption(catdata.aql.AqlOptions.AqlOption) Function(java.util.function.Function) HashSet(java.util.HashSet) Map(java.util.Map) UnionFind(catdata.graph.UnionFind) Pair(catdata.Pair) Quad(catdata.Quad) Fk(catdata.aql.exp.SchExpRaw.Fk) TyExpRaw(catdata.aql.exp.TyExpRaw) Util(catdata.Util) Set(java.util.Set) Att(catdata.aql.exp.SchExpRaw.Att) DMG(catdata.graph.DMG) Collectors(java.util.stream.Collectors) List(java.util.List) Sk(catdata.aql.exp.InstExpRaw.Sk) SchExpRaw(catdata.aql.exp.SchExpRaw) Triple(catdata.Triple) Ty(catdata.aql.exp.TyExpRaw.Ty) HashMap(java.util.HashMap) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) 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) Triple(catdata.Triple)

Example 17 with En

use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.

the class ColimitSchema method initialUser.

private Pair<Schema<Ty, En, Sym, Fk, Att>, Ctx<N, Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att>>> initialUser(AqlOptions options, Collage<Ty, Set<Pair<N, En>>, Sym, Pair<N, Fk>, Pair<N, Att>, Void, Void> col, Set<Triple<Pair<Var, Set<Pair<N, En>>>, Term<Ty, Set<Pair<N, En>>, Sym, Pair<N, Fk>, Pair<N, Att>, Void, Void>, Term<Ty, Set<Pair<N, En>>, Sym, Pair<N, Fk>, Pair<N, Att>, Void, Void>>> eqs, Ctx<Pair<N, En>, Set<Pair<N, En>>> eqcs, Schema<Ty, Set<Pair<N, En>>, Sym, Pair<N, Fk>, Pair<N, Att>> schema) {
    Ctx<N, Mapping<Ty, En, Sym, Fk, Att, Set<Pair<N, En>>, Pair<N, Fk>, Pair<N, Att>>> mappings = new Ctx<>();
    for (N n : nodes.keySet()) {
        Map<Att, Triple<Var, Set<Pair<N, En>>, Term<Ty, Set<Pair<N, En>>, Sym, Pair<N, Fk>, Pair<N, Att>, Void, Void>>> atts = new HashMap<>();
        Map<Fk, Pair<Set<Pair<N, En>>, List<Pair<N, Fk>>>> fks = new HashMap<>();
        Map<En, Set<Pair<N, En>>> ens0 = new HashMap<>();
        Schema<Ty, En, Sym, Fk, Att> s = nodes.get(n);
        for (En en : s.ens) {
            ens0.put(en, eqcs.get(new Pair<>(n, en)));
        }
        for (Fk fk : s.fks.keySet()) {
            fks.put(fk, new Pair<>(eqcs.get(new Pair<>(n, s.fks.get(fk).first)), Util.singList(new Pair<>(n, fk))));
        }
        for (Att att : s.atts.keySet()) {
            Var v = new Var("v");
            Term<Ty, Set<Pair<N, En>>, Sym, Pair<N, Fk>, Pair<N, Att>, Void, Void> t = Term.Att(new Pair<>(n, att), Term.Var(v));
            atts.put(att, new Triple<>(v, eqcs.get(new Pair<>(n, s.atts.get(att).first)), t));
        }
        // TODO aql allow as option?
        Mapping<Ty, En, Sym, Fk, Att, Set<Pair<N, En>>, Pair<N, Fk>, Pair<N, Att>> m = new Mapping<>(ens0, atts, fks, nodes.get(n), schema, false);
        mappings.put(n, m);
    }
    Collage<Ty, En, Sym, Fk, Att, Void, Void> colX = new Collage<>(ty.collage());
    colX.ens.addAll(col.ens.stream().map(ColimitSchema::conv1).collect(Collectors.toSet()));
    colX.atts.map.putAll(col.atts.map((k, v) -> new Pair<>(new Att(conv1(col.atts.get(k).first), conv2Att(k)), new Pair<>(conv1(v.first), v.second))).map);
    colX.fks.putAll(col.fks.map((k, v) -> new Pair<>(new Fk(conv1(col.fks.get(k).first), conv2Fk(k)), new Pair<>(conv1(v.first), conv1(v.second)))).map);
    Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqsX = eqs.stream().map(t -> new Triple<>(new Pair<>(t.first.first, conv1(t.first.second)), conv3(col, t.second), conv3(col, t.third))).collect(Collectors.toSet());
    colX.eqs.addAll(col.eqs.stream().map(t -> new Eq<>(t.ctx.map((k, v) -> new Pair<>(k, conv4(v))), conv3(col, t.lhs), conv3(col, t.rhs))).collect(Collectors.toSet()));
    // AqlOptions opsX = new AqlOptions(options, colX);
    DP<Ty, En, Sym, Fk, Att, Void, Void> dpX = AqlProver.create(options, colX, ty.js);
    Schema<Ty, En, Sym, Fk, Att> schemaStr = new Schema<>(ty, colX.ens, colX.atts.map, colX.fks.map, eqsX, dpX, false);
    Ctx<N, Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att>> mappingsStr = new Ctx<>();
    for (N n : mappings.keySet()) {
        mappingsStr.put(n, conv5(col, schemaStr, mappings.get(n)));
    }
    return new Pair<>(schemaStr, mappingsStr);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) Sym(catdata.aql.exp.TyExpRaw.Sym) Chc(catdata.Chc) HashMap(java.util.HashMap) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOption(catdata.aql.AqlOptions.AqlOption) Function(java.util.function.Function) HashSet(java.util.HashSet) Map(java.util.Map) UnionFind(catdata.graph.UnionFind) Pair(catdata.Pair) Quad(catdata.Quad) Fk(catdata.aql.exp.SchExpRaw.Fk) TyExpRaw(catdata.aql.exp.TyExpRaw) Util(catdata.Util) Set(java.util.Set) Att(catdata.aql.exp.SchExpRaw.Att) DMG(catdata.graph.DMG) Collectors(java.util.stream.Collectors) List(java.util.List) Sk(catdata.aql.exp.InstExpRaw.Sk) SchExpRaw(catdata.aql.exp.SchExpRaw) Triple(catdata.Triple) Ty(catdata.aql.exp.TyExpRaw.Ty) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) Pair(catdata.Pair) Fk(catdata.aql.exp.SchExpRaw.Fk) Ty(catdata.aql.exp.TyExpRaw.Ty) Sym(catdata.aql.exp.TyExpRaw.Sym) Triple(catdata.Triple)

Example 18 with En

use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.

the class ColimitSchema method quotient.

private static Schema<Ty, En, Sym, Fk, Att> quotient(Schema<Ty, En, Sym, Fk, Att> sch, Set<Quad<String, String, RawTerm, RawTerm>> eqTerms, AqlOptions options) {
    Collage<Ty, En, Sym, Fk, Att, Void, Void> col = new Collage<>(sch.collage());
    Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs0 = new HashSet<>(sch.eqs);
    for (Quad<String, String, RawTerm, RawTerm> eq : eqTerms) {
        Map<String, Chc<Ty, En>> ctx = Util.singMap(eq.first, eq.second == null ? null : Chc.inRight(new En(eq.second)));
        Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> eq0 = RawTerm.infer1x(ctx, eq.third, eq.fourth, null, col.convert(), "", sch.typeSide.js).first3();
        Chc<Ty, En> v = eq0.first.get(new Var(eq.first));
        if (v.left) {
            throw new RuntimeException("In " + eq.third + " = " + eq.fourth + ", variable " + eq.first + " has type " + v.l + " which is not an entity");
        }
        eqs0.add(new Triple<>(new Pair<>(new Var(eq.first), v.r), eq0.second.convert(), eq0.third.convert()));
        col.eqs.add(new Eq<>(new Ctx<>(new Var(eq.first), v), eq0.second.convert(), eq0.third.convert()));
    }
    boolean b = !(Boolean) options.getOrDefault(AqlOption.allow_java_eqs_unsafe);
    DP<Ty, En, Sym, Fk, Att, Void, Void> dp = AqlProver.create(options, col, sch.typeSide.js);
    Schema<Ty, En, Sym, Fk, Att> ret = new Schema<>(sch.typeSide, col.ens, col.atts.map, col.fks.map, eqs0, dp, b);
    return ret;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) HashSet(java.util.HashSet) Pair(catdata.Pair) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Triple(catdata.Triple) Chc(catdata.Chc)

Example 19 with En

use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.

the class ColimitSchema method removeFk.

public ColimitSchema<N> removeFk(Fk src, List<Fk> l, boolean checkJava) {
    Var v = new Var("v");
    Term<Ty, En, Sym, Fk, Att, Void, Void> t = Term.Fks(l, Term.Var(v));
    if (!schemaStr.fks.containsKey(src)) {
        throw new RuntimeException(src + " is not a foreign_key in \n" + schemaStr);
    }
    if (l.contains(src)) {
        throw new RuntimeException("Cannot replace " + src + " with " + Util.sep(l, ".") + " because that path contains " + src);
    }
    En en1 = schemaStr.fks.get(src).first;
    En en2 = schemaStr.fks.get(src).second;
    if (!schemaStr.type(new Pair<>(v, en1), t).equals(Chc.inRight(en2))) {
        throw new RuntimeException("The term " + t + " has type " + schemaStr.type(new Pair<>(v, en1), t).toStringMash() + " and not " + en2 + " as expected.");
    }
    if (!schemaStr.dp.eq(new Ctx<>(v, Chc.inRight(en1)), t, Term.Fk(src, Term.Var(v)))) {
        throw new RuntimeException("The term " + t + " is not provably equal to " + Term.Fk(src, Term.Var(v)));
    }
    Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> isoToUser = Mapping.id(schemaStr);
    Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> isoFromUser = Mapping.id(schemaStr);
    Map<Fk, Pair<En, En>> fks = new HashMap<>(schemaStr.fks.map);
    fks.remove(src);
    Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs = new HashSet<>();
    for (Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> eq : schemaStr.eqs) {
        Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> tr = new Triple<>(eq.first, eq.second.replaceHead(Head.Fk(src), Util.singList(v), t), eq.third.replaceHead(Head.Fk(src), Util.singList(v), t));
        if (!tr.second.equals(tr.third) && !eqs.contains(tr)) {
            eqs.add(tr);
        }
    }
    DP<Ty, En, Sym, Fk, Att, Void, Void> dp = new DP<Ty, En, Sym, Fk, Att, Void, Void>() {

        @Override
        public String toStringProver() {
            return "remove foreign key of " + schemaStr.dp.toStringProver();
        }

        @Override
        public boolean eq(Ctx<Var, Chc<Ty, En>> ctx, Term<Ty, En, Sym, Fk, Att, Void, Void> lhs, Term<Ty, En, Sym, Fk, Att, Void, Void> rhs) {
            return schemaStr.dp.eq(ctx, lhs, rhs);
        }
    };
    Schema<Ty, En, Sym, Fk, Att> schemaStr2 = new Schema<>(ty, schemaStr.ens, schemaStr.atts.map, fks, eqs, dp, checkJava);
    Map<Fk, Pair<En, List<Fk>>> fksM = new HashMap<>(isoToUser.fks.map);
    fksM.put(src, new Pair<>(en1, l));
    isoToUser = new Mapping<>(isoToUser.ens.map, isoToUser.atts.map, fksM, schemaStr, schemaStr2, checkJava);
    Map<Fk, Pair<En, List<Fk>>> fksM2 = new HashMap<>(isoFromUser.fks.map);
    fksM2.remove(src);
    isoFromUser = new Mapping<>(isoFromUser.ens.map, isoFromUser.atts.map, fksM2, schemaStr2, schemaStr, checkJava);
    return wrap(isoToUser, isoFromUser);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) HashMap(java.util.HashMap) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) 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) Triple(catdata.Triple)

Example 20 with En

use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.

the class AqlInACan method toHtml.

public static <X, Y> String toHtml(AqlEnv env, Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> I) {
    String ret = "<div>";
    // TODO aql hardcoded
    Map<En, Pair<List<String>, Object[][]>> tables = new AqlViewer(256, env).makeEnTables(I.algebra());
    for (En t : Util.alphabetical(tables.keySet())) {
        ret += "<table id=\"table" + i + "\" style=\"float: left; border: 1px solid black; padding: 5px; border-collapse: collapse; margin-right:10px\" border=\"1\"  cellpadding=\"3\">";
        ret += "<caption><b>" + t.toString() + "</b></caption>";
        List<String> cols = tables.get(t).first;
        cols.remove(0);
        cols.add(0, "ID");
        Object[][] rows = tables.get(t).second;
        ret += "<tr>";
        int j = 0;
        for (String col : cols) {
            ret += "<th onclick=\"sortTable('table" + i + "', " + j + ")\">" + col + "</th>";
            j++;
        }
        ret += "</tr>";
        for (Object[] row : rows) {
            ret += "<tr>";
            for (Object col : row) {
                // System.ou
                ret += "<td>" + strip(col.toString()) + "</td>";
            }
            ret += "</tr>";
        }
        ret += "</table>";
        i++;
    }
    return ret + "</div><br style=\"clear:both;\"/>";
}
Also used : AqlViewer(catdata.aql.gui.AqlViewer) En(catdata.aql.exp.SchExpRaw.En) Pair(catdata.Pair)

Aggregations

En (catdata.aql.exp.SchExpRaw.En)44 Att (catdata.aql.exp.SchExpRaw.Att)36 Fk (catdata.aql.exp.SchExpRaw.Fk)36 Ty (catdata.aql.exp.TyExpRaw.Ty)35 Sym (catdata.aql.exp.TyExpRaw.Sym)34 Ctx (catdata.Ctx)27 Pair (catdata.Pair)27 Gen (catdata.aql.exp.InstExpRaw.Gen)22 HashSet (java.util.HashSet)20 Chc (catdata.Chc)18 Triple (catdata.Triple)17 AqlOptions (catdata.aql.AqlOptions)17 List (java.util.List)17 Sk (catdata.aql.exp.InstExpRaw.Sk)16 HashMap (java.util.HashMap)15 LinkedList (java.util.LinkedList)14 Collage (catdata.aql.Collage)13 Term (catdata.aql.Term)13 RawTerm (catdata.aql.RawTerm)11 Set (java.util.Set)11