Search in sources :

Example 16 with Signature

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

the class PSMAnd 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 old = H2.first;
        Instance prp = new Instance(sig, PSMGen.gather(prop, sig, state));
        Instance prd = new Instance(sig, PSMGen.gather(prod, sig, state));
        Transform fst = new Transform(prd, prp, PSMGen.gather(prod + "_fst", sig, state));
        Transform snd = new Transform(prd, prp, PSMGen.gather(prod + "_snd", 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 idp : prd.getNode(c)) {
                Object id0 = lookup(fst.data.get(c.string), idp);
                Object id1 = lookup(snd.data.get(c.string), idp);
                Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>> idl = I1.get(c).get(id0);
                Instance A = H2.second.get(c).first.get(idl.first);
                Pair<Object, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>> idr = I1.get(c).get(id1);
                Instance B = H2.second.get(c).first.get(idr.first);
                if (!idl.second.equals(idr.second)) {
                    throw new RuntimeException("bad");
                }
                Instance nA;
                switch(kind) {
                    case "and":
                        nA = isect(A, B);
                        break;
                    case "or":
                        nA = union(A, B);
                        break;
                    case "implies":
                        nA = implies(sig0, H1, Hc, A, B);
                        break;
                    default:
                        throw new RuntimeException();
                }
                Object notId = H2.second.get(c).second.get(nA);
                Object x = I2.get(c).get(new Pair<>(notId, idl.second));
                data0.add(new Pair<>(idp, x));
            }
            data.add(new Pair<>(c.string, data0));
        }
        Transform ret = new Transform(prd, 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 17 with Signature

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

the class PSMBool 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);
        Instance unitI = new Instance(sig, PSMGen.gather(unit, sig, state));
        Instance propI = new Instance(sig, PSMGen.gather(prop, sig, state));
        Map<Node, Map<Object, Object>> subst_inv = new HashMap<>();
        for (Node n : sig.nodes) {
            Map<Object, Object> m = new HashMap<>();
            Set<Map<Object, Object>> g = state.get(unit + "_" + n + "_subst_inv");
            for (Map<Object, Object> j : g) {
                m.put(j.get("c0"), j.get("c1"));
            }
            subst_inv.put(n, m);
        }
        List<Pair<String, List<Pair<Object, Object>>>> data = new LinkedList<>();
        for (Node n : sig.nodes) {
            List<Pair<Object, Object>> set = new LinkedList<>();
            for (Pair<Object, Object> k : unitI.data.get(n.string)) {
                Object k0 = subst_inv.get(n).get(k.first);
                if (k0 == null) {
                    throw new RuntimeException();
                }
                LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> v = m1.get(n).get(k0);
                Instance tofind = bool ? interp.prop1.get(prop).first.get(n).first : new Instance(sig0);
                Object found = interp.prop2.get(prop).second.get(n).second.get(tofind);
                Object r = interp.prop4.get(prop).get(n).get(new Pair<>(found, v));
                set.add(new Pair<>(k.first, r));
            }
            data.add(new Pair<>(n.string, set));
        }
        Transform ret = new Transform(unitI, propI, data);
        PSMGen.shred(pre, ret, state);
    } catch (FQLException fe) {
        fe.printStackTrace();
        throw new RuntimeException(fe.getMessage());
    }
}
Also used : Path(catdata.fql.decl.Path) 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) 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 18 with Signature

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

the class FullSigmaTrans 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(srcH, C, state);
    List<Pair<String, List<Pair<Object, Object>>>> J0 = PSMGen.gather(dstH, C, state);
    List<Pair<String, List<Pair<Object, Object>>>> H0 = PSMGen.gather(h, C, state);
    List<Pair<String, List<Pair<Object, Object>>>> J0X = PSMGen.gather(dst, D, state);
    List<Pair<String, List<Pair<Object, Object>>>> tempI = new LinkedList<>();
    List<Pair<String, List<Pair<Object, Object>>>> tempH = new LinkedList<>();
    for (Node n : C.nodes) {
        Set<Map<Object, Object>> x2 = state.get(dst + "_" + n.string + "_e");
        tempH.add(new Pair<>(n.string, convX(x2)));
        Set<Map<Object, Object>> x1 = state.get(dst + "_" + f.nm.get(n).string);
        tempI.add(new Pair<>(n.string, conv(x1)));
    }
    for (Edge e : C.edges) {
        Set<Map<Object, Object>> x1 = eval(state, dst, f.em.get(e));
        tempI.add(new Pair<>(e.name, conv(x1)));
    }
    for (Attribute<Node> e : C.attrs) {
        Set<Map<Object, Object>> x1 = state.get(dst + "_" + f.am.get(e).name);
        tempI.add(new Pair<>(e.name, conv(x1)));
    }
    try {
        Instance I = new Instance(C, I0);
        Instance J = new Instance(C, J0);
        Transform H = new Transform(I, J, H0);
        // Instance IX = new Instance(D, I0X);
        Instance JX = new Instance(D, J0X);
        Instance temp = new Instance(C, tempI);
        Transform etaJ = new Transform(J, temp, tempH);
        Transform HX = Transform.composeX(H, etaJ);
        // should pass H, but compute etaJ after de-attr.
        // that way, HX.dst and delta JX have attr IDs in common
        // de-attr JX
        Integer current = interp.guid;
        interp.guid = interp.sigmas.get(src);
        Quad<Instance, Map<Node, Map<Object, Integer>>, Map<Node, Map<Integer, Object>>, Map<Object, List<Pair<String, Object>>>> xxx = LeftKanSigma.fullSigmaWithAttrs(interp, f, I, HX, JX, interp.sigmas2.get(src));
        interp.guid = current;
        for (Node n : D.nodes) {
            state.put(pre + "_" + n.string, conv0(xxx.third.get(n)));
        }
        for (Attribute<Node> a : D.attrs) {
            state.put(pre + "_" + a.name, new HashSet<>());
        }
        for (Edge a : D.edges) {
            state.put(pre + "_" + a.name, new HashSet<>());
        }
    } catch (FQLException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : Instance(catdata.fql.decl.Instance) Node(catdata.fql.decl.Node) LinkedList(java.util.LinkedList) FQLException(catdata.fql.FQLException) Signature(catdata.fql.decl.Signature) Transform(catdata.fql.decl.Transform) HashMap(java.util.HashMap) Map(java.util.Map) Edge(catdata.fql.decl.Edge) Pair(catdata.Pair)

Example 19 with Signature

use of catdata.fql.decl.Signature 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)

Aggregations

Signature (catdata.fql.decl.Signature)19 LinkedList (java.util.LinkedList)14 Pair (catdata.Pair)13 Node (catdata.fql.decl.Node)13 FQLException (catdata.fql.FQLException)11 HashMap (java.util.HashMap)11 Attribute (catdata.fql.decl.Attribute)10 Instance (catdata.fql.decl.Instance)10 Edge (catdata.fql.decl.Edge)9 Map (java.util.Map)7 Arr (catdata.fql.cat.Arr)6 Path (catdata.fql.decl.Path)6 Transform (catdata.fql.decl.Transform)6 LinkedHashMap (java.util.LinkedHashMap)6 Triple (catdata.Triple)5 List (java.util.List)5 HashSet (java.util.HashSet)4 Fn (catdata.fql.Fn)3 FinCat (catdata.fql.cat.FinCat)3 InstExp (catdata.fql.decl.InstExp)3