use of catdata.fql.FQLException in project fql by CategoricalData.
the class InstOps method visit.
@Override
public List<PSM> visit(String dst, TransExp.Pi e) {
try {
List<PSM> ret = new LinkedList<>();
Pair<String, String> ht = e.h.type(prog);
Signature sig = prog.insts.get(ht.first).type(prog).toSig(prog);
Mapping F = ((Pi) prog.insts.get(e.src)).F.toMap(prog);
Map<String, Triple<Node, Node, Arr<Node, Path>>[]> colmap1x = PSMGen.pi(F, e.h.type(prog).first, e.src).second;
String next = next();
ret.addAll(PSMGen.makeTables(next, sig, false));
ret.addAll(e.h.accept(next, this));
Signature sig2 = prog.insts.get(e.src).type(prog).toSig(prog);
for (Node n : sig2.nodes) {
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
Map<String, String> from = new HashMap<>();
from.put("limit1", e.src + "_" + n.string + "_limit");
from.put("limit2", e.dst + "_" + n.string + "_limit");
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
int i = 0;
for (Triple<Node, Node, Arr<Node, Path>> col : colmap1x.get(n.string)) {
from.put("l" + i, next + "_" + col.second.string);
where.add(new Pair<>(new Pair<>("l" + i, "c0"), new Pair<>("limit1", "c" + i)));
where.add(new Pair<>(new Pair<>("l" + i, "c1"), new Pair<>("limit2", "c" + i)));
i++;
}
// check
for (@SuppressWarnings("unused") Attribute<Node> a : sig2.attrsFor(n)) {
where.add(new Pair<>(new Pair<>("limit1", "c" + i), new Pair<>("limit2", "c" + i)));
i++;
}
select.put("c0", new Pair<>("limit1", "guid"));
select.put("c1", new Pair<>("limit2", "guid"));
Flower f = new Flower(select, from, where);
ret.add(new InsertSQL(dst + "_" + n.string, f, "c0", "c1"));
}
return ret;
} catch (FQLException fe) {
fe.printStackTrace();
throw new RuntimeException(fe.getLocalizedMessage());
}
}
use of catdata.fql.FQLException in project fql by CategoricalData.
the class InstOps method visit.
@Override
public Pair<List<PSM>, Object> visit(String dst, Times e) {
try {
SigExp k = e.type(prog);
Signature s = k.toSig(prog);
List<PSM> ret = new LinkedList<>();
ret.addAll(PSMGen.makeTables(dst + "_fst", s, false));
ret.addAll(PSMGen.makeTables(dst + "_snd", s, false));
Pair<Map<Node, List<Pair<Path, Attribute<Node>>>>, List<PSM>> l_obs = Relationalizer.observations(s, dst + "_l_obs", e.a, false);
Pair<Map<Node, List<Pair<Path, Attribute<Node>>>>, List<PSM>> r_obs = Relationalizer.observations(s, dst + "_r_obs", e.b, false);
if (!(l_obs.first.equals(r_obs.first))) {
throw new RuntimeException("Internal error, please report.");
}
ret.addAll(PSMGen.makeTables(dst + "_l_obs", s, false));
ret.addAll(l_obs.second);
ret.addAll(PSMGen.dropTables(dst + "_l_obs", s));
ret.addAll(PSMGen.makeTables(dst + "_r_obs", s, false));
ret.addAll(r_obs.second);
ret.addAll(PSMGen.dropTables(dst + "_r_obs", s));
for (Node n : s.nodes) {
List<Pair<Path, Attribute<Node>>> lats = l_obs.first.get(n);
// List<Pair<Path, Attribute<Node>>> rats = r_obs.first.get(n);
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
Map<String, String> from = new HashMap<>();
List<String> attrs = new LinkedList<>();
Map<String, String> attrsM = new HashMap<>();
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
from.put("lft", dst + "_l_obs_" + n.string + "_observables");
from.put("rght", dst + "_r_obs_" + n.string + "_observables");
attrs.add("lft");
attrs.add("rght");
attrsM.put("lft", PSM.VARCHAR());
attrsM.put("rght", PSM.VARCHAR());
select.put("lft", new Pair<>("lft", "id"));
select.put("rght", new Pair<>("rght", "id"));
int idx = 0;
for (Pair<Path, Attribute<Node>> aa : lats) {
Attribute<Node> a = aa.second;
where.add(new Pair<>(new Pair<>("lft", "c" + idx), new Pair<>("rght", "c" + idx)));
select.put("c" + idx, new Pair<>("lft", "c" + idx));
attrs.add("c" + idx);
attrsM.put("c" + idx, a.target.psm());
idx++;
}
Flower f = new Flower(select, from, where);
ret.add(new CreateTable(dst + "_prod_temp_" + n.string, attrsM, false));
ret.add(new InsertSQL2(dst + "_prod_temp_" + n.string, f, attrs));
Map<String, String> attrsM0 = new HashMap<>(attrsM);
attrsM0.put("gguid", PSM.VARCHAR());
ret.add(new CreateTable(dst + "_prod_guid_" + n.string, attrsM0, false));
ret.add(new InsertKeygen(dst + "_prod_guid_" + n.string, "gguid", dst + "_prod_temp_" + n.string, attrs));
List<Pair<Pair<String, String>, Pair<String, String>>> where0 = new LinkedList<>();
from = new HashMap<>();
from.put("t", dst + "_prod_guid_" + n.string);
select = new LinkedHashMap<>();
select.put("c0", new Pair<>("t", "gguid"));
select.put("c1", new Pair<>("t", "lft"));
f = new Flower(select, from, where0);
ret.add(new InsertSQL(dst + "_fst_" + n, f, "c0", "c1"));
from = new HashMap<>();
from.put("t", dst + "_prod_guid_" + n.string);
select = new LinkedHashMap<>();
select.put("c0", new Pair<>("t", "gguid"));
select.put("c1", new Pair<>("t", "rght"));
f = new Flower(select, from, where0);
ret.add(new InsertSQL(dst + "_snd_" + n, f, "c0", "c1"));
LinkedHashMap<String, Pair<String, String>> select0 = new LinkedHashMap<>();
select0.put("c0", new Pair<>("t", "gguid"));
select0.put("c1", new Pair<>("t", "gguid"));
Map<String, String> from0 = new HashMap<>();
from0.put("t", dst + "_prod_guid_" + n.string);
Flower sql = new Flower(select0, from0, where0);
ret.add(new InsertSQL(dst + "_" + n.string, sql, "c0", "c1"));
for (Attribute<Node> a : s.attrsFor(n)) {
select0 = new LinkedHashMap<>();
select0.put("c0", new Pair<>("t", "gguid"));
Arr<Node, Path> ppp = s.toCategory2().first.id(n);
int ppp0 = lats.indexOf(new Pair<>(ppp.arr, a));
select0.put("c1", new Pair<>("t", "c" + ppp0));
from0 = new HashMap<>();
from0.put("t", dst + "_prod_guid_" + n.string);
sql = new Flower(select0, from0, where0);
ret.add(new InsertSQL(dst + "_" + a.name, sql, "c0", "c1"));
}
// ret.add(new DropTable(dst + "_prod_temp_" + n));
}
for (Edge edge : s.edges) {
Map<String, String> from = new HashMap<>();
from.put("leftEdge", e.a + "_" + edge.name);
from.put("rightEdge", e.b + "_" + edge.name);
from.put("srcGuid", dst + "_prod_guid_" + edge.source);
from.put("dstGuid", dst + "_prod_guid_" + edge.target);
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
where.add(new Pair<>(new Pair<>("leftEdge", "c0"), new Pair<>("srcGuid", "lft")));
where.add(new Pair<>(new Pair<>("rightEdge", "c0"), new Pair<>("srcGuid", "rght")));
where.add(new Pair<>(new Pair<>("leftEdge", "c1"), new Pair<>("dstGuid", "lft")));
where.add(new Pair<>(new Pair<>("rightEdge", "c1"), new Pair<>("dstGuid", "rght")));
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
select.put("c0", new Pair<>("srcGuid", "gguid"));
select.put("c1", new Pair<>("dstGuid", "gguid"));
Flower f = new Flower(select, from, where);
ret.add(new InsertSQL(dst + "_" + edge.name, f, "c0", "c1"));
}
Fn<Quad<String, String, String, String>, List<PSM>> fn = x -> {
// x.third -> e.a
String f = x.first;
// x.third -> e.b
String g = x.second;
// String C = x.third;
String dst0 = x.fourth;
// must be a map x.third -> dst
List<PSM> ret1 = new LinkedList<>();
for (Node n : s.nodes) {
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
Map<String, String> from = new HashMap<>();
from.put("f", f + "_" + n.string);
from.put("g", g + "_" + n.string);
from.put("lim", dst + "_prod_guid_" + n.string);
where.add(new Pair<>(new Pair<>("f", "c0"), new Pair<>("g", "c0")));
where.add(new Pair<>(new Pair<>("lim", "lft"), new Pair<>("f", "c1")));
where.add(new Pair<>(new Pair<>("lim", "rght"), new Pair<>("g", "c1")));
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
select.put("c0", new Pair<>("f", "c0"));
select.put("c1", new Pair<>("lim", "gguid"));
Flower flower = new Flower(select, from, where);
ret1.add(new InsertSQL(dst0 + "_" + n.string, flower, "c0", "c1"));
}
return ret1;
};
return new Pair<>(ret, fn);
} catch (FQLException fe) {
throw new RuntimeException(fe.getLocalizedMessage());
}
}
use of catdata.fql.FQLException 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.FQLException in project fql by CategoricalData.
the class Mapping method filter.
// public String name;
// public boolean isId = false;
/*
* public Mapping(String name, Environment env, MappingDecl md) throws
* FQLException { this.name = name; switch (md.kind) { case COMPOSE: Mapping
* m1 = env.mappings.get(md.m1); Mapping m2 = env.mappings.get(md.m2);
*
* if (m1 == null) { throw new FQLException("For " + name +
* ", cannot find mapping " + md.m1); } if (m2 == null) { throw new
* FQLException("For " + name + ", cannot find mapping " + md.m2); } if
* (!m2.target.equals(m1.source)) { throw new FQLException("Ill-typed: " +
* md); } this.source = m2.source; this.target = m1.target; for (Node k :
* m1.source.nodes) { Node v = m1.nm.get(k); nm.put(k, m2.nm.get(v)); } for
* (Edge k : m1.source.edges) { Path v = m1.em.get(k); Path p0 = expand(v,
* m2.nm, m2.em); em.put(k, p0); }
*
* break; case ID: Signature s = env.getSchema(md.schema); if
* (!(md.schema.equals(md.source) && md.schema.equals(md.target))) { throw
* new FQLException("Bad identity mapping : " + md.name); } identity(env,
* s); break; case MORPHISM: // Pair<List<Pair<String, String>>,
* List<Pair<String, String>>> xxx // = filter(md.objs);
* initialize(env.getSchema(md.source), env.getSchema(md.target), md.objs,
* md.atts, md.arrows); break; default: throw new RuntimeException(); }
* validate(); }
*/
private Pair<List<Pair<String, String>>, List<Pair<String, String>>> filter(List<Pair<String, String>> objs) throws FQLException {
List<Pair<String, String>> ret = new LinkedList<>();
List<Pair<String, String>> ret2 = new LinkedList<>();
for (Pair<String, String> p : objs) {
if (source.isAttribute(p.first) && target.isAttribute(p.second)) {
ret.add(p);
} else if (source.isNode(p.first) && target.isNode(p.second)) {
ret2.add(p);
} else {
throw new FQLException("Bad mapping: " + p);
}
}
return new Pair<>(ret2, ret);
}
use of catdata.fql.FQLException in project fql by CategoricalData.
the class Mapping method initialize.
/**
* Does most of the work of the constructor.
*/
private void initialize(Signature sourceX, Signature targetX, List<Pair<String, String>> objs, List<Pair<String, String>> attrs, List<Pair<String, List<String>>> arrows) throws FQLException {
source = sourceX;
target = targetX;
Pair<List<Pair<String, String>>, List<Pair<String, String>>> s = filter(objs);
objs = s.first;
// List<Pair<String, String>> attrs = s.second;
for (Pair<String, String> p : objs) {
Node sn = source.getNode(p.first);
Node tn = target.getNode(p.second);
nm.put(sn, tn);
}
try {
for (Pair<String, List<String>> arrow : arrows) {
Edge e = source.getEdge(arrow.first);
Path p = new Path(target, arrow.second);
em.put(e, p);
}
} catch (FQLException e) {
throw new FQLException("In mapping " + this + ", bad path mapping: " + e);
}
for (Pair<String, String> a : attrs) {
Attribute<Node> a1 = source.getAttr(a.first);
Attribute<Node> a2 = target.getAttr(a.second);
if (a1 == null) {
throw new FQLException(/* "In mapping " + this
+ ", */
"Cannot find source attribute " + a.first);
}
if (a2 == null) {
throw new FQLException(/* "In mapping " + this
+ ", */
"Cannot find target attribute " + a.second);
}
if (a1.target.equals(a2.target)) {
am.put(a1, a2);
} else {
throw new FQLException("Incompatible attribute mapping types " + a);
}
}
for (Node n : source.nodes) {
if (nm.get(n) == null) {
throw new FQLException("Missing node mapping from " + n + " in " + this + "\n" + this);
}
}
for (Edge e : source.edges) {
if (em.get(e) == null) {
throw new FQLException("Missing arrow mapping from " + e + " in " + this);
}
}
for (Attribute<Node> a : source.attrs) {
if (am.get(a) == null) {
throw new FQLException("Missing attribute mapping from " + a + " in " + this);
}
}
}
Aggregations