use of catdata.opl.OplParser.VIt 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;
}
}
use of catdata.opl.OplParser.VIt in project fql by CategoricalData.
the class OplWarehouse method computeColimit.
// /////////////////////////////////////////////////////////////////////////
private static void computeColimit(Map<String, OplExp> env, OplGraph<String, String> shape, String base) {
OplUnion u0 = new OplUnion(new LinkedList<>(shape.nodes), base);
OplObject u1 = union(env, u0);
OplSCHEMA0<String, String, String> u = (OplSCHEMA0<String, String, String>) u1;
Map<String, Set<String>> equivs = new HashMap<>();
Map<String, String> equivs0 = new HashMap<>();
for (String schname : shape.nodes) {
if (!(env.get(schname) instanceof OplSCHEMA0)) {
throw new RuntimeException("Not a SCHEMA: " + schname);
}
OplSCHEMA0<String, String, String> sch = (OplSCHEMA0<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);
}
}
// TODO: aql 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;
OplSCHEMA0<String, String, String> s0 = (OplSCHEMA0<String, String, String>) env.get(s);
OplMapping<String, String, String, String, String> m0 = (OplMapping<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.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.edges.keySet()) {
Pair<List<String>, String> ty = u.edges.get(edge);
edges.put(edge, new Pair<>(ty.first.stream().map(fun).collect(Collectors.toList()), fun.apply(ty.second)));
}
for (String attr : u.attrs.keySet()) {
Pair<List<String>, String> ty = u.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.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, OplOps.fun2(equivs0, eq.second), OplOps.fun2(equivs0, eq.third)));
}
for (Triple<OplCtx<String, String>, OplTerm<String, String>, OplTerm<String, String>> eq : u.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, OplOps.fun2(equivs0, eq.second), OplOps.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;
OplSCHEMA0<String, String, String> s0 = (OplSCHEMA0<String, String, String>) env.get(s);
// OplSchema<String, String, String> t0 = (OplSchema<String, String, String>) ENV.get(t);
OplMapping<String, String, String, String, String> m0 = (OplMapping<String, String, String, String, String>) env.get(mname);
for (String edge : s0.edges.keySet()) {
Pair<OplCtx<String, String>, OplTerm<String, String>> edge2 = m0.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 = OplOps.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 = OplOps.fun2(equivs0, OplOps.prepend(t, edge2.second));
pathEqs.add(new Triple<>(ctx, lhs, rhs));
}
for (String edge : s0.attrs.keySet()) {
Pair<OplCtx<String, String>, OplTerm<String, String>> edge2 = m0.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 = OplOps.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 = OplOps.fun2(equivs0, OplOps.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, base);
env.putIfAbsent(SCHEMA + "_Colimit", ret);
for (String schname : shape.nodes) {
OplSCHEMA0<String, String, String> sch = (OplSCHEMA0<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.attrs.keySet()) {
Pair<List<String>, String> t = sch.attrs.get(c1);
List<Pair<String, String>> l = new LinkedList<>();
List<OplTerm<String, String>> vs = new LinkedList<>();
for (String s1 : t.first) {
String v = new VIt().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 = OplOps.fun2(equivs0, new OplTerm<>(schname + "_" + c1, vs));
inj_symbols.put(c1, new Pair<>(ctx, value));
}
for (String c1 : sch.edges.keySet()) {
Pair<List<String>, String> t = sch.edges.get(c1);
List<Pair<String, String>> l = new LinkedList<>();
List<OplTerm<String, String>> vs = new LinkedList<>();
for (String s1 : t.first) {
String v = new VIt().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 = OplOps.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, SCHEMA + "_Colimit");
env.putIfAbsent(MAPPING + "_" + schname + "_colimit", mapping);
}
}
Aggregations