use of catdata.fql.decl.Path in project fql by CategoricalData.
the class PSMGen method findEquiv.
private static Path findEquiv(Node c, Mapping f, Edge e) throws FQLException {
Signature C = f.source;
Signature D = f.target;
FinCat<Node, Path> C0 = C.toCategory2().first;
for (Arr<Node, Path> peqc : C0.arrows) {
Path path = peqc.arr;
// Path path = new Path(f.source, p);
if (!path.source.equals(c)) {
continue;
}
Path path_f = f.appy(D, path);
Fn<Path, Arr<Node, Path>> F = D.toCategory2().second;
if (F.of(path_f).equals(F.of(new Path(D, e)))) {
return path;
}
}
throw new FQLException("Could not find path mapping to " + e);
}
use of catdata.fql.decl.Path in project fql by CategoricalData.
the class PSMGen method sigma.
public static List<PSM> sigma(Mapping F, String pre, String inst) throws FQLException {
Signature C = F.source;
Signature D = F.target;
List<PSM> ret = new LinkedList<>();
if (!FinFunctor.isDiscreteOpFib(F.toFunctor2().first)) {
throw new FQLException("Not a discrete op-fibration");
}
for (Node d : D.nodes) {
List<Flower> tn = new LinkedList<>();
for (Node c : C.nodes) {
if (F.nm.get(c).equals(d)) {
tn.add(new CopyFlower(inst + "_" + c.string, "c0", "c1"));
}
}
if (tn.isEmpty()) {
continue;
}
SQL y = foldUnion(tn);
ret.add(new InsertSQL(pre + "_" + d.string, y, "c0", "c1"));
}
for (Edge e : D.edges) {
Node d = e.source;
// Node d0 = e.target;
List<Flower> tn = new LinkedList<>();
for (Node c : C.nodes) {
if (F.nm.get(c).equals(d)) {
Path pc = findEquiv(c, F, e);
Flower q = compose(inst, pc);
tn.add(q);
}
}
if (tn.isEmpty()) {
continue;
}
SQL y = foldUnion(tn);
ret.add(new InsertSQL(pre + "_" + e.name, y, "c0", "c1"));
}
for (Attribute<Node> a : D.attrs) {
Node d = a.source;
// Node d0 = e.target;
List<Flower> tn = new LinkedList<>();
for (Node c : C.nodes) {
if (F.nm.get(c).equals(d)) {
Attribute<Node> pc = findEquiv(c, F, a);
Flower q = new CopyFlower(inst + "_" + pc.name, "c0", "c1");
tn.add(q);
}
}
if (tn.isEmpty()) {
continue;
}
SQL y = foldUnion(tn);
ret.add(new InsertSQL(pre + "_" + a.name, y, "c0", "c1"));
}
return ret;
}
use of catdata.fql.decl.Path in project fql by CategoricalData.
the class Relationalizer method observations.
public static Pair<Map<Node, List<Pair<Path, Attribute<Node>>>>, List<PSM>> observations(Signature sig, String out, String in, boolean relationalize) throws FQLException {
List<PSM> ret = new LinkedList<>();
Map<Node, List<Pair<Path, Attribute<Node>>>> attrs = new HashMap<>();
// attrs = new HashMap<Node, List<Pair<Path, Attribute<Node>>>>();
Map<String, String> edge_types = new HashMap<>();
edge_types.put("c0", PSM.VARCHAR());
edge_types.put("c1", PSM.VARCHAR());
// copy in to out, to start with
ret.addAll(copy(sig, out, in));
FinCat<Node, Path> cat = sig.toCategory2().first;
for (Node n : sig.nodes) {
attrs.put(n, new LinkedList<>());
int count = 0;
List<Map<String, String>> alltypes = new LinkedList<>();
for (Arr<Node, Path> p : cat.arrows) {
// need identity path to get attributes from n
if (!p.src.equals(n)) {
continue;
}
Flower f = PSMGen.compose(in, p.arr);
ret.add(new CreateTable(out + "_" + n.string + "tempNoAttrs" + count, edge_types, false));
InsertSQL f0 = new InsertSQL(out + "_" + n.string + "tempNoAttrs" + count, f, "c0", "c1");
ret.add(f0);
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<>();
List<Attribute<Node>> l = sig.attrsFor(p.arr.target);
from.put(n.string, out + "_" + n.string + "tempNoAttrs" + count);
select.put("c0", new Pair<>(n.string, "c0"));
// select.put("c1", new Pair<>(n.string + "tempNoAttrs", "c1"));
int i = 1;
LinkedHashMap<String, String> types = new LinkedHashMap<>();
types.put("c0", PSM.VARCHAR());
// types.put("c1", PSM.VARCHAR());
for (Attribute<Node> a : l) {
from.put(a.name, in + "_" + a.name);
Pair<String, String> lhs = new Pair<>(n.string, "c1");
Pair<String, String> rhs = new Pair<>(a.name, "c0");
where.add(new Pair<>(lhs, rhs));
select.put("c" + i, new Pair<>(a.name, "c1"));
types.put("c" + i, a.target.psm());
attrs.get(n).add(new Pair<>(p.arr, a));
i++;
}
alltypes.add(types);
Flower g = new Flower(select, from, where);
ret.add(new CreateTable(out + "_" + n.string + "temp" + count, types, false));
ret.add(new InsertSQL2(out + "_" + n.string + "temp" + count, g, new LinkedList<>(types.keySet())));
count++;
}
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
Map<String, String> from = new HashMap<>();
from.put(in + "_" + n.string, in + "_" + n.string);
Map<String, String> ty = new HashMap<>();
int u = 0;
ty.put("id", PSM.VARCHAR());
select.put("id", new Pair<>(in + "_" + n.string, "c0"));
for (int i = 0; i < count; i++) {
Map<String, String> types = alltypes.get(i);
for (int v = 0; v < types.size() - 1; v++) {
// was c1
ty.put("c" + u, types.get("c" + (v + 1)));
select.put("c" + u, new Pair<>(n.string + "temp" + i, "c" + (v + 1)));
u++;
}
from.put(n.string + "temp" + i, out + "_" + n.string + "temp" + i);
where.add(new Pair<>(new Pair<>(n.string + "temp" + i, "c0"), new Pair<>(in + "_" + n.string, "c0")));
}
if (select.isEmpty()) {
throw new RuntimeException("No observable for " + n.string);
}
Flower j = new Flower(select, from, where);
ret.add(new CreateTable(out + "_" + n.string + "_observables", ty, false));
ret.add(new InsertSQL2(out + "_" + n.string + "_observables", j, new LinkedList<>(j.select.keySet())));
if (relationalize) {
ret.addAll(relationalize(select, from, where, sig, out, ty, n, u, edge_types));
}
for (int count0 = 0; count0 < count; count0++) {
ret.add(new DropTable(out + "_" + n.string + "temp" + count0));
ret.add(new DropTable(out + "_" + n.string + "tempNoAttrs" + count0));
}
}
return new Pair<>(attrs, ret);
}
use of catdata.fql.decl.Path 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.Path in project fql by CategoricalData.
the class ExpPSM method exec.
@Override
public void exec(PSMInterp interp, Map<String, Set<Map<Object, Object>>> state) {
try {
Instance Ix = new Instance(sig, PSMGen.gather(I, sig, state));
Instance Jx = new Instance(sig, PSMGen.gather(J, sig, state));
IntRef idx = new IntRef(interp.guid);
Quad<Instance, Map<Pair<Node, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>, Triple<Instance, Map<Node, Map<Object, Pair<Arr<Node, Path>, Object>>>, Map<Node, Map<Pair<Arr<Node, Path>, Object>, Object>>>>, Map<Node, Map<Object, Pair<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Transform>>>, Map<Node, Map<Pair<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Transform>, Object>>> ret = Instance.exp2(idx, Ix, Jx);
interp.guid = idx.i;
interp.exps2.put(pre, ret);
PSMGen.shred(pre, ret.first, state);
} catch (FQLException fe) {
fe.printStackTrace();
throw new RuntimeException(fe.getLocalizedMessage());
}
}
Aggregations