use of catdata.fql.decl.InstExp.Const in project fql by CategoricalData.
the class Relationalizer method terminal.
public static Triple<Const, Map<Node, Map<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>>, Map<Node, Map<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Object>>> terminal(FQLProgram prog, SigExp.Const sig0) {
Triple<Const, Map<Node, Map<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>>, Map<Node, Map<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Object>>> jjj = cache.get(new Pair<>(prog, sig0));
if (jjj != null) {
// so do not have to recompute when doing omega operations
return jjj;
}
try {
Signature sig = sig0.toSig(prog);
Pair<FinCat<Node, Path>, Fn<Path, Arr<Node, Path>>> start = sig.toCategory2();
// FinCat<Node, Path> cat = start.first;
Fn<Path, Arr<Node, Path>> map = start.second;
Map<Node, List<Pair<Arr<Node, Path>, Attribute<Node>>>> obs = sig.obs();
Map<Node, List<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>> m = sig.obsbar();
List<Pair<String, List<Pair<Object, Object>>>> nodes = new LinkedList<>();
List<Pair<String, List<Pair<Object, Object>>>> attrs = new LinkedList<>();
List<Pair<String, List<Pair<Object, Object>>>> arrows = new LinkedList<>();
// Map<String, Set<Pair<Object, Object>>> data = new HashMap<>();
Map<Node, Map<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>> m1 = new HashMap<>();
Map<Node, Map<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Object>> m2 = new HashMap<>();
int i = 0;
for (Node n : sig.nodes) {
Map<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>> map1 = new HashMap<>();
Map<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Object> map2 = new HashMap<>();
List<Pair<Object, Object>> set = new LinkedList<>();
m1.put(n, map1);
m2.put(n, map2);
for (LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> i2 : m.get(n)) {
Object o = Integer.toString(++i);
map1.put(o, i2);
map2.put(i2, o);
set.add(new Pair<>(o, o));
}
nodes.add(new Pair<>(n.string, set));
}
for (Attribute<Node> a : sig.attrs) {
List<Pair<Object, Object>> set = new LinkedList<>();
for (Pair<Object, Object> k : PropPSM.lookup(nodes, a.source.string)) {
LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> new_id = m1.get(a.source).get(k.first);
set.add(new Pair<>(k.first, new_id.get(new Pair<>(map.of(new Path(sig, a.source)), a))));
}
attrs.add(new Pair<>(a.name, set));
}
for (Edge a : sig.edges) {
List<Pair<Object, Object>> set = new LinkedList<>();
for (Pair<Object, Object> k : PropPSM.lookup(nodes, a.source.string)) {
LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> new_id = m1.get(a.source).get(k.first);
LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> new_id0 = PropPSM.truncate2(sig, new_id, new Arr<>(new Path(sig, a), a.source, a.target), obs.get(a.target));
// LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> new_id0 = PropPSM.truncate(sig, new_id, a, m.get(a.target));
Object o = m2.get(a.target).get(new_id0);
set.add(new Pair<>(k.first, o));
}
arrows.add(new Pair<>(a.name, set));
}
// Instance ret0 = new Instance(sig, data);
Const retX = new Const(nodes, attrs, arrows, sig.toConst());
Triple<Const, Map<Node, Map<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>>, Map<Node, Map<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Object>>> ret = new Triple<>(retX, m1, m2);
cache.put(new Pair<>(prog, sig0), ret);
return ret;
} catch (FQLException fe) {
throw new RuntimeException(fe.getLocalizedMessage());
}
}
use of catdata.fql.decl.InstExp.Const in project fql by CategoricalData.
the class InstanceEditor method show.
@SuppressWarnings({ "unchecked", "rawtypes" })
public Const show(Color c) {
String title = "Visual Editor for Instance " + name;
prejoin();
JComponent message = makePanel(c);
int i = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null);
if (i != JOptionPane.OK_OPTION) {
return null;
}
List<Pair<String, List<Pair<Object, Object>>>> nodes = new LinkedList<>();
Map<String, List<Pair<Object, Object>>> map = new HashMap<>();
for (Node n : thesig.nodes) {
JTable t = joined.get(n.string);
DefaultTableModel m = (DefaultTableModel) t.getModel();
int cols = m.getColumnCount();
Vector<Vector> rows = m.getDataVector();
List<Pair<Object, Object>> ids = new LinkedList<>();
for (Vector row : rows) {
Object id = row.get(0);
ids.add(new Pair<>(id, id));
for (int col = 1; col < cols; col++) {
String colname = m.getColumnName(col);
Object val = row.get(col);
if (!map.containsKey(colname)) {
map.put(colname, new LinkedList<>());
}
List<Pair<Object, Object>> attr = map.get(colname);
attr.add(new Pair<>(id, val));
}
}
nodes.add(new Pair<>(n.string, ids));
}
List<Pair<String, List<Pair<Object, Object>>>> attrs = new LinkedList<>();
List<Pair<String, List<Pair<Object, Object>>>> arrows = new LinkedList<>();
for (Edge e : thesig.edges) {
List<Pair<Object, Object>> g = map.get(e.name);
arrows.add(new Pair<>(e.name, g));
}
for (Attribute<Node> e : thesig.attrs) {
List<Pair<Object, Object>> g = map.get(e.name);
attrs.add(new Pair<>(e.name, g));
}
return new Const(nodes, attrs, arrows, inst.sig);
}
use of catdata.fql.decl.InstExp.Const in project fql by CategoricalData.
the class SqlToFql method transSQLSchema.
private static String transSQLSchema(List<EExternal> in, int depth) {
List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
List<Triple<String, String, String>> arrows = new LinkedList<>();
List<Triple<String, String, String>> attrs = new LinkedList<>();
List<String> nodes = new LinkedList<>();
List<Pair<String, List<Pair<Object, Object>>>> inodes = new LinkedList<>();
List<Pair<String, List<Pair<Object, Object>>>> iattrs = new LinkedList<>();
List<Pair<String, List<Pair<Object, Object>>>> iarrows = new LinkedList<>();
Set<String> seen = new HashSet<>();
Map<String, List<String>> cols = new HashMap<>();
for (EExternal k0 : in) {
if (k0 instanceof ECreateTable) {
ECreateTable k = (ECreateTable) k0;
if (seen.contains(k.name)) {
throw new RuntimeException("Duplicate name: " + k.name);
}
seen.add(k.name);
nodes.add(k.name);
inodes.add(new Pair<>(k.name, new LinkedList<>()));
boolean found = false;
List<String> lcols = new LinkedList<>();
for (Pair<String, String> col : k.types) {
lcols.add(col.first);
if (col.first.equals("id")) {
found = true;
continue;
}
String ref = lookup(col.first, k.fks);
if (ref == null) {
String col_t = col.second.equals("int") ? "int" : "string";
attrs.add(new Triple<>(k.name + "_" + col.first, k.name, col_t));
iattrs.add(new Pair<>(k.name + "_" + col.first, new LinkedList<>()));
} else {
if (!nodes.contains(ref)) {
throw new RuntimeException("Missing table " + ref + " in " + k + " (or cyclic schema with loop length > 1)");
}
arrows.add(new Triple<>(k.name + "_" + col.first, k.name, ref));
iarrows.add(new Pair<>(k.name + "_" + col.first, new LinkedList<>()));
if (ref.equals(k.name)) {
List<String> lhs = deep(depth - 1, k.name + "_" + col.first);
lhs.add(0, k.name);
List<String> rhs = deep(depth, k.name + "_" + col.first);
rhs.add(0, k.name);
eqs.add(new Pair<>(lhs, rhs));
}
}
}
if (!found) {
throw new RuntimeException("No id column in " + k);
}
for (Pair<String, String> fk : k.fks) {
if (fk.first.equals("id")) {
throw new RuntimeException("Primary keys cannot be foreign keys.");
}
if (lookup(fk.first, k.types) == null) {
throw new RuntimeException("Missing column " + fk.first + " in " + fk);
}
}
cols.put(k.name, lcols);
}
// add inst_ prefix below
if (k0 instanceof EInsertValues) {
EInsertValues k = (EInsertValues) k0;
List<String> lcols = cols.get(k.target);
for (List<String> tuple : k.values) {
if (lcols.size() != tuple.size()) {
throw new RuntimeException("Column size mismatch " + tuple + " in " + k.target);
}
List<Pair<Object, Object>> node = lookup2(k.target, inodes);
if (node == null) {
throw new RuntimeException("Missing table " + k.target);
}
node.add(new Pair<>(tuple.get(0), tuple.get(0)));
for (int colNum = 1; colNum < tuple.size(); colNum++) {
List<Pair<Object, Object>> xxx = lookup2(k.target + "_" + lcols.get(colNum), iattrs);
if (xxx == null) {
xxx = lookup2(k.target + "_" + lcols.get(colNum), iarrows);
}
if (xxx == null) {
throw new RuntimeException("Anomaly: please report");
}
xxx.add(new Pair<>(tuple.get(0), maybeQuote(tuple.get(colNum))));
}
}
}
}
SigExp.Const exp = new SigExp.Const(nodes, attrs, arrows, eqs);
Const inst = new Const(inodes, iattrs, iarrows, new Var("S"));
return "schema S = " + exp + "\n\ninstance I = " + inst + " : S";
}
use of catdata.fql.decl.InstExp.Const in project fql by CategoricalData.
the class FqlDisplay method handleInstanceFlowEdge.
private void handleInstanceFlowEdge(Object o) {
InstExp i = (InstExp) o;
Object f = i.accept(new Unit(), new InstExpVisitor<Object, Unit>() {
@Override
public MapExp visit(Unit env, Zero e) {
return null;
}
@Override
public MapExp visit(Unit env, One e) {
return null;
}
@Override
public MapExp visit(Unit env, Two e) {
throw new RuntimeException();
}
@Override
public MapExp visit(Unit env, Plus e) {
return null;
}
@Override
public MapExp visit(Unit env, Times e) {
return null;
}
@Override
public MapExp visit(Unit env, Exp e) {
throw new RuntimeException();
}
@Override
public MapExp visit(Unit env, Const e) {
return null;
}
@Override
public MapExp visit(Unit env, Delta e) {
return e.F;
}
@Override
public MapExp visit(Unit env, Sigma e) {
return e.F;
}
@Override
public MapExp visit(Unit env, Pi e) {
return e.F;
}
@Override
public MapExp visit(Unit env, FullSigma e) {
return e.F;
}
@Override
public Unit visit(Unit env, Relationalize e) {
return null;
}
@Override
public Unit visit(Unit env, External e) {
return null;
}
@Override
public Object visit(Unit env, Eval e) {
return e.q;
}
@Override
public Object visit(Unit env, FullEval e) {
return e.q;
}
@Override
public Object visit(Unit env, Kernel e) {
return null;
}
@Override
public Object visit(Unit env, Step e) {
// (Step) this should return a pair
return null;
}
});
if (f == null) {
return;
}
if (f instanceof QueryExp) {
QueryExp q = (QueryExp) f;
if (q instanceof QueryExp.Var) {
QueryExp.Var qq = (QueryExp.Var) q;
yyy.setSelectedValue(indices.get(qq.v), true);
return;
}
String k = FQLProgram.revLookup(prog.queries, q);
if (k != null) {
yyy.setSelectedValue(indices.get(k), true);
return;
}
String str = q.toString();
if (!extraInsts.contains(str)) {
Query view = q.toQuery(prog);
JPanel p = showQuery(prog, env, view);
x.add(p, str);
extraInsts.add(str);
}
yyy.clearSelection();
cl.show(x, str);
} else if (f instanceof FullQueryExp) {
FullQueryExp q = (FullQueryExp) f;
if (q instanceof FullQueryExp.Var) {
FullQueryExp.Var qq = (FullQueryExp.Var) q;
yyy.setSelectedValue(indices.get(qq.v), true);
return;
}
String k = FQLProgram.revLookup(prog.full_queries, q);
if (k != null) {
yyy.setSelectedValue(indices.get(k), true);
return;
}
String str = q.toString();
if (!extraInsts.contains(str)) {
FullQuery view = q.toFullQuery(prog);
JPanel p = showFullQuery(prog, env, view, q);
x.add(p, str);
extraInsts.add(str);
}
yyy.clearSelection();
cl.show(x, str);
} else if (f instanceof MapExp) {
MapExp q = (MapExp) f;
if (q instanceof MapExp.Var) {
MapExp.Var qq = (MapExp.Var) q;
yyy.setSelectedValue(indices.get(qq.v), true);
return;
}
String k = FQLProgram.revLookup(prog.maps, q);
if (k != null) {
yyy.setSelectedValue(indices.get(k), true);
return;
}
String str = q.toString();
if (!extraInsts.contains(str)) {
Mapping view = q.toMap(prog);
JPanel p = showMapping(env, prog.smap(view.source.toConst()), prog.smap(view.target.toConst()), view);
x.add(p, str);
extraInsts.add(str);
}
yyy.clearSelection();
cl.show(x, str);
} else {
throw new RuntimeException();
}
}
use of catdata.fql.decl.InstExp.Const in project fql by CategoricalData.
the class InstOps method visit.
@Override
public List<PSM> visit(String env, Bool e) {
List<PSM> ret = new LinkedList<>();
// .toSig(prog);
SigExp.Const sigX = prog.insts.get(e.unit).type(prog).toConst(prog);
Signature sig = sigX.toSig(prog);
Triple<Const, Map<Node, Map<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>>, Map<Node, Map<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Object>>> kkk = Relationalizer.terminal(prog, sigX);
ret.add(new PSMBool(e.bool, e.unit, e.prop, sig, env, kkk.first, kkk.second, kkk.third));
return ret;
}
Aggregations