Search in sources :

Example 1 with SaturatedInstance

use of catdata.aql.fdm.SaturatedInstance in project fql by CategoricalData.

the class InstExpJdbcAll method toInstance.

private Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> toInstance(AqlEnv env, SqlInstance inst, SqlSchema info) {
    AqlOptions ops = new AqlOptions(options, null, env.defaults);
    Schema<Ty, En, Sym, Fk, Att> sch = makeSchema(env, info, ops);
    Ctx<En, Collection<Gen>> ens0 = new Ctx<>(Util.newSetsFor0(sch.ens));
    Ctx<Ty, Collection<Null<?>>> tys0 = new Ctx<>();
    Ctx<Gen, Ctx<Fk, Gen>> fks0 = new Ctx<>();
    Ctx<Gen, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Null<?>>>> atts0 = new Ctx<>();
    Ctx<Null<?>, Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>> extraRepr = new Ctx<>();
    for (Ty ty : sch.typeSide.tys) {
        tys0.put(ty, new HashSet<>());
    }
    boolean schemaOnly = (Boolean) ops.getOrDefault(AqlOption.schema_only);
    boolean nullOnErr = (Boolean) ops.getOrDefault(AqlOption.import_null_on_err_unsafe);
    boolean dontCheckClosure = (Boolean) ops.getOrDefault(AqlOption.import_dont_check_closure_unsafe);
    if (!schemaOnly) {
        int fr = 0;
        Map<SqlTable, Map<Map<SqlColumn, Optional<Object>>, Gen>> iso1 = new HashMap<>();
        for (SqlTable table : info.tables) {
            Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
            Map<Map<SqlColumn, Optional<Object>>, Gen> i1 = new HashMap<>();
            SqlColumn thePk = null;
            if (table.pk.size() == 1) {
                thePk = Util.get0(table.pk);
            }
            for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
                Gen i = new Gen("v" + (fr++));
                /* can't do this until Gen need not be unique
					if (thePk == null) {
						i = new Gen("v" + (fr++));
					} else {
						Optional<Object> x = tuple.get(thePk);
						if (!x.isPresent()) {
							throw new RuntimeException("Primary key col is null in " + tuple);
						}
						i = new Gen(x.get().toString()); //TODO aql
					}
					*/
                i1.put(tuple, i);
                // tuple.keySet().
                // i2.put(i, tuple);
                ens0.get(new En(table.name)).add(i);
                for (SqlColumn c : table.columns) {
                    if (!atts0.containsKey(i)) {
                        atts0.put(i, new Ctx<>());
                    }
                    Optional<Object> val = tuple.get(c);
                    Term<Ty, Void, Sym, Void, Void, Void, Null<?>> xxx = InstExpJdbc.objectToSk(sch, val.orElse(null), i, new Att(new En(table.name), c.name), tys0, extraRepr, false, nullOnErr);
                    atts0.get(i).put(new Att(new En(table.name), c.name), xxx);
                }
            }
            iso1.put(table, i1);
        // iso2.put(table, i2);
        }
        for (SqlForeignKey fk : info.fks) {
            for (Map<SqlColumn, Optional<Object>> in : inst.get(fk.source)) {
                Map<SqlColumn, Optional<Object>> out = inst.follow(in, fk);
                Gen tgen = iso1.get(fk.target).get(out);
                Gen sgen = iso1.get(fk.source).get(in);
                if (!fks0.containsKey(sgen)) {
                    fks0.put(sgen, new Ctx<>());
                }
                fks0.get(sgen).put(new Fk(new En(fk.source.name), fk.toString()), tgen);
            }
        }
    }
    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, dontCheckClosure);
    return new SaturatedInstance<>(alg, alg, (Boolean) ops.getOrDefault(AqlOption.require_consistency), (Boolean) ops.getOrDefault(AqlOption.allow_java_eqs_unsafe), true, extraRepr);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Null(catdata.Null) HashMap(java.util.HashMap) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) ImportAlgebra(catdata.aql.ImportAlgebra) Optional(java.util.Optional) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Term(catdata.aql.Term) Gen(catdata.aql.exp.InstExpRaw.Gen) SqlForeignKey(catdata.sql.SqlForeignKey) Collection(java.util.Collection) AqlOptions(catdata.aql.AqlOptions) SqlTable(catdata.sql.SqlTable) SaturatedInstance(catdata.aql.fdm.SaturatedInstance) HashMap(java.util.HashMap) Map(java.util.Map) SqlColumn(catdata.sql.SqlColumn)

Example 2 with SaturatedInstance

use of catdata.aql.fdm.SaturatedInstance in project fql by CategoricalData.

the class InstExpImport method eval.

@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> eval(AqlEnv env) {
    Schema<Ty, En, Sym, Fk, Att> sch = schema.eval(env);
    for (Ty ty : sch.typeSide.tys) {
        if (!sch.typeSide.js.java_tys.containsKey(ty)) {
            throw new RuntimeException("Import is only allowed onto java types");
        }
    }
    op = new AqlOptions(options, null, env.defaults);
    import_as_theory = (boolean) op.getOrDefault(AqlOption.import_as_theory);
    isJoined = (boolean) op.getOrDefault(AqlOption.import_joined);
    idCol = (String) op.getOrDefault(AqlOption.id_column_name);
    nullOnErr = (Boolean) op.getOrDefault(AqlOption.import_null_on_err_unsafe);
    prepend_entity_on_ids = (Boolean) op.getOrDefault(AqlOption.prepend_entity_on_ids);
    import_col_seperator = (String) op.getOrDefault(AqlOption.import_col_seperator);
    prefix = (String) op.getOrDefault(AqlOption.csv_import_prefix);
    dont_check_closure = (boolean) op.getOrDefault(AqlOption.import_dont_check_closure_unsafe);
    ens0 = new Ctx<>(Util.newSetsFor0(sch.ens));
    tys0 = new Ctx<>(Util.newSetsFor0(sch.typeSide.tys));
    fks0 = new Ctx<>();
    atts0 = new Ctx<>();
    extraRepr = new Ctx<>();
    En last = null;
    try {
        Handle h = start(sch);
        if (!isJoined) {
            /*
				Map<En, Q> ens = new HashMap<>();
				Map<Ty, Q> tys = new HashMap<>();
				Map<Att, Q> atts = new HashMap<>();
				Map<Fk, Q> fks = new HashMap<>();
	
				for (String o : map.keySet()) {
					assertUnambig(o, sch);
					Q q = map.get(o);
					if (sch.typeSide.tys.contains(new Ty(o))) {
						tys.put(new Ty(o), q);
					} else if (sch.ens.contains(new En(o))) {
						ens.put(new En(o), q);
					} else if (sch.atts.map.containsKey(new Att(o))) {
						atts.put(new Att(o), q);
					} else if (sch.fks.map.containsKey(new Fk(o))) {
						fks.put(new Fk(o), q);
					} //TODO aql
				}
				totalityCheck(sch, ens, tys, atts, fks);
	
				for (En en : ens.keySet()) {
					shreddedEn(h, en, ens.get(en), sch);
				}
				for (Fk fk : fks.keySet()) {
					shreddedFk(h, fk, fks.get(fk), sch);
				}
				for (Att att : atts.keySet()) {
					shreddedAtt(h, att, atts.get(att), sch);
				}
	*/
            throw new RuntimeException("Unjoined form no longer supported.  To request, contact us.");
        } else {
            for (En en : sch.ens) {
                last = en;
                Q z = map.get(en.str);
                // TODO: aql: this check isn't needed
                // if (z == null) {
                // throw new RuntimeException("No binding given for " + en);
                // }
                joinedEn(h, en, z, sch);
            }
        }
        end(h);
    } catch (Exception exn) {
        exn.printStackTrace();
        String pre = "";
        if (last != null) {
            pre = "On entity " + last + ", ";
        }
        throw new RuntimeException(pre + exn.getMessage() + "\n\n" + getHelpStr());
    }
    if (import_as_theory) {
        return forTheory(sch, ens0, tys0, fks0, atts0, op);
    }
    ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>> alg = new ImportAlgebra<>(sch, ens0, tys0, fks0, atts0, Object::toString, Object::toString, dont_check_closure);
    return new SaturatedInstance<>(alg, alg, (Boolean) op.getOrDefault(AqlOption.require_consistency), (Boolean) op.getOrDefault(AqlOption.allow_java_eqs_unsafe), true, extraRepr);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) ImportAlgebra(catdata.aql.ImportAlgebra) Null(catdata.Null) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) En(catdata.aql.exp.SchExpRaw.En) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOptions(catdata.aql.AqlOptions) SaturatedInstance(catdata.aql.fdm.SaturatedInstance)

Example 3 with SaturatedInstance

use of catdata.aql.fdm.SaturatedInstance in project fql by CategoricalData.

the class InstExpRandom method eval.

// not exactly the smartest way
@Override
public SaturatedInstance<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>, Pair<Integer, En>, Pair<Integer, Att>> eval(AqlEnv env) {
    int seed = (Integer) new AqlOptions(options, null, env.defaults).getOrDefault(AqlOption.random_seed);
    Random rand = new Random(seed);
    Schema<Ty, En, Sym, Fk, Att> schema = sch.eval(env);
    Ctx<En, Collection<Pair<Integer, En>>> ens0 = new Ctx<>();
    Ctx<Ty, Collection<Pair<Integer, Att>>> tys = new Ctx<>();
    Ctx<Pair<Integer, En>, Ctx<Fk, Pair<Integer, En>>> fks = new Ctx<>();
    Ctx<Pair<Integer, En>, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Pair<Integer, Att>>>> atts = new Ctx<>();
    for (Ty ty : schema.typeSide.tys) {
        tys.put(ty, new LinkedList<>());
    }
    for (String en : ens.keySet()) {
        List<Pair<Integer, En>> l = new LinkedList<>();
        int size = ens.get(en);
        for (int i = 0; i < size; i++) {
            l.add(new Pair<>(i, new En(en)));
            Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Pair<Integer, Att>>> ctx = new Ctx<>();
            for (Att att : schema.attsFrom(new En(en))) {
                ctx.put(att, Term.Sk(new Pair<>(i, att)));
                tys.get(schema.atts.get(att).second).add(new Pair<>(i, att));
            }
            atts.put(new Pair<>(i, new En(en)), ctx);
            Ctx<Fk, Pair<Integer, En>> ctx0 = new Ctx<>();
            for (Fk fk : schema.fksFrom(new En(en))) {
                int size0 = ens.get(schema.fks.get(fk).second.str);
                Integer k = rand.nextInt(size0);
                ctx0.put(fk, new Pair<>(k, schema.fks.get(fk).second));
            }
            fks.put(new Pair<>(i, new En(en)), ctx0);
        }
        ens0.put(new En(en), l);
    }
    ImportAlgebra<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> alg = new ImportAlgebra<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>>(schema, ens0, tys, fks, atts, x -> x.toString(), x -> x.toString(), true);
    DP<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> dp = new DP<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>>() {

        @Override
        public String toStringProver() {
            return "Random";
        }

        @Override
        public boolean eq(Ctx<Var, Chc<Ty, En>> ctx, Term<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> lhs, Term<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> rhs) {
            if (!ctx.isEmpty()) {
                Util.anomaly();
            }
            return lhs.equals(rhs);
        }
    };
    return new SaturatedInstance<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>, Pair<Integer, En>, Pair<Integer, Att>>(alg, dp, false, true, false, new Ctx<>());
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) DP(catdata.aql.DP) Random(java.util.Random) Pair(catdata.Pair) ImportAlgebra(catdata.aql.ImportAlgebra) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Term(catdata.aql.Term) LinkedList(java.util.LinkedList) Collection(java.util.Collection) AqlOptions(catdata.aql.AqlOptions) SaturatedInstance(catdata.aql.fdm.SaturatedInstance)

Example 4 with SaturatedInstance

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

AqlOptions (catdata.aql.AqlOptions)4 ImportAlgebra (catdata.aql.ImportAlgebra)4 Att (catdata.aql.exp.SchExpRaw.Att)4 En (catdata.aql.exp.SchExpRaw.En)4 Fk (catdata.aql.exp.SchExpRaw.Fk)4 Sym (catdata.aql.exp.TyExpRaw.Sym)4 Ty (catdata.aql.exp.TyExpRaw.Ty)4 SaturatedInstance (catdata.aql.fdm.SaturatedInstance)4 Ctx (catdata.Ctx)3 Null (catdata.Null)3 Term (catdata.aql.Term)3 Gen (catdata.aql.exp.InstExpRaw.Gen)3 Collection (java.util.Collection)3 Pair (catdata.Pair)2 Chc (catdata.Chc)1 Collage (catdata.aql.Collage)1 DP (catdata.aql.DP)1 It (catdata.aql.It)1 ID (catdata.aql.It.ID)1 RawTerm (catdata.aql.RawTerm)1