use of catdata.fql.decl.InstExp.Pi in project fql by CategoricalData.
the class InstOps method visit.
@Override
public List<PSM> visit(String env, Return e) {
// String xxx = "return_temp_xxx";
List<PSM> ret = new LinkedList<>();
InstExp i1 = prog.insts.get(e.inst);
if (i1 instanceof Delta) {
String middle = ((Delta) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((Delta) i1).F.toMap(prog);
if (i2 instanceof Sigma) {
Sigma input0 = ((Sigma) i2);
String input = input0.I;
for (Node n : f.source.nodes) {
ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(input + "_" + n.string, middle + "_" + f.nm.get(n) + "_subst", e.inst + "_" + n.string + "_subst"), "c0", "c1"));
}
} else if (i2 instanceof FullSigma) {
FullSigma input0 = ((FullSigma) i2);
String input = input0.I;
for (Node n : f.source.nodes) {
ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(input + "_" + n.string, middle + "_" + n.string + "_e", e.inst + "_" + n.string + "_subst"), "c0", "c1"));
}
} else {
throw new RuntimeException();
}
} else if (i1 instanceof Pi) {
String middle = ((Pi) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((Pi) i1).F.toMap(prog);
if (i2 instanceof Delta) {
Delta input0 = ((Delta) i2);
String input = input0.I;
for (Node n : f.target.nodes) {
try {
Map<String, Triple<Node, Node, Arr<Node, Path>>[]> colmap = PSMGen.pi(f, middle, e.inst).second;
Triple<Node, Node, Arr<Node, Path>>[] col = colmap.get(n.string);
if (col.length == 0) {
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
Map<String, String> from = new HashMap<>();
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
from.put("lim", e.inst + "_" + n.string + "_limit");
from.put("middle", input + "_" + n.string);
select.put("c0", new Pair<>("middle", "c0"));
select.put("c1", new Pair<>("lim", "guid"));
Flower flower = new Flower(select, from, where);
ret.add(new InsertSQL(env + "_" + n.string, flower, "c0", "c1"));
return ret;
}
// LinkedHashMap<String, String> attrs = new LinkedHashMap<>();
// attrs.put("guid", PSM.VARCHAR());
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
Map<String, String> from = new HashMap<>();
from.put("lim", e.inst + "_" + n.string + "_limit");
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
int i = 0;
for (Triple<Node, Node, Arr<Node, Path>> col0 : col) {
from.put("c" + i + "_subst_inv", middle + "_" + col0.second.string + "_subst_inv");
where.add(new Pair<>(new Pair<>("lim", "c" + i), new Pair<>("c" + i + "_subst_inv", "c0")));
// attrs.put("c" + i, PSM.VARCHAR());
i++;
}
// if (col.length > 1) {
for (int j = 0; j < col.length; j++) {
if (col[j].third.arr.equals(f.target.toCategory2().second.of(new Path(f.target, n)).arr)) {
where.add(new Pair<>(new Pair<>("c" + 0 + "_subst_inv", "c1"), new Pair<>("c" + j + "_subst_inv", "c1")));
}
}
select.put("c" + 0, new Pair<>("c" + 0 + "_subst_inv", "c1"));
select.put("c1", new Pair<>("lim", "guid"));
// }
// ret.add(new CreateTable(xxx, attrs, false));
Flower flower = new Flower(select, from, where);
ret.add(new InsertSQL(env + "_" + n.string, flower, "c0", "c1"));
} catch (FQLException fe) {
fe.printStackTrace();
throw new RuntimeException(fe.getMessage());
}
}
} else {
throw new RuntimeException();
}
} else {
throw new RuntimeException();
}
return ret;
}
use of catdata.fql.decl.InstExp.Pi 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.Pi in project fql by CategoricalData.
the class InstOps method visit.
@Override
public List<PSM> visit(String env, Coreturn e) {
String xxx = "coreturn_temp_xxx";
List<PSM> ret = new LinkedList<>();
InstExp i1 = prog.insts.get(e.inst);
if (i1 instanceof Sigma) {
String middle = ((Sigma) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((Sigma) i1).F.toMap(prog);
if (i2 instanceof Delta) {
for (Node n : f.target.nodes) {
List<Flower> u = new LinkedList<>();
for (Node m : f.source.nodes) {
if (!f.nm.get(m).equals(n)) {
continue;
}
u.add(new CopyFlower(middle + "_" + m.string + "_subst_inv", "c0", "c1"));
}
ret.add(new SimpleCreateTable(xxx, PSM.VARCHAR(), false));
ret.add(new InsertSQL(xxx, new Union(u), "c0", "c1"));
ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(e.inst + "_" + n.string + "_subst_inv", xxx), "c0", "c1"));
ret.add(new DropTable(xxx));
}
}
} else if (i1 instanceof FullSigma) {
String middle = ((FullSigma) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((FullSigma) i1).F.toMap(prog);
if (i2 instanceof Delta) {
ret.add(new FullSigmaCounit(f, ((Delta) i2).I, middle, e.inst, env));
} else {
throw new RuntimeException();
}
} else if (i1 instanceof Delta) {
String middle = ((Delta) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((Delta) i1).F.toMap(prog);
if (i2 instanceof Pi) {
Pi input0 = ((Pi) i2);
String input = input0.I;
try {
Map<String, Triple<Node, Node, Arr<Node, Path>>[]> colmap = PSMGen.pi(f, input, middle).second;
for (Node m : f.source.nodes) {
Node n = f.nm.get(m);
Triple<Node, Node, Arr<Node, Path>>[] col = colmap.get(n.string);
Triple<Node, Node, Arr<Node, Path>> toFind = new Triple<>(n, m, new Arr<>(new Path(f.target, n), n, n));
int i = 0;
boolean found = false;
for (Triple<Node, Node, Arr<Node, Path>> cand : col) {
if (cand.equals(toFind)) {
found = true;
Map<String, String> from = new HashMap<>();
from.put("lim", middle + "_" + n + "_limit");
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
select.put("c0", new Pair<>("lim", "guid"));
select.put("c1", new Pair<>("lim", "c" + i));
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
Flower flower = new Flower(select, from, where);
ret.add(new SimpleCreateTable(xxx, PSM.VARCHAR(), false));
ret.add(new InsertSQL(xxx, flower, "c0", "c1"));
ret.add(new InsertSQL(env + "_" + m, PSMGen.compose(e.inst + "_" + m + "_subst_inv", xxx), "c0", "c1"));
ret.add(new DropTable(xxx));
break;
}
i++;
}
if (!found) {
throw new RuntimeException();
}
}
} catch (FQLException fe) {
fe.printStackTrace();
throw new RuntimeException(fe.getMessage());
}
} else {
throw new RuntimeException();
}
} else {
throw new RuntimeException();
}
return ret;
}
use of catdata.fql.decl.InstExp.Pi in project fql by CategoricalData.
the class FQLProgram method build.
private Graph<String, Object> build() {
// Graph<V, E> where V is the type of the vertices
Graph<String, Object> g2 = new DirectedSparseMultigraph<>();
Ref<Integer> guid = new Ref<>(0);
for (String k : insts.keySet()) {
InstExp i = insts.get(k);
i.type(this).toConst(this);
g2.addVertex(k);
i.accept(new Unit(), new InstExpVisitor<Unit, Unit>() {
@Override
public Unit visit(Unit env, Zero e) {
return null;
}
@Override
public Unit visit(Unit env, One e) {
return null;
}
@Override
public Unit visit(Unit env, Two e) {
return null;
}
@Override
public Unit visit(Unit env, Plus e) {
g2.addEdge(new Pair<>(pp(guid), e), e.a, k);
g2.addEdge(new Pair<>(pp(guid), e), e.b, k);
return null;
}
@Override
public Unit visit(Unit env, Times e) {
g2.addEdge(new Pair<>(pp(guid), e), e.a, k);
g2.addEdge(new Pair<>(pp(guid), e), e.b, k);
return null;
}
@Override
public Unit visit(Unit env, Exp e) {
g2.addEdge(new Pair<>(pp(guid), e), e.a, k);
g2.addEdge(new Pair<>(pp(guid), e), e.b, k);
return null;
}
@Override
public Unit visit(Unit env, Const e) {
return null;
}
@Override
public Unit visit(Unit env, Delta e) {
g2.addEdge(new Pair<>(pp(guid), e), e.I, k);
return null;
}
@Override
public Unit visit(Unit env, Sigma e) {
g2.addEdge(new Pair<>(pp(guid), e), e.I, k);
return null;
}
@Override
public Unit visit(Unit env, Pi e) {
g2.addEdge(new Pair<>(pp(guid), e), e.I, k);
return null;
}
@Override
public Unit visit(Unit env, FullSigma e) {
g2.addEdge(new Pair<>(pp(guid), e), e.I, k);
return null;
}
@Override
public Unit visit(Unit env, Relationalize e) {
g2.addEdge(new Pair<>(pp(guid), e), e.I, k);
return null;
}
@Override
public Unit visit(Unit env, External e) {
return null;
}
@Override
public Unit visit(Unit env, Eval e) {
g2.addEdge(new Pair<>(pp(guid), e), e.e, k);
return null;
}
@Override
public Unit visit(Unit env, FullEval e) {
g2.addEdge(new Pair<>(pp(guid), e), e.e, k);
return null;
}
@Override
public Unit visit(Unit env, Kernel e) {
TransExp t = transforms.get(e.trans);
Pair<String, String> p = t.type(FQLProgram.this);
g2.addEdge(new Pair<>(pp(guid), e), p.first, k);
g2.addEdge(new Pair<>(pp(guid), e), p.second, k);
return null;
}
private Integer pp(Ref<Integer> ref) {
int ret = ref.x;
ref.x = ret + 1;
return ref.x;
}
@Override
public Unit visit(Unit env, Step e) {
// (Step) this should add an edge
return null;
}
});
}
return g2;
}
Aggregations