Search in sources :

Example 11 with Ctx

use of catdata.Ctx 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 12 with Ctx

use of catdata.Ctx 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 13 with Ctx

use of catdata.Ctx 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 14 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class ColimitSchema method renameFk.

public ColimitSchema<N> renameFk(Fk src, Fk dst, boolean checkJava) {
    if (!schemaStr.fks.containsKey(src)) {
        throw new RuntimeException(src.en + "." + src.str + " is not a foreign_key in \n" + schemaStr);
    }
    if (schemaStr.fks.containsKey(dst)) {
        throw new RuntimeException(dst + " is already a foreign_key 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<Fk, Fk> fun = x -> x.equals(src) ? dst : x;
    Function<Fk, Fk> fun2 = x -> x.equals(dst) ? src : x;
    Map<Fk, Pair<En, En>> fks = new HashMap<>();
    for (Fk k : schemaStr.fks.keySet()) {
        fks.put(fun.apply(k), schemaStr.fks.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.mapFk(fun), eq.third.mapFk(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 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.mapFk(fun2), rhs.mapFk(fun2));
        }
    };
    Schema<Ty, En, Sym, Fk, Att> schemaStr2 = // TODO aql java
    new Schema<>(ty, schemaStr.ens, schemaStr.atts.map, fks, eqs, dp, checkJava);
    Map<Fk, Pair<En, List<Fk>>> fksM = new HashMap<>();
    for (Fk k : schemaStr.fks.keySet()) {
        fksM.put(k, new Pair<>(schemaStr.fks.get(k).first, k.equals(src) ? Util.singList(dst) : Util.singList(k)));
    }
    isoToUser = new Mapping<>(isoToUser.ens.map, isoToUser.atts.map, fksM, schemaStr, schemaStr2, checkJava);
    Map<Fk, Pair<En, List<Fk>>> fksM2 = new HashMap<>();
    for (Fk k : schemaStr2.fks.keySet()) {
        fksM2.put(k, new Pair<>(schemaStr2.fks.get(k).first, k.equals(dst) ? Util.singList(src) : Util.singList(k)));
    }
    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) 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 15 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class ColimitSchema method removeAtt.

public ColimitSchema<N> removeAtt(Att src, Var v, Term<Ty, En, Sym, Fk, Att, Void, Void> t, boolean checkJava) {
    if (!schemaStr.atts.containsKey(src)) {
        throw new RuntimeException(src + " is not an attribute in \n" + schemaStr);
    }
    En en1 = schemaStr.atts.get(src).first;
    Ty ty0 = schemaStr.atts.get(src).second;
    if (!schemaStr.type(new Pair<>(v, en1), t).equals(Chc.inLeft(ty0))) {
        throw new RuntimeException("The term " + t + " has type " + schemaStr.type(new Pair<>(v, en1), t).toStringMash() + " and not " + ty0 + " as expected.");
    }
    if (!schemaStr.dp.eq(new Ctx<>(v, Chc.inRight(en1)), t, Term.Att(src, Term.Var(v)))) {
        throw new RuntimeException("The term " + t + " is not provably equal to " + Term.Att(src, Term.Var(v)));
    }
    if (t.contains(Head.Att(src))) {
        throw new RuntimeException("Cannot replace " + src + " with " + t + " because that term contains " + src);
    }
    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<Att, Pair<En, Ty>> atts = new HashMap<>(schemaStr.atts.map);
    atts.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.Att(src), Util.singList(v), t), eq.third.replaceHead(Head.Att(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 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, rhs);
        }
    };
    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<>(isoToUser.atts.map);
    attsM.put(src, new Triple<>(v, en1, t));
    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<>(isoFromUser.atts.map);
    attsM2.remove(src);
    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) 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)

Aggregations

Ctx (catdata.Ctx)31 Ty (catdata.aql.exp.TyExpRaw.Ty)27 En (catdata.aql.exp.SchExpRaw.En)26 Sym (catdata.aql.exp.TyExpRaw.Sym)25 Att (catdata.aql.exp.SchExpRaw.Att)24 Fk (catdata.aql.exp.SchExpRaw.Fk)24 Pair (catdata.Pair)22 Chc (catdata.Chc)19 HashSet (java.util.HashSet)19 Triple (catdata.Triple)17 Gen (catdata.aql.exp.InstExpRaw.Gen)16 HashMap (java.util.HashMap)15 AqlOptions (catdata.aql.AqlOptions)14 List (java.util.List)14 Term (catdata.aql.Term)13 Sk (catdata.aql.exp.InstExpRaw.Sk)13 Collage (catdata.aql.Collage)12 RawTerm (catdata.aql.RawTerm)11 LinkedList (java.util.LinkedList)10 Var (catdata.aql.Var)9