use of catdata.fql.decl.InstExp in project fql by CategoricalData.
the class FqlDisplay method handleInstanceFlowEdge.
private void handleInstanceFlowEdge(Object o) {
InstExp i = (InstExp) o;
Object f = i.accept(new Unit(), new InstExpVisitor<Object, Unit>() {
@Override
public MapExp visit(Unit env, Zero e) {
return null;
}
@Override
public MapExp visit(Unit env, One e) {
return null;
}
@Override
public MapExp visit(Unit env, Two e) {
throw new RuntimeException();
}
@Override
public MapExp visit(Unit env, Plus e) {
return null;
}
@Override
public MapExp visit(Unit env, Times e) {
return null;
}
@Override
public MapExp visit(Unit env, Exp e) {
throw new RuntimeException();
}
@Override
public MapExp visit(Unit env, Const e) {
return null;
}
@Override
public MapExp visit(Unit env, Delta e) {
return e.F;
}
@Override
public MapExp visit(Unit env, Sigma e) {
return e.F;
}
@Override
public MapExp visit(Unit env, Pi e) {
return e.F;
}
@Override
public MapExp visit(Unit env, FullSigma e) {
return e.F;
}
@Override
public Unit visit(Unit env, Relationalize e) {
return null;
}
@Override
public Unit visit(Unit env, External e) {
return null;
}
@Override
public Object visit(Unit env, Eval e) {
return e.q;
}
@Override
public Object visit(Unit env, FullEval e) {
return e.q;
}
@Override
public Object visit(Unit env, Kernel e) {
return null;
}
@Override
public Object visit(Unit env, Step e) {
// (Step) this should return a pair
return null;
}
});
if (f == null) {
return;
}
if (f instanceof QueryExp) {
QueryExp q = (QueryExp) f;
if (q instanceof QueryExp.Var) {
QueryExp.Var qq = (QueryExp.Var) q;
yyy.setSelectedValue(indices.get(qq.v), true);
return;
}
String k = FQLProgram.revLookup(prog.queries, q);
if (k != null) {
yyy.setSelectedValue(indices.get(k), true);
return;
}
String str = q.toString();
if (!extraInsts.contains(str)) {
Query view = q.toQuery(prog);
JPanel p = showQuery(prog, env, view);
x.add(p, str);
extraInsts.add(str);
}
yyy.clearSelection();
cl.show(x, str);
} else if (f instanceof FullQueryExp) {
FullQueryExp q = (FullQueryExp) f;
if (q instanceof FullQueryExp.Var) {
FullQueryExp.Var qq = (FullQueryExp.Var) q;
yyy.setSelectedValue(indices.get(qq.v), true);
return;
}
String k = FQLProgram.revLookup(prog.full_queries, q);
if (k != null) {
yyy.setSelectedValue(indices.get(k), true);
return;
}
String str = q.toString();
if (!extraInsts.contains(str)) {
FullQuery view = q.toFullQuery(prog);
JPanel p = showFullQuery(prog, env, view, q);
x.add(p, str);
extraInsts.add(str);
}
yyy.clearSelection();
cl.show(x, str);
} else if (f instanceof MapExp) {
MapExp q = (MapExp) f;
if (q instanceof MapExp.Var) {
MapExp.Var qq = (MapExp.Var) q;
yyy.setSelectedValue(indices.get(qq.v), true);
return;
}
String k = FQLProgram.revLookup(prog.maps, q);
if (k != null) {
yyy.setSelectedValue(indices.get(k), true);
return;
}
String str = q.toString();
if (!extraInsts.contains(str)) {
Mapping view = q.toMap(prog);
JPanel p = showMapping(env, prog.smap(view.source.toConst()), prog.smap(view.target.toConst()), view);
x.add(p, str);
extraInsts.add(str);
}
yyy.clearSelection();
cl.show(x, str);
} else {
throw new RuntimeException();
}
}
use of catdata.fql.decl.InstExp in project fql by CategoricalData.
the class FQLParser method toInst.
@SuppressWarnings("rawtypes")
private static InstExp toInst(Object o) {
try {
Tuple4 t = (Tuple4) o;
Token z = (Token) t.a;
String y = z.toString();
if (y.equals("step")) {
return new Step(t.d.toString(), toMapping(t.b), toMapping(t.c));
}
} catch (RuntimeException cce) {
}
try {
Tuple3 t = (Tuple3) o;
Token z = (Token) t.a;
String y = z.toString();
switch(y) {
case "delta":
return new InstExp.Delta(toMapping(t.b), t.c.toString());
case "sigma":
return new InstExp.Sigma(toMapping(t.b), t.c.toString());
case "SIGMA":
return new FullSigma(toMapping(t.b), t.c.toString());
case "pi":
return new InstExp.Pi(toMapping(t.b), t.c.toString());
case "external":
return new External(toSchema(t.b), t.c.toString());
case "eval":
return new Eval(toQuery(t.b), t.c.toString());
case "EVAL":
return new FullEval(toFullQuery(t.b), t.c.toString());
default:
break;
}
} catch (RuntimeException cce) {
}
try {
Tuple3 t = (Tuple3) o;
Token z = (Token) t.b;
String y = z.toString();
if (y.equals("+")) {
return new Plus(t.a.toString(), t.c.toString());
} else if (y.equals("*")) {
return new Times(t.a.toString(), t.c.toString());
}
if (y.equals("^")) {
return new Exp(t.a.toString(), (t.c).toString());
}
} catch (RuntimeException cce) {
}
try {
org.jparsec.functors.Pair pr = (org.jparsec.functors.Pair) o;
if (pr.a.toString().equals("unit")) {
return new One(toSchema(pr.b));
} else if (pr.a.toString().equals("void")) {
return new Zero(toSchema(pr.b));
} else if (pr.a.toString().equals("prop")) {
return new Two(toSchema(pr.b));
} else if (pr.a.toString().equals("relationalize")) {
return new Relationalize(pr.b.toString());
} else if (pr.a.toString().equals("kernel")) {
return new Kernel(pr.b.toString());
}
throw new RuntimeException();
} catch (RuntimeException cce) {
}
return toInstConst(o);
}
use of catdata.fql.decl.InstExp in project fql by CategoricalData.
the class FqlCodeEditor method vedit.
public void vedit() {
FQLProgram init = tryParse(topArea.getText());
if (init == null) {
respArea.setText(toDisplay);
return;
}
if (init.lines.isEmpty()) {
return;
}
String which = null;
int start = -1;
int offs = topArea.getCaretPosition();
int end = -1;
int i = 0;
int pos = 0;
for (String k : init.lines.keySet()) {
Integer v = init.lines.get(k);
if (v < offs && v > start) {
start = v;
which = k;
pos = i;
}
i++;
}
if (which == null) {
throw new RuntimeException();
}
int j = 0;
for (String k : init.lines.keySet()) {
if (j == pos + 1) {
end = init.lines.get(k);
break;
}
j++;
}
if (end == -1) {
end = topArea.getText().length();
}
InstExp ie = init.insts.get(which);
TransExp te = init.transforms.get(which);
if ((ie == null && te == null) || (ie != null && !(ie instanceof InstExp.Const)) || (te != null && !(te instanceof Const))) {
respArea.setText("Cannot visually edit " + which + ": only constant instances or transforms are visually editable.");
return;
}
try {
if (ie != null) {
InstExp.Const iec = (InstExp.Const) ie;
InstExp.Const n = new InstanceEditor(which, iec.sig.toSig(init), iec).show(Color.black);
if (n == null) {
return;
}
String newText = "instance " + which + " = " + n + " : " + n.sig + "\n\n";
topArea.replaceRange(newText, start, end);
} else {
Const iec = (Const) te;
if (iec == null) {
throw new RuntimeException("Anomaly: please report");
}
InstExp.Const s = (InstExp.Const) init.insts.get(iec.src);
InstExp.Const t = (InstExp.Const) init.insts.get(iec.dst);
Const n = new TransformEditor(which, init.insts.get(iec.src).type(init).toSig(init), iec, s, t).show(Color.black);
if (n == null) {
return;
}
String newText = "transform " + which + " = " + n + " : " + n.src + " -> " + n.dst + "\n\n";
topArea.replaceRange(newText, start, end);
}
} catch (FQLException fe) {
fe.printStackTrace();
respArea.setText(fe.getLocalizedMessage());
}
}
use of catdata.fql.decl.InstExp in project fql by CategoricalData.
the class TransChecker method visit.
@Override
public Pair<String, String> visit(FQLProgram env, TransIso e) {
InstExp l = env.insts.get(e.l);
if (l == null) {
throw new RuntimeException("Missing instance: " + e.l);
}
InstExp r = env.insts.get(e.r);
if (r == null) {
throw new RuntimeException("Missing instance: " + e.r);
}
return e.lToR ? new Pair<>(e.l, e.r) : new Pair<>(e.r, e.l);
}
use of catdata.fql.decl.InstExp in project fql by CategoricalData.
the class TransChecker method visit.
@Override
public Pair<String, String> visit(FQLProgram env, Sigma e) {
Pair<String, String> ht = e.h.type(env);
InstExp i1 = env.insts.get(e.src);
if (!(i1 instanceof InstExp.Sigma)) {
throw new RuntimeException(i1 + " is not a sigma in " + e);
}
String i1x = ((InstExp.Sigma) i1).I;
if (!i1x.equals(ht.first)) {
throw new RuntimeException("Source mismatch on " + e + ": " + i1x + " and " + ht.first);
}
InstExp i2 = env.insts.get(e.dst);
if (!(i2 instanceof InstExp.Sigma)) {
throw new RuntimeException(i2 + " is not a sigma in " + e);
}
String i2x = ((InstExp.Sigma) i2).I;
if (!i2x.equals(ht.second)) {
throw new RuntimeException("Target mismatch on " + e + ": " + i2x + " and " + ht.second);
}
return new Pair<>(e.src, e.dst);
}
Aggregations