Search in sources :

Example 6 with Att

use of catdata.aql.exp.SchExpRaw.Att in project fql by CategoricalData.

the class EdsExpRaw method eval.

@Override
public Constraints<Ty, En, Sym, Fk, Att> eval(AqlEnv env) {
    Schema<Ty, En, Sym, Fk, Att> sch = schema.eval(env);
    Collection<ED<Ty, En, Sym, Fk, Att>> l = new LinkedList<>();
    for (String k : imports) {
        @SuppressWarnings("unchecked") Constraints<Ty, En, Sym, Fk, Att> v = env.defs.eds.get(k);
        l.addAll(v.eds);
    }
    for (EdExpRaw e : eds) {
        l.add(e.eval(sch, new AqlOptions(options, null, env.defaults)));
    }
    return new Constraints<>(sch, l, new AqlOptions(options, null, env.defaults));
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) En(catdata.aql.exp.SchExpRaw.En) LinkedList(java.util.LinkedList) Constraints(catdata.aql.Constraints) AqlOptions(catdata.aql.AqlOptions) ED(catdata.aql.ED)

Example 7 with Att

use of catdata.aql.exp.SchExpRaw.Att 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 8 with Att

use of catdata.aql.exp.SchExpRaw.Att 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 9 with Att

use of catdata.aql.exp.SchExpRaw.Att 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 10 with Att

use of catdata.aql.exp.SchExpRaw.Att in project fql by CategoricalData.

the class InstExpJdbc method checkColumns.

private void checkColumns(En en, String s, Schema<Ty, En, Sym, Fk, Att> sch, ResultSetMetaData rsmd) throws SQLException {
    Set<String> colNames = new HashSet<>();
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
        String colName = rsmd.getColumnLabel(i);
        if (!(colName.equalsIgnoreCase(idCol) || Util.containsUpToCase(sch.attsFrom(en).stream().map(x -> x.str).collect(Collectors.toList()), colName) || Util.containsUpToCase(sch.fksFrom(en).stream().map(x -> x.str).collect(Collectors.toList()), colName))) {
            throw new RuntimeException("Column name " + colName + " does not refer to a foreign key or attribute in \n\n" + s);
        }
        colNames.add(colName);
    }
    for (Att att : sch.attsFrom(en)) {
        if (!Util.containsUpToCase(colNames, att.str)) {
            throw new RuntimeException("Attribute " + att + " has no column in \n\n" + s);
        }
    }
    for (Fk fk : sch.fksFrom(en)) {
        if (!Util.containsUpToCase(colNames, fk.str)) {
            throw new RuntimeException("Foreign key " + fk + " has no column in \n\n" + s);
        }
    }
    if (!Util.containsUpToCase(colNames, idCol)) {
        throw new RuntimeException("No ID column " + idCol + " in \n\n" + s);
    }
}
Also used : Ctx(catdata.Ctx) Fk(catdata.aql.exp.SchExpRaw.Fk) Connection(java.sql.Connection) En(catdata.aql.exp.SchExpRaw.En) Sym(catdata.aql.exp.TyExpRaw.Sym) Util(catdata.Util) Set(java.util.Set) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOption(catdata.aql.AqlOptions.AqlOption) Att(catdata.aql.exp.SchExpRaw.Att) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) List(java.util.List) Schema(catdata.aql.Schema) ResultSet(java.sql.ResultSet) Statement(java.sql.Statement) HashCodeBuilder(org.apache.commons.lang3.builder.HashCodeBuilder) DriverManager(java.sql.DriverManager) ResultSetMetaData(java.sql.ResultSetMetaData) Pair(catdata.Pair) Ty(catdata.aql.exp.TyExpRaw.Ty) EqualsBuilder(org.apache.commons.lang3.builder.EqualsBuilder) Att(catdata.aql.exp.SchExpRaw.Att) Fk(catdata.aql.exp.SchExpRaw.Fk) HashSet(java.util.HashSet)

Aggregations

Att (catdata.aql.exp.SchExpRaw.Att)36 En (catdata.aql.exp.SchExpRaw.En)36 Fk (catdata.aql.exp.SchExpRaw.Fk)36 Sym (catdata.aql.exp.TyExpRaw.Sym)34 Ty (catdata.aql.exp.TyExpRaw.Ty)33 Ctx (catdata.Ctx)26 Pair (catdata.Pair)25 Gen (catdata.aql.exp.InstExpRaw.Gen)22 HashSet (java.util.HashSet)20 Chc (catdata.Chc)17 AqlOptions (catdata.aql.AqlOptions)17 Triple (catdata.Triple)16 Sk (catdata.aql.exp.InstExpRaw.Sk)16 List (java.util.List)15 Collage (catdata.aql.Collage)13 Term (catdata.aql.Term)13 HashMap (java.util.HashMap)13 LinkedList (java.util.LinkedList)12 RawTerm (catdata.aql.RawTerm)11 Set (java.util.Set)10