use of catdata.aql.exp.SchExpRaw.Att in project fql by CategoricalData.
the class InstExpRandom method eval.
// not exactly the smartest way
@Override
public SaturatedInstance<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>, Pair<Integer, En>, Pair<Integer, Att>> eval(AqlEnv env) {
int seed = (Integer) new AqlOptions(options, null, env.defaults).getOrDefault(AqlOption.random_seed);
Random rand = new Random(seed);
Schema<Ty, En, Sym, Fk, Att> schema = sch.eval(env);
Ctx<En, Collection<Pair<Integer, En>>> ens0 = new Ctx<>();
Ctx<Ty, Collection<Pair<Integer, Att>>> tys = new Ctx<>();
Ctx<Pair<Integer, En>, Ctx<Fk, Pair<Integer, En>>> fks = new Ctx<>();
Ctx<Pair<Integer, En>, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Pair<Integer, Att>>>> atts = new Ctx<>();
for (Ty ty : schema.typeSide.tys) {
tys.put(ty, new LinkedList<>());
}
for (String en : ens.keySet()) {
List<Pair<Integer, En>> l = new LinkedList<>();
int size = ens.get(en);
for (int i = 0; i < size; i++) {
l.add(new Pair<>(i, new En(en)));
Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Pair<Integer, Att>>> ctx = new Ctx<>();
for (Att att : schema.attsFrom(new En(en))) {
ctx.put(att, Term.Sk(new Pair<>(i, att)));
tys.get(schema.atts.get(att).second).add(new Pair<>(i, att));
}
atts.put(new Pair<>(i, new En(en)), ctx);
Ctx<Fk, Pair<Integer, En>> ctx0 = new Ctx<>();
for (Fk fk : schema.fksFrom(new En(en))) {
int size0 = ens.get(schema.fks.get(fk).second.str);
Integer k = rand.nextInt(size0);
ctx0.put(fk, new Pair<>(k, schema.fks.get(fk).second));
}
fks.put(new Pair<>(i, new En(en)), ctx0);
}
ens0.put(new En(en), l);
}
ImportAlgebra<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> alg = new ImportAlgebra<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>>(schema, ens0, tys, fks, atts, x -> x.toString(), x -> x.toString(), true);
DP<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> dp = new DP<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>>() {
@Override
public String toStringProver() {
return "Random";
}
@Override
public boolean eq(Ctx<Var, Chc<Ty, En>> ctx, Term<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> lhs, Term<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> rhs) {
if (!ctx.isEmpty()) {
Util.anomaly();
}
return lhs.equals(rhs);
}
};
return new SaturatedInstance<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>, Pair<Integer, En>, Pair<Integer, Att>>(alg, dp, false, true, false, new Ctx<>());
}
use of catdata.aql.exp.SchExpRaw.Att in project fql by CategoricalData.
the class InstExpRaw method eval.
@Override
public synchronized Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> eval(AqlEnv env) {
Schema<Ty, En, Sym, Fk, Att> sch = schema.eval(env);
Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col = new Collage<>(sch.collage());
Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>>> eqs0 = new HashSet<>();
for (String k : imports) {
@SuppressWarnings("unchecked") Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> v = env.defs.insts.get(k);
col.addAll(v.collage());
eqs0.addAll(v.eqs());
}
for (Pair<String, String> p : gens) {
String gen = p.first;
String ty = p.second;
if (col.ens.contains(new En(ty))) {
col.gens.put(new Gen(gen), new En(ty));
} else if (col.tys.contains(new Ty(ty))) {
col.sks.put(new Sk(gen), new Ty(ty));
} else {
throw new LocException(find("generators", p), "The sort for " + gen + ", namely " + ty + ", is not declared as a type or entity");
}
}
for (Pair<RawTerm, RawTerm> eq : eqs) {
try {
Map<String, Chc<Ty, En>> ctx = Collections.emptyMap();
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.first, eq.second, null, col, "", sch.typeSide.js).first3();
eqs0.add(new Pair<>(eq0.second, eq0.third));
col.eqs.add(new Eq<>(new Ctx<>(), eq0.second, eq0.third));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(find("equations", eq), "In equation " + eq.first + " = " + eq.second + ", " + ex.getMessage());
}
}
AqlOptions strat = new AqlOptions(options, col, env.defaults);
boolean interpret_as_algebra = (boolean) strat.getOrDefault(AqlOption.interpret_as_algebra);
boolean dont_check_closure = (boolean) strat.getOrDefault(AqlOption.import_dont_check_closure_unsafe);
if (interpret_as_algebra) {
Ctx<En, Set<Gen>> ens0x = new Ctx<>(Util.revS(col.gens.map));
Ctx<En, Collection<Gen>> ens0 = ens0x.map(x -> (Collection<Gen>) x);
if (!col.sks.isEmpty()) {
throw new RuntimeException("Cannot have generating labelled nulls with import_as_theory");
}
Ctx<Ty, Collection<Null<?>>> tys0 = new Ctx<>();
for (Ty ty : sch.typeSide.tys) {
tys0.put(ty, new HashSet<>());
}
Ctx<Gen, Ctx<Fk, Gen>> fks0 = new Ctx<>();
Ctx<Gen, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Null<?>>>> atts0 = new Ctx<>();
for (Gen gen : col.gens.keySet()) {
fks0.put(gen, new Ctx<>());
atts0.put(gen, new Ctx<>());
}
for (Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> e : eqs0) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> lhs = e.first;
Term<Ty, En, Sym, Fk, Att, Gen, Sk> rhs = e.second;
if (rhs.gen != null && lhs.fk != null && lhs.arg.gen != null) {
fks0.get(lhs.arg.gen).put(lhs.fk, rhs.gen);
} else if (lhs.gen != null && rhs.fk != null && rhs.arg.gen != null) {
fks0.get(rhs.arg.gen).put(rhs.fk, lhs.gen);
} else if (rhs.obj != null && lhs.att != null && lhs.arg.gen != null) {
atts0.get(lhs.arg.gen).put(lhs.att, Term.Obj(rhs.obj, rhs.ty));
} else if (lhs.obj != null && rhs.att != null && rhs.arg.gen != null) {
atts0.get(rhs.arg.gen).put(rhs.att, Term.Obj(lhs.obj, lhs.ty));
} else {
throw new RuntimeException("import_as_theory not compatible with equation " + lhs + " = " + rhs + "; each equation must be of the form gen.fk=gen or gen.att=javaobject");
}
}
Ctx<Null<?>, Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>> extraRepr = new Ctx<>();
for (Gen gen : col.gens.keySet()) {
for (Att att : sch.attsFrom(col.gens.get(gen))) {
if (!atts0.get(gen).containsKey(att)) {
atts0.get(gen).put(att, InstExpImport.objectToSk(sch, null, gen, att, tys0, extraRepr, false, false));
}
}
}
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, dont_check_closure);
return new SaturatedInstance(alg, alg, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe), true, extraRepr);
}
InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Sk, ID> initial = new InitialAlgebra<>(strat, sch, col, new It(), Object::toString, Object::toString);
return new LiteralInstance<>(sch, col.gens.map, col.sks.map, eqs0, initial.dp(), initial, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe));
}
use of catdata.aql.exp.SchExpRaw.Att 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.exp.SchExpRaw.Att in project fql by CategoricalData.
the class TransExpImport method eval.
@Override
public Transform<Ty, En, Sym, Fk, Att, Gen1, Sk1, Gen2, Sk2, X1, Y1, X2, Y2> eval(AqlEnv env) {
Instance<Ty, En, Sym, Fk, Att, Gen1, Sk1, X1, Y1> src0 = src.eval(env);
Instance<Ty, En, Sym, Fk, Att, Gen2, Sk2, X2, Y2> dst0 = dst.eval(env);
if (!src0.schema().equals(dst0.schema())) {
throw new RuntimeException("Schema of instance source is " + src0 + " but schema of target instance is " + dst0);
}
Schema<Ty, En, Sym, Fk, Att> sch = src0.schema();
for (String o : map.keySet()) {
if (!sch.ens.contains(new En(o))) {
throw new RuntimeException("there is an import for " + o + ", which is not an entity in the schema ");
}
}
gens = new Ctx<>();
sks = new Ctx<>();
op = new AqlOptions(options, null, env.defaults);
dontValidateEqs = (Boolean) op.getOrDefault(AqlOption.dont_validate_unsafe);
boolean autoMapNulls = (Boolean) op.getOrDefault(AqlOption.map_nulls_arbitrarily_unsafe);
for (Sk1 sk : src0.sks().keySet()) {
Ty ty = src0.sks().get(sk);
Set<Sk2> xxx = Util.revS(dst0.sks().map).get(ty);
if (xxx.isEmpty()) {
throw new RuntimeException("Cannot map null " + sk + " to target instance because target instance has no nulls at type " + ty);
}
if (xxx.size() > 1) {
if (autoMapNulls) {
Sk2 sk2 = Util.get0X(xxx);
sks.put(sk, Term.Sk(sk2));
} else {
throw new RuntimeException("Cannot automatically map null " + sk + " to target instance because target instance has " + xxx.size() + " nulls at type " + ty + ". Possible solution: add options map_nulls_arbitrarily_unsafe = true");
}
} else {
Sk2 sk2 = Util.get0(xxx);
sks.put(sk, Term.Sk(sk2));
}
}
try {
Handle h = start(sch);
for (En en : sch.ens) {
if (map.containsKey(en.str)) {
processEn(en, sch, h, map.get(en.str));
}
}
stop(h);
} catch (Exception exn) {
// .getMessage() + "\n\n" + getHelpStr());
throw new RuntimeException(exn);
}
return new LiteralTransform<>(gens.map, sks.map, src0, dst0, dontValidateEqs);
}
use of catdata.aql.exp.SchExpRaw.Att in project fql by CategoricalData.
the class MapExpRaw method eval.
@Override
public Mapping<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);
// Collage<String, String, String, String, String, Void, Void> scol =
// new Collage<>(src0);
Collage<Ty, En, Sym, Fk, Att, Void, Void> dcol = new Collage<>(dst0.collage());
Map<En, En> ens0 = new HashMap<>();
// Map<String, Pair<String, List<String>>> fks0 = new HashMap<>();
Map<Att, Triple<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>>> atts0 = new HashMap<>();
Map<Fk, Pair<En, List<Fk>>> fksX = new HashMap<>();
for (String k : imports) {
@SuppressWarnings("unchecked") Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> v = env.defs.maps.get(k);
Util.putAllSafely(ens0, v.ens.map);
Util.putAllSafely(fksX, v.fks.map);
Util.putAllSafely(atts0, v.atts.map);
}
Util.putAllSafely(ens0, Util.toMapSafely(ens.stream().map(x -> new Pair<>(new En(x.first), new En(x.second))).collect(Collectors.toList())));
for (En k : ens0.keySet()) {
if (!dst0.ens.contains(ens0.get(k))) {
throw new LocException(find("entities", new Pair<>(k, ens0.get(k))), "The mapping for " + k + ", namely " + ens0.get(k) + ", does not appear in the target schema");
} else if (!src0.ens.contains(k)) {
throw new LocException(find("entities", new Pair<>(k, ens0.get(k))), k + " does not appear in the source schema");
}
}
for (Pair<Pair<String, String>, List<String>> p : this.fks) {
Fk theFk = new Fk(new En(p.first.first), p.first.second);
if (!src0.fks.containsKey(theFk)) {
throw new RuntimeException("Not a foreign key in source: " + theFk.en + "." + theFk.str);
}
try {
En start_en_fixed = ens0.get(new En(p.first.first));
En start_en = ens0.get(new En(p.first.first));
List<Fk> r = new LinkedList<>();
for (String o : p.second) {
if (ens0.containsValue(new En(o))) {
if (fksX.containsKey(new Fk(start_en, o))) {
throw new RuntimeException(o + " is both a target foreign key and a target entity, so the path is ambiguous");
}
// if (start_en == null) {
// start_en = new En(p.second.get(0));
// }
} else {
/*
* if (start_en == null) { Pair<En, En> j =
* dst0.fks.get(new Fk(o)); if (j == null) { throw new
* RuntimeException(p.second.get(0) +
* " is not a foreign key in the target"); } start_en =
* j.first; }
*/
// }
r.add(new Fk(start_en, o));
start_en = dst0.fks.get(new Fk(start_en, o)).second;
}
}
// if (start_en == null) {
// throw new RuntimeException("Anomaly: please report");
// }
fksX.put(new Fk(new En(p.first.first), p.first.second), new Pair<>(start_en_fixed, r));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(fkPos.get(new Fk(new En(p.first.first), p.first.second)), "In foreign key mapping " + p.first + " -> " + Util.sep(p.second, ".") + ", " + ex.getMessage());
}
}
for (Pair<Pair<String, String>, Triple<String, String, RawTerm>> att : atts) {
try {
String var = att.second.first;
String var_en = att.second.second;
RawTerm term = att.second.third;
Pair<En, Ty> p = src0.atts.map.get(new Att(new En(att.first.first), att.first.second));
if (p == null) {
throw new RuntimeException(att.first + " is not a source attribute.");
}
En src_att_dom_en = p.first;
En dst_att_dom_en = ens0.get(src_att_dom_en);
if (dst_att_dom_en == null) {
throw new RuntimeException("no entity mapping for " + src_att_dom_en + " , required for domain for " + att.first);
}
if (var_en != null && !new En(var_en).equals(dst_att_dom_en)) {
throw new RuntimeException("the given source entity for the variable, " + var_en + ", is not " + dst_att_dom_en + " as expected.");
}
Ty src_att_cod_ty = p.second;
if (!dst0.typeSide.tys.contains(src_att_cod_ty)) {
throw new RuntimeException("type " + p.second + " does not exist in target typeside.");
}
Chc<Ty, En> proposed_ty2 = Chc.inLeft(src_att_cod_ty);
Chc<Ty, En> var_en2 = Chc.inRight(dst_att_dom_en);
Map<String, Chc<Ty, En>> ctx = Util.singMap(var, var_en2);
Term<Ty, En, Sym, Fk, Att, Gen, Sk> term0 = RawTerm.infer1x(ctx, term, null, proposed_ty2, dcol.convert(), "", src0.typeSide.js).second;
Util.putSafely(atts0, new Att(new En(att.first.first), att.first.second), new Triple<>(new Var(var), dst_att_dom_en, term0.convert()));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(attPos.get(new Att(new En(att.first.first), att.first.second)), "in mapping for " + att.first + ", " + ex.getMessage());
}
}
AqlOptions ops = new AqlOptions(options, null, env.defaults);
Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> ret = new Mapping<>(ens0, atts0, fksX, src0, dst0, (Boolean) ops.getOrDefault(AqlOption.dont_validate_unsafe));
return ret;
}
Aggregations