use of catdata.Pair in project fql by CategoricalData.
the class XCtx method elemGraph.
private Graph<Triple<C, C, List<C>>, Pair<Integer, C>> elemGraph() {
Graph<Triple<C, C, List<C>>, Pair<Integer, C>> g = new DirectedSparseMultigraph<>();
@SuppressWarnings("unchecked") C ccc = (C) "_1";
for (Triple<C, C, List<C>> arr : cat().arrowsFrom(ccc)) {
if (global.ids.contains(arr.second)) {
continue;
}
if (arr.second.equals("_1")) {
continue;
}
g.addVertex(arr);
}
int i = 0;
for (Triple<C, C, List<C>> arr : cat().arrowsFrom(ccc)) {
if (global.ids.contains(arr.second)) {
continue;
}
if (cat().isId(arr)) {
continue;
}
if (arr.second.equals("_1")) {
continue;
}
for (C c : schema.terms()) {
Pair<C, C> t = schema.type(c);
if (!t.first.equals(arr.second)) {
continue;
}
if (global.ids.contains(t.second)) {
continue;
}
if (t.second.equals("_1")) {
continue;
}
if (schema.ids.contains(c)) {
continue;
}
List<C> l = new LinkedList<>(arr.third);
l.add(c);
Triple<C, C, List<C>> tofind = new Triple<>(arr.first, t.second, l);
Triple<C, C, List<C>> found = find_fast(tofind);
g.addEdge(new Pair<>(i++, c), arr, found);
}
}
return g;
}
use of catdata.Pair in project fql by CategoricalData.
the class XCtx method makeElems.
private JComponent makeElems() {
Graph<Triple<C, C, List<C>>, Pair<Integer, C>> sgv = elemGraph();
if (sgv.getVertexCount() > 64) {
return new JTextArea("Too large to display");
}
initColors();
Layout<Triple<C, C, List<C>>, Pair<Integer, C>> layout = new FRLayout<>(sgv);
layout.setSize(new Dimension(600, 400));
VisualizationViewer<Triple<C, C, List<C>>, Pair<Integer, C>> vv = new VisualizationViewer<>(layout);
vv.getRenderContext().setLabelOffset(20);
DefaultModalGraphMouse<String, String> gm = new DefaultModalGraphMouse<>();
gm.setMode(Mode.TRANSFORMING);
vv.setGraphMouse(gm);
gm.setMode(Mode.PICKING);
JPanel botPanel = new JPanel(new GridLayout(1, 1));
com.google.common.base.Function<Triple<C, C, List<C>>, Paint> vertexPaint = (Triple<C, C, List<C>> x) -> colorMap.get(x.second);
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
vv.getPickedVertexState().addItemListener((ItemEvent e) -> {
if (e.getStateChange() != ItemEvent.SELECTED) {
return;
}
vv.getPickedEdgeState().clear();
@SuppressWarnings("unchecked") Triple<C, C, List<C>> arr = (Triple<C, C, List<C>>) e.getItem();
// cl.show(clx, xgrid.get(str));
JPanel foo = attPanels.computeIfAbsent(arr, k -> attsFor(arr));
botPanel.removeAll();
botPanel.add(foo);
botPanel.revalidate();
});
com.google.common.base.Function<Triple<C, C, List<C>>, String> ttt = (Triple<C, C, List<C>> arg0) -> {
// .toString();
String ret = abbrPrint(arg0.third);
return (ret.length() > 32) ? ret.substring(0, 31) + "..." : ret;
};
com.google.common.base.Function<Pair<Integer, C>, String> ttt2 = (Pair<Integer, C> arg0) -> arg0.second.toString();
vv.getRenderContext().setVertexLabelTransformer(ttt);
vv.getRenderContext().setEdgeLabelTransformer(ttt2);
GraphZoomScrollPane zzz = new GraphZoomScrollPane(vv);
JPanel ret = new JPanel(new GridLayout(1, 1));
ret.add(zzz);
ret.setBorder(BorderFactory.createEtchedBorder());
JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
// setDividerLocation(.9d);
jsp.setResizeWeight(.8d);
jsp.setDividerSize(4);
jsp.add(ret);
jsp.add(botPanel);
return jsp;
}
use of catdata.Pair 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.Pair in project fql by CategoricalData.
the class XCtx method make.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static XCtx<String> make(XCtx<String> S, XInst I) {
// Set<String> seen = new HashSet<>();
Map t = new HashMap<>();
Set e = new HashSet<>();
for (Pair<String, String> k : I.nodes) {
if (k.second.equals("_1")) {
throw new RuntimeException("Cannot create unit variable");
}
if (!S.types.containsKey(k.second) && !S.global.types.containsKey(k.second)) {
throw new RuntimeException("Unknown node/type: " + k.second);
}
if (t.containsKey(k.first)) {
if (t.get(k.first).equals(new Pair<>("_1", k.second))) {
throw new RuntimeException("Duplicate name: " + k.first);
}
}
if (S.types.containsKey(k.first)) {
throw new RuntimeException("Name of variable is also in schema " + k);
}
if (S.global.types.containsKey(k.first)) {
throw new RuntimeException("Name of variable is also global " + k);
}
t.put(k, new Pair<>("_1", k.second));
}
XCtx tmp = new XCtx(new HashSet<>(), t, e, S.global, S, "instance");
for (Pair<List<String>, List<String>> k : I.eqs) {
// : must expand paths
// : supress variable check for now
Set s = new HashSet<>();
s.add(new LinkedList<>());
List<List> lhs = new LinkedList<>(expand(s, k.first, S, tmp));
List<List> rhs = new LinkedList<>(expand(s, k.second, S, tmp));
if (lhs.size() == 1 && rhs.size() > 1) {
List rhsX = new LinkedList<>();
List x = new LinkedList();
x.add(new Pair<>(((Pair) rhs.get(0).get(0)).first, tmp.type(lhs.get(0)).second));
rhsX.add(x);
rhs = rhsX;
} else if (rhs.size() == 1 && lhs.size() > 1) {
List lhsX = new LinkedList<>();
List x = new LinkedList();
x.add(new Pair<>(((Pair) lhs.get(0).get(0)).first, tmp.type(rhs.get(0)).second));
lhsX.add(x);
lhs = lhsX;
}
if (rhs.isEmpty()) {
throw new RuntimeException("In equation " + Util.sep(k.first, ".") + " = " + Util.sep(k.second, ".") + ", the right hand side refers to non-existent terms. You should probably add terms at the global or instance level.");
}
if (lhs.isEmpty()) {
throw new RuntimeException("In equation " + Util.sep(k.first, ".") + " = " + Util.sep(k.second, ".") + ", the left hand side refers to non-existent terms. You should probably add terms at the global or instance level.");
}
if (rhs.size() > 1) {
throw new RuntimeException("In equation " + Util.sep(k.first, ".") + " = " + Util.sep(k.second, ".") + ", the right hand is ambiguous, and could mean any of {" + printDirty(rhs) + "}");
}
if (lhs.size() > 1) {
throw new RuntimeException("In equation " + Util.sep(k.first, ".") + " = " + Util.sep(k.second, ".") + ", the left hand is ambiguous, and could mean any of {" + printDirty(lhs) + "}");
}
e.add(new Pair<>(new LinkedList<>(lhs).get(0), new LinkedList<>(rhs).get(0)));
}
// s.parent = s_old;
// s.validate();
// s.init();
XCtx<String> ret = new XCtx<>(new HashSet<>(), t, e, S.global, S, "instance");
ret.saturated = I.saturated;
ret.shouldAbbreviate = true;
return ret;
}
use of catdata.Pair in project fql by CategoricalData.
the class XCtx method rel.
@SuppressWarnings({ "rawtypes", "unchecked" })
public XCtx<C> rel() {
Set<Pair<List<C>, List<C>>> new_eqs = new HashSet<>();
if (schema == null) {
throw new RuntimeException("Problem with relationalize");
}
Map new_types = new HashMap<>();
// Set<Map<Triple<C, C, List<C>>, Triple<C, C, List<C>>>> gens = new HashSet<>();
Map<Map<Triple<C, C, List<C>>, Triple<C, C, List<C>>>, Triple<C, C, List<C>>> genMap = new HashMap<>();
Map<Triple<C, C, List<C>>, Map<Triple<C, C, List<C>>, Triple<C, C, List<C>>>> genMap2 = new HashMap<>();
for (C c : schema.ids) {
for (Triple<C, C, List<C>> i : cat().hom((C) "_1", c)) {
Map<Triple<C, C, List<C>>, Triple<C, C, List<C>>> o = obs(i);
new_types.put(o, new Pair("_1", c));
genMap.put(o, i);
genMap2.put(i, o);
}
}
for (C c : schema.global.ids) {
for (Triple<C, C, List<C>> i : cat().hom((C) "_1", c)) {
if (schema.global.cat().hom((C) "_1", c).contains(i)) {
continue;
}
Map<Triple<C, C, List<C>>, Triple<C, C, List<C>>> o = new HashMap<>();
o.put(new Triple<>(c, c, new LinkedList<>()), i);
new_types.put(o, new Pair("_1", c));
genMap.put(o, i);
genMap2.put(i, o);
}
}
for (Object iX : new_types.keySet()) {
Map<Triple<C, C, List<C>>, Triple<C, C, List<C>>> o = (Map<Triple<C, C, List<C>>, Triple<C, C, List<C>>>) iX;
Triple<C, C, List<C>> i = genMap.get(o);
for (C e : allTerms()) {
if (!type(e).first.equals(i.second)) {
continue;
}
List<C> lhs = new LinkedList<>();
lhs.add((C) o);
lhs.add(e);
List<C> tofind = new LinkedList<>();
tofind.addAll(i.third);
tofind.add(e);
Triple<C, C, List<C>> rhs0 = new Triple<>(i.first, type(e).second, tofind);
Triple<C, C, List<C>> rhsX = find_fast(rhs0);
if (rhsX == null) {
throw new RuntimeException();
}
List<C> rhs = new LinkedList<>();
Map<Triple<C, C, List<C>>, Triple<C, C, List<C>>> o2 = genMap2.get(rhsX);
if (o2 == null) {
if (rhsX.third.isEmpty()) {
rhs.add(rhsX.first);
} else {
rhs.addAll(rhsX.third);
}
} else {
rhs.add((C) o2);
}
new_eqs.add(new Pair<>(lhs, rhs));
}
}
XCtx<C> ret = new XCtx<>(new HashSet<>(), new_types, new_eqs, global, schema, kind);
ret.saturated = true;
return ret;
}
Aggregations