use of catdata.aql.Term in project fql by CategoricalData.
the class SchExpRaw method eval.
// TODO: aql printing of contexts broken when conitain choices
@SuppressWarnings("unused")
@Override
public synchronized Schema<Ty, En, Sym, Fk, Att> eval(AqlEnv env) {
TypeSide<Ty, Sym> ts = typeSide.eval(env);
Collage<Ty, En, Sym, Fk, Att, Void, Void> col = new Collage<>(ts.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<>();
for (String k : imports) {
@SuppressWarnings("unchecked") Schema<Ty, En, Sym, Fk, Att> v = env.defs.schs.get(k);
col.addAll(v.collage());
eqs0.addAll(v.eqs);
}
col.ens.addAll(ens.stream().map(x -> new En(x)).collect(Collectors.toList()));
col.fks.putAll(conv1(fks));
col.atts.putAll(conv2(atts));
for (Quad<String, String, RawTerm, RawTerm> eq : t_eqs) {
try {
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(), "", ts.js).first3();
Chc<Ty, En> v = eq0.first.get(new Var(eq.first));
if (v.left) {
throw new RuntimeException(eq.first + " has type " + v.l + " which is not an entity");
}
En t = v.r;
eqs0.add(new Triple<>(new Pair<>(new Var(eq.first), t), eq0.second.convert(), eq0.third.convert()));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(find("obs equations", eq), "In equation " + eq.third + " = " + eq.fourth + ", " + ex.getMessage());
}
}
for (Pair<List<String>, List<String>> eq : p_eqs) {
try {
String vv = "v";
Var var = new Var(vv);
Map<String, Chc<Ty, En>> ctx = Util.singMap(vv, null);
RawTerm lhs = RawTerm.fold(col.fks.keySet(), col.ens, eq.first, vv);
RawTerm rhs = RawTerm.fold(col.fks.keySet(), col.ens, eq.second, vv);
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, lhs, rhs, null, col.convert(), "", ts.js).first3();
Chc<Ty, En> v = eq0.first.get(var);
if (v.left) {
throw new RuntimeException("the equation's source " + eq.first + " is type " + v.l + " which is not an entity");
}
En t = v.r;
if (eq0.first.size() != 1) {
throw new RuntimeException("java constants cannot be used ");
}
eqs0.add(new Triple<>(new Pair<>(var, t), eq0.second.convert(), eq0.third.convert()));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(find("path equations", eq), "In equation " + Util.sep(eq.first, ".") + " = " + Util.sep(eq.second, ".") + ", " + ex.getMessage());
}
}
for (Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> eq : eqs0) {
col.eqs.add(new Eq<>(new Ctx<>(eq.first).inRight(), eq.second, eq.third));
}
AqlOptions strat = new AqlOptions(options, col, env.defaults);
AqlOptions s = new AqlOptions(Util.singMap(AqlOption.prover.toString(), ProverName.fail.toString()), col, env.defaults);
// forces type checking before prover construction
new Schema<>(ts, col.ens, col.atts.map, col.fks.map, eqs0, AqlProver.create(s, col, ts.js), false);
Schema<Ty, En, Sym, Fk, Att> ret = new Schema<>(ts, col.ens, col.atts.map, col.fks.map, eqs0, AqlProver.create(strat, col, ts.js), !((Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe)));
return ret;
}
use of catdata.aql.Term 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.Term in project fql by CategoricalData.
the class TransExpRaw method eval.
@Override
public synchronized Transform<Ty, En, Sym, Fk, Att, Gen, Sk, Gen, Sk, String, String, String, String> eval(AqlEnv env) {
Instance<Ty, En, Sym, Fk, Att, Gen, Sk, String, String> src0 = src.eval(env);
Instance<Ty, En, Sym, Fk, Att, Gen, Sk, String, String> dst0 = dst.eval(env);
// Collage<String, String, String, String, String, Void, Void> scol = new Collage<>(src0);
Collage<Ty, En, Sym, Fk, Att, Gen, Sk> dcol = new Collage<>(dst0.collage());
Map<Gen, Term<Void, En, Void, Fk, Void, Gen, Void>> gens0 = new HashMap<>();
Map<Sk, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> sks0 = new HashMap<>();
for (String k : imports) {
@SuppressWarnings("unchecked") Transform<Ty, En, Sym, Fk, Att, Gen, Sk, Gen, Sk, String, String, String, String> v = env.defs.trans.get(k);
Util.putAllSafely(gens0, v.gens().map);
Util.putAllSafely(sks0, v.sks().map);
}
for (Pair<String, RawTerm> gen : gens) {
try {
RawTerm term = gen.second;
Map<String, Chc<Ty, En>> ctx = new HashMap<>();
Chc<Ty, En> required;
if (src0.gens().containsKey(new Gen(gen.first)) && src0.sks().containsKey(new Sk(gen.first))) {
throw new RuntimeException(gen.first + " is ambiguous");
} else if (src0.gens().containsKey(new Gen(gen.first))) {
required = Chc.inRight(src0.gens().get(new Gen(gen.first)));
} else if (src0.sks().containsKey(new Sk(gen.first))) {
required = Chc.inLeft(src0.sks().get(new Sk(gen.first)));
} else {
throw new RuntimeException(gen.first + " is not a source generator/labelled null");
}
Term<Ty, En, Sym, Fk, Att, Gen, Sk> term0 = RawTerm.infer1x(ctx, term, null, required, dcol, "", src0.schema().typeSide.js).second;
if (required.left) {
Util.putSafely(sks0, new Sk(gen.first), term0.convert());
} else {
Util.putSafely(gens0, new Gen(gen.first), term0.convert());
}
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(find("generators", gen), "In transform for " + gen.first + ", " + ex.getMessage());
}
}
AqlOptions ops = new AqlOptions(options, null, env.defaults);
LiteralTransform<Ty, En, Sym, Fk, Att, Gen, Sk, Gen, Sk, String, String, String, String> ret = new LiteralTransform<Ty, En, Sym, Fk, Att, Gen, Sk, Gen, Sk, String, String, String, String>(gens0, sks0, src0, dst0, (Boolean) ops.getOrDefault(AqlOption.dont_validate_unsafe));
return ret;
}
use of catdata.aql.Term in project fql by CategoricalData.
the class InstExpCsv method joinedEn.
@Override
protected void joinedEn(Map<En, List<String[]>> rows, En en0, Pair<List<Pair<String, String>>, List<Pair<String, String>>> s, Schema<Ty, En, Sym, Fk, Att> sch) throws Exception {
String en = en0.str;
Map<String, String> inner;
if (s == null) {
inner = new HashMap<>();
} else {
inner = Util.toMapSafely(s.second);
}
boolean autoGenIds = (Boolean) op.getOrDefault(inner, AqlOption.csv_generate_ids);
for (En en2 : rows.keySet()) {
if (rows.get(en2).size() == 0) {
throw new RuntimeException("No header in CSV file for " + en2);
}
}
// index of each column name
Ctx<String, Integer> m = new Ctx<>();
for (int i = 0; i < rows.get(en0).get(0).length; i++) {
m.put(rows.get(en0).get(0)[i], i);
}
boolean prepend = (boolean) op.getOrDefault(inner, AqlOption.csv_prepend_entity);
String sep = (String) op.getOrDefault(inner, AqlOption.import_col_seperator);
String pre = (String) op.getOrDefault(inner, AqlOption.csv_import_prefix);
// System.out.println("prefix is " + pre);
Map<String, String> map;
if (s != null) {
map = new Ctx<>(Util.toMapSafely(s.first)).map;
} else {
map = new HashMap<>();
}
Function<String, String> mediate = x -> {
if (map.containsKey(x)) {
return map.get(x);
}
String z = map.containsKey(x) ? map.get(x) : x;
if (prepend) {
int i = x.indexOf(en + sep);
if (i != 0) {
return pre + z;
}
String temp = x.substring((en + sep).length());
return pre + temp;
}
return pre + z;
};
int startId = 0;
for (String[] row : rows.get(en0).subList(1, rows.get(en0).size())) {
Gen l0;
String idCol = map.containsKey(en) ? map.get(en) : (String) op.getOrDefault(inner, AqlOption.id_column_name);
if (autoGenIds && !m.containsKey(idCol)) {
l0 = toGen(en0, "" + startId++);
} else if (!autoGenIds && !m.containsKey(idCol)) {
throw new RuntimeException("On " + en + ", ID column " + idCol + " not found in headers " + m.keySet() + ". \n\nPossible solution: provide a mapping.\n\nPossible solution: set csv_generate_ids=true to auto-generate IDs.\n\nPossible solution: rename the headers in the CSV file.\n\nPossible solution: add an ID column to the CSV file.");
} else {
l0 = toGen(en0, row[m.get(idCol)]);
}
ens0.get(en0).add(l0);
for (Fk fk : sch.fksFrom(en0)) {
if (!fks0.containsKey(l0)) {
fks0.put(l0, new Ctx<>());
}
Gen g = toGen(sch.fks.get(fk).second, row[m.get(mediate.apply(fk.str))]);
fks0.get(l0).put(fk, g);
}
for (Att att : sch.attsFrom(en0)) {
if (!atts0.containsKey(l0)) {
atts0.put(l0, new Ctx<>());
}
String zz = mediate.apply(att.str);
if (!m.containsKey(zz)) {
throw new RuntimeException("No column " + att + " in file for " + en + " nor explicit mapping for " + att + " given. Tried " + zz + " and options are " + m.keySet());
}
int z = m.get(zz);
if (z >= row.length) {
throw new RuntimeException("Cannot get index " + z + " from " + Arrays.toString(row));
}
String o = row[z];
Term<Ty, Void, Sym, Void, Void, Void, Null<?>> r = objectToSk(sch, o, l0, att, tys0, extraRepr, true, nullOnErr);
atts0.get(l0).put(att, r);
}
}
}
use of catdata.aql.Term in project fql by CategoricalData.
the class InstExpJdbcAll method toInstance.
private Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> toInstance(AqlEnv env, SqlInstance inst, SqlSchema info) {
AqlOptions ops = new AqlOptions(options, null, env.defaults);
Schema<Ty, En, Sym, Fk, Att> sch = makeSchema(env, info, ops);
Ctx<En, Collection<Gen>> ens0 = new Ctx<>(Util.newSetsFor0(sch.ens));
Ctx<Ty, Collection<Null<?>>> tys0 = new Ctx<>();
Ctx<Gen, Ctx<Fk, Gen>> fks0 = new Ctx<>();
Ctx<Gen, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Null<?>>>> atts0 = new Ctx<>();
Ctx<Null<?>, Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>> extraRepr = new Ctx<>();
for (Ty ty : sch.typeSide.tys) {
tys0.put(ty, new HashSet<>());
}
boolean schemaOnly = (Boolean) ops.getOrDefault(AqlOption.schema_only);
boolean nullOnErr = (Boolean) ops.getOrDefault(AqlOption.import_null_on_err_unsafe);
boolean dontCheckClosure = (Boolean) ops.getOrDefault(AqlOption.import_dont_check_closure_unsafe);
if (!schemaOnly) {
int fr = 0;
Map<SqlTable, Map<Map<SqlColumn, Optional<Object>>, Gen>> iso1 = new HashMap<>();
for (SqlTable table : info.tables) {
Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
Map<Map<SqlColumn, Optional<Object>>, Gen> i1 = new HashMap<>();
SqlColumn thePk = null;
if (table.pk.size() == 1) {
thePk = Util.get0(table.pk);
}
for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
Gen i = new Gen("v" + (fr++));
/* can't do this until Gen need not be unique
if (thePk == null) {
i = new Gen("v" + (fr++));
} else {
Optional<Object> x = tuple.get(thePk);
if (!x.isPresent()) {
throw new RuntimeException("Primary key col is null in " + tuple);
}
i = new Gen(x.get().toString()); //TODO aql
}
*/
i1.put(tuple, i);
// tuple.keySet().
// i2.put(i, tuple);
ens0.get(new En(table.name)).add(i);
for (SqlColumn c : table.columns) {
if (!atts0.containsKey(i)) {
atts0.put(i, new Ctx<>());
}
Optional<Object> val = tuple.get(c);
Term<Ty, Void, Sym, Void, Void, Void, Null<?>> xxx = InstExpJdbc.objectToSk(sch, val.orElse(null), i, new Att(new En(table.name), c.name), tys0, extraRepr, false, nullOnErr);
atts0.get(i).put(new Att(new En(table.name), c.name), xxx);
}
}
iso1.put(table, i1);
// iso2.put(table, i2);
}
for (SqlForeignKey fk : info.fks) {
for (Map<SqlColumn, Optional<Object>> in : inst.get(fk.source)) {
Map<SqlColumn, Optional<Object>> out = inst.follow(in, fk);
Gen tgen = iso1.get(fk.target).get(out);
Gen sgen = iso1.get(fk.source).get(in);
if (!fks0.containsKey(sgen)) {
fks0.put(sgen, new Ctx<>());
}
fks0.get(sgen).put(new Fk(new En(fk.source.name), fk.toString()), tgen);
}
}
}
ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>> alg = new ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>>(sch, ens0, tys0, fks0, atts0, Object::toString, Object::toString, dontCheckClosure);
return new SaturatedInstance<>(alg, alg, (Boolean) ops.getOrDefault(AqlOption.require_consistency), (Boolean) ops.getOrDefault(AqlOption.allow_java_eqs_unsafe), true, extraRepr);
}
Aggregations