use of catdata.aql.Schema in project fql by CategoricalData.
the class SchExpRaw method eval.
// TODO: aql printing of contexts broken when conitain choices
@SuppressWarnings("unused")
@Override
public synchronized Schema<Ty, En, Sym, Fk, Att> eval(AqlEnv env) {
TypeSide<Ty, Sym> ts = typeSide.eval(env);
Collage<Ty, En, Sym, Fk, Att, Void, Void> col = new Collage<>(ts.collage());
Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs0 = new HashSet<>();
for (String k : imports) {
@SuppressWarnings("unchecked") Schema<Ty, En, Sym, Fk, Att> v = env.defs.schs.get(k);
col.addAll(v.collage());
eqs0.addAll(v.eqs);
}
col.ens.addAll(ens.stream().map(x -> new En(x)).collect(Collectors.toList()));
col.fks.putAll(conv1(fks));
col.atts.putAll(conv2(atts));
for (Quad<String, String, RawTerm, RawTerm> eq : t_eqs) {
try {
Map<String, Chc<Ty, En>> ctx = Util.singMap(eq.first, eq.second == null ? null : Chc.inRight(new En(eq.second)));
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.third, eq.fourth, null, col.convert(), "", ts.js).first3();
Chc<Ty, En> v = eq0.first.get(new Var(eq.first));
if (v.left) {
throw new RuntimeException(eq.first + " has type " + v.l + " which is not an entity");
}
En t = v.r;
eqs0.add(new Triple<>(new Pair<>(new Var(eq.first), t), eq0.second.convert(), eq0.third.convert()));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(find("obs equations", eq), "In equation " + eq.third + " = " + eq.fourth + ", " + ex.getMessage());
}
}
for (Pair<List<String>, List<String>> eq : p_eqs) {
try {
String vv = "v";
Var var = new Var(vv);
Map<String, Chc<Ty, En>> ctx = Util.singMap(vv, null);
RawTerm lhs = RawTerm.fold(col.fks.keySet(), col.ens, eq.first, vv);
RawTerm rhs = RawTerm.fold(col.fks.keySet(), col.ens, eq.second, vv);
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, lhs, rhs, null, col.convert(), "", ts.js).first3();
Chc<Ty, En> v = eq0.first.get(var);
if (v.left) {
throw new RuntimeException("the equation's source " + eq.first + " is type " + v.l + " which is not an entity");
}
En t = v.r;
if (eq0.first.size() != 1) {
throw new RuntimeException("java constants cannot be used ");
}
eqs0.add(new Triple<>(new Pair<>(var, t), eq0.second.convert(), eq0.third.convert()));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(find("path equations", eq), "In equation " + Util.sep(eq.first, ".") + " = " + Util.sep(eq.second, ".") + ", " + ex.getMessage());
}
}
for (Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> eq : eqs0) {
col.eqs.add(new Eq<>(new Ctx<>(eq.first).inRight(), eq.second, eq.third));
}
AqlOptions strat = new AqlOptions(options, col, env.defaults);
AqlOptions s = new AqlOptions(Util.singMap(AqlOption.prover.toString(), ProverName.fail.toString()), col, env.defaults);
// forces type checking before prover construction
new Schema<>(ts, col.ens, col.atts.map, col.fks.map, eqs0, AqlProver.create(s, col, ts.js), false);
Schema<Ty, En, Sym, Fk, Att> ret = new Schema<>(ts, col.ens, col.atts.map, col.fks.map, eqs0, AqlProver.create(strat, col, ts.js), !((Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe)));
return ret;
}
use of catdata.aql.Schema 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);
}
}
}
use of catdata.aql.Schema 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);
}
}
use of catdata.aql.Schema in project fql by CategoricalData.
the class InstExpJdbcAll method makeSchema.
public Schema<Ty, En, Sym, Fk, Att> makeSchema(AqlEnv env, SqlSchema info, AqlOptions ops) {
boolean checkJava = !(Boolean) ops.getOrDefault(AqlOption.allow_java_eqs_unsafe);
TypeSide<Ty, Sym> typeSide = new SqlTypeSide(ops);
// typeSide.validate(true);
Collage<Ty, En, Sym, Fk, Att, Void, Void> col0 = new Collage<>(typeSide.collage());
Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs = new HashSet<>();
for (SqlTable table : info.tables) {
col0.ens.add(new En(table.name));
for (SqlColumn c : table.columns) {
if (col0.atts.containsKey(new Att(new En(table.name), c.name))) {
throw new RuntimeException("Name collision: table " + c.table.name + " col " + c.name + " against table " + col0.atts.get(new Att(new En(table.name), c.name)).first + "\n\n.Possible solution: set option jdbc_import_col_seperator so as to avoid name collisions.");
}
col0.atts.put(new Att(new En(table.name), c.name), new Pair<>(new En(table.name), new Ty(sqlTypeToAqlType(c.type.name))));
}
}
for (SqlForeignKey fk : info.fks) {
col0.fks.put(new Fk(new En(fk.source.name), fk.toString()), new Pair<>(new En(fk.source.name), new En(fk.target.name)));
Var v = new Var("x");
for (SqlColumn tcol : fk.map.keySet()) {
SqlColumn scol = fk.map.get(tcol);
Att l = new Att(new En(scol.table.name), scol.name);
Att r = new Att(new En(tcol.table.name), tcol.name);
Term<Ty, En, Sym, Fk, Att, Void, Void> lhs = Term.Att(l, Term.Var(v));
Term<Ty, En, Sym, Fk, Att, Void, Void> rhs = Term.Att(r, Term.Fk(new Fk(new En(fk.source.name), fk.toString()), Term.Var(v)));
eqs.add(new Triple<>(new Pair<>(v, new En(fk.source.name)), lhs, rhs));
col0.eqs.add(new Eq<>(new Ctx<>(new Pair<>(v, Chc.inRight(new En(fk.source.name)))), lhs, rhs));
}
}
DP<Ty, En, Sym, Fk, Att, Void, Void> dp = AqlProver.create(new AqlOptions(options, col0, env.defaults), col0, typeSide.js);
Schema<Ty, En, Sym, Fk, Att> sch = new Schema<>(typeSide, col0.ens, col0.atts.map, col0.fks.map, eqs, dp, checkJava);
return sch;
}
use of catdata.aql.Schema in project fql by CategoricalData.
the class AqlViewer method viewSchema.
private <Ty, En, Sym, Fk, Att> JComponent viewSchema(Schema<Ty, En, Sym, Fk, Att> schema) {
Graph<Chc<Ty, En>, Chc<Fk, Att>> sgv = new DirectedSparseMultigraph<>();
int i = 0;
boolean triggered = false;
for (En en : schema.ens) {
sgv.addVertex(Chc.inRight(en));
i++;
if (i >= maxrows) {
triggered = true;
break;
}
}
// if (i <= maxrows) {
i = 0;
for (Ty ty : schema.typeSide.tys) {
sgv.addVertex(Chc.inLeft(ty));
i++;
if (i >= maxrows * maxrows) {
triggered = true;
break;
}
}
for (Att att : schema.atts.keySet()) {
sgv.addEdge(Chc.inRight(att), Chc.inRight(schema.atts.get(att).first), Chc.inLeft(schema.atts.get(att).second));
i++;
if (i >= maxrows * maxrows) {
triggered = true;
break;
}
}
for (Fk fk : schema.fks.keySet()) {
sgv.addEdge(Chc.inLeft(fk), Chc.inRight(schema.fks.get(fk).first), Chc.inRight(schema.fks.get(fk).second));
i++;
if (i >= maxrows * maxrows) {
triggered = true;
break;
}
}
if (sgv.getVertexCount() == 0) {
return new JPanel();
}
// Layout<Chc<Ty, En>, Chc<Fk, Att>> layout = new KKLayout<>(sgv);
Layout<Chc<Ty, En>, Chc<Fk, Att>> layout = new FRLayout<>(sgv);
layout.setSize(new Dimension(600, 400));
VisualizationViewer<Chc<Ty, En>, Chc<Fk, Att>> vv = new VisualizationViewer<>(layout);
Function<Chc<Ty, En>, Paint> vertexPaint = x -> x.left ? Color.gray : Color.black;
DefaultModalGraphMouse<Chc<Ty, En>, Chc<Fk, Att>> gm = new DefaultModalGraphMouse<>();
gm.setMode(Mode.TRANSFORMING);
vv.setGraphMouse(gm);
gm.setMode(Mode.PICKING);
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
Function<Chc<Fk, Att>, String> et = Chc::toStringMash;
Function<Chc<Ty, En>, String> vt = Chc::toStringMash;
vv.getRenderContext().setEdgeLabelTransformer(et);
vv.getRenderContext().setVertexLabelTransformer(vt);
GraphZoomScrollPane zzz = new GraphZoomScrollPane(vv);
JPanel ret = new JPanel(new GridLayout(1, 1));
ret.add(zzz);
ret.setBorder(BorderFactory.createEtchedBorder());
vv.getRenderContext().setLabelOffset(16);
// vv.getRenderContext().set
vv.setBackground(Color.white);
if (triggered) {
ret.setBorder(BorderFactory.createTitledBorder("Partial"));
}
return ret;
}
Aggregations