Search in sources :

Example 26 with Edge

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

the class PropPSM method exec.

@Override
public void exec(PSMInterp interp, Map<String, Set<Map<Object, Object>>> state) {
    try {
        IntRef ref = new IntRef(interp.guid);
        Signature sigX = new Signature(sig.nodes, sig.edges, new LinkedList<>(), sig.eqs);
        Map<Node, List<Pair<Arr<Node, Path>, Attribute<Node>>>> obs = sig.obs();
        Pair<FinCat<Node, Path>, Fn<Path, Arr<Node, Path>>> ooo = sig.toCategory2();
        Fn<Path, Arr<Node, Path>> fn = ooo.second;
        Pair<Pair<Map<Node, Triple<Instance, Map<Object, Path>, Map<Path, Object>>>, Map<Edge, Transform>>, Pair<Instance, Map<Node, Pair<Map<Object, Instance>, Map<Instance, Object>>>>> xxx = sigX.omega(ref);
        interp.prop1.put(pre, xxx.first);
        interp.prop2.put(pre, xxx.second);
        Instance old = xxx.second.first;
        Map<Node, List<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>> m = sig.obsbar();
        Map<String, Set<Pair<Object, Object>>> data = new HashMap<>();
        Map<Node, Map<Object, Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>>> m1 = new HashMap<>();
        Map<Node, Map<Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>, Object>> m2 = new HashMap<>();
        for (Node n : sig.nodes) {
            Map<Object, Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>> map1 = new HashMap<>();
            Map<Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>, Object> map2 = new HashMap<>();
            Set<Pair<Object, Object>> set = new HashSet<>();
            m1.put(n, map1);
            m2.put(n, map2);
            for (Pair<Object, Object> i1 : old.data.get(n.string)) {
                for (LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> i2 : m.get(n)) {
                    Object o = Integer.toString(++ref.i);
                    map1.put(o, new Pair<>(i1.first, i2));
                    map2.put(new Pair<>(i1.first, i2), o);
                    set.add(new Pair<>(o, o));
                }
            }
            data.put(n.string, set);
        }
        for (Attribute<Node> a : sig.attrs) {
            Set<Pair<Object, Object>> set = new HashSet<>();
            for (Pair<Object, Object> k : data.get(a.source.string)) {
                Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>> kk = m1.get(a.source).get(k.first);
                // Object old_id = kk.first;
                LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> new_id = kk.second;
                set.add(new Pair<>(k.first, new_id.get(new Pair<>(new Arr<>(new Path(sig, a.source), a.source, a.source), a))));
            }
            data.put(a.name, set);
        }
        for (Edge a : sig.edges) {
            Set<Pair<Object, Object>> set = new HashSet<>();
            for (Pair<Object, Object> k : data.get(a.source.string)) {
                Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>> kk = m1.get(a.source).get(k.first);
                Object old_id = kk.first;
                Object old_id0 = lookup(old.data.get(a.name), old_id);
                LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> new_id = kk.second;
                LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> new_id0 = truncate2(sig, new_id, fn.of(new Path(sig, a)), obs.get(a.target));
                Object o = m2.get(a.target).get(new Pair<>(old_id0, new_id0));
                set.add(new Pair<>(k.first, o));
            }
            data.put(a.name, set);
        }
        interp.prop3.put(pre, m1);
        interp.prop4.put(pre, m2);
        Instance ne = new Instance(sig, data);
        PSMGen.shred(pre, ne, state);
        interp.guid = ref.i;
    } catch (FQLException fe) {
        fe.printStackTrace();
        throw new RuntimeException(fe.getMessage());
    }
}
Also used : Arr(catdata.fql.cat.Arr) Set(java.util.Set) HashSet(java.util.HashSet) Attribute(catdata.fql.decl.Attribute) Instance(catdata.fql.decl.Instance) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FinCat(catdata.fql.cat.FinCat) Node(catdata.fql.decl.Node) LinkedHashMap(java.util.LinkedHashMap) IntRef(catdata.IntRef) FQLException(catdata.fql.FQLException) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) HashSet(java.util.HashSet) Path(catdata.fql.decl.Path) Fn(catdata.fql.Fn) Triple(catdata.Triple) Signature(catdata.fql.decl.Signature) Transform(catdata.fql.decl.Transform) Edge(catdata.fql.decl.Edge) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 27 with Edge

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

the class Relationalizer method applySubst.

private static Collection<PSM> applySubst(Signature sig, Node N, String out) {
    List<PSM> ret = new LinkedList<>();
    Map<String, String> attrs = new HashMap<>();
    attrs.put("c0", PSM.VARCHAR());
    attrs.put("c1", PSM.VARCHAR());
    List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
    Map<String, String> from = new HashMap<>();
    LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
    from.put(N + "_squash", out + "_" + N + "_squash");
    select.put("c0", new Pair<>(N + "_squash", "c1"));
    select.put("c1", new Pair<>(N + "_squash", "c1"));
    Flower f = new Flower(select, from, where);
    ret.add(new CreateTable(out + "_" + N + "_relationalize_temp", attrs, false));
    ret.add(new InsertSQL(out + "_" + N + "_relationalize_temp", f, "c0", "c1"));
    ret.add(new DropTable(out + "_" + N.string));
    ret.add(new CreateTable(out + "_" + N.string, attrs, false));
    ret.add(new InsertSQL(out + "_" + N.string, new CopyFlower(out + "_" + N + "_relationalize_temp", "c0", "c1"), "c0", "c1"));
    ret.add(new DropTable(out + "_" + N + "_relationalize_temp"));
    for (Edge n : sig.edges) {
        if (!n.source.equals(N)) {
            continue;
        }
        where = new LinkedList<>();
        from = new HashMap<>();
        select = new LinkedHashMap<>();
        from.put(N + "_squash", out + "_" + N + "_squash");
        from.put(out + "_" + n.name, out + "_" + n.name);
        where.add(new Pair<>(new Pair<>(N + "_squash", "c0"), new Pair<>(out + "_" + n.name, "c0")));
        select.put("c0", new Pair<>(N + "_squash", "c1"));
        select.put("c1", new Pair<>(out + "_" + n.name, "c1"));
        f = new Flower(select, from, where);
        ret.add(new CreateTable(out + "_" + n.name + "_relationalize_temp", attrs, false));
        ret.add(new InsertSQL(out + "_" + n.name + "_relationalize_temp", f, "c0", "c1"));
        ret.add(new DropTable(out + "_" + n.name));
        ret.add(new CreateTable(out + "_" + n.name, attrs, false));
        ret.add(new InsertSQL(out + "_" + n.name, new CopyFlower(out + "_" + n.name + "_relationalize_temp", "c0", "c1"), "c0", "c1"));
        ret.add(new DropTable(out + "_" + n.name + "_relationalize_temp"));
    }
    for (Attribute<Node> n : sig.attrs) {
        if (!n.source.equals(N)) {
            continue;
        }
        where = new LinkedList<>();
        from = new HashMap<>();
        select = new LinkedHashMap<>();
        from.put(N + "_squash", out + "_" + N + "_squash");
        from.put(out + "_" + n.name, out + "_" + n.name);
        where.add(new Pair<>(new Pair<>(N + "_squash", "c0"), new Pair<>(out + "_" + n.name, "c0")));
        select.put("c0", new Pair<>(N + "_squash", "c1"));
        select.put("c1", new Pair<>(out + "_" + n.name, "c1"));
        f = new Flower(select, from, where);
        ret.add(new CreateTable(out + "_" + "relationalize_temp", attrs, false));
        ret.add(new InsertSQL(out + "_" + "relationalize_temp", f, "c0", "c1"));
        ret.add(new DropTable(out + "_" + n.name));
        ret.add(new CreateTable(out + "_" + n.name, attrs, false));
        ret.add(new InsertSQL(out + "_" + n.name, new CopyFlower(out + "_" + "relationalize_temp", "c0", "c1"), "c0", "c1"));
        ret.add(new DropTable(out + "_" + "relationalize_temp"));
    }
    for (Edge n : sig.edges) {
        if (!n.target.equals(N)) {
            continue;
        }
        where = new LinkedList<>();
        from = new HashMap<>();
        select = new LinkedHashMap<>();
        from.put(N + "_squash", out + "_" + N + "_squash");
        from.put(out + "_" + n.name, out + "_" + n.name);
        where.add(new Pair<>(new Pair<>(N + "_squash", "c0"), new Pair<>(out + "_" + n.name, "c1")));
        select.put("c0", new Pair<>(out + "_" + n.name, "c0"));
        select.put("c1", new Pair<>(N + "_squash", "c1"));
        f = new Flower(select, from, where);
        ret.add(new CreateTable(out + "_" + "relationalize_temp", attrs, false));
        ret.add(new InsertSQL(out + "_" + "relationalize_temp", f, "c0", "c1"));
        ret.add(new DropTable(out + "_" + n.name));
        ret.add(new CreateTable(out + "_" + n.name, attrs, false));
        ret.add(new InsertSQL(out + "_" + n.name, new CopyFlower(out + "_" + "relationalize_temp", "c0", "c1"), "c0", "c1"));
        ret.add(new DropTable(out + "_" + "relationalize_temp"));
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(catdata.fql.decl.Node) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) Edge(catdata.fql.decl.Edge) Pair(catdata.Pair)

Example 28 with Edge

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

the class PSMAnd method isect.

private static Instance isect(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<>();
        for (Pair<Object, Object> p : a.data.get(n.string)) {
            if (b.data.get(n.string).contains(p)) {
                set.add(p);
            }
        }
        data.put(n.string, set);
    }
    for (Edge n : a.thesig.edges) {
        Set<Pair<Object, Object>> set = new HashSet<>();
        for (Pair<Object, Object> p : a.data.get(n.name)) {
            if (b.data.get(n.name).contains(p)) {
                set.add(p);
            }
        }
        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)

Example 29 with Edge

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

the class PSMAnd method implies.

private Instance implies(Signature sig0, Pair<Map<Node, Triple<Instance, Map<Object, Path>, Map<Path, Object>>>, Map<Edge, Transform>> H1, Triple<Instance, Map<Object, Path>, Map<Path, Object>> Hc, Instance A, Instance B) throws FQLException {
    Map<String, Set<Pair<Object, Object>>> notA_data = new HashMap<>();
    for (Node d : sig.nodes) {
        Set<Pair<Object, Object>> dd = new HashSet<>();
        xxx: for (Object f : Hc.first.getNode(d)) {
            Path ff = Hc.second.get(f);
            for (Node d0 : sig.nodes) {
                for (Arr<Node, Path> g : sig.toCategory2().first.hom(d, d0)) {
                    Arr<Node, Path> fg = sig.toCategory2().first.compose(sig.toCategory2().second.of(ff), g);
                    Object xxx = H1.first.get(d0).third.get(fg.arr);
                    if (xxx == null) {
                        throw new RuntimeException();
                    }
                    if (!A.getNode(d0).contains(xxx) || B.getNode(d0).contains(xxx)) {
                    } else {
                        continue xxx;
                    }
                }
            }
            dd.add(new Pair<>(f, f));
        }
        notA_data.put(d.string, dd);
    }
    for (Edge h : sig.edges) {
        Set<Pair<Object, Object>> dd = new HashSet<>();
        for (Object f : notA_data.get(h.source.string)) {
            Path ff = Hc.second.get(f);
            Arr<Node, Path> fg = sig.toCategory2().first.compose(sig.toCategory2().second.of(ff), sig.toCategory2().second.of(new Path(sig, h)));
            Object xxx = Hc.third.get(fg.arr);
            dd.add(new Pair<>(f, xxx));
        }
        notA_data.put(h.name, dd);
    }
    Instance notA = new Instance(sig0, notA_data);
    return notA;
}
Also used : Path(catdata.fql.decl.Path) Arr(catdata.fql.cat.Arr) 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)

Example 30 with Edge

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

the class PSMChi method exec.

@Override
public void exec(PSMInterp interp, Map<String, Set<Map<Object, Object>>> state) {
    try {
        Instance I = new Instance(sig, PSMGen.gather(a, sig, state));
        Instance Jfull = new Instance(fullSig, PSMGen.gather(b, fullSig, state));
        Instance J = new Instance(sig, PSMGen.gather(b, sig, state));
        Instance Pfull = new Instance(fullSig, PSMGen.gather(prop, fullSig, state));
        Instance P = interp.prop2.get(prop).first;
        Transform t = new Transform(I, J, PSMGen.gather(f, sig, state));
        List<Pair<String, List<Pair<Object, Object>>>> data = new LinkedList<>();
        for (Node c : sig.nodes) {
            List<Pair<Object, Object>> l = new LinkedList<>();
            Instance Hc = interp.prop1.get(prop).first.get(c).first;
            for (Pair<Object, Object> x : J.data.get(c.string)) {
                List<Pair<String, List<Pair<Object, Object>>>> z = new LinkedList<>();
                for (Node d : sig.nodes) {
                    List<Pair<Object, Object>> y = new LinkedList<>();
                    for (Pair<Object, Object> ff : Hc.data.get(d.string)) {
                        // c->d
                        Path f = interp.prop1.get(prop).first.get(c).second.get(ff.first);
                        Object xd = lookup(J.evaluate(f), x.first);
                        y.add(new Pair<>(ff.first, xd));
                    }
                    z.add(new Pair<>(d.string, y));
                }
                Transform xx = new Transform(Hc, J, z);
                Map<String, Set<Pair<Object, Object>>> q = new HashMap<>();
                for (Node d : sig.nodes) {
                    Set<Pair<Object, Object>> g = new HashSet<>();
                    for (Pair<Object, Object> y : Hc.data.get(d.string)) {
                        if (proj2(t.data.get(d.string)).contains(lookup(xx.data.get(d.string), y.first))) {
                            g.add(y);
                        }
                    }
                    q.put(d.string, g);
                }
                for (Edge e : sig.edges) {
                    Set<Pair<Object, Object>> set = new HashSet<>();
                    for (Pair<Object, Object> j : Hc.data.get(e.name)) {
                        if (proj1(q.get(e.source.string)).contains(j.first) && proj2(q.get(e.target.string)).contains(j.second)) {
                            set.add(j);
                        }
                    }
                    q.put(e.name, set);
                }
                // also do edges
                Instance pb = new Instance(sig, q);
                Object fnl = interp.prop2.get(prop).second.get(c).second.get(pb);
                l.add(new Pair<>(x.first, fnl));
            }
            data.add(new Pair<>(c.string, l));
        }
        Transform ret = new Transform(J, P, data);
        List<Pair<String, List<Pair<Object, Object>>>> dataFull = new LinkedList<>();
        for (Node n : sig.nodes) {
            List<Pair<Object, Object>> set = new LinkedList<>();
            for (Pair<Object, Object> k : ret.data.get(n.string)) {
                Object lhs = k.second;
                LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> rhs = Jfull.flag(n, k.first);
                Object xxx = interp.prop4.get(prop).get(n).get(new Pair<>(lhs, rhs));
                set.add(new Pair<>(k.first, xxx));
            }
            dataFull.add(new Pair<>(n.string, set));
        }
        Transform ret0 = new Transform(Jfull, Pfull, dataFull);
        PSMGen.shred(pre, ret0, state);
    } catch (FQLException fe) {
        fe.printStackTrace();
        throw new RuntimeException(fe.getLocalizedMessage());
    }
}
Also used : Path(catdata.fql.decl.Path) Set(java.util.Set) HashSet(java.util.HashSet) Instance(catdata.fql.decl.Instance) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(catdata.fql.decl.Node) LinkedList(java.util.LinkedList) FQLException(catdata.fql.FQLException) Transform(catdata.fql.decl.Transform) 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