use of catdata.fqlpp.cat.Category in project fql by CategoricalData.
the class XCtx method satcat.
private Category<C, Triple<C, C, List<C>>> satcat() {
Category<C, Triple<C, C, List<C>>> sch = schema.cat();
eqm = new HashMap<>();
Set<Triple<C, C, List<C>>> new_arrs = new HashSet<>();
for (C a : schema.allIds()) {
for (C v : types.keySet()) {
Pair<C, C> t = type(v);
C b = t.second;
if (b.equals("_1")) {
continue;
}
List<C> l = new LinkedList<>();
@SuppressWarnings("unchecked") C ccc = (C) ("!_" + a);
l.add(ccc);
l.add(v);
Triple<C, C, List<C>> arr = new Triple<>(a, b, l);
new_arrs.add(arr);
}
}
Set<Triple<C, C, List<C>>> arrs = new HashSet<>();
arrs.addAll(sch.arrows());
arrs.addAll(new_arrs);
Map<Pair<Triple<C, C, List<C>>, Triple<C, C, List<C>>>, Triple<C, C, List<C>>> comp_cache = new HashMap<>();
@SuppressWarnings("serial") Category<C, Triple<C, C, List<C>>> ret = new Category<C, Triple<C, C, List<C>>>() {
@Override
public Set<C> objects() {
return sch.objects();
}
@Override
public Set<Triple<C, C, List<C>>> arrows() {
return arrs;
}
@Override
public C source(Triple<C, C, List<C>> a) {
return a.first;
}
@Override
public C target(Triple<C, C, List<C>> a) {
return a.second;
}
@Override
public Triple<C, C, List<C>> identity(C o) {
return sch.identity(o);
}
@Override
public Triple<C, C, List<C>> compose(Triple<C, C, List<C>> f, Triple<C, C, List<C>> g) {
Pair<Triple<C, C, List<C>>, Triple<C, C, List<C>>> p = new Pair<>(f, g);
Triple<C, C, List<C>> ret = comp_cache.get(p);
if (ret != null) {
return ret;
}
ret = local_compose(f, g);
comp_cache.put(p, ret);
return ret;
}
@SuppressWarnings({ "rawtypes", "unchecked", "cast", "ConstantConditions" })
private Triple<C, C, List<C>> local_compose(Triple<C, C, List<C>> f, Triple<C, C, List<C>> g) {
if (!arrows().contains(f)) {
throw new RuntimeException(f.toString());
}
if (!arrows().contains(g)) {
throw new RuntimeException(g.toString());
}
if (!f.second.equals(g.first)) {
throw new RuntimeException("cannot compose " + f + " and " + g);
}
if (sch.hom(f.first, f.second).contains(f) && sch.hom(g.first, g.second).contains(g)) {
return sch.compose(f, g);
}
if (new_arrs.contains(f) && new_arrs.contains(g)) {
Pair<C, C> ft = new Pair<>(f.first, f.second);
Pair<C, C> gt = new Pair<>(g.first, g.second);
C a = ft.first;
C b = gt.first;
// C v = f.third.get(1);
C v0 = g.third.get(1);
if (schema.allIds().contains(a) && !b.equals("_1")) {
List<C> l = new LinkedList<>();
C ccc = (C) ("!_" + a);
l.add(ccc);
l.add(v0);
Triple<C, C, List<C>> ret = new Triple<>(a, type(v0).second, l);
if (!ret.first.equals(f.first) || !ret.second.equals(g.second)) {
throw new RuntimeException();
}
if (!arrows().contains(ret)) {
throw new RuntimeException(ret.toString());
}
return ret;
}
}
if (new_arrs.contains(f) && sch.arrows().contains(g)) {
if (g.third.isEmpty()) {
if (!f.first.equals(g.first) || !f.second.equals(g.second)) {
throw new RuntimeException();
}
if (!arrows().contains(f)) {
throw new RuntimeException(f.toString());
}
return f;
}
// C b = g.first;
C b0 = g.second;
C a = f.first;
C v = f.third.get(1);
if (b0.equals("_1") && a.equals("_1")) {
Triple ret = new Triple("_1", "_1", new LinkedList());
if (!ret.first.equals(f.first) || !ret.second.equals(g.second)) {
throw new RuntimeException();
}
if (!arrows().contains(ret)) {
throw new RuntimeException(ret.toString());
}
return ret;
}
if (b0.equals("_1") && !a.equals("_1")) {
List l = new LinkedList();
l.add("!_" + a);
Triple ret = new Triple(a, "_1", l);
if (!ret.first.equals(f.first) || !ret.second.equals(g.second)) {
throw new RuntimeException();
}
if (!arrows().contains(ret)) {
throw new RuntimeException(ret.toString());
}
return ret;
}
if (g.third.get(0).toString().startsWith("!") && !a.equals("_1")) {
List<C> l = new LinkedList();
l.add((C) ("!_" + a));
l.addAll(g.third.subList(1, g.third.size()));
Triple<C, C, List<C>> ret = new Triple<>(a, g.second, l);
ret = find_old(getKB(), ret, hom(ret.first, ret.second));
if (ret == null) {
throw new RuntimeException("Anomaly: please report");
}
if (!ret.first.equals(f.first) || !ret.second.equals(g.second)) {
throw new RuntimeException();
}
if (!arrows().contains(ret)) {
throw new RuntimeException(ret.toString());
}
return ret;
}
if (g.third.get(0).toString().startsWith("!") && a.equals("_1")) {
List<C> l = new LinkedList();
l.addAll(g.third.subList(1, g.third.size()));
Triple<C, C, List<C>> ret = new Triple<>(f.first, g.second, l);
if (!ret.first.equals(f.first) || !ret.second.equals(g.second)) {
throw new RuntimeException();
}
// must find equivalent - see CTDB example
ret = find_old(getKB(), ret, hom(ret.first, ret.second));
if (!arrows().contains(ret)) {
throw new RuntimeException("Anomaly: please report: " + ret);
}
return ret;
}
List<C> vl = new LinkedList<>();
vl.add(v);
Triple<C, C, List<C>> sofar = new Triple<>(type(v).first, type(v).second, vl);
List gnX = new LinkedList<>(g.third);
for (C gn : g.third) {
gnX.remove(0);
sofar = findEq(sofar, gn);
if (sch.arrows().contains(sofar)) {
List hhh = new LinkedList();
hhh.add("!_" + a);
hhh.addAll(sofar.third);
hhh.addAll(gnX);
Triple<C, C, List<C>> ret0 = new Triple<>(a, g.second, hhh);
Triple ret = find_old(schema.getKB(), ret0, sch.hom(ret0.first, ret0.second));
if (!arrows().contains(ret)) {
throw new RuntimeException("f " + f + " and " + g + "\n\nbad: " + ret + " not found inn\n\n" + Util.sep(arrows(), "\n"));
}
if (!ret.first.equals(f.first) || !ret.second.equals(g.second)) {
throw new RuntimeException();
}
return ret;
}
}
List<C> retl = new LinkedList<>();
retl.add((C) ("!_" + a));
retl.addAll(sofar.third);
Triple<C, C, List<C>> ret = new Triple<>(f.first, g.second, retl);
if (a.equals("_1") && global.allIds().contains(sofar.second) && global.cat().hom((C) "_1", sofar.second).contains(sofar)) {
if (!arrows().contains(sofar)) {
throw new RuntimeException(sofar.toString());
}
if (!sofar.first.equals(f.first) || !sofar.second.equals(g.second)) {
throw new RuntimeException();
}
return sofar;
}
if (!ret.first.equals(f.first) || !ret.second.equals(g.second)) {
throw new RuntimeException(ret + " not " + f + " and " + g);
}
// another one where have to use KB
ret = find_old(getKB(), ret, hom(ret.first, ret.second));
if (!arrows().contains(ret)) {
throw new RuntimeException("f " + f + " and " + g + "\n\nbad: " + ret + " not found inn\n\n" + Util.sep(arrows(), "\n"));
}
return ret;
}
if (sch.arrows().contains(f) && new_arrs.contains(g)) {
C a0 = f.first;
// C a = f.second;
C v = g.third.get(1);
List<C> l = new LinkedList<>();
l.add((C) ("!_" + a0));
l.add(v);
Triple<C, C, List<C>> ret = new Triple<>(a0, g.second, l);
if (!ret.first.equals(f.first) || !ret.second.equals(g.second)) {
throw new RuntimeException();
}
if (!arrows().contains(ret)) {
throw new RuntimeException(ret.toString());
}
return ret;
}
throw new RuntimeException("bottomed out: " + f + " and " + g + "\n" + sch.hom(f.first, f.second) + "\n" + sch.hom(g.first, g.second));
}
@SuppressWarnings({ "unchecked" })
private Triple<C, C, List<C>> findEq(Triple<C, C, List<C>> sofar, C gn) {
if (sofar.third.size() != 1) {
throw new RuntimeException("sofar third not length 1 is " + sofar);
}
C v = sofar.third.get(0);
List<C> tofind = new LinkedList<>();
tofind.add(v);
tofind.add(gn);
List<C> found = eqm.get(new Pair<>(v, gn));
// Pair<List<C>, List<C>> xxx = null;
for (Pair<List<C>, List<C>> eq : eqs) {
if (found != null) {
break;
}
if (eq.first.equals(tofind)) {
found = eq.second;
// xxx = eq;
break;
}
if (eq.second.equals(tofind)) {
found = eq.first;
// xxx = eq;
break;
}
}
eqm.put(new Pair<>(v, gn), found);
if (found == null) {
throw new RuntimeException("sofar " + sofar + " gn " + gn + "\n\n" + allEqs());
}
@SuppressWarnings("rawtypes") List l = new LinkedList<>();
l.addAll(found);
Triple<C, C, List<C>> ret = new Triple<>(type(found).first, type(found).second, l);
return ret;
}
};
// cache the composition table
if (DefunctGlobalOptions.debug.fpql.validate_amalgams) {
ret.validate();
}
return ret;
}
use of catdata.fqlpp.cat.Category in project fql by CategoricalData.
the class CatOps method visit.
@Override
public Functor visit(FQLPPProgram env, Iso e) {
Category l = e.l.accept(env, this);
Category r = e.r.accept(env, this);
Optional<Pair<Functor, Functor>> k = FinCat.iso(l, r);
if (!k.isPresent()) {
throw new RuntimeException("Not isomorphic: " + e.l + " and " + e.r);
}
return e.lToR ? k.get().first : k.get().second;
}
use of catdata.fqlpp.cat.Category in project fql by CategoricalData.
the class CatOps method visit.
@Override
public Functor visit(FQLPPProgram env, MapConst ic) {
Triple<Category, Category, Mapping<String, String, String, String>> xxx = toMapping(env, ic);
Mapping<String, String, String, String> I = xxx.third;
FUNCTION f = p0 -> {
Path p = (Path) p0;
return I.apply(p);
};
Functor et = new Functor(xxx.first, xxx.second, x -> I.nm.get(x), f);
et.mapping0 = xxx.third;
return et;
}
use of catdata.fqlpp.cat.Category in project fql by CategoricalData.
the class CatOps method visit.
@Override
public Functor visit(FQLPPProgram env, FinalConst ic) {
CatExp e = resolve(env, ic.src);
if (!(e instanceof Const)) {
throw new RuntimeException("Can only create functors from finitely-presented categories.");
}
Const c = (Const) e;
Category cat = c.accept(env, this);
Signature<String, String> sig = new Signature<>(c.nodes, c.arrows, c.eqs);
Category target = ic.C.accept(env, this);
Map<Node, Functor> nm = new HashMap<>();
for (Node n : sig.nodes) {
FunctorExp kkk = ic.nm.get(n.name);
if (kkk == null) {
throw new RuntimeException("Missing node mapping from " + n);
}
Functor F = kkk.accept(env, this);
nm.put(n, F);
}
Map<Edge, Transform> em = new HashMap<>();
for (Edge n : sig.edges) {
TransExp chc = ic.em.get(n.name);
if (chc == null) {
throw new RuntimeException("Missing edge mapping from " + n);
}
em.put(n, chc.accept(env, this));
}
FUNCTION fff = p0 -> {
Path p = (Path) p0;
Object fn = target.identity(nm.get(p.source));
for (Object nnn : p.path) {
Edge n = (Edge) nnn;
fn = target.compose(fn, em.get(n));
}
return fn;
};
return new Functor(cat, target, nm::get, fff);
}
use of catdata.fqlpp.cat.Category in project fql by CategoricalData.
the class FqlppDisplay method doFNView.
@SuppressWarnings("unchecked")
private <X, Y> JComponent doFNView(Functor fn, JPanel p, Color clr, Graph<X, Y> sgv) {
Layout<X, Y> layout = new FRLayout<>(sgv);
layout.setSize(new Dimension(600, 400));
VisualizationViewer<X, Y> vv = new VisualizationViewer<>(layout);
Function<X, Paint> vertexPaint = z -> clr;
DefaultModalGraphMouse<String, String> gm = new DefaultModalGraphMouse<>();
gm.setMode(Mode.TRANSFORMING);
vv.setGraphMouse(gm);
gm.setMode(Mode.PICKING);
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
Function fff = arg0 -> Util.nice(arg0.toString());
vv.getRenderContext().setVertexLabelTransformer(fff);
vv.getRenderContext().setEdgeLabelTransformer(fff);
vv.getPickedVertexState().addItemListener((ItemEvent e) -> {
if (e.getStateChange() != ItemEvent.SELECTED) {
return;
}
vv.getPickedEdgeState().clear();
X str = ((X) e.getItem());
Object y = fn.applyO(str);
p.removeAll();
if (y instanceof Category) {
Category ttt = (Category) y;
JPanel sss = showCat(ttt, getColor(ttt));
p.add(sss);
} else if (y instanceof Set) {
Set ttt = (Set) y;
JPanel sss = showSet(ttt, getColor(ttt));
p.add(sss);
} else if (y instanceof Functor) {
Functor ttt = (Functor) y;
JPanel sss = showFtr(ttt, getColor(ttt), null);
p.add(sss);
} else {
String sss = Util.nice(y.toString());
p.add(new CodeTextPanel(BorderFactory.createEtchedBorder(), null, sss));
}
p.revalidate();
});
vv.getPickedEdgeState().addItemListener((ItemEvent e) -> {
if (e.getStateChange() != ItemEvent.SELECTED) {
return;
}
vv.getPickedVertexState().clear();
X str = ((X) e.getItem());
Object y = fn.applyA(str);
p.removeAll();
if (y instanceof Functor) {
Functor ttt = (Functor) y;
JPanel sss = showFtr(ttt, getColor(ttt.source), null);
p.add(sss);
} else if (y instanceof Fn) {
Fn ttt = (Fn) y;
JPanel sss = showFn(ttt, getColor(ttt.source), getColor(ttt.target));
p.add(sss);
} else if (y instanceof Transform) {
Transform ttt = (Transform) y;
JPanel sss = showTrans(ttt, getColor(ttt.source));
p.add(sss);
} else {
String sss = Util.nice(y.toString());
p.add(new CodeTextPanel(BorderFactory.createEtchedBorder(), null, sss));
}
p.revalidate();
});
GraphZoomScrollPane zzz = new GraphZoomScrollPane(vv);
JPanel ret = new JPanel(new GridLayout(1, 1));
ret.add(zzz);
ret.setBorder(BorderFactory.createEtchedBorder());
return ret;
}
Aggregations