Search in sources :

Example 6 with Edge

use of catdata.fql.decl.Edge 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());
    }
}
Also used : Arr(catdata.fql.cat.Arr) Attribute(catdata.fql.decl.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FinCat(catdata.fql.cat.FinCat) Node(catdata.fql.decl.Node) LinkedHashMap(java.util.LinkedHashMap) FQLException(catdata.fql.FQLException) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) Path(catdata.fql.decl.Path) Const(catdata.fql.decl.InstExp.Const) Fn(catdata.fql.Fn) LinkedList(java.util.LinkedList) Triple(catdata.Triple) Signature(catdata.fql.decl.Signature) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Edge(catdata.fql.decl.Edge)

Example 7 with Edge

use of catdata.fql.decl.Edge in project fql by CategoricalData.

the class EmbeddedDependency method matrix.

@SuppressWarnings("unused")
private static List<String> matrix(String pre, Path p) {
    List<String> ret = new LinkedList<>();
    int i = 0;
    for (Edge e : p.path) {
        ret.add(pre + i++);
    }
    return ret;
}
Also used : Edge(catdata.fql.decl.Edge) LinkedList(java.util.LinkedList)

Example 8 with Edge

use of catdata.fql.decl.Edge in project fql by CategoricalData.

the class FullSigmaCounit method exec.

@Override
public void exec(PSMInterp interp, Map<String, Set<Map<Object, Object>>> state) {
    Set<Map<Object, Object>> lineage = state.get(i3 + "_lineage");
    for (Node n : F.target.nodes) {
        Set<Map<Object, Object>> i3i = state.get(i3 + "_" + n);
        Set<Map<Object, Object>> m = new HashSet<>();
        for (Map<Object, Object> row : i3i) {
            Object id = row.get("c0").toString();
            for (Map<Object, Object> v : lineage) {
                Object id0 = v.get("c0").toString();
                if (id.equals(id0)) {
                    String node = v.get("c1").toString();
                    String idX = v.get("c2").toString();
                    String[] cols = v.get("c3").toString().split("\\s+");
                    Set<Map<Object, Object>> subst_inv = state.get(i2 + "_" + node + "_subst_inv");
                    for (Map<Object, Object> y : subst_inv) {
                        if (y.get("c0").toString().equals(idX)) {
                            String ret = y.get("c1").toString();
                            for (String col : cols) {
                                if (col.trim().isEmpty()) {
                                    continue;
                                }
                                Set<Map<Object, Object>> u = state.get(i1 + "_" + col);
                                for (Map<Object, Object> e : u) {
                                    if (e.get("c0").toString().equals(ret)) {
                                        ret = e.get("c1").toString();
                                    }
                                }
                            }
                            Map<Object, Object> rowX = new HashMap<>();
                            rowX.put("c0", id);
                            rowX.put("c1", ret);
                            m.add(rowX);
                        }
                    }
                }
            }
        }
        state.put(trans + "_" + n, m);
    }
    for (Attribute<Node> n : F.target.attrs) {
        state.put(trans + "_" + n.name, new HashSet<>());
    }
    for (Edge n : F.target.edges) {
        state.put(trans + "_" + n.name, new HashSet<>());
    }
}
Also used : HashMap(java.util.HashMap) Node(catdata.fql.decl.Node) Map(java.util.Map) HashMap(java.util.HashMap) Edge(catdata.fql.decl.Edge) HashSet(java.util.HashSet)

Example 9 with Edge

use of catdata.fql.decl.Edge in project fql by CategoricalData.

the class FullSigma method exec.

@Override
public void exec(PSMInterp interp, Map<String, Set<Map<Object, Object>>> state) {
    Signature C = f.source;
    Signature D = f.target;
    List<Pair<String, List<Pair<Object, Object>>>> I0 = PSMGen.gather(inst, C, state);
    try {
        Instance I = new Instance(C, I0);
        interp.sigmas.put(pre, interp.guid);
        Quad<Instance, Map<Node, Map<Object, Integer>>, Map<Node, Map<Integer, Object>>, Map<Object, List<Pair<String, Object>>>> xxx = LeftKanSigma.fullSigmaWithAttrs(interp, f, I, null, null, null);
        interp.sigmas2.put(pre, interp.guid);
        Instance J = xxx.first;
        Map<Node, Map<Object, Integer>> yyy = xxx.second;
        for (Node n : C.nodes) {
            state.put(pre + "_" + n.string + "_e", conv2(yyy.get(n)));
        }
        for (Node n : D.nodes) {
            state.put(pre + "_" + n.string, conv(J.data.get(n.string)));
        }
        for (Edge n : D.edges) {
            state.put(pre + "_" + n.name, conv(J.data.get(n.name)));
        }
        for (Attribute<Node> n : D.attrs) {
            state.put(pre + "_" + n.name, conv(J.data.get(n.name)));
        }
        Set<Map<Object, Object>> l = new HashSet<>();
        for (Object k : xxx.fourth.keySet()) {
            List<Pair<String, Object>> v = xxx.fourth.get(k);
            if (v.isEmpty()) {
                continue;
            }
            Map<Object, Object> m = new HashMap<>();
            m.put("c0", k);
            boolean first = true;
            String rest = "";
            for (Pair<String, Object> p : v) {
                if (first) {
                    first = false;
                    m.put("c1", p.first);
                    m.put("c2", p.second);
                } else {
                    rest += p.first;
                }
            }
            m.put("c3", rest);
            l.add(m);
        }
        state.put(pre + "_lineage", l);
    } catch (Throwable e) {
        e.printStackTrace();
        throw new RuntimeException("Error in instance " + pre + ": " + e.getLocalizedMessage());
    }
}
Also used : Instance(catdata.fql.decl.Instance) HashMap(java.util.HashMap) Node(catdata.fql.decl.Node) Signature(catdata.fql.decl.Signature) HashMap(java.util.HashMap) Map(java.util.Map) Edge(catdata.fql.decl.Edge) Pair(catdata.Pair) HashSet(java.util.HashSet)

Example 10 with Edge

use of catdata.fql.decl.Edge in project fql by CategoricalData.

the class PSMAnd method union.

private static Instance union(Instance a, Instance b) throws FQLException {
    Map<String, Set<Pair<Object, Object>>> data = new HashMap<>();
    for (Node n : a.thesig.nodes) {
        Set<Pair<Object, Object>> set = new HashSet<>();
        set.addAll(a.data.get(n.string));
        set.addAll(b.data.get(n.string));
        data.put(n.string, set);
    }
    for (Edge n : a.thesig.edges) {
        Set<Pair<Object, Object>> set = new HashSet<>();
        set.addAll(a.data.get(n.name));
        set.addAll(b.data.get(n.name));
        data.put(n.name, set);
    }
    return new Instance(a.thesig, data);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Instance(catdata.fql.decl.Instance) Node(catdata.fql.decl.Node) Edge(catdata.fql.decl.Edge) Pair(catdata.Pair) HashSet(java.util.HashSet)

Aggregations

Edge (catdata.fql.decl.Edge)37 Node (catdata.fql.decl.Node)33 HashMap (java.util.HashMap)27 Pair (catdata.Pair)25 HashSet (java.util.HashSet)19 LinkedList (java.util.LinkedList)19 LinkedHashMap (java.util.LinkedHashMap)17 Instance (catdata.fql.decl.Instance)15 Set (java.util.Set)13 Map (java.util.Map)12 FQLException (catdata.fql.FQLException)11 Path (catdata.fql.decl.Path)11 Attribute (catdata.fql.decl.Attribute)10 Signature (catdata.fql.decl.Signature)9 List (java.util.List)7 Arr (catdata.fql.cat.Arr)6 Fn (catdata.fql.Fn)4 Transform (catdata.fql.decl.Transform)4 Triple (catdata.Triple)3 FinCat (catdata.fql.cat.FinCat)3