use of catdata.Chc 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);
}
use of catdata.Chc in project fql by CategoricalData.
the class Constraints method step.
// TODO aql needs to be over all eds
public <Gen, Sk, X, Y> Instance<Ty, En, Sym, Fk, Att, ?, ?, ?, ?> step(Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> I, AqlOptions options) {
Collection<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>> T = triggers(I, options);
if (T.isEmpty()) {
return null;
}
DMG<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Void> shape = new DMG<>(T, new HashMap<>());
Ctx<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>>> nodesA = new Ctx<>();
Ctx<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>>> nodesE = new Ctx<>();
Map<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Term<Void, En, Void, Fk, Void, Gen, Void>> aaa = new HashMap<>();
Map<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Term<Void, En, Void, Fk, Void, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Void>> xxx = new HashMap<>();
for (Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>> t : T) {
Query<Ty, En, Sym, Fk, Att, WHICH, Unit, Void> Q = t.first.Q;
Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> A = Q.ens.get(WHICH.FRONT);
Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> E = Q.ens.get(WHICH.BACK);
Transform<Ty, En, Sym, Fk, Att, Var, Var, Var, Var, ID, Chc<Var, Pair<ID, Att>>, ID, Chc<Var, Pair<ID, Att>>> AE = Q.fks.get(new Unit());
nodesA.put(t, A);
nodesE.put(t, E);
for (Var v : AE.src().gens().keySet()) {
// revisit after colimit fixed
xxx.put(new Pair<>(t, v), Term.Gen(new Pair<>(t, v)));
aaa.put(new Pair<>(t, v), I.algebra().repr(t.second.get(v)));
}
/*for (Void v : AE.src().sks().keySet()) {
yyy.put(v, Util.abort(v)); // revisit after colimit fixed
bbb.put(v, Util.abort(v));
}*/
}
ColimitInstance<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Void, Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> A0 = new ColimitInstance<>(schema, shape, nodesA, new Ctx<>(), options);
ColimitInstance<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Void, Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> E0 = new ColimitInstance<>(schema, shape, nodesE, new Ctx<>(), options);
LiteralTransform<Ty, En, Sym, Fk, Att, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, ID, Chc<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<ID, Att>>, ID, Chc<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<ID, Att>>> A0E0 = new LiteralTransform<>(xxx, new HashMap<>(), A0, E0, false);
LiteralTransform<Ty, En, Sym, Fk, Att, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Gen, Sk, ID, Chc<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<ID, Att>>, X, Y> A0I = new LiteralTransform<>(aaa, new HashMap<>(), A0, I, false);
return pushout(A0E0, A0I, options);
// TODO aql disable checking for speed
}
use of catdata.Chc in project fql by CategoricalData.
the class Algebra method createAndLoad.
/**
* MUST close this connection
*/
public Connection createAndLoad(Map<En, List<String>> indices, Pair<Map<X, Integer>, Map<Integer, X>> I, int vlen) {
try {
Map<En, Triple<List<Chc<Fk, Att>>, List<String>, List<String>>> xxx = schema().toSQL("", "integer", "id", -1, Object::toString, vlen);
Connection conn = DriverManager.getConnection("jdbc:h2:mem:db_temp_" + session_id++ + ";DB_CLOSE_DELAY=-1");
try (Statement stmt = conn.createStatement()) {
for (En en1 : schema().ens) {
Triple<List<Chc<Fk, Att>>, List<String>, List<String>> qqq = xxx.get(en1);
for (String s : qqq.second) {
stmt.execute(s);
}
for (String s : qqq.third) {
// don't need fks for AQL's internal use
if (!s.startsWith("alter table")) {
stmt.execute(s);
}
}
for (String s : indices.get(en1)) {
stmt.execute(s);
}
for (X x : en(en1)) {
storeMyRecord(I, conn, x, qqq.first, en1.toString(), "", -1);
}
}
stmt.close();
// this.conn = conn;
return conn;
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
} catch (SQLException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
use of catdata.Chc in project fql by CategoricalData.
the class RawTerm method infer2.
public static Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> infer2(List<Pair<String, String>> l, RawTerm a, RawTerm b, Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col, AqlJs<Ty, Sym> js) {
Map<String, Chc<Ty, En>> ctx = new HashMap<>();
for (Pair<String, String> p : l) {
if (ctx.containsKey(p.first)) {
throw new RuntimeException("Duplicate variable " + p.first + " in context " + Ctx.toString(l));
}
if (p.second != null) {
if (col.tys.contains(p.second) && col.ens.contains(p.second)) {
throw new RuntimeException("Ambiguous: " + p.second + " is an entity and a type");
} else if (col.tys.contains(p.second)) {
Ty tt = new Ty(p.second);
// TODO aql remove for
ctx.put(p.first, Chc.inLeft(tt));
// loops for other ones
} else if (col.ens.contains(p.second)) {
En tt = new En(p.second);
ctx.put(p.first, Chc.inRight(tt));
} else {
throw new RuntimeException(p.second + " is neither a type nor entity");
}
} else {
ctx.put(p.first, null);
}
}
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> eq0 = infer1x(ctx, a, b, null, col, "", js).first3();
LinkedHashMap<Var, Chc<Ty, En>> map = new LinkedHashMap<>();
for (String k : ctx.keySet()) {
Chc<Ty, En> v = eq0.first.get(new Var(k));
map.put(new Var(k), v);
}
Ctx<Var, Chc<Ty, En>> ctx2 = new Ctx<>(map);
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> tr = new Triple<>(ctx2, eq0.second, eq0.third);
return tr;
}
use of catdata.Chc in project fql by CategoricalData.
the class RawTerm method infer_good.
private static Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> infer_good(RawTerm e, Chc<Ty, En> expected, Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col, String pre, AqlJs<Ty, Sym> js, Map<Var, Chc<Ty, En>> vars) {
if (e.annotation != null && !col.tys.contains(new Ty(e.annotation))) {
throw new RuntimeException(pre + "Annotation " + e.annotation + " is not a type (" + col.tys + ").");
}
Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> ret = new HashSet<>();
if (vars.keySet().contains(new Var((String) e.head)) && e.annotation == null) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Var(new Var((String) e.head));
if (expected != null) {
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
ret2.put(new Var((String) e.head), expected);
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, expected));
}
} else {
for (En en : col.ens) {
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
ret2.put(new Var((String) e.head), Chc.inRight(en));
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, Chc.inRight(en)));
}
}
for (Ty ty : col.tys) {
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret2.put(new Var((String) e.head), Chc.inLeft(ty));
}
ret.add(new Triple<>(ret1, ret2, Chc.inLeft(ty)));
}
}
}
if (col.syms.containsKey(new Sym(e.head)) && e.annotation == null) {
// //System.out.println("a " + e);
List<List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>>> l = new LinkedList<>();
l.add(new LinkedList<>());
for (int i = 0; i < e.args.size(); i++) {
RawTerm arg = e.args.get(i);
// //System.out.println("arg " + arg);
Ty ty = col.syms.get(new Sym(e.head)).first.get(i);
Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> z = infer_good(arg, Chc.inLeft(ty), col, pre, js, vars);
List<List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>>> l2 = new LinkedList<>();
for (List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> old : l) {
// //System.out.println("old " + old);
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> y : z) {
if (y.third.equals(Chc.inLeft(ty))) {
// //System.out.println("z z");
l2.add(Util.append(old, Util.singList(y)));
}
}
}
l = l2;
}
outer: for (List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> outcome : l) {
// //System.out.println("outcome " + outcome);
List<Term<Ty, En, Sym, Fk, Att, Gen, Sk>> w = outcome.stream().map(x -> x.first).collect(Collectors.toList());
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Sym(new Sym(e.head), w);
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> ctx0 : outcome) {
if (!ctx0.second.agreeOnOverlap(ret2) || !ctx0.second.agreeOnOverlap(Ctx.fromNullable(vars))) {
// //System.out.println("xxx ");
continue outer;
}
// //System.out.println("yyy");
ret2.map.putAll(ctx0.second.map);
}
for (int i = 0; i < e.args.size(); i++) {
RawTerm arg = e.args.get(i);
// //System.out.println("2arx " + arg);
Chc<Ty, En> ty = Chc.inLeft(col.syms.get(new Sym(e.head)).first.get(i));
Var v = new Var((String) arg.head);
if (vars.keySet().contains(v)) {
// //System.out.println("a " + v);
if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
// //System.out.println("b " + v);
continue;
} else if (!ret2.containsKey(v)) {
// //System.out.println("c " + v);
ret2.put(new Var(e.args.get(i).head), ty);
}
}
}
Chc<Ty, En> ret3 = Chc.inLeft(col.syms.get(new Sym(e.head)).second);
if (expected != null && !expected.equals(ret3)) {
// //System.out.println("d " );
} else {
// //System.out.println("e " );
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, ret3));
}
}
}
}
for (En en : col.ens) {
if (col.fks.containsKey(new Fk(en, e.head)) && e.args.size() == 1 && e.annotation == null) {
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> outcome : infer_good(e.args.get(0), Chc.inRight(col.fks.get(new Fk(en, e.head)).first), col, pre, js, vars)) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Fk(new Fk(en, e.head), outcome.first);
// System.out.println("trying " + en + " and " + e.head);
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>(outcome.second.map);
Var v = new Var(e.args.get(0).head);
Chc<Ty, En> ty = Chc.inRight(col.fks.get(new Fk(en, e.head)).first);
if (vars.keySet().contains(v)) {
if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
// System.out.println("no1");
continue;
} else if (!ret2.containsKey(v)) {
ret2.put(new Var(e.args.get(0).head), ty);
}
}
Chc<Ty, En> ret3 = Chc.inRight(col.fks.get(new Fk(en, e.head)).second);
Chc<Ty, En> argt = Chc.inRight(col.fks.get(new Fk(en, e.head)).first);
if (expected != null && !expected.equals(ret3)) {
} else {
if (argt.equals(outcome.third)) {
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, ret3));
} else {
// System.out.println("b3");
}
} else {
// System.out.println("c3");
}
}
}
}
if (col.atts.containsKey(new Att(en, e.head)) && e.args.size() == 1 && e.annotation == null) {
// System.out.println("x " + e);
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> outcome : infer_good(e.args.get(0), Chc.inRight(col.atts.get(new Att(en, e.head)).first), col, pre, js, vars)) {
// System.out.println("y " + outcome);
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Att(new Att(en, e.head), outcome.first);
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>(outcome.second.map);
Var v = new Var(e.args.get(0).head);
Chc<Ty, En> ty = Chc.inRight(col.atts.get(new Att(en, e.head)).first);
if (vars.keySet().contains(v)) {
if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
continue;
} else if (!ret2.containsKey(v)) {
// System.out.println("b " + v);
ret2.put(v, ty);
}
}
Chc<Ty, En> ret3 = Chc.inLeft(col.atts.get(new Att(en, e.head)).second);
Chc<Ty, En> argt = Chc.inRight(col.atts.get(new Att(en, e.head)).first);
if (expected != null && !expected.equals(ret3)) {
// System.out.println("d " + v);
} else {
// System.out.println("e " + v);
if (argt.equals(outcome.third)) {
// System.out.println("f " + v);
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, ret3));
}
}
}
}
}
}
if (col.gens.containsKey(new Gen(e.head)) && e.args.isEmpty() && e.annotation == null) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Gen(new Gen(e.head));
Chc<Ty, En> ret3 = Chc.inRight(col.gens.get(new Gen(e.head)));
if (expected != null && !expected.equals(ret3)) {
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
}
}
if (col.sks.containsKey(new Sk(e.head)) && e.args.isEmpty() && e.annotation == null) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Sk(new Sk(e.head));
Chc<Ty, En> ret3 = Chc.inLeft(col.sks.get(new Sk(e.head)));
if (expected != null && !expected.equals(ret3)) {
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
}
}
if (e.args.isEmpty() && e.annotation != null) {
Ty ty = new Ty(e.annotation);
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Obj(js.parse(ty, e.head), ty);
Chc<Ty, En> ret3 = Chc.inLeft(ty);
if (expected != null && !expected.equals(ret3)) {
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
}
}
// as primitive - only if not a variable/generator/etc in scope i.e. none above fired
if (e.args.isEmpty() && e.annotation == null && ret.isEmpty()) {
for (Ty ty : col.tys) {
if (expected != null && !expected.equals(Chc.inLeft(ty))) {
continue;
}
try {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Obj(js.parse(ty, e.head), ty);
Chc<Ty, En> ret3 = Chc.inLeft(ty);
if (expected != null && !expected.equals(ret3)) {
// System.out.println("zzz");
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
// System.out.println("added " + ret + " and " + ret3);
}
} catch (Exception ex) {
if (expected != null) {
ex.printStackTrace();
// throw ex;
}
// //ex.printStackTrace();
}
}
}
return ret;
}
Aggregations