Search in sources :

Example 1 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XCtx method foo.

private List<Triple<C, C, List<C>>> foo() {
    // try {
    List<Triple<C, C, List<C>>> paths = new LinkedList<>();
    for (C c : allIds()) {
        paths.add(new Triple<>(c, c, new LinkedList<>()));
    }
    List<Triple<C, C, List<C>>> consts = new LinkedList<>();
    for (C c : global.terms()) {
        if (!global.ids.contains(c)) {
            if (global.type(c).first.equals("_1")) {
                List<C> l = new LinkedList<>();
                l.add(c);
                consts.add(new Triple<>(global.type(c).first, global.type(c).second, l));
            }
        }
    }
    int iter = 0;
    for (; iter < DefunctGlobalOptions.debug.fpql.MAX_PATH_LENGTH; iter++) {
        Set<Triple<C, C, List<C>>> newPaths1 = extend2(paths, global.types, consts);
        paths.addAll(newPaths1);
        Set<Triple<C, C, List<C>>> newPaths2 = extend2(paths, schema.types, consts);
        paths.addAll(newPaths2);
        Set<Triple<C, C, List<C>>> newPaths3 = extend2(paths, types, consts);
        if (paths.containsAll(newPaths3)) {
            // need one more iteration for all attributes
            newPaths1 = extend2(paths, global.types, consts);
            paths.addAll(newPaths1);
            newPaths2 = extend2(paths, schema.types, consts);
            paths.addAll(newPaths2);
            break;
        }
        paths.addAll(newPaths3);
    }
    if (iter == DefunctGlobalOptions.debug.fpql.MAX_PATH_LENGTH) {
        throw new RuntimeException("Exceeded maximum path length");
    }
    // new FQLTextPanel(BorderFactory.createEtchedBorder(),
    return paths;
// "", paths.toString());
// } catch (Exception e) {
// e.printStackTrace();
// return new FQLTextPanel(BorderFactory.createEtchedBorder(), "",
// "ERROR: " + e.getMessage());
// }
}
Also used : Triple(catdata.Triple) LinkedList(java.util.LinkedList) Paint(java.awt.Paint)

Example 2 with Triple

use of catdata.Triple 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;
}
Also used : Paint(java.awt.Paint) LinkedList(java.util.LinkedList) Triple(catdata.Triple) DirectedSparseMultigraph(edu.uci.ics.jung.graph.DirectedSparseMultigraph) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair)

Example 3 with Triple

use of catdata.Triple 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;
}
Also used : JPanel(javax.swing.JPanel) ItemEvent(java.awt.event.ItemEvent) JTextArea(javax.swing.JTextArea) FRLayout(edu.uci.ics.jung.algorithms.layout.FRLayout) GridLayout(java.awt.GridLayout) VisualizationViewer(edu.uci.ics.jung.visualization.VisualizationViewer) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair) DefaultModalGraphMouse(edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse) GraphZoomScrollPane(edu.uci.ics.jung.visualization.GraphZoomScrollPane) Dimension(java.awt.Dimension) Paint(java.awt.Paint) Triple(catdata.Triple) JSplitPane(javax.swing.JSplitPane)

Example 4 with Triple

use of catdata.Triple 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;
}
Also used : Category(catdata.fqlpp.cat.Category) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) Triple(catdata.Triple) LinkedList(java.util.LinkedList) List(java.util.List) HashSet(java.util.HashSet) Pair(catdata.Pair)

Example 5 with Triple

use of catdata.Triple 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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) Triple(catdata.Triple) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(catdata.Pair) HashSet(java.util.HashSet)

Aggregations

Triple (catdata.Triple)116 Pair (catdata.Pair)93 LinkedList (java.util.LinkedList)84 List (java.util.List)75 HashMap (java.util.HashMap)65 Map (java.util.Map)49 HashSet (java.util.HashSet)47 LinkedHashMap (java.util.LinkedHashMap)36 Set (java.util.Set)28 Chc (catdata.Chc)22 Util (catdata.Util)18 En (catdata.aql.exp.SchExpRaw.En)18 Ty (catdata.aql.exp.TyExpRaw.Ty)18 Ctx (catdata.Ctx)17 Sym (catdata.aql.exp.TyExpRaw.Sym)17 Collectors (java.util.stream.Collectors)17 Att (catdata.aql.exp.SchExpRaw.Att)16 Fk (catdata.aql.exp.SchExpRaw.Fk)16 Tuple3 (org.jparsec.functors.Tuple3)16 Quad (catdata.Quad)13