Search in sources :

Example 21 with En

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

the class RawTerm method toHeadNoPrim.

// @SuppressWarnings("unchecked")
// only used for precedences with aql options
public static Head<Ty, En, Sym, Fk, Att, Gen, Sk> toHeadNoPrim(String head, Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col) {
    Util.assertNotNull(head);
    Util.assertNotNull(col);
    assertUnambig(head, col);
    if (col.syms.containsKey(new Sym(head))) {
        return Head.Sym(new Sym(head));
    } else if (col.gens.containsKey(new Gen(head))) {
        return Head.Gen(new Gen(head));
    } else if (col.sks.containsKey(new Sk(head))) {
        return Head.Sk(new Sk(head));
    }
    for (En en : col.ens) {
        // TODO aql won't work with ambig
        if (col.fks.containsKey(new Fk(en, head))) {
            return Head.Fk(new Fk(en, head));
        }
        if (col.atts.containsKey(new Att(en, head))) {
            return Head.Att(new Att(en, head));
        }
    }
    throw new RuntimeException("Anomaly: please report");
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Gen(catdata.aql.exp.InstExpRaw.Gen) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Sk(catdata.aql.exp.InstExpRaw.Sk) En(catdata.aql.exp.SchExpRaw.En)

Example 22 with En

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

the class ColimitSchema method makeCoprodSchema.

private void makeCoprodSchema(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) {
    for (N n : nodes.keySet()) {
        Schema<Ty, En, Sym, Fk, Att> s = nodes.get(n);
        for (Att att : s.atts.keySet()) {
            col.atts.put(new Pair<>(n, att), new Pair<>(eqcs.get(new Pair<>(n, s.atts.get(att).first)), s.atts.get(att).second));
        }
        for (Fk fk : s.fks.keySet()) {
            col.fks.put(new Pair<>(n, fk), new Pair<>(eqcs.get(new Pair<>(n, s.fks.get(fk).first)), eqcs.get(new Pair<>(n, s.fks.get(fk).second))));
        }
        for (Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> eq : s.eqs) {
            Term<Ty, Set<Pair<N, En>>, Sym, Pair<N, Fk>, Pair<N, Att>, Void, Void> lhs = eq.second.map(Function.identity(), Function.identity(), z -> new Pair<>(n, z), z -> new Pair<>(n, z), Function.identity(), Function.identity());
            Term<Ty, Set<Pair<N, En>>, Sym, Pair<N, Fk>, Pair<N, Att>, Void, Void> rhs = eq.third.map(Function.identity(), Function.identity(), z -> new Pair<>(n, z), z -> new Pair<>(n, z), Function.identity(), Function.identity());
            Pair<Var, Set<Pair<N, En>>> x = new Pair<>(eq.first.first, eqcs.get(new Pair<>(n, eq.first.second)));
            eqs.add(new Triple<>(x, lhs, rhs));
            col.eqs.add(new Eq<>(new Ctx<>(x).inRight(), lhs, rhs));
        }
    }
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) HashSet(java.util.HashSet) Set(java.util.Set) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) En(catdata.aql.exp.SchExpRaw.En) Pair(catdata.Pair)

Example 23 with En

use of catdata.aql.exp.SchExpRaw.En 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 24 with En

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

the class ColimitSchema method isoOneWay.

private void isoOneWay(Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> F, Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> G, String str) {
    if (!F.dst.equals(G.src)) {
        throw new RuntimeException("Target of " + F + " \n, namely " + F.dst + "\ndoes not match source of " + G + ", namely " + F.src + "\n" + str);
    }
    Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> f = Mapping.compose(F, G);
    for (En en : f.src.ens) {
        En en2 = f.ens.get(en);
        if (!en.equals(en2)) {
            throw new RuntimeException(en + " taken to " + en2 + ", rather than itself, " + str);
        }
    }
    for (Fk fk : f.src.fks.keySet()) {
        Pair<En, List<Fk>> fk2 = f.fks.get(fk);
        Var v = new Var("v");
        Term<Ty, En, Sym, Fk, Att, Void, Void> t = Term.Fks(fk2.second, Term.Var(v));
        Term<Ty, En, Sym, Fk, Att, Void, Void> s = Term.Fk(fk, Term.Var(v));
        boolean eq = F.src.dp.eq(new Ctx<>(new Pair<>(v, Chc.inRight(fk2.first))), s, t);
        if (!eq) {
            throw new RuntimeException(fk + " taken to " + t + ", which is not provably equal to itself, " + str);
        }
    }
    for (Att att : f.src.atts.keySet()) {
        Triple<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>> att2 = f.atts.get(att);
        Var v = att2.first;
        // Term.Fks(att2.second, Term.Var(v));
        Term<Ty, En, Sym, Fk, Att, Void, Void> t = att2.third;
        Term<Ty, En, Sym, Fk, Att, Void, Void> s = Term.Att(att, Term.Var(v));
        boolean eq = F.src.dp.eq(new Ctx<>(new Pair<>(v, Chc.inRight(att2.second))), s, t);
        if (!eq) {
            throw new RuntimeException(att + " taken to " + t + ", which is not provably equal to itself, " + str);
        }
    }
}
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) List(java.util.List) Pair(catdata.Pair)

Example 25 with En

use of catdata.aql.exp.SchExpRaw.En 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

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