Search in sources :

Example 1 with It

use of catdata.aql.It 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 It

use of catdata.aql.It 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 It

use of catdata.aql.It 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 4 with It

use of catdata.aql.It 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)

Example 5 with It

use of catdata.aql.It 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));
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Null(catdata.Null) Ctx(catdata.Ctx) ID(catdata.aql.It.ID) HashSet(java.util.HashSet) ImportAlgebra(catdata.aql.ImportAlgebra) Ty(catdata.aql.exp.TyExpRaw.Ty) Sym(catdata.aql.exp.TyExpRaw.Sym) RawTerm(catdata.aql.RawTerm) Term(catdata.aql.Term) Gen(catdata.aql.exp.InstExpRaw.Gen) Collection(java.util.Collection) SaturatedInstance(catdata.aql.fdm.SaturatedInstance) Collage(catdata.aql.Collage) HashSet(java.util.HashSet) Set(java.util.Set) En(catdata.aql.exp.SchExpRaw.En) It(catdata.aql.It) LiteralInstance(catdata.aql.fdm.LiteralInstance) InitialAlgebra(catdata.aql.fdm.InitialAlgebra) Pair(catdata.Pair) Fk(catdata.aql.exp.SchExpRaw.Fk) RawTerm(catdata.aql.RawTerm) Sk(catdata.aql.exp.InstExpRaw.Sk) AqlOptions(catdata.aql.AqlOptions) Chc(catdata.Chc)

Aggregations

Ctx (catdata.Ctx)5 Pair (catdata.Pair)5 Collage (catdata.aql.Collage)5 It (catdata.aql.It)5 ID (catdata.aql.It.ID)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 Chc (catdata.Chc)3 Sk (catdata.aql.exp.InstExpRaw.Sk)3 Null (catdata.Null)2 RawTerm (catdata.aql.RawTerm)2 Term (catdata.aql.Term)2