Search in sources :

Example 96 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class FinCat method toSig.

/**
 * Converts a category to a signature.
 *
 * @return a signature and isomorphism
 * @throws FQLException
 */
public Quad<Signature, Pair<Map<Obj, String>, Map<String, Obj>>, Pair<Map<Arr<Obj, Arrow>, String>, Map<String, Arr<Obj, Arrow>>>, Pair<Map<Attribute<Obj>, String>, Map<String, Attribute<Obj>>>> toSig(Map<String, Type> types) throws FQLException {
    Map<Attribute<Obj>, String> attM = new HashMap<>();
    Map<String, Attribute<Obj>> attM2 = new HashMap<>();
    int ax = 0;
    List<String> objs = new LinkedList<>();
    List<Triple<String, String, String>> attrs0 = new LinkedList<>();
    int i = 0;
    Map<String, Obj> objM = new HashMap<>();
    Map<Obj, String> objM2 = new HashMap<>();
    for (Obj o : objects) {
        objM2.put(o, "obj" + i);
        objM.put("obj" + i, o);
        objs.add("obj" + i);
        i++;
    }
    if (attrs != null) {
        for (Attribute<Obj> att : attrs) {
            attM.put(att, "attrib" + ax);
            attM2.put("attrib" + ax, att);
            attrs0.add(new Triple<>("attrib" + ax++, objM2.get(att.source), att.target.toString()));
        }
    }
    List<Triple<String, String, String>> arrs = new LinkedList<>();
    int j = 0;
    Map<String, Arr<Obj, Arrow>> arrM = new HashMap<>();
    Map<Arr<Obj, Arrow>, String> arrM2 = new HashMap<>();
    for (Arr<Obj, Arrow> a : arrows) {
        if (isId(a)) {
            continue;
        }
        arrM.put("arrow" + j, a);
        arrM2.put(a, "arrow" + j);
        arrs.add(new Triple<>(arrM2.get(a), objM2.get(a.src), objM2.get(a.dst)));
        j++;
    }
    LinkedList<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
    for (Pair<Arr<Obj, Arrow>, Arr<Obj, Arrow>> k : composition.keySet()) {
        Arr<Obj, Arrow> v = composition.get(k);
        String s = arrM2.get(k.first);
        String t = arrM2.get(k.second);
        String u = arrM2.get(v);
        String ob = objM2.get(v.src);
        List<String> lhs = new LinkedList<>();
        List<String> rhs = new LinkedList<>();
        lhs.add(ob);
        rhs.add(ob);
        if (s != null) {
            lhs.add(s);
        }
        if (t != null) {
            lhs.add(t);
        }
        if (u != null) {
            rhs.add(u);
        }
        if (!lhs.equals(rhs)) {
            eqs.add(new Pair<>(lhs, rhs));
        }
    }
    Signature ret2 = new Signature(types, objs, attrs0, arrs, eqs);
    Quad<Signature, Pair<Map<Obj, String>, Map<String, Obj>>, Pair<Map<Arr<Obj, Arrow>, String>, Map<String, Arr<Obj, Arrow>>>, Pair<Map<Attribute<Obj>, String>, Map<String, Attribute<Obj>>>> retret = new Quad<>(ret2, new Pair<>(objM2, objM), new Pair<>(arrM2, arrM), new Pair<>(attM, attM2));
    return retret;
}
Also used : Quad(catdata.Quad) Attribute(catdata.fql.decl.Attribute) HashMap(java.util.HashMap) Pair(catdata.Pair) LinkedList(java.util.LinkedList) Triple(catdata.Triple) Signature(catdata.fql.decl.Signature)

Example 97 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class SigExpChecker method visit.

@Override
public SigExp visit(FQLProgram env, Union e) {
    SigExp lt = e.l.typeOf(env);
    SigExp rt = e.r.typeOf(env);
    if (!(lt instanceof Const)) {
        throw new RuntimeException(e.l + " does not have constant schema, has " + lt);
    }
    if (!(rt instanceof Const)) {
        throw new RuntimeException(e.r + " does not have constant schema, has " + lt);
    }
    Const lt0 = (Const) lt;
    Const rt0 = (Const) rt;
    Set<String> nodes = new HashSet<>(lt0.nodes);
    nodes.addAll(rt0.nodes);
    Set<Triple<String, String, String>> attrs = new HashSet<>(lt0.attrs);
    attrs.addAll(rt0.attrs);
    Set<Triple<String, String, String>> arrows = new HashSet<>(lt0.arrows);
    arrows.addAll(rt0.arrows);
    Set<Pair<List<String>, List<String>>> eqs = new HashSet<>(lt0.eqs);
    eqs.addAll(rt0.eqs);
    return new Const(new LinkedList<>(nodes), new LinkedList<>(attrs), new LinkedList<>(arrows), new LinkedList<>(eqs));
}
Also used : Triple(catdata.Triple) Const(catdata.fql.decl.SigExp.Const) HashSet(java.util.HashSet) Pair(catdata.Pair)

Example 98 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class Signature method toED.

public static List<EmbeddedDependency> toED(String pre, Pair<Signature, List<Pair<Attribute<Node>, Pair<Edge, Attribute<Node>>>>> xxx) {
    Signature sig = xxx.first;
    // public List<EmbeddedDependency> toED(String pre) {
    List<EmbeddedDependency> ret = new LinkedList<>();
    int v = 0;
    for (Node n : sig.nodes) {
        List<String> forall = new LinkedList<>();
        List<String> exists = new LinkedList<>();
        List<Triple<String, String, String>> where = new LinkedList<>();
        List<Triple<String, String, String>> tgd = new LinkedList<>();
        List<Pair<String, String>> egd = new LinkedList<>();
        // List<Triple<String, String, String>> not = new LinkedList<>();
        String u = "v" + (v++);
        String w = "v" + (v++);
        forall.add(u);
        forall.add(w);
        where.add(new Triple<>(pre + n.string, u, w));
        egd.add(new Pair<>(u, w));
        EmbeddedDependency ed = new EmbeddedDependency(forall, exists, where, tgd, egd);
        ret.add(ed);
    }
    for (Edge e : sig.edges) {
        List<String> forall = new LinkedList<>();
        List<String> exists = new LinkedList<>();
        List<Triple<String, String, String>> where = new LinkedList<>();
        List<Triple<String, String, String>> tgd = new LinkedList<>();
        List<Pair<String, String>> egd = new LinkedList<>();
        // List<Triple<String, String, String>> not = new LinkedList<>();
        String u = "v" + (v++);
        String w = "v" + (v++);
        forall.add(u);
        forall.add(w);
        where.add(new Triple<>(pre + e.name, u, w));
        tgd.add(new Triple<>(pre + e.source.string, u, u));
        tgd.add(new Triple<>(pre + e.target.string, w, w));
        EmbeddedDependency ed = new EmbeddedDependency(forall, exists, where, tgd, egd);
        ret.add(ed);
        forall = new LinkedList<>();
        exists = new LinkedList<>();
        where = new LinkedList<>();
        tgd = new LinkedList<>();
        egd = new LinkedList<>();
        // not = new LinkedList<>();
        String x = "v" + (v++);
        forall.add(u);
        forall.add(w);
        forall.add(x);
        where.add(new Triple<>(pre + e.name, u, w));
        where.add(new Triple<>(pre + e.name, u, x));
        egd.add(new Pair<>(w, x));
        ed = new EmbeddedDependency(forall, exists, where, tgd, egd);
        ret.add(ed);
        forall = new LinkedList<>();
        exists = new LinkedList<>();
        where = new LinkedList<>();
        tgd = new LinkedList<>();
        egd = new LinkedList<>();
        // not = new LinkedList<>();
        forall.add(x);
        where.add(new Triple<>(pre + e.source.string, x, x));
        String z = "v" + (v++);
        exists.add(z);
        tgd.add(new Triple<>(pre + e.name, x, z));
        ed = new EmbeddedDependency(forall, exists, where, tgd, egd);
        ret.add(ed);
    }
    for (Attribute<Node> e : sig.attrs) {
        List<String> forall = new LinkedList<>();
        List<String> exists = new LinkedList<>();
        List<Triple<String, String, String>> where = new LinkedList<>();
        List<Triple<String, String, String>> tgd = new LinkedList<>();
        List<Pair<String, String>> egd = new LinkedList<>();
        // List<Triple<String, String, String>> not = new LinkedList<>();
        String u = "v" + (v++);
        String w = "v" + (v++);
        forall.add(u);
        forall.add(w);
        where.add(new Triple<>(pre + e.name, u, w));
        tgd.add(new Triple<>(pre + e.source.string, u, u));
        // tgd.add(new Triple<>(name0 + "." + e.target., w, w));
        EmbeddedDependency ed = new EmbeddedDependency(forall, exists, where, tgd, egd);
        ret.add(ed);
        forall = new LinkedList<>();
        exists = new LinkedList<>();
        where = new LinkedList<>();
        tgd = new LinkedList<>();
        egd = new LinkedList<>();
        // not = new LinkedList<>();
        String x = "v" + (v++);
        forall.add(u);
        forall.add(w);
        forall.add(x);
        where.add(new Triple<>(pre + e.name, u, w));
        where.add(new Triple<>(pre + e.name, u, x));
        egd.add(new Pair<>(w, x));
        ed = new EmbeddedDependency(forall, exists, where, tgd, egd);
        ret.add(ed);
        forall = new LinkedList<>();
        exists = new LinkedList<>();
        where = new LinkedList<>();
        tgd = new LinkedList<>();
        egd = new LinkedList<>();
        // not = new LinkedList<>();
        forall.add(x);
        where.add(new Triple<>(pre + e.source.string, x, x));
        String z = "v" + (v++);
        exists.add(z);
        tgd.add(new Triple<>(pre + e.name, x, z));
        ed = new EmbeddedDependency(forall, exists, where, tgd, egd);
        ret.add(ed);
    }
    for (Eq eq : sig.eqs) {
        // TODO phokion
        ret.add(EmbeddedDependency.eq2(pre, eq.lhs, eq.rhs));
    }
    for (Pair<Attribute<Node>, Pair<Edge, Attribute<Node>>> eq : xxx.second) {
        ret.add(EmbeddedDependency.eq3(pre, eq));
    }
    return ret;
}
Also used : LinkedList(java.util.LinkedList) Paint(java.awt.Paint) Triple(catdata.Triple) EmbeddedDependency(catdata.fql.sql.EmbeddedDependency) Pair(catdata.Pair)

Example 99 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XCtx method makeTables.

// private Map<C, String> xgrid;
// public JComponent getGrid(C c) {
// if (xgrid != null) {
// return xgrid.get(c);
// }
// if (DEBUG.debug.x_tables) {
// //			makeTables(x -> cat().arrows(), new HashSet<>());
// return getGrid(c);
// }
// return new JPanel();
// }
// have this suppress pair IDs when possible
@SuppressWarnings({ "unchecked", "rawtypes" })
private JComponent makeTables(Function<Unit, Collection<Triple<C, C, List<C>>>> fn, Set<C> ignore) {
    cl = new CardLayout();
    // xgrid = new HashMap<>();
    clx = new JPanel();
    clx.setLayout(cl);
    clx.add(new JPanel(), "0");
    cl.show(clx, "0");
    // xgrid.put((C)"_1", "0");
    int www = 1;
    try {
        // Category<C, Triple<C, C, List<C>>> cat = cat();
        List<JComponent> grid = new LinkedList<>();
        Collection<Triple<C, C, List<C>>> cat = fn.apply(new Unit());
        // Map<C, Set<List<C>>> entities = new HashMap<>();
        Map entities = new HashMap<>();
        Map<C, Set<C>> m = new HashMap<>();
        for (C c : allIds()) {
            entities.put(c, new HashSet<>());
            m.put(c, new HashSet<>());
        }
        for (Triple<C, C, List<C>> k : cat) {
            if (k.first.equals("_1")) {
                // uncomment causes exception
                Set set = (Set<List<C>>) entities.get(k.second);
                // set.add(k);
                set.add(k.third);
            }
        }
        for (C c : allTerms()) {
            Pair<C, C> t = type(c);
            Set<C> set = m.get(t.first);
            set.add(c);
        }
        List<C> keys = new LinkedList<>(m.keySet());
        keys.sort((Object o1, Object o2) -> ((Comparable) o1).compareTo(o2));
        for (C c : keys) {
            if (c.equals("_1")) {
                continue;
            }
            if (ignore.contains(c)) {
                continue;
            }
            Pair<C, C> t = type(c);
            Set<List<C>> src = (Set<List<C>>) entities.get(t.first);
            List<C> cols = new LinkedList<>(m.get(c));
            cols = cols.stream().filter(x -> !x.toString().startsWith("!")).collect(Collectors.toList());
            Object[][] rowData = new Object[src.size()][cols.size()];
            int idx = cols.indexOf(c);
            if (idx != -1) {
                C old = cols.get(0);
                cols.set(0, c);
                cols.set(idx, old);
                if (cols.size() > 1) {
                    List<C> colsX = new LinkedList<>(cols.subList(1, cols.size()));
                    colsX.sort(null);
                    colsX.add(0, c);
                    cols = colsX;
                }
            }
            List<String> colNames3 = cols.stream().map(x -> type(x).second.equals(x) ? x.toString() : x + " (" + type(x).second + ")").collect(Collectors.toList());
            Object[] colNames = colNames3.toArray();
            int row = 0;
            for (List<C> l : src) {
                rowData[row][0] = l;
                int cl = 0;
                for (C col : cols) {
                    List<C> r = new LinkedList<>(l);
                    r.add(col);
                    for (Triple<C, C, List<C>> cand : cat) {
                        if (!cand.first.equals("_1")) {
                            continue;
                        }
                        if (!cand.second.equals(type(col).second)) {
                            continue;
                        }
                        if (kb.equiv(cand.third, r)) {
                            rowData[row][cl] = abbrPrint(cand.third);
                            break;
                        }
                    }
                    cl++;
                }
                row++;
            }
            JPanel table = GuiUtil.makeTable(BorderFactory.createEtchedBorder(), c + " (" + src.size() + ") rows", rowData, colNames);
            JPanel table2 = GuiUtil.makeTable(BorderFactory.createEtchedBorder(), c + " (" + src.size() + ") rows", rowData, colNames);
            // xgrid.put(c, Integer.toString(www));
            clx.add(new JScrollPane(table2), Integer.toString(www));
            www++;
            grid.add(table);
        }
        return GuiUtil.makeGrid(grid);
    } catch (Exception e) {
        e.printStackTrace();
        return new CodeTextPanel(BorderFactory.createEtchedBorder(), "", "ERROR\n\n" + e.getMessage());
    }
}
Also used : Color(java.awt.Color) CardLayout(java.awt.CardLayout) JSplitPane(javax.swing.JSplitPane) JTextField(javax.swing.JTextField) VisualizationViewer(edu.uci.ics.jung.visualization.VisualizationViewer) HashMap(java.util.HashMap) Function(java.util.function.Function) GridLayout(java.awt.GridLayout) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) XInst(catdata.fpql.XExp.XInst) Map(java.util.Map) JTabbedPane(javax.swing.JTabbedPane) Graph(edu.uci.ics.jung.graph.Graph) DirectedSparseMultigraph(edu.uci.ics.jung.graph.DirectedSparseMultigraph) LinkedList(java.util.LinkedList) Pair(catdata.Pair) Thue(catdata.provers.Thue) JComponent(javax.swing.JComponent) ItemEvent(java.awt.event.ItemEvent) Layout(edu.uci.ics.jung.algorithms.layout.Layout) JButton(javax.swing.JButton) CodeTextPanel(catdata.ide.CodeTextPanel) Iterator(java.util.Iterator) Collection(java.util.Collection) Util(catdata.Util) Category(catdata.fqlpp.cat.Category) Set(java.util.Set) BorderFactory(javax.swing.BorderFactory) Unit(catdata.Unit) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) JScrollPane(javax.swing.JScrollPane) GraphZoomScrollPane(edu.uci.ics.jung.visualization.GraphZoomScrollPane) Dimension(java.awt.Dimension) List(java.util.List) Paint(java.awt.Paint) Entry(java.util.Map.Entry) Triple(catdata.Triple) XSchema(catdata.fpql.XExp.XSchema) DefaultModalGraphMouse(edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse) JTextArea(javax.swing.JTextArea) FRLayout(edu.uci.ics.jung.algorithms.layout.FRLayout) GuiUtil(catdata.ide.GuiUtil) JPanel(javax.swing.JPanel) DefunctGlobalOptions(catdata.ide.DefunctGlobalOptions) Mode(edu.uci.ics.jung.visualization.control.ModalGraphMouse.Mode) JPanel(javax.swing.JPanel) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Unit(catdata.Unit) CodeTextPanel(catdata.ide.CodeTextPanel) LinkedList(java.util.LinkedList) List(java.util.List) JScrollPane(javax.swing.JScrollPane) CardLayout(java.awt.CardLayout) JComponent(javax.swing.JComponent) Paint(java.awt.Paint) LinkedList(java.util.LinkedList) Triple(catdata.Triple) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 100 with Triple

use of catdata.Triple in project fql by CategoricalData.

the class XCtx method attsFor.

private JPanel attsFor(Triple<C, C, List<C>> arr) {
    Map<C, String> tys = new HashMap<>();
    Map<C, String> vals = new HashMap<>();
    for (C c : schema.terms()) {
        Pair<C, C> t = schema.type(c);
        if (!t.first.equals(arr.second)) {
            continue;
        }
        if (schema.ids.contains(t.second)) {
            continue;
        }
        if (t.second.equals("_1")) {
            continue;
        }
        tys.put(c, t.second.toString());
        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);
        vals.put(c, abbrPrint(found.third));
    }
    Object[][] rowData = new Object[tys.keySet().size()][3];
    Object[] colNames = { "Attribute", "Type", "Value" };
    int i = 0;
    for (C c : tys.keySet()) {
        rowData[i][0] = c;
        rowData[i][1] = tys.get(c);
        rowData[i][2] = vals.get(c);
        i++;
    }
    String str = "Attributes for " + abbrPrint(arr.third) + " (" + rowData.length + ")";
    JPanel ret = new JPanel(new GridLayout(1, 1));
    ret.add(GuiUtil.makeTable(BorderFactory.createEmptyBorder(), str, rowData, colNames));
    return ret;
}
Also used : JPanel(javax.swing.JPanel) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) Paint(java.awt.Paint) Triple(catdata.Triple) GridLayout(java.awt.GridLayout) LinkedList(java.util.LinkedList) List(java.util.List)

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