Search in sources :

Example 1 with Null

use of catdata.Null in project fql by CategoricalData.

the class InstExpCsv method joinedEn.

@Override
protected void joinedEn(Map<En, List<String[]>> rows, En en0, Pair<List<Pair<String, String>>, List<Pair<String, String>>> s, Schema<Ty, En, Sym, Fk, Att> sch) throws Exception {
    String en = en0.str;
    Map<String, String> inner;
    if (s == null) {
        inner = new HashMap<>();
    } else {
        inner = Util.toMapSafely(s.second);
    }
    boolean autoGenIds = (Boolean) op.getOrDefault(inner, AqlOption.csv_generate_ids);
    for (En en2 : rows.keySet()) {
        if (rows.get(en2).size() == 0) {
            throw new RuntimeException("No header in CSV file for " + en2);
        }
    }
    // index of each column name
    Ctx<String, Integer> m = new Ctx<>();
    for (int i = 0; i < rows.get(en0).get(0).length; i++) {
        m.put(rows.get(en0).get(0)[i], i);
    }
    boolean prepend = (boolean) op.getOrDefault(inner, AqlOption.csv_prepend_entity);
    String sep = (String) op.getOrDefault(inner, AqlOption.import_col_seperator);
    String pre = (String) op.getOrDefault(inner, AqlOption.csv_import_prefix);
    // System.out.println("prefix is " + pre);
    Map<String, String> map;
    if (s != null) {
        map = new Ctx<>(Util.toMapSafely(s.first)).map;
    } else {
        map = new HashMap<>();
    }
    Function<String, String> mediate = x -> {
        if (map.containsKey(x)) {
            return map.get(x);
        }
        String z = map.containsKey(x) ? map.get(x) : x;
        if (prepend) {
            int i = x.indexOf(en + sep);
            if (i != 0) {
                return pre + z;
            }
            String temp = x.substring((en + sep).length());
            return pre + temp;
        }
        return pre + z;
    };
    int startId = 0;
    for (String[] row : rows.get(en0).subList(1, rows.get(en0).size())) {
        Gen l0;
        String idCol = map.containsKey(en) ? map.get(en) : (String) op.getOrDefault(inner, AqlOption.id_column_name);
        if (autoGenIds && !m.containsKey(idCol)) {
            l0 = toGen(en0, "" + startId++);
        } else if (!autoGenIds && !m.containsKey(idCol)) {
            throw new RuntimeException("On " + en + ", ID column " + idCol + " not found in headers " + m.keySet() + ". \n\nPossible solution: provide a mapping.\n\nPossible solution: set csv_generate_ids=true to auto-generate IDs.\n\nPossible solution: rename the headers in the CSV file.\n\nPossible solution: add an ID column to the CSV file.");
        } else {
            l0 = toGen(en0, row[m.get(idCol)]);
        }
        ens0.get(en0).add(l0);
        for (Fk fk : sch.fksFrom(en0)) {
            if (!fks0.containsKey(l0)) {
                fks0.put(l0, new Ctx<>());
            }
            Gen g = toGen(sch.fks.get(fk).second, row[m.get(mediate.apply(fk.str))]);
            fks0.get(l0).put(fk, g);
        }
        for (Att att : sch.attsFrom(en0)) {
            if (!atts0.containsKey(l0)) {
                atts0.put(l0, new Ctx<>());
            }
            String zz = mediate.apply(att.str);
            if (!m.containsKey(zz)) {
                throw new RuntimeException("No column " + att + " in file for " + en + " nor explicit mapping for " + att + " given. Tried " + zz + " and options are " + m.keySet());
            }
            int z = m.get(zz);
            if (z >= row.length) {
                throw new RuntimeException("Cannot get index " + z + " from " + Arrays.toString(row));
            }
            String o = row[z];
            Term<Ty, Void, Sym, Void, Void, Void, Null<?>> r = objectToSk(sch, o, l0, att, tys0, extraRepr, true, nullOnErr);
            atts0.get(l0).put(att, r);
        }
    }
}
Also used : AqlOptions(catdata.aql.AqlOptions) Ctx(catdata.Ctx) Arrays(java.util.Arrays) En(catdata.aql.exp.SchExpRaw.En) CSVReader(com.opencsv.CSVReader) Sym(catdata.aql.exp.TyExpRaw.Sym) HashMap(java.util.HashMap) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOption(catdata.aql.AqlOptions.AqlOption) Function(java.util.function.Function) CSVParser(com.opencsv.CSVParser) Map(java.util.Map) HashCodeBuilder(org.apache.commons.lang3.builder.HashCodeBuilder) Term(catdata.aql.Term) LinkedList(java.util.LinkedList) Pair(catdata.Pair) EqualsBuilder(org.apache.commons.lang3.builder.EqualsBuilder) Null(catdata.Null) Fk(catdata.aql.exp.SchExpRaw.Fk) Util(catdata.Util) CSVParserBuilder(com.opencsv.CSVParserBuilder) Field(java.lang.reflect.Field) Att(catdata.aql.exp.SchExpRaw.Att) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Schema(catdata.aql.Schema) FileReader(java.io.FileReader) CSVReaderBuilder(com.opencsv.CSVReaderBuilder) CSVReaderNullFieldIndicator(com.opencsv.enums.CSVReaderNullFieldIndicator) Ty(catdata.aql.exp.TyExpRaw.Ty) Att(catdata.aql.exp.SchExpRaw.Att) Null(catdata.Null) Fk(catdata.aql.exp.SchExpRaw.Fk) Ty(catdata.aql.exp.TyExpRaw.Ty) Ctx(catdata.Ctx) Sym(catdata.aql.exp.TyExpRaw.Sym) En(catdata.aql.exp.SchExpRaw.En) Gen(catdata.aql.exp.InstExpRaw.Gen)

Example 2 with Null

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

use of catdata.Null in project fql by CategoricalData.

the class InstExpImport method objectToSk.

public static Term<Ty, Void, Sym, Void, Void, Void, Null<?>> objectToSk(Schema<Ty, En, Sym, Fk, Att> sch, Object rhs, Gen x, Att att, Ctx<Ty, Collection<Null<?>>> sks, Ctx<Null<?>, Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>> extraRepr, boolean shouldJS, boolean errMeansNull) {
    Ty ty = sch.atts.get(att).second;
    if (rhs == null) {
        Null<?> n = new Null<>(Term.Att(att, Term.Gen(x)));
        extraRepr.put(n, Term.Att(att, Term.Gen(x)));
        sks.get(ty).add(n);
        return Term.Sk(n);
    } else if (sch.typeSide.js.java_tys.containsKey(ty)) {
        if (shouldJS) {
            try {
                return Term.Obj(sch.typeSide.js.parse(ty, (String) rhs), ty);
            } catch (Exception ex) {
                if (errMeansNull) {
                    return objectToSk(sch, null, x, att, sks, extraRepr, shouldJS, errMeansNull);
                } else {
                    ex.printStackTrace();
                    throw new RuntimeException("Error while importing " + rhs + " of class " + rhs.getClass() + ".  Consider option import_null_on_err_unsafe.  Error was " + ex.getMessage());
                }
            }
        }
        try {
            if (!Class.forName(sch.typeSide.js.java_tys.get(ty)).isInstance(rhs)) {
                if (errMeansNull) {
                    return objectToSk(sch, null, x, att, sks, extraRepr, shouldJS, errMeansNull);
                } else {
                    throw new RuntimeException("On " + x + "." + att + ", error while importing " + rhs + " of " + rhs.getClass() + " was expecting " + sch.typeSide.js.java_tys.get(ty) + ".\n\nConsider option " + AqlOption.import_null_on_err_unsafe);
                }
            }
        } catch (ClassNotFoundException ex) {
            Util.anomaly();
        }
        return Term.Obj(rhs, ty);
    }
    return Util.anomaly();
}
Also used : Null(catdata.Null) Ty(catdata.aql.exp.TyExpRaw.Ty)

Example 4 with Null

use of catdata.Null 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 5 with Null

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

Aggregations

Null (catdata.Null)6 Ty (catdata.aql.exp.TyExpRaw.Ty)6 Gen (catdata.aql.exp.InstExpRaw.Gen)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 Ctx (catdata.Ctx)4 AqlOptions (catdata.aql.AqlOptions)4 SaturatedInstance (catdata.aql.fdm.SaturatedInstance)4 Pair (catdata.Pair)3 ImportAlgebra (catdata.aql.ImportAlgebra)3 Term (catdata.aql.Term)3 Chc (catdata.Chc)2 Collage (catdata.aql.Collage)2 It (catdata.aql.It)2 ID (catdata.aql.It.ID)2 InitialAlgebra (catdata.aql.fdm.InitialAlgebra)2 LiteralInstance (catdata.aql.fdm.LiteralInstance)2 Collection (java.util.Collection)2