Search in sources :

Example 1 with Eq

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);
}
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) Trans(catdata.aql.exp.QueryExpRaw.Trans) 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) Term(catdata.aql.Term) RawTerm(catdata.aql.RawTerm) 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 2 with Eq

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);
}
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) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOption(catdata.aql.AqlOptions.AqlOption) AqlProver(catdata.aql.AqlProver) HashSet(java.util.HashSet) Map(java.util.Map) Collage(catdata.aql.Collage) DP(catdata.aql.DP) Eq(catdata.aql.Eq) Term(catdata.aql.Term) LinkedList(java.util.LinkedList) Pair(catdata.Pair) Fk(catdata.aql.exp.SchExpRaw.Fk) PreBlock(catdata.aql.exp.QueryExpRaw.PreBlock) Block(catdata.aql.exp.QueryExpRaw.Block) Collection(java.util.Collection) Kind(catdata.aql.Kind) Util(catdata.Util) Query(catdata.aql.Query) Att(catdata.aql.exp.SchExpRaw.Att) List(java.util.List) Schema(catdata.aql.Schema) Var(catdata.aql.Var) Sk(catdata.aql.exp.InstExpRaw.Sk) Triple(catdata.Triple) SchExpCod(catdata.aql.exp.SchExp.SchExpCod) Collections(java.util.Collections) Ty(catdata.aql.exp.TyExpRaw.Ty) Var(catdata.aql.Var) Ctx(catdata.Ctx) Schema(catdata.aql.Schema) En(catdata.aql.exp.SchExpRaw.En) Eq(catdata.aql.Eq) 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) Term(catdata.aql.Term) RawTerm(catdata.aql.RawTerm) 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 3 with Eq

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);
}
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 4 with Eq

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;
}
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

Chc (catdata.Chc)4 Ctx (catdata.Ctx)4 Triple (catdata.Triple)4 AqlOptions (catdata.aql.AqlOptions)4 Collage (catdata.aql.Collage)4 Eq (catdata.aql.Eq)4 RawTerm (catdata.aql.RawTerm)4 Term (catdata.aql.Term)4 Var (catdata.aql.Var)4 En (catdata.aql.exp.SchExpRaw.En)4 Sym (catdata.aql.exp.TyExpRaw.Sym)4 Ty (catdata.aql.exp.TyExpRaw.Ty)4 Pair (catdata.Pair)3 Att (catdata.aql.exp.SchExpRaw.Att)3 Fk (catdata.aql.exp.SchExpRaw.Fk)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Util (catdata.Util)2 Kind (catdata.aql.Kind)2