use of catdata.aql.Eq in project fql by CategoricalData.
the class QueryExpRaw method eval.
@Override
public Query<Ty, En, Sym, Fk, Att, En, Fk, Att> eval(AqlEnv env) {
Schema<Ty, En, Sym, Fk, Att> src0 = src.eval(env);
Schema<Ty, En, Sym, Fk, Att> dst0 = dst.eval(env);
Ctx<En, Triple<Ctx<Var, En>, Collection<Eq<Ty, En, Sym, Fk, Att, Var, Var>>, AqlOptions>> ens0 = new Ctx<>();
Ctx<Att, Term<Ty, En, Sym, Fk, Att, Var, Var>> atts0 = new Ctx<>();
Ctx<Fk, Pair<Ctx<Var, Term<Void, En, Void, Fk, Void, Var, Void>>, Boolean>> fks0 = new Ctx<>();
Ctx<Var, Ty> xxx = new Ctx<>();
Ctx<Var, Term<Ty, Void, Sym, Void, Void, Void, Void>> yyy = new Ctx<>();
for (String k : imports) {
@SuppressWarnings("unchecked") Query<Ty, En, Sym, Fk, Att, En, Fk, Att> v = env.defs.qs.get(k);
for (Var var : v.params.keySet()) {
// allow benign collisions
xxx.map.put(var, v.params.get(var));
}
for (Var var : v.consts.keySet()) {
yyy.map.put(var, v.consts.get(var));
}
for (En En : v.ens.keySet()) {
ens0.put(En, new Triple<>(v.ens.get(En).gens, v.ens.get(En).eqs, v.ens.get(En).options));
}
for (Att Att : v.atts.keySet()) {
atts0.put(Att, v.atts.get(Att));
}
for (Fk Fk : v.fks.keySet()) {
fks0.put(Fk, new Pair<>(v.fks.get(Fk).gens(), v.doNotValidate.get(Fk)));
}
}
Ctx<En, Collage<Ty, En, Sym, Fk, Att, Var, Var>> cols = new Ctx<>();
for (Block p : blocks) {
try {
if (!dst0.ens.contains(p.en)) {
throw new RuntimeException("the proposed target entity " + p.en + " does not actually appear in the target schema");
}
processBlock(options, env, src0, ens0, cols, p, params);
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(b1.get(p.en), "In block for target entity " + p.en + ", " + ex.getMessage());
}
for (Pair<catdata.aql.exp.SchExpRaw.Att, RawTerm> pp : p.atts) {
try {
processAtt(src0, dst0, ens0, atts0, cols, pp, params);
} catch (Exception ex) {
ex.printStackTrace();
throw new LocException(b3.get(pp.first), "In return clause for " + pp.first + ", " + ex.getMessage());
}
}
}
// two loops bc need stuff in en to do this part
for (Block p : blocks) {
for (Pair<catdata.aql.exp.SchExpRaw.Fk, Trans> pp : p.fks) {
try {
Ctx<Var, Term<Void, En, Void, Fk, Void, Var, Void>> trans = new Ctx<>();
for (Pair<Var, RawTerm> v : pp.second.gens) {
Ctx<String, Chc<Ty, En>> ctx = unVar(ens0.get(dst0.fks.get(pp.first).first).first.inRight());
Collage<Ty, En, Sym, Fk, Att, Var, Var> col = cols.get(dst0.fks.get(pp.first).first);
Chc<Ty, En> required = Chc.inRight(ens0.get(dst0.fks.get(pp.first).second).first.get(v.first));
Term<catdata.aql.exp.TyExpRaw.Ty, catdata.aql.exp.SchExpRaw.En, catdata.aql.exp.TyExpRaw.Sym, catdata.aql.exp.SchExpRaw.Fk, catdata.aql.exp.SchExpRaw.Att, Gen, Sk> term = RawTerm.infer1x(ctx.map, v.second, null, required, col.convert(), "in foreign key " + pp.first.str + ", ", src0.typeSide.js).second;
trans.put(v.first, freeze(term.convert(), params).convert());
}
boolean doNotCheckEqs = (Boolean) new AqlOptions(pp.second.options, null, env.defaults).getOrDefault(AqlOption.dont_validate_unsafe);
fks0.put(pp.first, new Pair<>(trans, doNotCheckEqs));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(b2.get(pp.first), ex.getMessage());
}
}
}
boolean doNotCheckEqs = (Boolean) new AqlOptions(options, null, env.defaults).getOrDefault(AqlOption.dont_validate_unsafe);
boolean elimRed = (Boolean) new AqlOptions(options, null, env.defaults).getOrDefault(AqlOption.query_remove_redundancy);
for (String s : params.keySet()) {
xxx.put(new Var(s), new Ty(params.get(s)));
}
for (String s : consts.keySet()) {
Chc<Ty, En> required = Chc.inLeft(xxx.get(new Var(s)));
Term<catdata.aql.exp.TyExpRaw.Ty, catdata.aql.exp.SchExpRaw.En, catdata.aql.exp.TyExpRaw.Sym, catdata.aql.exp.SchExpRaw.Fk, catdata.aql.exp.SchExpRaw.Att, Gen, Sk> term = RawTerm.infer1x(new HashMap<>(), consts.get(s), null, required, src0.collage().convert(), "", src0.typeSide.js).second;
yyy.put(new Var(s), term.convert());
}
return Query.makeQuery2(xxx, yyy, ens0, atts0, fks0, src0, dst0, doNotCheckEqs, elimRed);
}
use of catdata.aql.Eq in project fql by CategoricalData.
the class QueryExpRawSimple method eval.
// TODO aql merge with queryexpraw
@Override
public Query<Ty, En, Sym, Fk, Att, En, Fk, Att> eval(AqlEnv env) {
Schema<Ty, En, Sym, Fk, Att> src0 = src.eval(env);
Collage<Ty, En, Sym, Fk, Att, Void, Void> srcCol = src0.collage();
En En = new En("Q");
AqlOptions ops = new AqlOptions(block.options, null, env.defaults);
boolean doNotCheckEqs = (Boolean) ops.getOrDefault(AqlOption.dont_validate_unsafe);
boolean elimRed = (Boolean) ops.getOrDefault(AqlOption.query_remove_redundancy);
boolean checkJava = !(Boolean) ops.getOrDefault(AqlOption.allow_java_eqs_unsafe);
Ctx<En, Triple<Ctx<Var, En>, Collection<Eq<Ty, En, Sym, Fk, Att, Var, Var>>, AqlOptions>> ens0 = new Ctx<>();
Ctx<Att, Term<Ty, En, Sym, Fk, Att, Var, Var>> atts0 = new Ctx<>();
Ctx<Fk, Pair<Ctx<Var, Term<Void, En, Void, Fk, Void, Var, Void>>, Boolean>> fks0 = new Ctx<>();
Ctx<En, Collage<Ty, En, Sym, Fk, Att, Var, Var>> cols = new Ctx<>();
QueryExpRaw.processBlock(block.options, env, src0, ens0, cols, block, new Ctx<>());
Collage<Ty, En, Sym, Fk, Att, Void, Void> colForDst = new Collage<>(src0.typeSide.collage());
colForDst.ens.add(En);
for (Pair<Att, RawTerm> p : block.atts) {
Map<String, Chc<Ty, En>> s = QueryExpRaw.unVar(cols.get(En).gens).<Ty>inRight().map;
Term<Ty, catdata.aql.exp.SchExpRaw.En, Sym, Fk, Att, Gen, Sk> term = RawTerm.infer1x(s, p.second, p.second, null, srcCol.convert(), "", src0.typeSide.js).second;
Chc<Ty, En> ty = srcCol.type(new Ctx<>(s).map((k, v) -> new Pair<>(new Var(k), v)), term.convert());
if (!ty.left) {
throw new LocException(find("attributes", p), "In return clause for " + p.first + ", the type is " + ty.r + ", which is an entity.");
}
colForDst.atts.put(p.first, new Pair<>(En, ty.l));
}
DP<Ty, En, Sym, Fk, Att, Void, Void> dp = AqlProver.create(ops, colForDst, src0.typeSide.js);
Schema<Ty, En, Sym, Fk, Att> dst0 = new Schema<Ty, En, Sym, Fk, Att>(src0.typeSide, colForDst.ens, colForDst.atts.map, colForDst.fks.map, new HashSet<>(), dp, checkJava);
for (Pair<Att, RawTerm> p : block.atts) {
try {
QueryExpRaw.processAtt(src0, dst0, ens0, atts0, cols, p, new Ctx<>());
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(find("attributes", p), "In return clause for " + p.first + ", " + ex.getMessage());
}
}
// TODO aql
return Query.makeQuery(ens0, atts0, fks0, src0, dst0, doNotCheckEqs, elimRed);
}
use of catdata.aql.Eq in project fql by CategoricalData.
the class QueryExpRaw method processBlock.
public static void processBlock(Map<String, String> options, AqlEnv env, Schema<Ty, En, Sym, Fk, Att> src0, Ctx<En, Triple<Ctx<Var, En>, Collection<Eq<Ty, En, Sym, Fk, Att, Var, Var>>, AqlOptions>> ens0, Ctx<En, Collage<Ty, En, Sym, Fk, Att, Var, Var>> cols, Block p, Ctx<String, String> params) {
// p.second.gens);
Ctx<Var, En> ctx = new Ctx<Var, En>(Util.toMapSafely(p.gens));
for (Var v : ctx.map.keySet()) {
En en = ctx.get(v);
if (!src0.ens.contains(en)) {
throw new RuntimeException("from clause contains " + v + ":" + en + ", but " + en + " is not a source entity");
}
}
Collage<Ty, En, Sym, Fk, Att, Var, Var> col = new Collage<>(src0.collage());
Ctx<String, Chc<Ty, En>> ctx0 = unVar(ctx.inRight());
col.gens.putAll(ctx.map);
for (String q : params.keySet()) {
ctx0.put(q, Chc.inLeft(new Ty(params.get(q))));
col.sks.put(new Var(q), new Ty(params.get(q)));
}
cols.put(p.en, col);
Collection<Eq<Ty, En, Sym, Fk, Att, Var, Var>> eqs = new HashSet<>();
for (Pair<RawTerm, RawTerm> eq : p.eqs) {
Triple<Ctx<Var, Chc<catdata.aql.exp.TyExpRaw.Ty, catdata.aql.exp.SchExpRaw.En>>, Term<catdata.aql.exp.TyExpRaw.Ty, catdata.aql.exp.SchExpRaw.En, catdata.aql.exp.TyExpRaw.Sym, catdata.aql.exp.SchExpRaw.Fk, catdata.aql.exp.SchExpRaw.Att, Gen, Sk>, Term<catdata.aql.exp.TyExpRaw.Ty, catdata.aql.exp.SchExpRaw.En, catdata.aql.exp.TyExpRaw.Sym, catdata.aql.exp.SchExpRaw.Fk, catdata.aql.exp.SchExpRaw.Att, Gen, Sk>> x = RawTerm.infer1x(ctx0.map, eq.first, eq.second, null, col.convert(), "In equation " + eq.first + " = " + eq.second + ", ", src0.typeSide.js).first3();
eqs.add(new Eq<>(new Ctx<>(), freeze(x.second.convert(), params), freeze(x.third.convert(), params)));
}
Map<String, String> uu = new HashMap<>(options);
uu.putAll(p.options);
AqlOptions theops = new AqlOptions(uu, null, env.defaults);
Triple<Ctx<Var, En>, Collection<Eq<Ty, En, Sym, Fk, Att, Var, Var>>, AqlOptions> b = new Triple<>(ctx, eqs, theops);
ens0.put(p.en, b);
}
use of catdata.aql.Eq in project fql by CategoricalData.
the class TyExpRaw method eval.
@Override
public synchronized TypeSide<Ty, Sym> eval(AqlEnv env) {
AqlOptions ops = new AqlOptions(options, col, env.defaults);
// changed my mind: do not defer equation checking since invokes javascript
// col.
AqlJs<Ty, Sym> js = new AqlJs<>(col.syms, col.java_tys, col.java_parsers, col.java_fns);
for (Triple<List<Pair<String, String>>, RawTerm, RawTerm> eq : eqs) {
try {
Triple<Ctx<Var, Chc<Ty, Void>>, Term<Ty, Void, Sym, Void, Void, Void, Void>, Term<Ty, Void, Sym, Void, Void, Void, Void>> tr = infer1x(yyy(eq.first), eq.second, eq.third, null, col, "", js);
col.eqs.add(new Eq<>(tr.first, tr.second, tr.third));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(find("equations", eq), "In equation " + eq.second + " = " + eq.third + ", " + ex.getMessage());
}
}
for (Pair<Integer, TyExp<Ty, Sym>> k : imports) {
TypeSide<Ty, Sym> v = k.second.eval(env);
col.addAll(v.collage());
}
Set<Triple<Ctx<Var, Ty>, Term<Ty, Void, Sym, Void, Void, Void, Void>, Term<Ty, Void, Sym, Void, Void, Void, Void>>> eqs0 = col.eqsAsTriples().stream().map(x -> new Triple<>(xxx(x.first), x.second, x.third)).collect(Collectors.toSet());
TypeSide<Ty, Sym> ret = new TypeSide<Ty, Sym>(col.tys, col.syms.map, eqs0, col.java_tys.map, col.java_parsers.map, col.java_fns.map, ops);
return ret;
}
Aggregations