Search in sources :

Example 1 with Comp

use of catdata.fql.decl.FullQueryExp.Comp in project fql by CategoricalData.

the class ToFullQueryExp method visit.

@Override
public FullQueryExp visit(FQLProgram env, Match e) {
    try {
        Const s = e.src.typeOf(env).toConst(env);
        Const t = e.dst.typeOf(env).toConst(env);
        Pair<Map<Set<Pair<String, String>>, String>, Map<Set<Pair<String, String>>, String>> xxx = computeEqCs(s, t, e.rel);
        Map<Set<Pair<String, String>>, String> node_map = xxx.first;
        Map<Set<Pair<String, String>>, String> attr_map = xxx.second;
        Set<Pair<List<String>, List<String>>> eqs = new HashSet<>();
        Set<Triple<String, String, String>> arrows = new HashSet<>();
        Set<String> nodes = new HashSet<>();
        Set<Triple<String, String, String>> attrs = new HashSet<>();
        List<Pair<String, String>> inj1Node = new LinkedList<>();
        List<Pair<String, String>> inj1Attrs = new LinkedList<>();
        List<Pair<String, String>> inj2Node = new LinkedList<>();
        List<Pair<String, String>> inj2Attrs = new LinkedList<>();
        List<Pair<String, List<String>>> inj2Arrows = new LinkedList<>();
        List<Pair<String, List<String>>> inj1Arrows = new LinkedList<>();
        for (Triple<String, String, String> att : s.attrs) {
            String eqc = lookupAttr("left", att, attr_map);
            attrs.add(new Triple<>(eqc, lookupNode("left", att.second, node_map), att.third));
            inj1Attrs.add(new Pair<>(att.first, eqc));
        }
        for (Triple<String, String, String> att : t.attrs) {
            String eqc = lookupAttr("right", att, attr_map);
            attrs.add(new Triple<>(eqc, lookupNode("right", att.second, node_map), att.third));
            inj2Attrs.add(new Pair<>(att.first, eqc));
        }
        for (String n : s.nodes) {
            String eqc = lookupNode("left", n, node_map);
            nodes.add(eqc);
            inj1Node.add(new Pair<>(n, eqc));
        }
        for (String n : t.nodes) {
            String eqc = lookupNode("right", n, node_map);
            nodes.add(eqc);
            inj2Node.add(new Pair<>(n, eqc));
        }
        for (Triple<String, String, String> n : s.arrows) {
            String eqc1 = lookupNode("left", n.second, node_map);
            String eqc2 = lookupNode("left", n.third, node_map);
            arrows.add(new Triple<>("left_" + n.first, eqc1, eqc2));
            List<String> l = new LinkedList<>();
            l.add(eqc1);
            l.add("left_" + n.first);
            inj1Arrows.add(new Pair<>(n.first, l));
        }
        for (Triple<String, String, String> n : t.arrows) {
            String eqc1 = lookupNode("right", n.second, node_map);
            String eqc2 = lookupNode("right", n.third, node_map);
            arrows.add(new Triple<>("right_" + n.first, eqc1, eqc2));
            List<String> l = new LinkedList<>();
            l.add(eqc1);
            l.add("right_" + n.first);
            inj2Arrows.add(new Pair<>(n.first, l));
        }
        for (Pair<List<String>, List<String>> eq : s.eqs) {
            List<String> lhs = new LinkedList<>();
            lhs.add(lookupNode("left", eq.first.get(0), node_map));
            for (int i = 1; i < eq.first.size(); i++) {
                lhs.add("left_" + eq.first.get(i));
            }
            List<String> rhs = new LinkedList<>();
            rhs.add(lookupNode("left", eq.second.get(0), node_map));
            for (int i = 1; i < eq.second.size(); i++) {
                rhs.add("left_" + eq.second.get(i));
            }
            eqs.add(new Pair<>(lhs, rhs));
        }
        for (Pair<List<String>, List<String>> eq : t.eqs) {
            List<String> lhs = new LinkedList<>();
            lhs.add(lookupNode("right", eq.first.get(0), node_map));
            for (int i = 1; i < eq.first.size(); i++) {
                lhs.add("right_" + eq.first.get(i));
            }
            List<String> rhs = new LinkedList<>();
            rhs.add(lookupNode("right", eq.second.get(0), node_map));
            for (int i = 1; i < eq.second.size(); i++) {
                rhs.add("right_" + eq.second.get(i));
            }
            eqs.add(new Pair<>(lhs, rhs));
        }
        Const x = new Const(new LinkedList<>(nodes), new LinkedList<>(attrs), new LinkedList<>(arrows), new LinkedList<>(eqs));
        MapExp.Const inj1 = new MapExp.Const(inj1Node, inj1Attrs, inj1Arrows, s, x);
        MapExp.Const inj2 = new MapExp.Const(inj2Node, inj2Attrs, inj2Arrows, t, x);
        switch(e.kind) {
            case "delta sigma forward":
                {
                    FullQueryExp q = new Comp(new Sigma(inj2), new Delta(inj1));
                    return q;
                }
            case "delta pi forward":
                {
                    FullQueryExp q = new Comp(new Pi(inj2), new Delta(inj1));
                    return q;
                }
            case "delta sigma backward":
                {
                    FullQueryExp q = new Comp(new Sigma(inj1), new Delta(inj2));
                    return q;
                }
            case "delta pi backward":
                FullQueryExp q = new Comp(new Pi(inj1), new Delta(inj2));
                return q;
            default:
                break;
        }
        throw new RuntimeException("Unknown kind: " + e.kind);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex.getLocalizedMessage());
    }
}
Also used : Sigma(catdata.fql.decl.FullQueryExp.Sigma) Comp(catdata.fql.decl.FullQueryExp.Comp) Pi(catdata.fql.decl.FullQueryExp.Pi) Pair(catdata.Pair) Const(catdata.fql.decl.SigExp.Const) Triple(catdata.Triple) Delta(catdata.fql.decl.FullQueryExp.Delta)

Aggregations

Pair (catdata.Pair)1 Triple (catdata.Triple)1 Comp (catdata.fql.decl.FullQueryExp.Comp)1 Delta (catdata.fql.decl.FullQueryExp.Delta)1 Pi (catdata.fql.decl.FullQueryExp.Pi)1 Sigma (catdata.fql.decl.FullQueryExp.Sigma)1 Const (catdata.fql.decl.SigExp.Const)1