use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.
the class InstExpCsv method start2.
/*
* @Override public boolean equals(Object obj) { return (obj instanceof
* InstExpCsv) && super.equals(obj); }
*/
/**
* Expects filenames in the map
*/
public static Map<En, List<String[]>> start2(Map<String, String> map, AqlOptions op, Schema<Ty, En, Sym, Fk, Att> sch, boolean omitCheck) throws Exception {
Character sepChar = (Character) op.getOrDefault(AqlOption.csv_field_delim_char);
Character quoteChar = (Character) op.getOrDefault(AqlOption.csv_quote_char);
Character escapeChar = (Character) op.getOrDefault(AqlOption.csv_escape_char);
final CSVParser parser = new CSVParserBuilder().withSeparator(sepChar).withQuoteChar(quoteChar).withEscapeChar(escapeChar).withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS).build();
Map<En, List<String[]>> ret = new HashMap<>();
for (String k : map.keySet()) {
if (!omitCheck) {
if (!sch.ens.contains(new En(k))) {
throw new RuntimeException("Not an entity: " + k);
}
}
File file = new File(map.get(k));
FileReader fileReader = new FileReader(file);
final CSVReader reader = new CSVReaderBuilder(fileReader).withCSVParser(parser).withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS).build();
List<String[]> rows = reader.readAll();
fileReader.close();
ret.put(new En(k), rows);
}
if (!omitCheck) {
for (En en : sch.ens) {
if (!ret.containsKey(en)) {
ret.put(en, new LinkedList<>(Util.singList(Util.union(sch.attsFrom(en).stream().map(Object::toString).collect(Collectors.toList()), sch.fksFrom(en).stream().map(Object::toString).collect(Collectors.toList())).toArray(new String[0]))));
}
}
}
return ret;
}
use of catdata.aql.exp.SchExpRaw.En 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);
}
use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.
the class InstExpJdbc method joinedEn.
@Override
protected void joinedEn(Connection conn, En en, String s, Schema<Ty, En, Sym, Fk, Att> sch) throws Exception {
if (s == null) {
if (!(boolean) op.getOrDefault(AqlOption.import_missing_is_empty)) {
throw new RuntimeException("Missing query for entity: " + en + ". \n\nPossible options to consider: " + AqlOption.import_missing_is_empty);
} else {
return;
}
}
Statement stmt = conn.createStatement();
stmt.execute(s);
ResultSet rs = stmt.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
checkColumns(en, s, sch, rsmd);
while (rs.next()) {
Object gen = rs.getObject(idCol);
if (gen == null) {
stmt.close();
rs.close();
conn.close();
throw new RuntimeException("Encountered a NULL generator in ID column " + idCol);
}
Gen g1 = toGen(en, gen.toString());
// store strings
ens0.get(en).add(g1);
for (Fk fk : sch.fksFrom(en)) {
Object rhs = rs.getObject(fk.str);
if (rhs == null) {
stmt.close();
rs.close();
conn.close();
throw new RuntimeException("ID " + gen + " has a NULL foreign key value on " + fk);
}
En en2 = sch.fks.get(fk).second;
// store strings
Gen g2 = toGen(en2, rhs.toString());
if (!fks0.containsKey(g1)) {
fks0.put(g1, new Ctx<>());
}
fks0.get(g1).put(fk, g2);
}
for (Att att : sch.attsFrom(en)) {
Object rhs = rs.getObject(att.str);
if (!atts0.map.containsKey(g1)) {
atts0.put(g1, new Ctx<>());
}
atts0.get(g1).put(att, objectToSk(sch, rhs, g1, att, tys0, extraRepr, false, nullOnErr));
}
}
}
use of catdata.aql.exp.SchExpRaw.En 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));
}
use of catdata.aql.exp.SchExpRaw.En 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));
}
Aggregations