Search in sources :

Example 1 with OplSCHEMA0

use of catdata.opl.OplExp.OplSCHEMA0 in project fql by CategoricalData.

the class OplOps method visit.

@Override
public OplObject visit(Program<OplExp> env, OplColim e) {
    OplObject o = ENV.get(e.name);
    if (!(o instanceof OplGraph)) {
        throw new RuntimeException("Not a graph: " + e.name);
    }
    OplGraph<String, String> shape = (OplGraph<String, String>) o;
    OplObject base0 = ENV.get(e.base);
    String typeSide;
    if (base0 instanceof OplSig) {
        typeSide = e.base;
        OplUnion u0 = new OplUnion(new LinkedList<>(shape.nodes), typeSide);
        OplObject u1 = u0.accept(env, this);
        OplSchema<String, String, String> u = (OplSchema<String, String, String>) u1;
        Map<String, Set<String>> equivs = new HashMap<>();
        Map<String, String> equivs0 = new HashMap<>();
        for (String schname : shape.nodes) {
            OplSchema<String, String, String> sch = (OplSchema<String, String, String>) ENV.get(schname);
            for (String ename : sch.entities) {
                Set<String> set = new HashSet<>();
                set.add(schname + "_" + ename);
                equivs.put(schname + "_" + ename, set);
            }
        }
        // : type check colimit
        for (String mname : shape.edges.keySet()) {
            Pair<String, String> mt = shape.edges.get(mname);
            String s = mt.first;
            String t = mt.second;
            OplSchema<String, String, String> s0 = (OplSchema<String, String, String>) ENV.get(s);
            OplTyMapping<String, String, String, String, String> m0 = (OplTyMapping<String, String, String, String, String>) ENV.get(mname);
            if (!m0.src0.equals(s)) {
                throw new RuntimeException("Source of " + m0 + " is " + m0.src + " and not " + s + "as expected");
            }
            if (!m0.dst0.equals(t)) {
                throw new RuntimeException("Target of " + m0 + " is " + m0.dst + " and not " + t + "as expected");
            }
            for (String ob : s0.entities) {
                String ob0 = m0.m.sorts.get(ob);
                Set<String> set1 = equivs.get(s + "_" + ob);
                Set<String> set2 = equivs.get(t + "_" + ob0);
                set1.addAll(set2);
                equivs.put(s + "_" + ob, set1);
                equivs.put(t + "_" + ob0, set1);
            }
        }
        for (String k : equivs.keySet()) {
            List<String> v = new LinkedList<>(equivs.get(k));
            v.sort(String.CASE_INSENSITIVE_ORDER);
            equivs0.put(k, Util.sep(v, "__"));
        }
        Set<String> entities = new HashSet<>(equivs0.values());
        Map<String, Pair<List<String>, String>> edges = new HashMap<>();
        Map<String, Pair<List<String>, String>> attrs = new HashMap<>();
        List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> pathEqs = new LinkedList<>();
        List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> obsEqs = new LinkedList<>();
        Function<String, String> fun = x -> {
            if (equivs0.containsKey(x)) {
                return equivs0.get(x);
            }
            return x;
        };
        for (String edge : u.toSchema0().edges.keySet()) {
            Pair<List<String>, String> ty = u.toSchema0().edges.get(edge);
            edges.put(edge, new Pair<>(ty.first.stream().map(fun).collect(Collectors.toList()), fun.apply(ty.second)));
        }
        for (String attr : u.toSchema0().attrs.keySet()) {
            Pair<List<String>, String> ty = u.toSchema0().attrs.get(attr);
            attrs.put(attr, new Pair<>(ty.first.stream().map(fun).collect(Collectors.toList()), fun.apply(ty.second)));
        }
        for (Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>> eq : u.toSchema0().pathEqs) {
            OplCtx<String, String> ctx = new OplCtx<>(eq.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(x.second))).collect(Collectors.toList()));
            pathEqs.add(new Triple<>(ctx, fun2(equivs0, eq.second), fun2(equivs0, eq.third)));
        }
        for (Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>> eq : u.toSchema0().obsEqs) {
            OplCtx<String, String> ctx = new OplCtx<>(eq.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(x.second))).collect(Collectors.toList()));
            obsEqs.add(new Triple<>(ctx, fun2(equivs0, eq.second), fun2(equivs0, eq.third)));
        }
        for (String mname : shape.edges.keySet()) {
            Pair<String, String> mt = shape.edges.get(mname);
            String s = mt.first;
            String t = mt.second;
            OplSchema<String, String, String> s0 = (OplSchema<String, String, String>) ENV.get(s);
            // OplSchema<String, String, String> t0 = (OplSchema<String,
            // String, String>) ENV.get(t);
            OplTyMapping<String, String, String, String, String> m0 = (OplTyMapping<String, String, String, String, String>) ENV.get(mname);
            for (String edge : s0.projE().symbols.keySet()) {
                Pair<OplCtx<String, String>, OplTerm<String, String>> edge2 = m0.m.symbols.get(edge);
                List<OplTerm<String, String>> args = edge2.first.vars0.keySet().stream().map((Function<String, OplTerm<String, String>>) OplTerm::new).collect(Collectors.toList());
                OplTerm<String, String> lhs = fun2(equivs0, new OplTerm<>(s + "_" + edge, args));
                OplCtx<String, String> ctx = new OplCtx<>(edge2.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(s + "_" + x.second))).collect(Collectors.toList()));
                OplTerm<String, String> rhs = fun2(equivs0, prepend(t, edge2.second));
                pathEqs.add(new Triple<>(ctx, lhs, rhs));
            }
            for (String edge : s0.projA().symbols.keySet()) {
                Pair<OplCtx<String, String>, OplTerm<String, String>> edge2 = m0.m.symbols.get(edge);
                List<OplTerm<String, String>> args = edge2.first.vars0.keySet().stream().map((Function<String, OplTerm<String, String>>) OplTerm::new).collect(Collectors.toList());
                OplTerm<String, String> lhs = fun2(equivs0, new OplTerm<>(s + "_" + edge, args));
                OplCtx<String, String> ctx = new OplCtx<>(edge2.first.values2().stream().map(x -> new Pair<>(x.first, fun.apply(s + "_" + x.second))).collect(Collectors.toList()));
                OplTerm<String, String> rhs = fun2(equivs0, prepend(t, edge2.second));
                obsEqs.add(new Triple<>(ctx, lhs, rhs));
            }
        }
        OplSCHEMA0<String, String, String> ret = new OplSCHEMA0<>(new HashMap<>(), entities, edges, attrs, pathEqs, obsEqs, typeSide);
        OplSchema retsch = (OplSchema) ret.accept(env, this);
        e.compiled.put("Colimit", ret);
        for (String schname : shape.nodes) {
            OplSchema<String, String, String> sch = (OplSchema<String, String, String>) ENV.get(schname);
            Map<String, String> inj_sorts = new HashMap<>();
            Map<String, Pair<OplCtx<String, String>, OplTerm<String, String>>> inj_symbols = new HashMap<>();
            for (String ename : sch.entities) {
                inj_sorts.put(ename, fun.apply(schname + "_" + ename));
            }
            for (String c1 : sch.projEA().symbols.keySet()) {
                Pair<List<String>, String> t = sch.projEA().symbols.get(c1);
                List<Pair<String, String>> l = new LinkedList<>();
                List<OplTerm<String, String>> vs = new LinkedList<>();
                for (String s1 : t.first) {
                    String v = (String) retsch.sig.fr.next();
                    vs.add(new OplTerm<>(v));
                    l.add(new Pair<>(v, fun.apply(schname + "_" + s1)));
                }
                OplCtx<String, String> ctx = new OplCtx<>(l);
                OplTerm<String, String> value = fun2(equivs0, new OplTerm<>(schname + "_" + c1, vs));
                inj_symbols.put(c1, new Pair<>(ctx, value));
            }
            OplMapping<String, String, String, String, String> mapping = new OplMapping<>(inj_sorts, inj_symbols, schname, "Colimit");
            // : name of colimit
            OplTyMapping<String, String, String, String, String> tm = new OplTyMapping<>(schname, "Colimit", sch, retsch, mapping);
            tm.extend().validate(sch.sig, retsch.sig);
            e.compiled.put(schname + "To" + "Colimit", mapping);
        }
        return e;
    } else if (base0 instanceof OplSchema) {
        OplSchema sch2 = (OplSchema) base0;
        // OplSCHEMA0 sch = sch2.toSchema0();
        List<Pair<OplTerm<Chc<String, String>, String>, OplTerm<Chc<String, String>, String>>> equations = new LinkedList<>();
        // List<Pair<OplTerm<Object, String>, OplTerm<Object, String>>> equations1 = new LinkedList<>();
        Map<String, String> gens = new HashMap<>();
        Map<String, Integer> prec = new HashMap<>();
        for (String s : shape.nodes) {
            OplInst<String, String, String, String> toAdd = (OplInst<String, String, String, String>) ENV.get(s);
            for (Object gen : toAdd.P.gens.keySet()) {
                Object ty = toAdd.P.gens.get(gen);
                gens.put(s + "_" + gen, ty.toString());
            }
            for (Object gen : toAdd.P.prec.keySet()) {
                if (toAdd.P.gens.keySet().contains(gen)) {
                    if (toAdd.P.prec.containsKey(gen)) {
                        prec.put(s + "_" + gen, toAdd.P.prec.get(gen));
                    }
                } else {
                    if (toAdd.P.prec.containsKey(gen)) {
                        prec.put(toAdd.P.S + "_" + gen, toAdd.P.prec.get(gen));
                    }
                }
            }
            for (Pair<OplTerm<Chc<String, String>, String>, OplTerm<Chc<String, String>, String>> tr : toAdd.P.equations) {
                OplTerm lhs1 = prepend3(s, tr.first);
                OplTerm rhs1 = prepend3(s, tr.second);
                equations.add(new Pair<>(lhs1, rhs1));
            }
        }
        for (String mname : shape.edges.keySet()) {
            Pair<String, String> mt = shape.edges.get(mname);
            String s = mt.first;
            String t = mt.second;
            OplInst<String, String, String, String> s0 = (OplInst<String, String, String, String>) ENV.get(s);
            // OplSchema<String, String, String> t0 = (OplSchema<String,
            // String, String>) ENV.get(t);
            OplPresTrans<String, String, String, String, String> m0 = (OplPresTrans<String, String, String, String, String>) ENV.get(mname);
            for (String edge : s0.projE().gens.keySet()) {
                Pair<OplCtx<String, String>, OplTerm<Chc<String, String>, String>> edge2 = m0.mapping.symbols.get(Chc.inRight(edge));
                OplTerm<Chc<String, String>, String> lhs = new OplTerm<>(Chc.inRight(s + "_" + edge), new LinkedList<>());
                equations.add(new Pair<>(lhs, prepend3(t, edge2.second)));
            }
        }
        OplPres<String, String, String, String> pres = new OplPres<>(prec, e.base, sch2.sig, gens, equations);
        OplInst<String, String, String, String> colimInst = new OplInst<>(e.base, "?", null);
        colimInst.validate(sch2, pres, null);
        e.compiled.put("ColimitInstance", colimInst);
        for (String s : shape.nodes) {
            OplInst<String, String, String, String> toAdd = (OplInst<String, String, String, String>) ENV.get(s);
            Map<String, Map<String, OplTerm<Chc<String, String>, String>>> inj_map = new HashMap<>();
            for (String entity : toAdd.S.entities) {
                inj_map.put(entity, new HashMap<>());
            }
            for (Object gen : toAdd.P.gens.keySet()) {
                Object ty = toAdd.P.gens.get(gen);
                gens.put(s + "_" + gen, ty.toString());
                Map<String, OplTerm<Chc<String, String>, String>> m = inj_map.get(ty);
                OplTerm<Chc<String, String>, String> term = new OplTerm<>(Chc.inRight(s + "_" + gen), new LinkedList<>());
                m.put((String) gen, term);
            }
            // OplPresTrans
            OplPresTrans<String, String, String, String, String> inj = new OplPresTrans<>(inj_map, s, "ColimitInstance", toAdd.P, pres);
            e.compiled.put(s + "ToColimitInstance", inj);
        }
        return colimInst;
    }
    throw new RuntimeException("Report your program to Ryan");
}
Also used : OplPres(catdata.opl.OplExp.OplPres) OplTyMapping(catdata.opl.OplExp.OplTyMapping) OplGround(catdata.opl.OplExp.OplGround) OplExpVisitor(catdata.opl.OplExp.OplExpVisitor) OplPushoutSch(catdata.opl.OplExp.OplPushoutSch) Map(java.util.Map) Environment(catdata.Environment) OplSchema(catdata.opl.OplExp.OplSchema) OplColim(catdata.opl.OplExp.OplColim) OplPushout(catdata.opl.OplExp.OplPushout) OplSetInst(catdata.opl.OplExp.OplSetInst) Pair(catdata.Pair) OplPushoutBen(catdata.opl.OplExp.OplPushoutBen) OplInst(catdata.opl.OplExp.OplInst) Set(java.util.Set) Collectors(java.util.stream.Collectors) OplChaseExp(catdata.opl.OplExp.OplChaseExp) OplDelta(catdata.opl.OplExp.OplDelta) List(java.util.List) OplUnion(catdata.opl.OplExp.OplUnion) Program(catdata.Program) OplId(catdata.opl.OplExp.OplId) Triple(catdata.Triple) OplSat(catdata.opl.OplExp.OplSat) DefunctGlobalOptions(catdata.ide.DefunctGlobalOptions) OplJavaInst(catdata.opl.OplExp.OplJavaInst) OplDelta0(catdata.opl.OplExp.OplDelta0) Chc(catdata.Chc) HashMap(java.util.HashMap) OplApply(catdata.opl.OplExp.OplApply) OplDistinct(catdata.opl.OplExp.OplDistinct) OplUberSat(catdata.opl.OplExp.OplUberSat) Function(java.util.function.Function) OplSig(catdata.opl.OplExp.OplSig) HashSet(java.util.HashSet) OplFlower(catdata.opl.OplExp.OplFlower) OplPragma(catdata.opl.OplExp.OplPragma) OplPresTrans(catdata.opl.OplExp.OplPresTrans) OplSetTrans(catdata.opl.OplExp.OplSetTrans) LinkedList(java.util.LinkedList) Util(catdata.Util) OplSCHEMA0(catdata.opl.OplExp.OplSCHEMA0) OplEval(catdata.opl.OplExp.OplEval) OplPivot(catdata.opl.OplExp.OplPivot) OplSchemaProj(catdata.opl.OplExp.OplSchemaProj) OplGraph(catdata.opl.OplExp.OplGraph) OplUnSat(catdata.opl.OplExp.OplUnSat) OplInst0(catdata.opl.OplExp.OplInst0) OplSigma(catdata.opl.OplExp.OplSigma) OplVar(catdata.opl.OplExp.OplVar) OplMapping(catdata.opl.OplExp.OplMapping) Collections(java.util.Collections) OplSchema(catdata.opl.OplExp.OplSchema) HashMap(java.util.HashMap) OplPres(catdata.opl.OplExp.OplPres) List(java.util.List) LinkedList(java.util.LinkedList) OplGraph(catdata.opl.OplExp.OplGraph) HashSet(java.util.HashSet) LinkedList(java.util.LinkedList) Triple(catdata.Triple) Map(java.util.Map) HashMap(java.util.HashMap) Set(java.util.Set) HashSet(java.util.HashSet) OplInst(catdata.opl.OplExp.OplInst) Function(java.util.function.Function) OplTyMapping(catdata.opl.OplExp.OplTyMapping) Pair(catdata.Pair) OplPresTrans(catdata.opl.OplExp.OplPresTrans) OplUnion(catdata.opl.OplExp.OplUnion) OplSig(catdata.opl.OplExp.OplSig) OplSCHEMA0(catdata.opl.OplExp.OplSCHEMA0) OplMapping(catdata.opl.OplExp.OplMapping) Chc(catdata.Chc)

Example 2 with OplSCHEMA0

use of catdata.opl.OplExp.OplSCHEMA0 in project fql by CategoricalData.

the class OplOps method visit.

// private static int temp = 0;
@Override
public OplObject visit(Program<OplExp> env, OplUnion e) {
    String typeSide;
    OplSchema schema = null;
    Map<String, Integer> prec = new HashMap<>();
    OplObject o = ENV.get(e.base);
    if (o instanceof OplSchema) {
        schema = (OplSchema) o;
        typeSide = schema.forSchema0;
        if (typeSide == null) {
            throw new RuntimeException(e.base + " is not a SCHEMA literal");
        }
    } else if (o instanceof OplSig) {
        typeSide = e.base;
    } else {
        throw new RuntimeException("Report this program to Ryan");
    }
    if (schema == null) {
        Set<String> entities = new HashSet<>();
        Map<String, Pair<List<String>, String>> edges = new HashMap<>();
        Map<String, Pair<List<String>, String>> attrs = new HashMap<>();
        List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> pathEqs = new LinkedList<>();
        List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> obsEqs = new LinkedList<>();
        for (String s : e.names) {
            OplExp exp = env.exps.get(s);
            if (exp == null) {
                throw new RuntimeException("Missing expression: " + s);
            }
            if (!(exp instanceof OplSCHEMA0)) {
                throw new RuntimeException("Not a schema: " + s);
            }
            OplSCHEMA0<String, String, String> toAdd = (OplSCHEMA0<String, String, String>) exp;
            if (!toAdd.typeSide.equals(typeSide)) {
                throw new RuntimeException("not all equal typesides in " + e);
            }
            for (Object entity : toAdd.entities) {
                String proposed = s + "_" + entity;
                if (entities.contains(proposed)) {
                    throw new RuntimeException("name clash: " + entity);
                }
                entities.add(proposed);
            }
            for (Object edge : toAdd.edges.keySet()) {
                String proposed = s + "_" + edge;
                if (edges.containsKey(proposed)) {
                    throw new RuntimeException("name clash: " + edge);
                }
                edges.put(proposed, new Pair<>(Util.singList(s + "_" + toAdd.edges.get(edge).first.get(0)), s + "_" + toAdd.edges.get(edge).second));
                if (toAdd.prec.containsKey(edge)) {
                    prec.put(proposed, toAdd.prec.get(edge));
                }
            }
            for (Object att : toAdd.attrs.keySet()) {
                String proposed = s + "_" + att;
                if (attrs.containsKey(proposed)) {
                    throw new RuntimeException("name clash: " + att);
                }
                attrs.put(proposed, new Pair<>(Util.singList(s + "_" + toAdd.attrs.get(att).first.get(0)), toAdd.attrs.get(att).second));
                if (toAdd.prec.containsKey(att)) {
                    prec.put(proposed, toAdd.prec.get(att));
                }
            }
            for (Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>> tr : toAdd.pathEqs) {
                String v = tr.first.names().get(0);
                String t = s + "_" + tr.first.values().get(0);
                OplCtx<String, String> ctx = new OplCtx<>(Util.singList(new Pair<>(v, t)));
                OplTerm<String, String> lhs1 = prepend(s, tr.second);
                OplTerm<String, String> rhs1 = prepend(s, tr.third);
                pathEqs.add(new Triple<>(ctx, lhs1, rhs1));
            }
            for (Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>> tr : toAdd.obsEqs) {
                String v = tr.first.names().get(0);
                String t = s + "_" + tr.first.values().get(0);
                OplCtx<String, String> ctx = new OplCtx<>(Util.singList(new Pair<>(v, t)));
                OplTerm<String, String> lhs1 = prepend(s, tr.second);
                OplTerm<String, String> rhs1 = prepend(s, tr.third);
                obsEqs.add(new Triple<>(ctx, lhs1, rhs1));
            }
        }
        return new OplSCHEMA0<>(prec, entities, edges, attrs, pathEqs, obsEqs, typeSide).accept(env, this);
    } else {
        for (String s : e.names) {
            OplObject exp = ENV.get(s);
            if (!(exp instanceof OplInst)) {
                throw new RuntimeException("Not an instance: " + s);
            }
            OplInst<String, String, String, String> toAdd = (OplInst<String, String, String, String>) exp;
            if (!toAdd.S.equals(schema)) {
                throw new RuntimeException(s + " is not on schema " + schema);
            }
        }
        OplSig<String, String, String> sig = schema.sig;
        Map<String, String> gens = new HashMap<>();
        List<Pair<OplTerm<Object, String>, OplTerm<Object, String>>> equations1 = new LinkedList<>();
        // List<Pair<OplTerm<Chc<String,String>, String>, OplTerm<Chc<String,String>, String>>> equations2 = new LinkedList<>();
        for (String s : e.names) {
            OplExp exp = env.exps.get(s);
            OplInst0<String, String, String, String> toAdd = (OplInst0<String, String, String, String>) exp;
            for (Object gen : toAdd.P.gens.keySet()) {
                Object ty = toAdd.P.gens.get(gen);
                gens.put(s + "_" + gen, /* toAdd.P.S + "_" + */
                ty.toString());
            }
            for (Object gen : toAdd.P.prec.keySet()) {
                if (toAdd.P.gens.keySet().contains(gen)) {
                    if (toAdd.P.prec.containsKey(gen)) {
                        prec.put(s + "_" + gen, toAdd.P.prec.get(gen));
                    }
                } else {
                    if (toAdd.P.prec.containsKey(gen)) {
                        prec.put(toAdd.P.S + "_" + gen, toAdd.P.prec.get(gen));
                    }
                }
            }
            for (Pair<OplTerm<Chc<String, String>, String>, OplTerm<Chc<String, String>, String>> tr : toAdd.P.equations) {
                OplTerm<Object, String> lhs1 = prepend2((OplTerm) tr.first, s, toAdd.P.gens.keySet());
                OplTerm<Object, String> rhs1 = prepend2((OplTerm) tr.second, s, toAdd.P.gens.keySet());
                equations1.add(new Pair<>(lhs1, rhs1));
            }
        }
        // OplPres<String, String, String, String> pres = new OplPres(prec, "_temp" + temp, sig, gens, equations2);
        OplPres<String, String, String, String> pres = new OplPres(prec, e.base, sig, gens, equations1);
        if (DefunctGlobalOptions.debug.opl.opl_prover_simplify_instances) {
            pres = pres.simplify();
        }
        // temp++;
        OplInst0 ret = new OplInst0<>(pres);
        OplObject x = (OplObject) ret.accept(env, this);
        return x;
    }
}
Also used : OplSchema(catdata.opl.OplExp.OplSchema) HashMap(java.util.HashMap) OplInst(catdata.opl.OplExp.OplInst) OplPres(catdata.opl.OplExp.OplPres) HashSet(java.util.HashSet) Pair(catdata.Pair) OplInst0(catdata.opl.OplExp.OplInst0) OplSig(catdata.opl.OplExp.OplSig) LinkedList(java.util.LinkedList) OplSCHEMA0(catdata.opl.OplExp.OplSCHEMA0) Triple(catdata.Triple)

Example 3 with OplSCHEMA0

use of catdata.opl.OplExp.OplSCHEMA0 in project fql by CategoricalData.

the class OplWarehouse method addBlanks.

// TODO aql alphabetize tabs
// TODO aql  heuristic: if something already there, leave it.  otherwise, create new
private void addBlanks() {
    switch(state) {
        case INITIAL:
            if (!bindings.containsKey(THEORY)) {
                bindings.put(THEORY, new OplSig<>(new VIt(), new HashMap<>(), new HashSet<>(), new HashMap<>(), new LinkedList<>()));
            }
            break;
        case THEORY:
            if (!bindings.containsKey(SHAPE)) {
                bindings.put(SHAPE, new OplGraph<String, String>(new HashSet<>(), new HashMap<>()));
            }
            break;
        case SHAPE:
            {
                OplGraph<String, String> shape = (OplGraph<String, String>) bindings.get(SHAPE);
                for (String en : shape.nodes) {
                    if (bindings.containsKey(SCHEMA + "_" + en)) {
                        // TODO: aql false
                        continue;
                    }
                    bindings.put(SCHEMA + "_" + en, new OplSCHEMA0<String, String, String>(new HashMap<>(), new HashSet<>(), new HashMap<>(), new HashMap<>(), new LinkedList<>(), new LinkedList<>(), THEORY));
                }
                break;
            }
        case SCHEMA:
            {
                OplGraph<String, String> shape = (OplGraph<String, String>) bindings.get(SHAPE);
                for (String ed : shape.edges.keySet()) {
                    if (bindings.containsKey(MAPPING + "_" + ed)) {
                        continue;
                    }
                    String src = SCHEMA + "_" + shape.edges.get(ed).first;
                    // String dst = SCHEMA + "_" + shape.edges.get(ed).second;
                    OplSCHEMA0<String, String, String> src0 = (OplSCHEMA0<String, String, String>) bindings.get(src);
                    Map<String, String> sorts = new HashMap<>();
                    for (String x : src0.entities) {
                        sorts.put(x, "?");
                    }
                    Map<String, Pair<OplCtx<String, String>, OplTerm<String, String>>> symbols = new HashMap<>();
                    for (String g : src0.attrs.keySet()) {
                        OplCtx<String, String> ctx = new OplCtx<>(Util.singList(new Pair<>("x", "?")));
                        OplTerm<String, String> term = new OplTerm<>("?");
                        symbols.put(g, new Pair<>(ctx, term));
                    }
                    for (String g : src0.edges.keySet()) {
                        OplCtx<String, String> ctx = new OplCtx<>(Util.singList(new Pair<>("t", "?")));
                        OplTerm<String, String> term = new OplTerm<>("?");
                        symbols.put(g, new Pair<>(ctx, term));
                    }
                    bindings.put(MAPPING + "_" + ed, new OplMapping<>(sorts, symbols, src, SCHEMA + "_" + shape.edges.get(ed).second));
                }
                break;
            }
        case MAPPING:
            {
                OplGraph<String, String> shape = (OplGraph<String, String>) bindings.get(SHAPE);
                Set<String> en2 = shape.nodes.stream().map(x -> SCHEMA + "_" + x).collect(Collectors.toSet());
                Map<String, Pair<String, String>> ed2 = new HashMap<>();
                for (String e : shape.edges.keySet()) {
                    Pair<String, String> x = shape.edges.get(e);
                    String l = SCHEMA + "_" + x.first;
                    ed2.put(MAPPING + "_" + e, new Pair<>(l, SCHEMA + "_" + x.second));
                }
                OplGraph<String, String> shape2 = new OplGraph<>(en2, ed2);
                // TODO: aql needs to compile to here
                computeColimit(bindings, shape2, THEORY);
                for (String en : shape.nodes) {
                    if (bindings.containsKey(INSTANCE + "_" + en)) {
                        // TODO aql false
                        continue;
                    }
                    bindings.put(INSTANCE + "_" + en, new OplInst0<>(new OplPres<>(new HashMap<>(), SCHEMA + "_" + en, null, new HashMap<>(), new LinkedList<>())));
                }
                break;
            }
        case INSTANCE:
            {
                OplGraph<String, String> shape = (OplGraph<String, String>) bindings.get(SHAPE);
                for (String ed : shape.edges.keySet()) {
                    String en = shape.edges.get(ed).first;
                    String en2 = shape.edges.get(ed).second;
                    String src = INSTANCE + "_" + ed + "_forward";
                    bindings.put(src, new OplSigma(MAPPING + "_" + ed, INSTANCE + "_" + en));
                    if (bindings.containsKey(TRANSFORM + "_" + ed + "_forward")) {
                        continue;
                    }
                    OplMapping<String, String, String, String, String> mmm = (OplMapping<String, String, String, String, String>) bindings.get(MAPPING + "_" + ed);
                    // TODO: aql factor getting into separate method, or use compiler
                    OplInst0<String, String, String, String> src1 = (OplInst0<String, String, String, String>) bindings.get(INSTANCE + "_" + en);
                    OplInst0<String, String, String, String> src0 = (OplInst0<String, String, String, String>) bindings.get(INSTANCE + "_" + en2);
                    OplSCHEMA0<String, String, String> src0_sch = (OplSCHEMA0<String, String, String>) bindings.get(src0.P.S);
                    OplSCHEMA0<String, String, String> src1_sch = (OplSCHEMA0<String, String, String>) bindings.get(src1.P.S);
                    Map<String, Map<String, OplTerm<Object, String>>> sorts = new HashMap<>();
                    for (String x : src0_sch.entities) {
                        sorts.put(x, new HashMap<>());
                    }
                    for (String g : src1.P.gens.keySet()) {
                        String gty = src1.P.gens.get(g);
                        String s2 = mmm.sorts.get(gty);
                        if (s2 == null) {
                            continue;
                        }
                        Map<String, OplTerm<Object, String>> symbols = sorts.get(s2);
                        if (symbols != null) {
                            symbols.put(g, new OplTerm<>("?"));
                        }
                    }
                    bindings.put(TRANSFORM + "_" + ed + "_forward", new OplPresTrans<String, String, String, String, String>(sorts, src, INSTANCE + "_" + shape.edges.get(ed).second));
                }
                break;
            }
        case TRANSFORM:
            OplGraph<String, String> shape = (OplGraph<String, String>) bindings.get(SHAPE);
            for (String en : shape.nodes) {
                bindings.put(INSTANCE + "_" + en + "_colimit", new OplSigma(MAPPING + "_" + SCHEMA + "_" + en + "_colimit", INSTANCE + "_" + en));
            }
            Map<String, Pair<String, String>> ed2 = new HashMap<>();
            for (String e : shape.edges.keySet()) {
                Pair<String, String> x = shape.edges.get(e);
                String l = INSTANCE + "_" + x.first + "_colimit";
                // String en = x.first.get(0);
                String en = x.second;
                bindings.put(TRANSFORM + "_" + e + "_colimit", new OplSigma(MAPPING + "_" + SCHEMA + "_" + en + "_colimit", TRANSFORM + "_" + e + "_forward"));
                ed2.put(TRANSFORM + "_" + e + "_colimit", new Pair<>(l, INSTANCE + "_" + x.second + "_colimit"));
            }
            Set<String> en2 = shape.nodes.stream().map(x -> INSTANCE + "_" + x + "_colimit").collect(Collectors.toSet());
            OplGraph<String, String> shape2 = new OplGraph<>(en2, ed2);
            bindings.put(SCHEMA + "_for_result", shape2);
            bindings.put(FINISHED, new OplExp.OplString("colimit " + SCHEMA + "_Colimit" + " " + SCHEMA + "_for_result"));
            // bindings.put(FINISHED + " all", new OplExp.OplString(complete().toString()));
            break;
        default:
            break;
    }
}
Also used : OplPres(catdata.opl.OplExp.OplPres) JSplitPane(javax.swing.JSplitPane) HashMap(java.util.HashMap) Function(java.util.function.Function) OplSig(catdata.opl.OplExp.OplSig) GridLayout(java.awt.GridLayout) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) WizardModel(catdata.ide.WizardModel) OplPresTrans(catdata.opl.OplExp.OplPresTrans) Map(java.util.Map) JTabbedPane(javax.swing.JTabbedPane) VIt(catdata.opl.OplParser.VIt) ChangeListener(javax.swing.event.ChangeListener) LinkedList(java.util.LinkedList) JComboBox(javax.swing.JComboBox) Pair(catdata.Pair) ChangeEvent(javax.swing.event.ChangeEvent) CodeTextPanel(catdata.ide.CodeTextPanel) Util(catdata.Util) Set(java.util.Set) BorderFactory(javax.swing.BorderFactory) Collectors(java.util.stream.Collectors) OplSCHEMA0(catdata.opl.OplExp.OplSCHEMA0) List(java.util.List) OplUnion(catdata.opl.OplExp.OplUnion) OplGraph(catdata.opl.OplExp.OplGraph) Program(catdata.Program) Triple(catdata.Triple) OplInst0(catdata.opl.OplExp.OplInst0) OplSigma(catdata.opl.OplExp.OplSigma) OplMapping(catdata.opl.OplExp.OplMapping) JPanel(javax.swing.JPanel) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OplGraph(catdata.opl.OplExp.OplGraph) HashSet(java.util.HashSet) Pair(catdata.Pair) OplSigma(catdata.opl.OplExp.OplSigma) VIt(catdata.opl.OplParser.VIt) OplPresTrans(catdata.opl.OplExp.OplPresTrans) OplInst0(catdata.opl.OplExp.OplInst0) LinkedList(java.util.LinkedList) OplSCHEMA0(catdata.opl.OplExp.OplSCHEMA0) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) OplMapping(catdata.opl.OplExp.OplMapping)

Example 4 with OplSCHEMA0

use of catdata.opl.OplExp.OplSCHEMA0 in project fql by CategoricalData.

the class OplDriver method inferPrec.

private static void inferPrec(Program<OplExp> init) {
    int curPrec = 10000;
    // constants < generators < attributes < foreign keys
    for (String k : init.order) {
        OplExp v = init.exps.get(k);
        if (v instanceof OplSig) {
            OplSig sig = (OplSig) v;
            sig.prec = new HashMap<>();
            for (Object c : sig.symbols.keySet()) {
                sig.prec.put(c, curPrec++);
            }
        }
    }
    for (String k : init.order) {
        OplExp v = init.exps.get(k);
        if (v instanceof OplInst0) {
            OplInst0 sig = (OplInst0) v;
            sig.P.prec = new HashMap<>();
            for (Object c : sig.P.gens.keySet()) {
                sig.P.prec.put(c, curPrec++);
            }
        }
    }
    for (String k : init.order) {
        OplExp v = init.exps.get(k);
        if (v instanceof OplSCHEMA0) {
            OplSCHEMA0 sig = (OplSCHEMA0) v;
            sig.prec = new HashMap<>();
            for (Object c : sig.attrs.keySet()) {
                sig.prec.put(c, curPrec++);
            }
        }
    }
    for (String k : init.order) {
        OplExp v = init.exps.get(k);
        if (v instanceof OplSCHEMA0) {
            OplSCHEMA0 sig = (OplSCHEMA0) v;
            for (Object c : sig.edges.keySet()) {
                sig.prec.put(c, curPrec++);
            }
        }
    }
}
Also used : OplInst0(catdata.opl.OplExp.OplInst0) OplSig(catdata.opl.OplExp.OplSig) OplSCHEMA0(catdata.opl.OplExp.OplSCHEMA0)

Example 5 with OplSCHEMA0

use of catdata.opl.OplExp.OplSCHEMA0 in project fql by CategoricalData.

the class OplParser method toSCHEMA.

private static OplExp toSCHEMA(Object ox) {
    Tuple4 oy = (Tuple4) ox;
    String ts = (String) oy.d;
    org.jparsec.functors.Pair newobj = (org.jparsec.functors.Pair) oy.b;
    List<String> imports = newobj.a == null ? new LinkedList<>() : (List<String>) ((org.jparsec.functors.Pair) newobj.a).b;
    Tuple5 t = (Tuple5) newobj.b;
    Tuple3 a = (Tuple3) t.a;
    Tuple3 b = (Tuple3) t.b;
    Tuple3 c = (Tuple3) t.c;
    Tuple3 d = (Tuple3) t.d;
    Tuple3 e = (Tuple3) t.e;
    Set<String> sorts = a == null ? new HashSet<>() : new HashSet<>((Collection<String>) a.b);
    List<Tuple3> symbolsE0 = b == null ? new LinkedList<>() : (List<Tuple3>) b.b;
    List<Tuple3> symbolsA0 = c == null ? new LinkedList<>() : (List<Tuple3>) c.b;
    List<org.jparsec.functors.Pair> equationsE0 = c == null ? new LinkedList<>() : (List<org.jparsec.functors.Pair>) d.b;
    List<org.jparsec.functors.Pair> equationsA0 = c == null ? new LinkedList<>() : (List<org.jparsec.functors.Pair>) e.b;
    Map<String, Pair<List<String>, String>> symbolsE = new HashMap<>();
    Map<String, Pair<List<String>, String>> symbolsA = new HashMap<>();
    Map<String, Pair<List<String>, String>> symbolsEA = new HashMap<>();
    Map<String, Integer> prec = new HashMap<>();
    for (Tuple3 x : symbolsE0) {
        String dom;
        List<String> args;
        if (x.c instanceof Tuple3) {
            Tuple3 zzz = (Tuple3) x.c;
            args = (List<String>) zzz.a;
            dom = (String) zzz.c;
        } else {
            dom = (String) x.c;
            args = new LinkedList<>();
        }
        List<org.jparsec.functors.Pair> name0s = (List<org.jparsec.functors.Pair>) x.a;
        for (org.jparsec.functors.Pair name0 : name0s) {
            String name = (String) name0.a;
            if (name0.b != null) {
                org.jparsec.functors.Pair zzz = (org.jparsec.functors.Pair) name0.b;
                Integer i = (Integer) zzz.b;
                prec.put(name, i);
            }
            if (symbolsE.containsKey(name)) {
                throw new DoNotIgnore("Duplicate symbol " + name);
            }
            symbolsE.put(name, new Pair<>(args, dom));
            symbolsEA.put(name, new Pair<>(args, dom));
        }
    }
    for (Tuple3 x : symbolsA0) {
        String dom;
        List<String> args;
        if (x.c instanceof Tuple3) {
            Tuple3 zzz = (Tuple3) x.c;
            args = (List<String>) zzz.a;
            dom = (String) zzz.c;
        } else {
            dom = (String) x.c;
            args = new LinkedList<>();
        }
        List<org.jparsec.functors.Pair> name0s = (List<org.jparsec.functors.Pair>) x.a;
        for (org.jparsec.functors.Pair name0 : name0s) {
            String name = (String) name0.a;
            if (name0.b != null) {
                org.jparsec.functors.Pair zzz = (org.jparsec.functors.Pair) name0.b;
                Integer i = (Integer) zzz.b;
                prec.put(name, i);
            }
            if (symbolsA.containsKey(name)) {
                throw new DoNotIgnore("Duplicate symbol " + name);
            }
            symbolsA.put(name, new Pair<>(args, dom));
            symbolsEA.put(name, new Pair<>(args, dom));
        }
    }
    // /////////////
    List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> equationsE = new LinkedList<>();
    List<Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>>> equationsA = new LinkedList<>();
    for (org.jparsec.functors.Pair<Tuple3, Tuple3> x : equationsE0) {
        List<Tuple3> fa = x.a == null ? new LinkedList<>() : (List<Tuple3>) x.a.b;
        OplCtx<String, String> ctx = toCtx(fa);
        Tuple3 eq = x.b;
        OplTerm lhs = toTerm(ctx.names(), consts(symbolsEA), eq.a, false);
        OplTerm rhs = toTerm(ctx.names(), consts(symbolsEA), eq.c, false);
        equationsE.add(new Triple<>(ctx, lhs, rhs));
    }
    for (org.jparsec.functors.Pair<Tuple3, Tuple3> x : equationsA0) {
        List<Tuple3> fa = x.a == null ? new LinkedList<>() : (List<Tuple3>) x.a.b;
        OplCtx<String, String> ctx = toCtx(fa);
        Tuple3 eq = x.b;
        OplTerm lhs = toTerm(ctx.names(), consts(symbolsEA), eq.a, false);
        OplTerm rhs = toTerm(ctx.names(), consts(symbolsEA), eq.c, false);
        equationsA.add(new Triple<>(ctx, lhs, rhs));
    }
    OplSCHEMA0 ret = new OplSCHEMA0(prec, sorts, symbolsE, symbolsA, equationsE, equationsA, ts);
    ret.imports = new HashSet<>(imports);
    return ret;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) LinkedList(java.util.LinkedList) OplSCHEMA0(catdata.opl.OplExp.OplSCHEMA0) Tuple4(org.jparsec.functors.Tuple4) Triple(catdata.Triple) Tuple5(org.jparsec.functors.Tuple5) Tuple3(org.jparsec.functors.Tuple3) Collection(java.util.Collection)

Aggregations

OplSCHEMA0 (catdata.opl.OplExp.OplSCHEMA0)6 Pair (catdata.Pair)5 Triple (catdata.Triple)5 OplInst0 (catdata.opl.OplExp.OplInst0)5 OplSig (catdata.opl.OplExp.OplSig)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 OplPres (catdata.opl.OplExp.OplPres)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Program (catdata.Program)3 Util (catdata.Util)3 OplGraph (catdata.opl.OplExp.OplGraph)3 OplMapping (catdata.opl.OplExp.OplMapping)3 OplPresTrans (catdata.opl.OplExp.OplPresTrans)3 OplSigma (catdata.opl.OplExp.OplSigma)3 OplUnion (catdata.opl.OplExp.OplUnion)3 Map (java.util.Map)3 Set (java.util.Set)3 Function (java.util.function.Function)3