use of catdata.aql.exp.SchExpRaw.Fk 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;
}
use of catdata.aql.exp.SchExpRaw.Fk 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);
}
use of catdata.aql.exp.SchExpRaw.Fk 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");
}
use of catdata.aql.exp.SchExpRaw.Fk 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));
}
}
}
use of catdata.aql.exp.SchExpRaw.Fk 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);
}
Aggregations