Search in sources :

Example 6 with Transform

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

the class Inst method hom.

/*
	private static List<Mapping> bistuff(Signature base, Signature exp)
			throws FQLException {

		if (base.nodes.size() != exp.nodes.size()) {
			return null;
		}
		if (base.attrs.size() != exp.attrs.size()) {
			return null;
		}

		Pair<FinCat<Node, Path>, Fn<Path, Arr<Node, Path>>> xxx = base
				.toCategory2();
		FinCat<Node, Path> base0 = xxx.first;
		Pair<FinCat<Node, Path>, Fn<Path, Arr<Node, Path>>> yyy = exp
				.toCategory2();
		FinCat<Node, Path> exp0 = yyy.first;

		if (xxx.first.arrows.size() != yyy.first.arrows.size()) {
			return null;
		}

		List<LinkedHashMap<Node, Node>> nms = bijections(exp.nodes, base.nodes);

		List<Mapping> ret = new LinkedList<>();
		outmost: for (LinkedHashMap<Node, Node> nm : nms) {
			Map<Pair<Node, Node>, List<LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>>> bijm = new HashMap<>();
			Map<Node, List<LinkedHashMap<Attribute<Node>, Attribute<Node>>>> atm = new HashMap<>();
			for (Node s : nm.keySet()) {
				List<Attribute<Node>> sa = exp.attrsFor(s);
				List<Attribute<Node>> ta = base.attrsFor(nm.get(s));
				if (sa.size() != ta.size()) {
					continue outmost;
				}
				List<LinkedHashMap<Attribute<Node>, Attribute<Node>>> am = bijections(
						sa, ta);
				Iterator<LinkedHashMap<Attribute<Node>, Attribute<Node>>> it = am
						.iterator();
				outer: while (it.hasNext()) {
					Map<Attribute<Node>, Attribute<Node>> k = it.next();
					for (Entry<Attribute<Node>, Attribute<Node>> v : k
							.entrySet()) {
						if (!v.getKey().target.equals(v.getValue().target)) {
							it.remove();
							continue outer;
						}
					}
				}
				if (am.isEmpty() && !sa.isEmpty()) {
					continue outmost;
				}
				atm.put(s, am);
				Node sx = nm.get(s);
				for (Node t : nm.keySet()) {
					Set<Arr<Node, Path>> h1 = exp0.hom(s, t);
					Node st = nm.get(t);
					Set<Arr<Node, Path>> h2 = base0.hom(sx, st);
					if (h1.size() != h2.size()) {
						continue outmost;
					}
					List<LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>> bij = bijections(
							new LinkedList<>(h1), new LinkedList<>(h2));
					if (!bij.isEmpty()) {
						bijm.put(new Pair<>(s, t), bij);
					}
				}
			}

			List<LinkedHashMap<Pair<Node, Node>, LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>>> bijmX = homomorphs(bijm);
			List<LinkedHashMap<Node, LinkedHashMap<Attribute<Node>, Attribute<Node>>>> atmX = homomorphs(atm);
	
			List<LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>> bijmZ = new LinkedList<>();
			for (LinkedHashMap<Pair<Node, Node>, LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>> k : bijmX) {
				LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>> bijmY = new LinkedHashMap<>();
				for (Entry<Pair<Node, Node>, LinkedHashMap<Arr<Node, Path>, Arr<Node, Path>>> v : k
						.entrySet()) {
					bijmY.putAll(v.getValue());
				}
				bijmZ.add(bijmY);
			}

			List<LinkedHashMap<Attribute<Node>, Attribute<Node>>> atmZ = new LinkedList<>();
			for (LinkedHashMap<Node, LinkedHashMap<Attribute<Node>, Attribute<Node>>> k : atmX) {
				LinkedHashMap<Attribute<Node>, Attribute<Node>> atmY = new LinkedHashMap<>();
				for (Entry<Node, LinkedHashMap<Attribute<Node>, Attribute<Node>>> v : k
						.entrySet()) {
					atmY.putAll(v.getValue());
				}
				atmZ.add(atmY);
			}
			if (exp.attrs.isEmpty()) {
				atmZ.add(new LinkedHashMap<>());
			}
			if (exp.edges.isEmpty()) {
				bijmZ.add(new LinkedHashMap<>());
			}

			for (Map<Arr<Node, Path>, Arr<Node, Path>> f : bijmZ) {
				LinkedHashMap<Edge, Path> em = new LinkedHashMap<>();
				for (Edge e : exp.edges) {
					em.put(e, f.get(yyy.second.of(new Path(exp, e))).arr);
				}
				for (LinkedHashMap<Attribute<Node>, Attribute<Node>> g : atmZ) {
					try {
						Mapping m = new Mapping(true, exp, base, nm, em, g);
						ret.add(m);
					} catch (FQLException fe) {
						// fe.printStackTrace();
					}
				}
			}
			// break;
		}

		return ret;
	}
*/
// I => J
public static List<Transform> hom(Instance I, Instance J) {
    if (!I.thesig.equals(J.thesig)) {
        throw new RuntimeException();
    }
    List<Transform> ret = new LinkedList<>();
    Map<Node, List<LinkedHashMap<Pair<Object, Object>, Pair<Object, Object>>>> m = new HashMap<>();
    for (Node n : I.thesig.nodes) {
        List<Pair<Object, Object>> src = new LinkedList<>(I.data.get(n.string));
        List<Pair<Object, Object>> dst = new LinkedList<>(J.data.get(n.string));
        List<LinkedHashMap<Pair<Object, Object>, Pair<Object, Object>>> h = homomorphs(src, dst);
        m.put(n, h);
    }
    List<LinkedHashMap<Node, LinkedHashMap<Pair<Object, Object>, Pair<Object, Object>>>> map = homomorphs(m);
    for (LinkedHashMap<Node, LinkedHashMap<Pair<Object, Object>, Pair<Object, Object>>> t : map) {
        try {
            List<Pair<String, List<Pair<Object, Object>>>> l = new LinkedList<>();
            for (Entry<Node, LinkedHashMap<Pair<Object, Object>, Pair<Object, Object>>> u : t.entrySet()) {
                l.add(new Pair<>(u.getKey().string, conv(u.getValue())));
            }
            Transform tr = new Transform(I, J, l);
            ret.add(tr);
        } catch (Exception e) {
        }
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(catdata.fql.decl.Node) LinkedList(java.util.LinkedList) FQLException(catdata.fql.FQLException) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) LinkedList(java.util.LinkedList) Transform(catdata.fql.decl.Transform) Pair(catdata.Pair)

Example 7 with Transform

use of catdata.fql.decl.Transform 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 8 with Transform

use of catdata.fql.decl.Transform 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 9 with Transform

use of catdata.fql.decl.Transform 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 10 with Transform

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

Transform (catdata.fql.decl.Transform)13 Pair (catdata.Pair)12 FQLException (catdata.fql.FQLException)12 Instance (catdata.fql.decl.Instance)12 Node (catdata.fql.decl.Node)11 LinkedList (java.util.LinkedList)10 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 Map (java.util.Map)8 Arr (catdata.fql.cat.Arr)6 Attribute (catdata.fql.decl.Attribute)6 Signature (catdata.fql.decl.Signature)6 Path (catdata.fql.decl.Path)5 Edge (catdata.fql.decl.Edge)4 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 Triple (catdata.Triple)2 IntRef (catdata.IntRef)1 Quad (catdata.Quad)1