Search in sources :

Example 16 with AqlOptions

use of catdata.aql.AqlOptions 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);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) En(catdata.aql.exp.SchExpRaw.En) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) LiteralTransform(catdata.aql.fdm.LiteralTransform) AqlOptions(catdata.aql.AqlOptions)

Example 17 with AqlOptions

use of catdata.aql.AqlOptions 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;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) AqlOptions(catdata.aql.AqlOptions) Ctx(catdata.Ctx) RawTerm(catdata.aql.RawTerm) En(catdata.aql.exp.SchExpRaw.En) Sym(catdata.aql.exp.TyExpRaw.Sym) Chc(catdata.Chc) HashMap(java.util.HashMap) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOption(catdata.aql.AqlOptions.AqlOption) HashSet(java.util.HashSet) Mapping(catdata.aql.Mapping) Map(java.util.Map) Collage(catdata.aql.Collage) Term(catdata.aql.Term) LinkedList(java.util.LinkedList) Pair(catdata.Pair) Fk(catdata.aql.exp.SchExpRaw.Fk) Collection(java.util.Collection) Kind(catdata.aql.Kind) Util(catdata.Util) Set(java.util.Set) Att(catdata.aql.exp.SchExpRaw.Att) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) Schema(catdata.aql.Schema) Var(catdata.aql.Var) Sk(catdata.aql.exp.InstExpRaw.Sk) Entry(java.util.Map.Entry) Triple(catdata.Triple) Ty(catdata.aql.exp.TyExpRaw.Ty) HashMap(java.util.HashMap) Var(catdata.aql.Var) En(catdata.aql.exp.SchExpRaw.En) Mapping(catdata.aql.Mapping) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) RawTerm(catdata.aql.RawTerm) LinkedList(java.util.LinkedList) Triple(catdata.Triple) Gen(catdata.aql.exp.InstExpRaw.Gen) Sk(catdata.aql.exp.InstExpRaw.Sk) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage) Chc(catdata.Chc)

Example 18 with AqlOptions

use of catdata.aql.AqlOptions 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);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) HashMap(java.util.HashMap) Var(catdata.aql.Var) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) Eq(catdata.aql.Eq) HashSet(java.util.HashSet) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) RawTerm(catdata.aql.RawTerm) Term(catdata.aql.Term) RawTerm(catdata.aql.RawTerm) Triple(catdata.Triple) Collection(java.util.Collection) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage) Chc(catdata.Chc)

Example 19 with AqlOptions

use of catdata.aql.AqlOptions 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;
}
Also used : AqlOptions(catdata.aql.AqlOptions) Ctx(catdata.Ctx) TypeSide(catdata.aql.TypeSide) RawTerm(catdata.aql.RawTerm) En(catdata.aql.exp.SchExpRaw.En) Sym(catdata.aql.exp.TyExpRaw.Sym) Chc(catdata.Chc) HashMap(java.util.HashMap) Function(java.util.function.Function) HashSet(java.util.HashSet) Map(java.util.Map) AqlJs(catdata.aql.AqlJs) Collage(catdata.aql.Collage) Eq(catdata.aql.Eq) Term(catdata.aql.Term) LinkedList(java.util.LinkedList) Pair(catdata.Pair) Collection(java.util.Collection) Kind(catdata.aql.Kind) Util(catdata.Util) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Var(catdata.aql.Var) Entry(java.util.Map.Entry) Triple(catdata.Triple) Ty(catdata.aql.exp.TyExpRaw.Ty) Ctx(catdata.Ctx) Var(catdata.aql.Var) AqlJs(catdata.aql.AqlJs) LinkedList(java.util.LinkedList) List(java.util.List) TypeSide(catdata.aql.TypeSide) Ty(catdata.aql.exp.TyExpRaw.Ty) Sym(catdata.aql.exp.TyExpRaw.Sym) RawTerm(catdata.aql.RawTerm) Term(catdata.aql.Term) RawTerm(catdata.aql.RawTerm) Triple(catdata.Triple) AqlOptions(catdata.aql.AqlOptions)

Aggregations

AqlOptions (catdata.aql.AqlOptions)19 En (catdata.aql.exp.SchExpRaw.En)17 Sym (catdata.aql.exp.TyExpRaw.Sym)17 Ty (catdata.aql.exp.TyExpRaw.Ty)17 Att (catdata.aql.exp.SchExpRaw.Att)16 Fk (catdata.aql.exp.SchExpRaw.Fk)16 Ctx (catdata.Ctx)13 Collage (catdata.aql.Collage)12 Pair (catdata.Pair)11 Term (catdata.aql.Term)11 HashSet (java.util.HashSet)10 Chc (catdata.Chc)9 RawTerm (catdata.aql.RawTerm)9 Gen (catdata.aql.exp.InstExpRaw.Gen)9 Triple (catdata.Triple)7 Var (catdata.aql.Var)7 Sk (catdata.aql.exp.InstExpRaw.Sk)7 Collection (java.util.Collection)7 HashMap (java.util.HashMap)7 LinkedList (java.util.LinkedList)6