use of catdata.Chc in project fql by CategoricalData.
the class ColimSchExpModify method eval.
// TODO aql add options
@Override
public ColimitSchema<N> eval(AqlEnv env) {
boolean checkJava = !(Boolean) env.defaults.getOrDefault(options, AqlOption.allow_java_eqs_unsafe);
ColimitSchema<N> colim0 = colim.eval(env);
// colim0.schemaStr.co
for (Pair<String, String> k : ens) {
colim0 = colim0.renameEntity(new En(k.first), new En(k.second), checkJava);
}
for (Pair<Pair<String, String>, String> k : fks0) {
colim0 = colim0.renameFk(new Fk(new En(k.first.first), k.first.second), new Fk(new En(k.first.first), k.second), checkJava);
}
for (Pair<Pair<String, String>, String> k : atts0) {
colim0 = colim0.renameAtt(new Att(new En(k.first.first), k.first.second), new Att(new En(k.first.first), k.second), checkJava);
}
for (Pair<Pair<String, String>, List<String>> k : fks) {
if (!colim0.schemaStr.fks.containsKey(new Fk(new En(k.first.first), k.first.second))) {
throw new RuntimeException("Not an fk: " + k.first + " in\n\n" + colim0.schemaStr);
}
String pre = "In processing " + k.first + " -> " + k.second + ", ";
Collage<Ty, En, Sym, Fk, Att, Void, Void> xxx = colim0.schemaStr.collage();
RawTerm term = RawTerm.fold(k.second, "v");
En tr = colim0.schemaStr.fks.get(new Fk(new En(k.first.first), k.first.second)).second;
Ctx<String, Chc<Ty, En>> ctx = new Ctx<>("v", Chc.inRight(new En(k.first.first)));
Term<Ty, En, Sym, Fk, Att, Gen, Sk> t = RawTerm.infer1x(ctx.map, term, null, Chc.inRight(tr), xxx.convert(), pre, colim0.schemaStr.typeSide.js).second;
// colim0 = colim0.removeAtt(new Att(k.first), new Var(k.second.first), t.convert(), checkJava);
colim0 = colim0.removeFk(new Fk(new En(k.first.first), k.first.second), t.toFkList(), checkJava);
}
for (Pair<Pair<String, String>, Triple<String, String, RawTerm>> k : atts) {
if (!colim0.schemaStr.atts.containsKey(new Att(new En(k.first.first), k.first.second))) {
throw new RuntimeException("Not an attribute: " + k.first + " in\n\n" + colim0.schemaStr);
}
String pre = "In processing " + k.first + " -> lambda " + k.second.first + "." + k.second.third + ", ";
Pair<En, Ty> r = colim0.schemaStr.atts.get(new Att(new En(k.first.first), k.first.second));
if (k.second.second != null && !k.second.second.equals(r.first)) {
throw new RuntimeException(pre + " given type is " + k.second.second + " but expected " + r.first);
}
Collage<Ty, En, Sym, Fk, Att, Void, Void> xxx = colim0.schemaStr.collage();
Ctx<String, Chc<Ty, En>> ctx = new Ctx<>(k.second.first, Chc.inRight(r.first));
Term<Ty, En, Sym, Fk, Att, Gen, Sk> t = RawTerm.infer1x(ctx.map, k.second.third, null, Chc.inLeft(r.second), xxx.convert(), pre, colim0.schemaStr.typeSide.js).second;
colim0 = colim0.removeAtt(new Att(new En(k.first.first), k.first.second), new Var(k.second.first), t.convert(), checkJava);
}
return colim0;
}
use of catdata.Chc in project fql by CategoricalData.
the class InstExpImport method forTheory.
public static <Ty, En, Sym, Fk, Att, Gen> Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> forTheory(Schema<Ty, En, Sym, Fk, Att> sch, Ctx<En, Collection<Gen>> ens0, Ctx<Ty, Collection<Null<?>>> tys0, Ctx<Gen, Ctx<Fk, Gen>> fks0, Ctx<Gen, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Null<?>>>> atts0, AqlOptions op) {
Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>, Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>>> eqs0 = new HashSet<>();
Collage<Ty, En, Sym, Fk, Att, Gen, Null<?>> col = new Collage<>(sch.collage());
for (Gen gen : fks0.keySet()) {
for (Fk fk : fks0.get(gen).keySet()) {
eqs0.add(new Pair<>(Term.Fk(fk, Term.Gen(gen)), Term.Gen(fks0.get(gen).get(fk))));
col.eqs.add(new Eq<>(new Ctx<>(), Term.Fk(fk, Term.Gen(gen)), Term.Gen(fks0.get(gen).get(fk))));
}
}
for (Gen gen : atts0.keySet()) {
for (Att att : atts0.get(gen).keySet()) {
eqs0.add(new Pair<>(Term.Att(att, Term.Gen(gen)), atts0.get(gen).get(att).convert()));
col.eqs.add(new Eq<>(new Ctx<>(), Term.Att(att, Term.Gen(gen)), atts0.get(gen).get(att).convert()));
}
}
for (En en : ens0.keySet()) {
for (Gen gen : ens0.get(en)) {
col.gens.put(gen, en);
}
}
for (Ty ty : tys0.keySet()) {
for (Null<?> sk : tys0.get(ty)) {
col.sks.put(sk, ty);
}
}
InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>, ID> initial = new InitialAlgebra<>(op, sch, col, new It(), (Gen x) -> x.toString(), (Null<?> x) -> x.toString());
Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, ID, Chc<Null<?>, Pair<ID, Att>>> I = new LiteralInstance<>(sch, col.gens.map, col.sks.map, eqs0, initial.dp(), initial, (Boolean) op.getOrDefault(AqlOption.require_consistency), (Boolean) op.getOrDefault(AqlOption.allow_java_eqs_unsafe));
@SuppressWarnings("unchecked") Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> J = (Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>>) ((Object) I);
return J;
}
use of catdata.Chc in project fql by CategoricalData.
the class ColimitSchema method renameEntity.
public ColimitSchema<N> renameEntity(En src, En dst, boolean checkJava) {
if (!schemaStr.ens.contains(src)) {
throw new RuntimeException(src + " is not an entity in \n" + schemaStr);
}
if (schemaStr.ens.contains(dst)) {
throw new RuntimeException(dst + " is already an entity 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> updateFk = x -> {
if (x.en.equals(src)) {
return new Fk(dst, x.str);
}
return x;
};
Function<Fk, Fk> deUpdateFk = x -> {
if (x.en.equals(dst)) {
return new Fk(src, x.str);
}
return x;
};
Function<Att, Att> updateAtt = x -> {
if (x.en.equals(src)) {
return new Att(dst, x.str);
}
return x;
};
Function<Att, Att> deUpdateAtt = x -> {
if (x.en.equals(dst)) {
return new Att(src, x.str);
}
return x;
};
Set<En> ens = new HashSet<>(schemaStr.ens);
ens.remove(src);
ens.add(dst);
Map<Att, Pair<En, Ty>> atts = new HashMap<>();
for (Att k : schemaStr.atts.keySet()) {
Pair<En, Ty> v = schemaStr.atts.get(k);
En s = v.first.equals(src) ? dst : v.first;
atts.put(updateAtt.apply(k), new Pair<>(s, v.second));
}
Map<Fk, Pair<En, En>> fks = new HashMap<>();
for (Fk k : schemaStr.fks.keySet()) {
Pair<En, En> v = schemaStr.fks.get(k);
En s = v.first.equals(src) ? dst : v.first;
En t = v.second.equals(src) ? dst : v.second;
fks.put(updateFk.apply(k), new Pair<>(s, t));
}
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) {
Pair<Var, En> v = eq.first;
En t = v.second.equals(src) ? dst : v.second;
eqs.add(new Triple<>(new Pair<>(v.first, t), eq.second.mapFk(updateFk).mapAtt(updateAtt), eq.third.mapFk(updateFk).mapAtt(updateAtt)));
}
DP<Ty, En, Sym, Fk, Att, Void, Void> dp = new DP<Ty, En, Sym, Fk, Att, Void, Void>() {
@Override
public String toStringProver() {
return "rename entity of " + schemaStr.dp.toStringProver();
}
@Override
public // TODO aql check this
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.map(v -> v.left ? v : (v.r.equals(dst) ? Chc.inRight(src) : v)), lhs.mapFk(deUpdateFk).mapAtt(deUpdateAtt), rhs.mapFk(deUpdateFk).mapAtt(deUpdateAtt));
}
};
Schema<Ty, En, Sym, Fk, Att> schemaStr2 = // TODO aql java
new Schema<>(ty, ens, atts, fks, eqs, dp, checkJava);
Map<En, En> ensM = new HashMap<>();
for (En k : schemaStr.ens) {
ensM.put(k, k.equals(src) ? dst : k);
}
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<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>>(isoToUser.atts.get(k).first, isoToUser.atts.get(k).second.equals(src) ? dst : isoToUser.atts.get(k).second, isoToUser.atts.get(k).third.mapAtt(updateAtt).mapFk(updateFk)));
}
Map<Fk, Pair<En, List<Fk>>> fksM = new HashMap<>();
for (Fk k : schemaStr.fks.keySet()) {
fksM.put(k, new Pair<>(isoToUser.fks.get(k).first.equals(src) ? dst : isoToUser.fks.get(k).first, isoToUser.fks.get(k).second.stream().map(updateFk).collect(Collectors.toList())));
}
isoToUser = new Mapping<>(ensM, attsM, fksM, schemaStr, schemaStr2, checkJava);
Map<En, En> ensM2 = new HashMap<>();
for (En k : schemaStr2.ens) {
ensM2.put(k, k.equals(dst) ? src : k);
}
Map<Att, Triple<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>>> attsM2 = new HashMap<>();
for (Att k : schemaStr2.atts.keySet()) {
attsM2.put(updateAtt.apply(k), new Triple<>(isoFromUser.atts.get(deUpdateAtt.apply(k)).first, isoFromUser.atts.get(deUpdateAtt.apply(k)).second.equals(dst) ? src : isoFromUser.atts.get(deUpdateAtt.apply(k)).second, isoFromUser.atts.get(deUpdateAtt.apply(k)).third));
}
Map<Fk, Pair<En, List<Fk>>> fksM2 = new HashMap<>();
for (Fk k : schemaStr2.fks.keySet()) {
fksM2.put(updateFk.apply(k), new Pair<>(isoFromUser.fks.get(deUpdateFk.apply(k)).first.equals(dst) ? src : isoFromUser.fks.get(deUpdateFk.apply(k)).first, isoFromUser.fks.get(deUpdateFk.apply(k)).second.stream().map(deUpdateFk).collect(Collectors.toList())));
}
isoFromUser = new Mapping<>(ensM2, attsM2, fksM2, schemaStr2, schemaStr, checkJava);
return wrap(isoToUser, isoFromUser);
}
use of catdata.Chc 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);
}
use of catdata.Chc 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;
}
Aggregations