Search in sources :

Example 1 with ID

use of catdata.aql.It.ID in project fql by CategoricalData.

the class InstExpCsvQuotient method eval.

@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> eval(AqlEnv env) {
    Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> J = I.eval(env);
    Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col = new Collage<>(J.collage());
    AqlOptions strat = new AqlOptions(options, col, env.defaults);
    Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>>> eqs0 = new HashSet<>(J.eqs());
    Map<String, String> map = new HashMap<>();
    for (String q : queries) {
        map.put(q, q);
    }
    try {
        Map<En, List<String[]>> ret = InstExpCsv.start2(map, strat, J.schema(), true);
        for (En q : ret.keySet()) {
            for (String[] row : ret.get(q)) {
                if (row.length != 2) {
                    throw new RuntimeException("On " + q + ", encountered a row of length != 2: " + Arrays.toString(row));
                }
                Gen gen1 = (Gen) row[0];
                Gen gen2 = (Gen) row[1];
                if (gen1 == null) {
                    throw new RuntimeException("Encountered a NULL generator in column 1 of " + q);
                }
                if (gen2 == null) {
                    throw new RuntimeException("Encountered a NULL generator in column 2 of " + q);
                }
                if (!J.gens().containsKey(gen1)) {
                    throw new RuntimeException("Cannot import record linkage: " + gen1 + " is not a generator in the input instance");
                } else if (!J.gens().containsKey(gen2)) {
                    throw new RuntimeException("Cannot import record linkage: " + gen2 + " is not a generator in the input instance");
                }
                Term<Ty, En, Sym, Fk, Att, Gen, Sk> l = Term.Gen(gen1);
                Term<Ty, En, Sym, Fk, Att, Gen, Sk> r = Term.Gen(gen2);
                eqs0.add(new Pair<>(l, r));
                col.eqs.add(new Eq<>(new Ctx<>(), l, r));
            }
        }
        InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Sk, ID> initial0 = new InitialAlgebra<>(strat, J.schema(), col, new It(), Object::toString, Object::toString);
        return new LiteralInstance<>(J.schema(), col.gens.map, col.sks.map, eqs0, initial0.dp(), initial0, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe));
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage());
    }
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) HashMap(java.util.HashMap) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) It(catdata.aql.It) LiteralInstance(catdata.aql.fdm.LiteralInstance) InitialAlgebra(catdata.aql.fdm.InitialAlgebra) List(java.util.List) ID(catdata.aql.It.ID) Pair(catdata.Pair) HashSet(java.util.HashSet) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage)

Example 2 with ID

use of catdata.aql.It.ID in project fql by CategoricalData.

the class InstExpImport method forTheory.

public static <Ty, En, Sym, Fk, Att, Gen> Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> forTheory(Schema<Ty, En, Sym, Fk, Att> sch, Ctx<En, Collection<Gen>> ens0, Ctx<Ty, Collection<Null<?>>> tys0, Ctx<Gen, Ctx<Fk, Gen>> fks0, Ctx<Gen, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Null<?>>>> atts0, AqlOptions op) {
    Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>, Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>>> eqs0 = new HashSet<>();
    Collage<Ty, En, Sym, Fk, Att, Gen, Null<?>> col = new Collage<>(sch.collage());
    for (Gen gen : fks0.keySet()) {
        for (Fk fk : fks0.get(gen).keySet()) {
            eqs0.add(new Pair<>(Term.Fk(fk, Term.Gen(gen)), Term.Gen(fks0.get(gen).get(fk))));
            col.eqs.add(new Eq<>(new Ctx<>(), Term.Fk(fk, Term.Gen(gen)), Term.Gen(fks0.get(gen).get(fk))));
        }
    }
    for (Gen gen : atts0.keySet()) {
        for (Att att : atts0.get(gen).keySet()) {
            eqs0.add(new Pair<>(Term.Att(att, Term.Gen(gen)), atts0.get(gen).get(att).convert()));
            col.eqs.add(new Eq<>(new Ctx<>(), Term.Att(att, Term.Gen(gen)), atts0.get(gen).get(att).convert()));
        }
    }
    for (En en : ens0.keySet()) {
        for (Gen gen : ens0.get(en)) {
            col.gens.put(gen, en);
        }
    }
    for (Ty ty : tys0.keySet()) {
        for (Null<?> sk : tys0.get(ty)) {
            col.sks.put(sk, ty);
        }
    }
    InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>, ID> initial = new InitialAlgebra<>(op, sch, col, new It(), (Gen x) -> x.toString(), (Null<?> x) -> x.toString());
    Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, ID, Chc<Null<?>, Pair<ID, Att>>> I = new LiteralInstance<>(sch, col.gens.map, col.sks.map, eqs0, initial.dp(), initial, (Boolean) op.getOrDefault(AqlOption.require_consistency), (Boolean) op.getOrDefault(AqlOption.allow_java_eqs_unsafe));
    @SuppressWarnings("unchecked") Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> J = (Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>>) ((Object) I);
    return J;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Null(catdata.Null) Instance(catdata.aql.Instance) LiteralInstance(catdata.aql.fdm.LiteralInstance) SaturatedInstance(catdata.aql.fdm.SaturatedInstance) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) It(catdata.aql.It) LiteralInstance(catdata.aql.fdm.LiteralInstance) InitialAlgebra(catdata.aql.fdm.InitialAlgebra) ID(catdata.aql.It.ID) Pair(catdata.Pair) HashSet(java.util.HashSet) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Gen(catdata.aql.exp.InstExpRaw.Gen) Collage(catdata.aql.Collage) Chc(catdata.Chc)

Example 3 with ID

use of catdata.aql.It.ID in project fql by CategoricalData.

the class Constraints method step.

// TODO aql needs to be over all eds
public <Gen, Sk, X, Y> Instance<Ty, En, Sym, Fk, Att, ?, ?, ?, ?> step(Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> I, AqlOptions options) {
    Collection<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>> T = triggers(I, options);
    if (T.isEmpty()) {
        return null;
    }
    DMG<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Void> shape = new DMG<>(T, new HashMap<>());
    Ctx<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>>> nodesA = new Ctx<>();
    Ctx<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>>> nodesE = new Ctx<>();
    Map<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Term<Void, En, Void, Fk, Void, Gen, Void>> aaa = new HashMap<>();
    Map<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Term<Void, En, Void, Fk, Void, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Void>> xxx = new HashMap<>();
    for (Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>> t : T) {
        Query<Ty, En, Sym, Fk, Att, WHICH, Unit, Void> Q = t.first.Q;
        Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> A = Q.ens.get(WHICH.FRONT);
        Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> E = Q.ens.get(WHICH.BACK);
        Transform<Ty, En, Sym, Fk, Att, Var, Var, Var, Var, ID, Chc<Var, Pair<ID, Att>>, ID, Chc<Var, Pair<ID, Att>>> AE = Q.fks.get(new Unit());
        nodesA.put(t, A);
        nodesE.put(t, E);
        for (Var v : AE.src().gens().keySet()) {
            // revisit after colimit fixed
            xxx.put(new Pair<>(t, v), Term.Gen(new Pair<>(t, v)));
            aaa.put(new Pair<>(t, v), I.algebra().repr(t.second.get(v)));
        }
    /*for (Void v : AE.src().sks().keySet()) {
				yyy.put(v, Util.abort(v)); // revisit after colimit fixed
				bbb.put(v, Util.abort(v));
			}*/
    }
    ColimitInstance<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Void, Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> A0 = new ColimitInstance<>(schema, shape, nodesA, new Ctx<>(), options);
    ColimitInstance<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Void, Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> E0 = new ColimitInstance<>(schema, shape, nodesE, new Ctx<>(), options);
    LiteralTransform<Ty, En, Sym, Fk, Att, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, ID, Chc<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<ID, Att>>, ID, Chc<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<ID, Att>>> A0E0 = new LiteralTransform<>(xxx, new HashMap<>(), A0, E0, false);
    LiteralTransform<Ty, En, Sym, Fk, Att, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Gen, Sk, ID, Chc<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<ID, Att>>, X, Y> A0I = new LiteralTransform<>(aaa, new HashMap<>(), A0, I, false);
    return pushout(A0E0, A0I, options);
// TODO aql disable checking for speed
}
Also used : EvalInstance(catdata.aql.fdm.EvalInstance) ColimitInstance(catdata.aql.fdm.ColimitInstance) HashMap(java.util.HashMap) Ctx(catdata.Ctx) ColimitInstance(catdata.aql.fdm.ColimitInstance) Unit(catdata.Unit) WHICH(catdata.aql.ED.WHICH) ID(catdata.aql.It.ID) Pair(catdata.Pair) DMG(catdata.graph.DMG) LiteralTransform(catdata.aql.fdm.LiteralTransform) Row(catdata.aql.fdm.EvalAlgebra.Row) Chc(catdata.Chc)

Example 4 with ID

use of catdata.aql.It.ID in project fql by CategoricalData.

the class InstExpJdbcQuotient method eval.

@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> eval(AqlEnv env) {
    Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> J = I.eval(env);
    Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col = new Collage<>(J.collage());
    AqlOptions strat = new AqlOptions(options, col, env.defaults);
    String toGet = jdbcString;
    String driver = clazz;
    if (clazz.trim().isEmpty()) {
        driver = (String) strat.getOrDefault(AqlOption.jdbc_default_class);
        Util.checkClass(driver);
    }
    if (jdbcString.trim().isEmpty()) {
        toGet = (String) strat.getOrDefault(AqlOption.jdbc_default_string);
    }
    Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>>> eqs0 = new HashSet<>(J.eqs());
    try (Connection conn = DriverManager.getConnection(toGet)) {
        for (Entry<LocStr, String> q : queries.entrySet()) {
            if (!J.schema().ens.contains(new En(q.getKey().str))) {
                throw new LocException(q.getKey().loc, "Not an entity: " + q.getKey().str + ", expected one of " + J.schema().ens);
            }
            Statement stmt = conn.createStatement();
            stmt.execute(q.getValue());
            ResultSet rs = stmt.getResultSet();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnsNumber = rsmd.getColumnCount();
            if (columnsNumber != 2) {
                stmt.close();
                rs.close();
                throw new LocException(q.getKey().loc, "Expected 2 columns but received " + columnsNumber);
            }
            while (rs.next()) {
                // input instance need not be jdbc, so dont call toGen from import here
                // (Gen) ;
                Gen gen1 = new Gen(rs.getObject(1).toString());
                // (Gen) rs.getObject(2).toString();
                Gen gen2 = new Gen(rs.getObject(2).toString());
                if (gen1 == null || gen2 == null) {
                    stmt.close();
                    rs.close();
                    throw new LocException(q.getKey().loc, "Encountered a NULL generator");
                } else if (!J.gens().containsKey(gen1)) {
                    throw new LocException(q.getKey().loc, "Cannot import record linkage: " + gen1 + " is not a generator in the input instance");
                } else if (!J.gens().containsKey(gen2)) {
                    throw new LocException(q.getKey().loc, "Cannot import record linkage: " + gen2 + " is not a generator in the input instance");
                }
                Term<Ty, En, Sym, Fk, Att, Gen, Sk> l = Term.Gen(gen1);
                Term<Ty, En, Sym, Fk, Att, Gen, Sk> r = Term.Gen(gen2);
                eqs0.add(new Pair<>(l, r));
                col.eqs.add(new Eq<>(new Ctx<>(), l, r));
            }
            stmt.close();
            rs.close();
        }
    // } catch (SQLException exn) {
    // exn.printStackTrace();
    // throw new RuntimeException(/*"JDBC exception: " + */ exn /*.getMessage() */);
    } catch (Throwable thr) {
        // thr.printStackTrace();
        throw new RuntimeException(thr);
    }
    InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Sk, ID> initial0 = new InitialAlgebra<>(strat, J.schema(), col, new It(), Object::toString, Object::toString);
    return new LiteralInstance<>(J.schema(), col.gens.map, col.sks.map, eqs0, initial0.dp(), initial0, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe));
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) It(catdata.aql.It) LiteralInstance(catdata.aql.fdm.LiteralInstance) ResultSetMetaData(java.sql.ResultSetMetaData) InitialAlgebra(catdata.aql.fdm.InitialAlgebra) ResultSet(java.sql.ResultSet) ID(catdata.aql.It.ID) Pair(catdata.Pair) HashSet(java.util.HashSet) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Statement(java.sql.Statement) Connection(java.sql.Connection) Gen(catdata.aql.exp.InstExpRaw.Gen) Sk(catdata.aql.exp.InstExpRaw.Sk) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage)

Example 5 with ID

use of catdata.aql.It.ID in project fql by CategoricalData.

the class InstExpQuotient method eval.

@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> eval(AqlEnv env) {
    Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> J = I.eval(env);
    Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col = new Collage<>(J.collage());
    Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>>> eqs0 = new HashSet<>();
    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, "", J.schema().typeSide.js).first3();
            if (J.type(eq0.second).left) {
                throw new RuntimeException("Attempt to equate values at type in quotient: " + eq0.second + " at type " + J.type(eq0.second).l);
            }
            if (J.type(eq0.third).left) {
                throw new RuntimeException("Attempt to equate values at type in quotient: " + eq0.third + " at type " + J.type(eq0.third).l);
            }
            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);
    InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Sk, ID> initial = new InitialAlgebra<>(strat, J.schema(), col, new It(), Object::toString, Object::toString);
    return new LiteralInstance<>(J.schema(), col.gens.map, col.sks.map, eqs0, initial.dp(), initial, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe));
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) It(catdata.aql.It) LiteralInstance(catdata.aql.fdm.LiteralInstance) InitialAlgebra(catdata.aql.fdm.InitialAlgebra) ID(catdata.aql.It.ID) Pair(catdata.Pair) 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) Gen(catdata.aql.exp.InstExpRaw.Gen) Sk(catdata.aql.exp.InstExpRaw.Sk) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage) Chc(catdata.Chc)

Aggregations

Ctx (catdata.Ctx)7 ID (catdata.aql.It.ID)7 Pair (catdata.Pair)6 Chc (catdata.Chc)5 Collage (catdata.aql.Collage)5 It (catdata.aql.It)5 Att (catdata.aql.exp.SchExpRaw.Att)5 En (catdata.aql.exp.SchExpRaw.En)5 Fk (catdata.aql.exp.SchExpRaw.Fk)5 Sym (catdata.aql.exp.TyExpRaw.Sym)5 Ty (catdata.aql.exp.TyExpRaw.Ty)5 InitialAlgebra (catdata.aql.fdm.InitialAlgebra)5 LiteralInstance (catdata.aql.fdm.LiteralInstance)5 HashSet (java.util.HashSet)5 AqlOptions (catdata.aql.AqlOptions)4 Gen (catdata.aql.exp.InstExpRaw.Gen)4 Sk (catdata.aql.exp.InstExpRaw.Sk)3 Null (catdata.Null)2 RawTerm (catdata.aql.RawTerm)2 Term (catdata.aql.Term)2