use of catdata.opl.OplExp.OplInst0 in project fql by CategoricalData.
the class OplOps method visit.
@Override
public OplObject visit(Program<OplExp> env, OplInst0 e) {
OplObject zzz = ENV.get(e.P.S);
if (!(zzz instanceof OplSchema)) {
throw new RuntimeException("Not a SCHEMA: " + e.P.S);
}
for (Object k2 : e.imports) {
String k = (String) k2;
OplExp o = env.exps.get(k);
OplInst0 a;
if (o instanceof OplInst0) {
a = (OplInst0) o;
} else if (o instanceof OplGround) {
a = ((OplGround) o).validate(((OplInst) ENV.get(k)).S);
} else {
throw new RuntimeException("Not an instance: " + k);
}
Util.putAllSafely(e.P.gens, a.P.gens);
e.P.equations.addAll(a.P.equations);
e.P.prec.putAll(a.P.prec);
// Util.putAllSafely(e.P.prec, a.P.prec);
}
OplPres P = (OplPres) visit(env, e.P);
OplInst ret = new OplInst(e.P.S, "?", "none");
OplObject S0 = ENV.get(e.P.S);
if (!(S0 instanceof OplSchema)) {
throw new RuntimeException("Not a schema: " + e.P.S);
}
OplSchema S = (OplSchema) S0;
ret.validate(S, P, null);
return ret;
}
use of catdata.opl.OplExp.OplInst0 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;
}
}
use of catdata.opl.OplExp.OplInst0 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.OplExp.OplInst0 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++);
}
}
}
}
use of catdata.opl.OplExp.OplInst0 in project fql by CategoricalData.
the class OplParser method toINSTANCE.
private static OplExp toINSTANCE(Object o) {
if (!o.toString().contains("INSTANCE")) {
throw new RuntimeException();
}
OplPres ex = toPresentation(o);
OplInst0 ret = new OplInst0(ex);
Tuple4 t = (Tuple4) o;
Tuple3 e = (Tuple3) t.b;
List<String> imports = e.a == null ? new LinkedList<>() : (List<String>) ((org.jparsec.functors.Pair) e.a).b;
ret.imports = new HashSet<>(imports);
return ret;
}
Aggregations