Search in sources :

Example 1 with Var

use of catdata.aql.Var 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;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Var(catdata.aql.Var) Ctx(catdata.Ctx) Schema(catdata.aql.Schema) En(catdata.aql.exp.SchExpRaw.En) LinkedList(java.util.LinkedList) List(java.util.List) HashSet(java.util.HashSet) 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) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage) Chc(catdata.Chc)

Example 2 with Var

use of catdata.aql.Var 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 3 with Var

use of catdata.aql.Var in project fql by CategoricalData.

the class EvalAlgebra method eval.

// TODO must convert back to original unfolded vars
private Pair<List<Var>, Collection<Row<En2, X>>> eval(En2 en2, Frozen<Ty, En1, Sym, Fk1, Att1> q, Connection conn, boolean useSql) {
    Collection<Row<En2, X>> ret = null;
    Integer k = maxTempSize();
    if (useSql) {
        Pair<List<Var>, Collection<Row<En2, X>>> ret2 = evalSql(en2, q, I.algebra().intifyX((int) options.getOrDefault(AqlOption.start_ids_at)), conn);
        // TODO aql should also stop on max temp size?
        return ret2;
    } else {
        ret = new LinkedList<>();
        List<Var> plan = q.order(options, I);
        boolean useIndices = useIndices() && q.gens.size() > 1 && I.algebra().hasFreeTypeAlgebra();
        ret.add(new Row<>(en2));
        for (Var v : plan) {
            ret = Row.extend(en2, ret, v, q, I, k, useIndices);
        }
        return new Pair<>(plan, ret);
    }
}
Also used : Var(catdata.aql.Var) Collection(java.util.Collection) LinkedList(java.util.LinkedList) List(java.util.List) Row(catdata.aql.fdm.EvalAlgebra.Row) Pair(catdata.Pair)

Example 4 with Var

use of catdata.aql.Var in project fql by CategoricalData.

the class ColimSchExpModify method eval.

// TODO aql add options
@Override
public ColimitSchema<N> eval(AqlEnv env) {
    boolean checkJava = !(Boolean) env.defaults.getOrDefault(options, AqlOption.allow_java_eqs_unsafe);
    ColimitSchema<N> colim0 = colim.eval(env);
    // colim0.schemaStr.co
    for (Pair<String, String> k : ens) {
        colim0 = colim0.renameEntity(new En(k.first), new En(k.second), checkJava);
    }
    for (Pair<Pair<String, String>, String> k : fks0) {
        colim0 = colim0.renameFk(new Fk(new En(k.first.first), k.first.second), new Fk(new En(k.first.first), k.second), checkJava);
    }
    for (Pair<Pair<String, String>, String> k : atts0) {
        colim0 = colim0.renameAtt(new Att(new En(k.first.first), k.first.second), new Att(new En(k.first.first), k.second), checkJava);
    }
    for (Pair<Pair<String, String>, List<String>> k : fks) {
        if (!colim0.schemaStr.fks.containsKey(new Fk(new En(k.first.first), k.first.second))) {
            throw new RuntimeException("Not an fk: " + k.first + " in\n\n" + colim0.schemaStr);
        }
        String pre = "In processing " + k.first + " -> " + k.second + ", ";
        Collage<Ty, En, Sym, Fk, Att, Void, Void> xxx = colim0.schemaStr.collage();
        RawTerm term = RawTerm.fold(k.second, "v");
        En tr = colim0.schemaStr.fks.get(new Fk(new En(k.first.first), k.first.second)).second;
        Ctx<String, Chc<Ty, En>> ctx = new Ctx<>("v", Chc.inRight(new En(k.first.first)));
        Term<Ty, En, Sym, Fk, Att, Gen, Sk> t = RawTerm.infer1x(ctx.map, term, null, Chc.inRight(tr), xxx.convert(), pre, colim0.schemaStr.typeSide.js).second;
        // colim0 = colim0.removeAtt(new Att(k.first), new Var(k.second.first), t.convert(), checkJava);
        colim0 = colim0.removeFk(new Fk(new En(k.first.first), k.first.second), t.toFkList(), checkJava);
    }
    for (Pair<Pair<String, String>, Triple<String, String, RawTerm>> k : atts) {
        if (!colim0.schemaStr.atts.containsKey(new Att(new En(k.first.first), k.first.second))) {
            throw new RuntimeException("Not an attribute: " + k.first + " in\n\n" + colim0.schemaStr);
        }
        String pre = "In processing " + k.first + " -> lambda " + k.second.first + "." + k.second.third + ", ";
        Pair<En, Ty> r = colim0.schemaStr.atts.get(new Att(new En(k.first.first), k.first.second));
        if (k.second.second != null && !k.second.second.equals(r.first)) {
            throw new RuntimeException(pre + " given type is " + k.second.second + " but expected " + r.first);
        }
        Collage<Ty, En, Sym, Fk, Att, Void, Void> xxx = colim0.schemaStr.collage();
        Ctx<String, Chc<Ty, En>> ctx = new Ctx<>(k.second.first, Chc.inRight(r.first));
        Term<Ty, En, Sym, Fk, Att, Gen, Sk> t = RawTerm.infer1x(ctx.map, k.second.third, null, Chc.inLeft(r.second), xxx.convert(), pre, colim0.schemaStr.typeSide.js).second;
        colim0 = colim0.removeAtt(new Att(new En(k.first.first), k.first.second), new Var(k.second.first), t.convert(), checkJava);
    }
    return colim0;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) Var(catdata.aql.Var) En(catdata.aql.exp.SchExpRaw.En) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair) Fk(catdata.aql.exp.SchExpRaw.Fk) Ty(catdata.aql.exp.TyExpRaw.Ty) Sym(catdata.aql.exp.TyExpRaw.Sym) RawTerm(catdata.aql.RawTerm) Triple(catdata.Triple) Gen(catdata.aql.exp.InstExpRaw.Gen) Sk(catdata.aql.exp.InstExpRaw.Sk) Chc(catdata.Chc)

Example 5 with Var

use of catdata.aql.Var in project fql by CategoricalData.

the class InstExpJdbcAll method makeSchema.

public Schema<Ty, En, Sym, Fk, Att> makeSchema(AqlEnv env, SqlSchema info, AqlOptions ops) {
    boolean checkJava = !(Boolean) ops.getOrDefault(AqlOption.allow_java_eqs_unsafe);
    TypeSide<Ty, Sym> typeSide = new SqlTypeSide(ops);
    // typeSide.validate(true);
    Collage<Ty, En, Sym, Fk, Att, Void, Void> col0 = new Collage<>(typeSide.collage());
    Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs = new HashSet<>();
    for (SqlTable table : info.tables) {
        col0.ens.add(new En(table.name));
        for (SqlColumn c : table.columns) {
            if (col0.atts.containsKey(new Att(new En(table.name), c.name))) {
                throw new RuntimeException("Name collision: table " + c.table.name + " col " + c.name + " against table " + col0.atts.get(new Att(new En(table.name), c.name)).first + "\n\n.Possible solution: set option jdbc_import_col_seperator so as to avoid name collisions.");
            }
            col0.atts.put(new Att(new En(table.name), c.name), new Pair<>(new En(table.name), new Ty(sqlTypeToAqlType(c.type.name))));
        }
    }
    for (SqlForeignKey fk : info.fks) {
        col0.fks.put(new Fk(new En(fk.source.name), fk.toString()), new Pair<>(new En(fk.source.name), new En(fk.target.name)));
        Var v = new Var("x");
        for (SqlColumn tcol : fk.map.keySet()) {
            SqlColumn scol = fk.map.get(tcol);
            Att l = new Att(new En(scol.table.name), scol.name);
            Att r = new Att(new En(tcol.table.name), tcol.name);
            Term<Ty, En, Sym, Fk, Att, Void, Void> lhs = Term.Att(l, Term.Var(v));
            Term<Ty, En, Sym, Fk, Att, Void, Void> rhs = Term.Att(r, Term.Fk(new Fk(new En(fk.source.name), fk.toString()), Term.Var(v)));
            eqs.add(new Triple<>(new Pair<>(v, new En(fk.source.name)), lhs, rhs));
            col0.eqs.add(new Eq<>(new Ctx<>(new Pair<>(v, Chc.inRight(new En(fk.source.name)))), lhs, rhs));
        }
    }
    DP<Ty, En, Sym, Fk, Att, Void, Void> dp = AqlProver.create(new AqlOptions(options, col0, env.defaults), col0, typeSide.js);
    Schema<Ty, En, Sym, Fk, Att> sch = new Schema<>(typeSide, col0.ens, col0.atts.map, col0.fks.map, eqs, dp, checkJava);
    return sch;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) SqlTypeSide(catdata.aql.SqlTypeSide) Var(catdata.aql.Var) Ctx(catdata.Ctx) SqlSchema(catdata.sql.SqlSchema) Schema(catdata.aql.Schema) En(catdata.aql.exp.SchExpRaw.En) HashSet(java.util.HashSet) Pair(catdata.Pair) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Triple(catdata.Triple) SqlForeignKey(catdata.sql.SqlForeignKey) SqlTable(catdata.sql.SqlTable) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage) SqlColumn(catdata.sql.SqlColumn)

Aggregations

Var (catdata.aql.Var)11 Ctx (catdata.Ctx)9 Pair (catdata.Pair)9 Chc (catdata.Chc)8 Triple (catdata.Triple)8 En (catdata.aql.exp.SchExpRaw.En)8 Sym (catdata.aql.exp.TyExpRaw.Sym)8 Ty (catdata.aql.exp.TyExpRaw.Ty)8 AqlOptions (catdata.aql.AqlOptions)7 Collage (catdata.aql.Collage)7 RawTerm (catdata.aql.RawTerm)7 Att (catdata.aql.exp.SchExpRaw.Att)7 Fk (catdata.aql.exp.SchExpRaw.Fk)7 LinkedList (java.util.LinkedList)7 Term (catdata.aql.Term)6 HashSet (java.util.HashSet)6 List (java.util.List)6 Collection (java.util.Collection)5 Eq (catdata.aql.Eq)4 Schema (catdata.aql.Schema)4