use of catdata.fql.decl.SigExp.Var in project fql by CategoricalData.
the class FqlDisplay method doView2.
@SuppressWarnings("unchecked")
private JComponent doView2(Graph<String, Object> sgv) {
try {
Class<?> c = Class.forName(FqlOptions.layout_prefix + DefunctGlobalOptions.debug.fql.schFlow_graph);
Constructor<?> x = c.getConstructor(Graph.class);
Layout<String, Object> layout = (Layout<String, Object>) x.newInstance(sgv);
layout.setSize(new Dimension(600, 540));
VisualizationViewer<String, Object> vv = new VisualizationViewer<>(layout);
Function<String, Paint> vertexPaint = (String i) -> prog.smap(new Var(i));
DefaultModalGraphMouse<String, String> gm = new DefaultModalGraphMouse<>();
gm.setMode(Mode.TRANSFORMING);
vv.setGraphMouse(gm);
gm.setMode(Mode.PICKING);
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getPickedVertexState().addItemListener((ItemEvent e) -> {
if (e.getStateChange() != ItemEvent.SELECTED) {
return;
}
vv.getPickedEdgeState().clear();
String str = ((String) e.getItem());
yyy.setSelectedValue(indices.get(str), true);
});
vv.getPickedEdgeState().addItemListener((ItemEvent e) -> {
if (e.getStateChange() != ItemEvent.SELECTED) {
return;
}
vv.getPickedVertexState().clear();
String str = ((String) e.getItem());
yyy.setSelectedValue(indices.get(str), true);
});
vv.getRenderContext().setLabelOffset(20);
GraphZoomScrollPane zzz = new GraphZoomScrollPane(vv);
JPanel ret = new JPanel(new GridLayout(1, 1));
ret.add(zzz);
ret.setBorder(BorderFactory.createEtchedBorder());
return ret;
} catch (Throwable cnf) {
cnf.printStackTrace();
throw new RuntimeException();
}
}
use of catdata.fql.decl.SigExp.Var in project fql by CategoricalData.
the class RaToFql method trans.
private static Pair<String, Const> trans(Const src, EFlower fl, String pre) {
// SigExp src0 = new SigExp.Var("S");
LinkedList<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
List<String> nodes1 = new LinkedList<>();
List<String> nodes2 = new LinkedList<>();
List<String> nodes3 = new LinkedList<>();
nodes1.add("adom");
nodes2.add("adom");
nodes2.add("guid");
nodes3.add("adom");
List<Triple<String, String, String>> attrs = new LinkedList<>();
attrs.add(new Triple<>("att", "adom", "str"));
List<Triple<String, String, String>> edges1 = new LinkedList<>();
List<Triple<String, String, String>> edges2 = new LinkedList<>();
List<Triple<String, String, String>> edges3 = new LinkedList<>();
List<Pair<String, String>> inodes1 = new LinkedList<>();
List<Pair<String, String>> inodes2 = new LinkedList<>();
List<Pair<String, String>> inodes3 = new LinkedList<>();
inodes1.add(new Pair<>("adom", "adom"));
inodes2.add(new Pair<>("adom", "adom"));
inodes3.add(new Pair<>("adom", "adom"));
List<Pair<String, String>> iattrs = new LinkedList<>();
iattrs.add(new Pair<>("att", "att"));
List<Pair<String, List<String>>> iedges1 = new LinkedList<>();
List<Pair<String, List<String>>> iedges2 = new LinkedList<>();
List<Pair<String, List<String>>> iedges3 = new LinkedList<>();
for (String k : fl.from.keySet()) {
String v = fl.from.get(k);
inodes1.add(new Pair<>(k, v));
nodes1.add(k);
inodes2.add(new Pair<>(k, "guid"));
for (Triple<String, String, String> arr : src.arrows) {
if (arr.second.equals(v)) {
List<String> l = new LinkedList<>();
l.add(v);
l.add(arr.first);
edges1.add(new Triple<>(k + "_" + arr.first, k, "adom"));
iedges1.add(new Pair<>(k + "_" + arr.first, l));
edges2.add(new Triple<>(k + "_" + arr.first, "guid", "adom"));
List<String> l0 = new LinkedList<>();
l0.add("guid");
l0.add(k + "_" + arr.first);
iedges2.add(new Pair<>(k + "_" + arr.first, l0));
}
}
}
List<List<Triple<String, String, String>>> eqcs = merge(edges2, fl);
Iterator<Triple<String, String, String>> it = edges2.iterator();
while (it.hasNext()) {
Triple<String, String, String> k = it.next();
for (List<Triple<String, String, String>> v : eqcs) {
if (v.contains(k) && !v.get(0).equals(k)) {
it.remove();
}
}
}
for (Pair<String, List<String>> kk : iedges2) {
Triple<String, String, String> k = new Triple<>(kk.second.get(1), "guid", "adom");
for (List<Triple<String, String, String>> v : eqcs) {
if (v.contains(k) && !v.get(0).equals(k)) {
List<String> xxx = new LinkedList<>();
xxx.add("guid");
xxx.add(v.get(0).first);
kk.second = xxx;
break;
}
}
}
nodes3.add("guid");
inodes3.add(new Pair<>("guid", "guid"));
for (String k : fl.select.keySet()) {
Pair<String, String> v = fl.select.get(k);
edges3.add(new Triple<>(k, "guid", "adom"));
Triple<String, String, String> t = new Triple<>(v.first + "_" + fl.from.get(v.first) + "_" + v.second, "guid", "adom");
if (fl.from.get(v.first) == null) {
throw new RuntimeException(v.first + " is not selectable in " + fl);
}
for (List<Triple<String, String, String>> eqc : eqcs) {
if (eqc.contains(t)) {
List<String> li = new LinkedList<>();
li.add("guid");
li.add(eqc.get(0).first);
iedges3.add(new Pair<>(k, li));
}
}
}
Const sig1 = new Const(nodes1, attrs, edges1, eqs);
Const sig2 = new Const(nodes2, attrs, edges2, eqs);
Const sig3 = new Const(nodes3, attrs, edges3, eqs);
MapExp.Const map1 = new MapExp.Const(inodes1, iattrs, iedges1, sig1, new Var("S"));
MapExp.Const map2 = new MapExp.Const(inodes2, iattrs, iedges2, src, sig2);
MapExp.Const map3 = new MapExp.Const(inodes3, iattrs, iedges3, sig3, sig2);
String xxx = "";
xxx += "\n\nschema " + pre + "fromSchema = " + sig1;
xxx += "\n\nmapping " + pre + "fromMapping = " + map1 + " : " + pre + "fromSchema -> S";
xxx += "\n\ninstance " + pre + "fromInstance = delta " + pre + "fromMapping I";
xxx += "\n\nschema " + pre + "whereSchema = " + sig2;
xxx += "\n\nmapping " + pre + "whereMapping = " + map2 + " : " + pre + "fromSchema -> " + pre + "whereSchema";
xxx += "\n\ninstance " + pre + "whereInstance = pi " + pre + "whereMapping " + pre + "fromInstance";
xxx += "\n\nschema " + pre + "Schema = " + sig3;
xxx += "\n\nmapping " + pre + "selectMapping = " + map3 + " : " + pre + "Schema -> " + pre + "whereSchema";
if (fl.distinct) {
xxx += "\n\ninstance " + pre + "selectInstance = delta " + pre + "selectMapping " + pre + "whereInstance";
xxx += "\n\ninstance " + pre + " = relationalize " + pre + "selectInstance";
} else {
xxx += "\n\ninstance " + pre + " = delta " + pre + "selectMapping " + pre + "whereInstance";
}
String comment = longSlash + "\n/* " + "Translation of " + pre + " */\n" + longSlash;
return new Pair<>(comment + xxx, sig3);
}
use of catdata.fql.decl.SigExp.Var 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.SigExp.Var in project fql by CategoricalData.
the class FQLProgram method doColors.
public void doColors() {
for (String k : sigs.keySet()) {
Color c = nColor();
nmap.put(k, c);
smap.put(new Var(k), c);
smap.put(sigs.get(k), c);
smap.put(sigs.get(k).toConst(this), c);
}
for (String k : insts.keySet()) {
nmap.put(k, nColor());
}
build = build();
build2 = build2();
}
use of catdata.fql.decl.SigExp.Var in project fql by CategoricalData.
the class RaToFql method transSQLSchema.
private static String transSQLSchema(List<Pair<String, EExternal>> in) {
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<>();
String adom = "adom";
nodes.add(adom);
List<Pair<Object, Object>> adomT = new LinkedList<>();
LinkedList<Pair<Object, Object>> attT = new LinkedList<>();
inodes.add(new Pair<>(adom, adomT));
iattrs.add(new Pair<>("att", attT));
attrs.add(new Triple<>("att", adom, "str"));
Set<Object> enums = new HashSet<>();
Map<String, Integer> dom1 = new HashMap<>();
List<Pair<String, EExternal>> queries = new LinkedList<>();
int count = 0;
Set<String> seen = new HashSet<>();
Map<String, List<String>> cols = new HashMap<>();
for (Pair<String, EExternal> kk0 : in) {
EExternal k0 = kk0.second;
// String key = kk0.first;
if (k0 instanceof ECreateTable) {
ECreateTable k = (ECreateTable) k0;
if (seen.contains(k.name)) {
throw new RuntimeException("Duplicate name: " + k.name);
}
if (k.name.equals("adom") || k.name.equals("att")) {
throw new RuntimeException("The names adom and att cannot be used.");
}
seen.add(k.name);
nodes.add(k.name);
inodes.add(new Pair<>(k.name, new LinkedList<>()));
List<String> lcols = new LinkedList<>();
for (Pair<String, String> col : k.types) {
lcols.add(col.first);
if (seen.contains(col.first)) {
throw new RuntimeException("Duplicate name: " + col.first);
}
seen.add(col.first);
arrows.add(new Triple<>(k.name + "_" + col.first, k.name, adom));
iarrows.add(new Pair<>(k.name + "_" + col.first, new LinkedList<>()));
}
cols.put(k.name, lcols);
}
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);
}
String id = "" + count++;
node.add(new Pair<>(id, id));
for (int colNum = 0; colNum < tuple.size(); colNum++) {
Integer xxx = dom1.get(tuple.get(colNum));
if (xxx == null) {
dom1.put(tuple.get(colNum), count);
enums.add(tuple.get(colNum));
adomT.add(new Pair<>(count, count));
attT.add(new Pair<>(count, "\"" + tuple.get(colNum) + "\""));
xxx = count;
count++;
}
List<Pair<Object, Object>> yyy = lookup2(k.target + "_" + lcols.get(colNum), iarrows);
if (yyy == null) {
throw new RuntimeException("Anomaly: please report");
}
yyy.add(new Pair<>(id, xxx));
}
}
}
if (k0 instanceof EFlower || k0 instanceof EUnion || k0 instanceof EDiff || k0 instanceof EED) {
queries.add(kk0);
}
}
Const exp = new Const(nodes, attrs, arrows, eqs);
InstExp.Const inst = new InstExp.Const(inodes, iattrs, iarrows, new Var("S"));
// int ctx = 0;
String xxx = "\n\n";
Map<String, String> schemas = new HashMap<>();
Map<String, Const> schemas0 = new HashMap<>();
Map<String, Boolean> done = new HashMap<>();
for (Pair<String, EExternal> gh0 : queries) {
String k = gh0.first;
EExternal gh = gh0.second;
if (gh instanceof EFlower) {
EFlower fl = (EFlower) gh;
Pair<String, Const> yyy = trans(exp, fl, k);
xxx += yyy.first + "\n\n";
schemas.put(k, k + "Schema");
schemas0.put(k, yyy.second);
} else if (gh instanceof EUnion) {
EUnion g = (EUnion) gh;
String s1 = schemas.get(g.l);
schemas.put(k, s1);
schemas0.put(k, schemas0.get(g.l));
xxx += longSlash + "\n/* Translation of " + k + " */\n" + longSlash;
if (g.distinct) {
xxx += "\n\n" + "instance " + k + "_temp = (" + g.l + " + " + g.r + ")";
xxx += "\n\n" + "instance " + k + " = relationalize " + k + "_temp";
} else {
xxx += "\n\n" + "instance " + k + " = (" + g.l + " + " + g.r + ")";
}
xxx += "\n\n";
} else if (gh instanceof EDiff) {
String f1x = ((EDiff) gh).l;
String f2x = ((EDiff) gh).r;
String s1 = schemas.get(f1x);
Const s1x = schemas0.get(f1x);
schemas.put(k, s1);
String f1y = doAdom(s1x, s1);
if (!done.containsKey(s1)) {
xxx += "\n\n" + f1y;
done.put(s1, true);
}
xxx += doAdom2(s1, f1x, k + "_l");
xxx += doAdom2(s1, f2x, k + "_r");
String f1 = k + "_l" + "_rel";
String f2 = k + "_r" + "_rel";
xxx += longSlash + "\n/* Translation of " + k + " */\n" + longSlash;
xxx += "\n\ninstance " + k + "prp = prop " + s1;
xxx += "\n\ninstance " + k + "one = unit " + s1;
xxx += "\n\ninstance " + k + "prp2 = (" + k + "prp * " + k + "prp)";
xxx += "\n\ntransform " + k + f1 + "t = " + k + "one.unit " + f1;
xxx += "\n\ntransform " + k + f2 + "t = " + k + "one.unit " + f2;
xxx += "\n\ntransform " + k + f1 + "tchi = " + k + "prp.char " + k + f1 + "t";
xxx += "\n\ntransform " + k + f2 + "tchi = " + k + "prp.char " + k + f2 + "t";
xxx += "\n\ntransform " + k + "n = (" + k + f2 + "tchi then " + k + "prp.not)";
xxx += "\n\ntransform " + k + "j1 = " + k + "prp2.(" + k + f1 + "tchi * " + k + "n)";
xxx += "\n\ntransform " + k + "j2 = (" + k + "j1 then " + k + "prp2.and)";
if (((EDiff) gh).distinct) {
xxx += "\n\ninstance " + k + "temp = kernel " + k + "j2";
xxx += "\n\ninstance " + k + " = relationalize " + k + "temp";
} else {
xxx += "\n\ninstance " + k + " = kernel " + k + "j2";
}
xxx += "\n\n";
} else if (gh instanceof EED) {
EED c = (EED) gh;
MapExp.Const f = doED(cols, c.from1, c.where1, exp);
Const src = (Const) f.src;
c.from2.putAll(c.from1);
c.where2.addAll(c.where1);
MapExp.Const g = doED(cols, c.from2, c.where2, exp);
List<Pair<String, String>> l = new LinkedList<>();
for (String x : src.nodes) {
l.add(new Pair<>(x, x));
}
List<Pair<String, List<String>>> em = new LinkedList<>();
for (Triple<String, String, String> e : src.arrows) {
List<String> y = new LinkedList<>();
y.add(e.second);
y.add(e.first);
em.add(new Pair<>(e.first, y));
}
MapExp.Const i = new MapExp.Const(l, new LinkedList<>(), em, f.src, g.src);
xxx += longSlash + "\n/* Translation of " + k + " */\n" + longSlash;
xxx += "\n\nschema " + k + "A = " + f.src;
xxx += "\n\nschema " + k + "E = " + g.src;
xxx += "\n\nmapping " + k + "I = " + i + " : " + k + "A -> " + k + "E";
xxx += "\n\nmapping " + k + " = " + g + " : " + k + "E -> S";
xxx += "\n\n";
} else {
throw new RuntimeException();
}
}
String enum0 = "";
boolean b = false;
for (Object o : enums) {
if (b) {
enum0 += ", ";
}
b = true;
enum0 += "\"" + o + "\"";
}
String comment = "//schema S and instance I represent the entire input database.\n\n";
String preS = "enum str = { " + enum0 + " }";
return comment + preS + "\n\nschema S = " + exp + "\n\ninstance I = " + inst + " : S" + xxx;
}
Aggregations