Search in sources :

Example 1 with Attribute

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

the class PSMNot method exec.

@Override
public void exec(PSMInterp interp, Map<String, Set<Map<Object, Object>>> state) {
    try {
        Signature sig0 = new Signature(sig.nodes, sig.edges, new LinkedList<>(), sig.eqs);
        Pair<Map<Node, Triple<Instance, Map<Object, Path>, Map<Path, Object>>>, Map<Edge, Transform>> H1 = interp.prop1.get(prop);
        Pair<Instance, Map<Node, Pair<Map<Object, Instance>, Map<Instance, Object>>>> H2 = interp.prop2.get(prop);
        Instance prp = new Instance(sig, PSMGen.gather(prop, sig, state));
        Map<Node, Map<Object, Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>>> I1 = interp.prop3.get(prop);
        Map<Node, Map<Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>, Object>> I2 = interp.prop4.get(prop);
        List<Pair<String, List<Pair<Object, Object>>>> data = new LinkedList<>();
        for (Node c : sig.nodes) {
            List<Pair<Object, Object>> data0 = new LinkedList<>();
            Triple<Instance, Map<Object, Path>, Map<Path, Object>> Hc = H1.first.get(c);
            for (Object id : prp.getNode(c)) {
                Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>> id0 = I1.get(c).get(id);
                Instance A = H2.second.get(c).first.get(id0.first);
                Instance notA = calcSub(sig0, /*H1, */
                Hc, A);
                Object notId = H2.second.get(c).second.get(notA);
                Object x = I2.get(c).get(new Pair<>(notId, id0.second));
                data0.add(new Pair<>(id, x));
            }
            data.add(new Pair<>(c.string, data0));
        }
        Transform ret = new Transform(prp, prp, data);
        PSMGen.shred(pre, ret, state);
    } catch (FQLException fe) {
        fe.printStackTrace();
        throw new RuntimeException(fe.getMessage());
    }
}
Also used : Arr(catdata.fql.cat.Arr) Instance(catdata.fql.decl.Instance) Attribute(catdata.fql.decl.Attribute) Node(catdata.fql.decl.Node) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) FQLException(catdata.fql.FQLException) Signature(catdata.fql.decl.Signature) Transform(catdata.fql.decl.Transform) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(catdata.Pair)

Example 2 with Attribute

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

the class PSMUnChi method exec.

@Override
public void exec(PSMInterp interp, Map<String, Set<Map<Object, Object>>> state) {
    try {
        Map<Object, Object> m1 = new HashMap<>();
        Map<Object, Object> m2 = new HashMap<>();
        Instance B = new Instance(sig, PSMGen.gather(b, sig, state));
        Instance P = new Instance(sig, PSMGen.gather(prop, sig, state));
        // Instance P = interp.prop2.get(prop).first;
        Transform F = new Transform(B, P, PSMGen.gather(f, sig, state));
        Map<String, Set<Pair<Object, Object>>> data = new HashMap<>();
        List<Pair<String, List<Pair<Object, Object>>>> data2 = new LinkedList<>();
        for (Node d : sig.nodes) {
            Instance tofind = interp.prop1.get(prop).first.get(d).first;
            Object found = interp.prop2.get(prop).second.get(d).second.get(tofind);
            Set<Pair<Object, Object>> dta = new HashSet<>();
            List<Pair<Object, Object>> dta2 = new LinkedList<>();
            for (Pair<Object, Object> k : B.data.get(d.string)) {
                Object v = lookup(F.data.get(d.string), k.first);
                Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>> vv = interp.prop3.get(prop).get(d).get(v);
                if (vv.first.equals(found)) {
                    Object u = Integer.toString(++interp.guid);
                    m1.put(u, k.first);
                    m2.put(k.first, u);
                    dta.add(new Pair<>(u, u));
                    dta2.add(new Pair<>(u, k.first));
                }
            }
            data.put(d.string, dta);
            data2.add(new Pair<>(d.string, dta2));
        }
        for (Edge e : sig.edges) {
            Set<Pair<Object, Object>> dta = new HashSet<>();
            for (Pair<Object, Object> k : data.get(e.source.string)) {
                Object v = m1.get(k.first);
                Object vx = lookup(B.data.get(e.name), v);
                Object vz = m2.get(vx);
                dta.add(new Pair<>(k.first, vz));
            }
            data.put(e.name, dta);
        }
        for (Attribute<Node> e : sig.attrs) {
            Set<Pair<Object, Object>> dta = new HashSet<>();
            for (Pair<Object, Object> k : data.get(e.source.string)) {
                Object v = m1.get(k.first);
                Object vx = lookup(B.data.get(e.name), v);
                dta.add(new Pair<>(k.first, vx));
            }
            data.put(e.name, dta);
        }
        Instance A = new Instance(sig, data);
        Transform T = new Transform(A, B, data2);
        PSMGen.shred(pre, A, state);
        PSMGen.shred(pre + "_trans", T, state);
    } 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) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Instance(catdata.fql.decl.Instance) Attribute(catdata.fql.decl.Attribute) Node(catdata.fql.decl.Node) LinkedHashMap(java.util.LinkedHashMap) FQLException(catdata.fql.FQLException) Pair(catdata.Pair) HashSet(java.util.HashSet) LinkedList(java.util.LinkedList) Transform(catdata.fql.decl.Transform) Edge(catdata.fql.decl.Edge)

Example 3 with Attribute

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

the class Relationalizer method observations.

public static Pair<Map<Node, List<Pair<Path, Attribute<Node>>>>, List<PSM>> observations(Signature sig, String out, String in, boolean relationalize) throws FQLException {
    List<PSM> ret = new LinkedList<>();
    Map<Node, List<Pair<Path, Attribute<Node>>>> attrs = new HashMap<>();
    // attrs = new HashMap<Node, List<Pair<Path, Attribute<Node>>>>();
    Map<String, String> edge_types = new HashMap<>();
    edge_types.put("c0", PSM.VARCHAR());
    edge_types.put("c1", PSM.VARCHAR());
    // copy in to out, to start with
    ret.addAll(copy(sig, out, in));
    FinCat<Node, Path> cat = sig.toCategory2().first;
    for (Node n : sig.nodes) {
        attrs.put(n, new LinkedList<>());
        int count = 0;
        List<Map<String, String>> alltypes = new LinkedList<>();
        for (Arr<Node, Path> p : cat.arrows) {
            // need identity path to get attributes from n
            if (!p.src.equals(n)) {
                continue;
            }
            Flower f = PSMGen.compose(in, p.arr);
            ret.add(new CreateTable(out + "_" + n.string + "tempNoAttrs" + count, edge_types, false));
            InsertSQL f0 = new InsertSQL(out + "_" + n.string + "tempNoAttrs" + count, f, "c0", "c1");
            ret.add(f0);
            LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
            Map<String, String> from = new HashMap<>();
            List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
            List<Attribute<Node>> l = sig.attrsFor(p.arr.target);
            from.put(n.string, out + "_" + n.string + "tempNoAttrs" + count);
            select.put("c0", new Pair<>(n.string, "c0"));
            // select.put("c1", new Pair<>(n.string + "tempNoAttrs", "c1"));
            int i = 1;
            LinkedHashMap<String, String> types = new LinkedHashMap<>();
            types.put("c0", PSM.VARCHAR());
            // types.put("c1", PSM.VARCHAR());
            for (Attribute<Node> a : l) {
                from.put(a.name, in + "_" + a.name);
                Pair<String, String> lhs = new Pair<>(n.string, "c1");
                Pair<String, String> rhs = new Pair<>(a.name, "c0");
                where.add(new Pair<>(lhs, rhs));
                select.put("c" + i, new Pair<>(a.name, "c1"));
                types.put("c" + i, a.target.psm());
                attrs.get(n).add(new Pair<>(p.arr, a));
                i++;
            }
            alltypes.add(types);
            Flower g = new Flower(select, from, where);
            ret.add(new CreateTable(out + "_" + n.string + "temp" + count, types, false));
            ret.add(new InsertSQL2(out + "_" + n.string + "temp" + count, g, new LinkedList<>(types.keySet())));
            count++;
        }
        LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
        List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
        Map<String, String> from = new HashMap<>();
        from.put(in + "_" + n.string, in + "_" + n.string);
        Map<String, String> ty = new HashMap<>();
        int u = 0;
        ty.put("id", PSM.VARCHAR());
        select.put("id", new Pair<>(in + "_" + n.string, "c0"));
        for (int i = 0; i < count; i++) {
            Map<String, String> types = alltypes.get(i);
            for (int v = 0; v < types.size() - 1; v++) {
                // was c1
                ty.put("c" + u, types.get("c" + (v + 1)));
                select.put("c" + u, new Pair<>(n.string + "temp" + i, "c" + (v + 1)));
                u++;
            }
            from.put(n.string + "temp" + i, out + "_" + n.string + "temp" + i);
            where.add(new Pair<>(new Pair<>(n.string + "temp" + i, "c0"), new Pair<>(in + "_" + n.string, "c0")));
        }
        if (select.isEmpty()) {
            throw new RuntimeException("No observable for " + n.string);
        }
        Flower j = new Flower(select, from, where);
        ret.add(new CreateTable(out + "_" + n.string + "_observables", ty, false));
        ret.add(new InsertSQL2(out + "_" + n.string + "_observables", j, new LinkedList<>(j.select.keySet())));
        if (relationalize) {
            ret.addAll(relationalize(select, from, where, sig, out, ty, n, u, edge_types));
        }
        for (int count0 = 0; count0 < count; count0++) {
            ret.add(new DropTable(out + "_" + n.string + "temp" + count0));
            ret.add(new DropTable(out + "_" + n.string + "tempNoAttrs" + count0));
        }
    }
    return new Pair<>(attrs, ret);
}
Also used : Attribute(catdata.fql.decl.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(catdata.fql.decl.Node) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) Path(catdata.fql.decl.Path) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with Attribute

use of catdata.fql.decl.Attribute 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 5 with Attribute

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

the class ExpPSM method exec.

@Override
public void exec(PSMInterp interp, Map<String, Set<Map<Object, Object>>> state) {
    try {
        Instance Ix = new Instance(sig, PSMGen.gather(I, sig, state));
        Instance Jx = new Instance(sig, PSMGen.gather(J, sig, state));
        IntRef idx = new IntRef(interp.guid);
        Quad<Instance, Map<Pair<Node, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>, Triple<Instance, Map<Node, Map<Object, Pair<Arr<Node, Path>, Object>>>, Map<Node, Map<Pair<Arr<Node, Path>, Object>, Object>>>>, Map<Node, Map<Object, Pair<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Transform>>>, Map<Node, Map<Pair<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Transform>, Object>>> ret = Instance.exp2(idx, Ix, Jx);
        interp.guid = idx.i;
        interp.exps2.put(pre, ret);
        PSMGen.shred(pre, ret.first, state);
    } catch (FQLException fe) {
        fe.printStackTrace();
        throw new RuntimeException(fe.getLocalizedMessage());
    }
}
Also used : Arr(catdata.fql.cat.Arr) Path(catdata.fql.decl.Path) Instance(catdata.fql.decl.Instance) Attribute(catdata.fql.decl.Attribute) Node(catdata.fql.decl.Node) LinkedHashMap(java.util.LinkedHashMap) IntRef(catdata.IntRef) FQLException(catdata.fql.FQLException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(catdata.Pair)

Aggregations

Attribute (catdata.fql.decl.Attribute)21 Pair (catdata.Pair)20 Node (catdata.fql.decl.Node)19 HashMap (java.util.HashMap)15 LinkedList (java.util.LinkedList)15 Map (java.util.Map)12 FQLException (catdata.fql.FQLException)11 Instance (catdata.fql.decl.Instance)11 LinkedHashMap (java.util.LinkedHashMap)11 Arr (catdata.fql.cat.Arr)10 Edge (catdata.fql.decl.Edge)10 Path (catdata.fql.decl.Path)10 Signature (catdata.fql.decl.Signature)10 List (java.util.List)8 Triple (catdata.Triple)6 Transform (catdata.fql.decl.Transform)6 HashSet (java.util.HashSet)6 Set (java.util.Set)6 Fn (catdata.fql.Fn)4 FinCat (catdata.fql.cat.FinCat)3