use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.
the class RawTerm method infer2.
public static Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> infer2(List<Pair<String, String>> l, RawTerm a, RawTerm b, Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col, AqlJs<Ty, Sym> js) {
Map<String, Chc<Ty, En>> ctx = new HashMap<>();
for (Pair<String, String> p : l) {
if (ctx.containsKey(p.first)) {
throw new RuntimeException("Duplicate variable " + p.first + " in context " + Ctx.toString(l));
}
if (p.second != null) {
if (col.tys.contains(p.second) && col.ens.contains(p.second)) {
throw new RuntimeException("Ambiguous: " + p.second + " is an entity and a type");
} else if (col.tys.contains(p.second)) {
Ty tt = new Ty(p.second);
// TODO aql remove for
ctx.put(p.first, Chc.inLeft(tt));
// loops for other ones
} else if (col.ens.contains(p.second)) {
En tt = new En(p.second);
ctx.put(p.first, Chc.inRight(tt));
} else {
throw new RuntimeException(p.second + " is neither a type nor entity");
}
} else {
ctx.put(p.first, null);
}
}
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> eq0 = infer1x(ctx, a, b, null, col, "", js).first3();
LinkedHashMap<Var, Chc<Ty, En>> map = new LinkedHashMap<>();
for (String k : ctx.keySet()) {
Chc<Ty, En> v = eq0.first.get(new Var(k));
map.put(new Var(k), v);
}
Ctx<Var, Chc<Ty, En>> ctx2 = new Ctx<>(map);
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> tr = new Triple<>(ctx2, eq0.second, eq0.third);
return tr;
}
use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.
the class RawTerm method infer_good.
private static Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> infer_good(RawTerm e, Chc<Ty, En> expected, Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col, String pre, AqlJs<Ty, Sym> js, Map<Var, Chc<Ty, En>> vars) {
if (e.annotation != null && !col.tys.contains(new Ty(e.annotation))) {
throw new RuntimeException(pre + "Annotation " + e.annotation + " is not a type (" + col.tys + ").");
}
Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> ret = new HashSet<>();
if (vars.keySet().contains(new Var((String) e.head)) && e.annotation == null) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Var(new Var((String) e.head));
if (expected != null) {
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
ret2.put(new Var((String) e.head), expected);
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, expected));
}
} else {
for (En en : col.ens) {
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
ret2.put(new Var((String) e.head), Chc.inRight(en));
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, Chc.inRight(en)));
}
}
for (Ty ty : col.tys) {
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret2.put(new Var((String) e.head), Chc.inLeft(ty));
}
ret.add(new Triple<>(ret1, ret2, Chc.inLeft(ty)));
}
}
}
if (col.syms.containsKey(new Sym(e.head)) && e.annotation == null) {
// //System.out.println("a " + e);
List<List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>>> l = new LinkedList<>();
l.add(new LinkedList<>());
for (int i = 0; i < e.args.size(); i++) {
RawTerm arg = e.args.get(i);
// //System.out.println("arg " + arg);
Ty ty = col.syms.get(new Sym(e.head)).first.get(i);
Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> z = infer_good(arg, Chc.inLeft(ty), col, pre, js, vars);
List<List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>>> l2 = new LinkedList<>();
for (List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> old : l) {
// //System.out.println("old " + old);
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> y : z) {
if (y.third.equals(Chc.inLeft(ty))) {
// //System.out.println("z z");
l2.add(Util.append(old, Util.singList(y)));
}
}
}
l = l2;
}
outer: for (List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> outcome : l) {
// //System.out.println("outcome " + outcome);
List<Term<Ty, En, Sym, Fk, Att, Gen, Sk>> w = outcome.stream().map(x -> x.first).collect(Collectors.toList());
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Sym(new Sym(e.head), w);
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> ctx0 : outcome) {
if (!ctx0.second.agreeOnOverlap(ret2) || !ctx0.second.agreeOnOverlap(Ctx.fromNullable(vars))) {
// //System.out.println("xxx ");
continue outer;
}
// //System.out.println("yyy");
ret2.map.putAll(ctx0.second.map);
}
for (int i = 0; i < e.args.size(); i++) {
RawTerm arg = e.args.get(i);
// //System.out.println("2arx " + arg);
Chc<Ty, En> ty = Chc.inLeft(col.syms.get(new Sym(e.head)).first.get(i));
Var v = new Var((String) arg.head);
if (vars.keySet().contains(v)) {
// //System.out.println("a " + v);
if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
// //System.out.println("b " + v);
continue;
} else if (!ret2.containsKey(v)) {
// //System.out.println("c " + v);
ret2.put(new Var(e.args.get(i).head), ty);
}
}
}
Chc<Ty, En> ret3 = Chc.inLeft(col.syms.get(new Sym(e.head)).second);
if (expected != null && !expected.equals(ret3)) {
// //System.out.println("d " );
} else {
// //System.out.println("e " );
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, ret3));
}
}
}
}
for (En en : col.ens) {
if (col.fks.containsKey(new Fk(en, e.head)) && e.args.size() == 1 && e.annotation == null) {
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> outcome : infer_good(e.args.get(0), Chc.inRight(col.fks.get(new Fk(en, e.head)).first), col, pre, js, vars)) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Fk(new Fk(en, e.head), outcome.first);
// System.out.println("trying " + en + " and " + e.head);
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>(outcome.second.map);
Var v = new Var(e.args.get(0).head);
Chc<Ty, En> ty = Chc.inRight(col.fks.get(new Fk(en, e.head)).first);
if (vars.keySet().contains(v)) {
if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
// System.out.println("no1");
continue;
} else if (!ret2.containsKey(v)) {
ret2.put(new Var(e.args.get(0).head), ty);
}
}
Chc<Ty, En> ret3 = Chc.inRight(col.fks.get(new Fk(en, e.head)).second);
Chc<Ty, En> argt = Chc.inRight(col.fks.get(new Fk(en, e.head)).first);
if (expected != null && !expected.equals(ret3)) {
} else {
if (argt.equals(outcome.third)) {
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, ret3));
} else {
// System.out.println("b3");
}
} else {
// System.out.println("c3");
}
}
}
}
if (col.atts.containsKey(new Att(en, e.head)) && e.args.size() == 1 && e.annotation == null) {
// System.out.println("x " + e);
for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> outcome : infer_good(e.args.get(0), Chc.inRight(col.atts.get(new Att(en, e.head)).first), col, pre, js, vars)) {
// System.out.println("y " + outcome);
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Att(new Att(en, e.head), outcome.first);
Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>(outcome.second.map);
Var v = new Var(e.args.get(0).head);
Chc<Ty, En> ty = Chc.inRight(col.atts.get(new Att(en, e.head)).first);
if (vars.keySet().contains(v)) {
if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
continue;
} else if (!ret2.containsKey(v)) {
// System.out.println("b " + v);
ret2.put(v, ty);
}
}
Chc<Ty, En> ret3 = Chc.inLeft(col.atts.get(new Att(en, e.head)).second);
Chc<Ty, En> argt = Chc.inRight(col.atts.get(new Att(en, e.head)).first);
if (expected != null && !expected.equals(ret3)) {
// System.out.println("d " + v);
} else {
// System.out.println("e " + v);
if (argt.equals(outcome.third)) {
// System.out.println("f " + v);
if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
ret.add(new Triple<>(ret1, ret2, ret3));
}
}
}
}
}
}
if (col.gens.containsKey(new Gen(e.head)) && e.args.isEmpty() && e.annotation == null) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Gen(new Gen(e.head));
Chc<Ty, En> ret3 = Chc.inRight(col.gens.get(new Gen(e.head)));
if (expected != null && !expected.equals(ret3)) {
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
}
}
if (col.sks.containsKey(new Sk(e.head)) && e.args.isEmpty() && e.annotation == null) {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Sk(new Sk(e.head));
Chc<Ty, En> ret3 = Chc.inLeft(col.sks.get(new Sk(e.head)));
if (expected != null && !expected.equals(ret3)) {
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
}
}
if (e.args.isEmpty() && e.annotation != null) {
Ty ty = new Ty(e.annotation);
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Obj(js.parse(ty, e.head), ty);
Chc<Ty, En> ret3 = Chc.inLeft(ty);
if (expected != null && !expected.equals(ret3)) {
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
}
}
// as primitive - only if not a variable/generator/etc in scope i.e. none above fired
if (e.args.isEmpty() && e.annotation == null && ret.isEmpty()) {
for (Ty ty : col.tys) {
if (expected != null && !expected.equals(Chc.inLeft(ty))) {
continue;
}
try {
Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Obj(js.parse(ty, e.head), ty);
Chc<Ty, En> ret3 = Chc.inLeft(ty);
if (expected != null && !expected.equals(ret3)) {
// System.out.println("zzz");
} else {
ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
// System.out.println("added " + ret + " and " + ret3);
}
} catch (Exception ex) {
if (expected != null) {
ex.printStackTrace();
// throw ex;
}
// //ex.printStackTrace();
}
}
}
return ret;
}
use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.
the class AqlViewer method viewDP.
/*public static <Ty, En, Sym, Fk, Att> JComponent viewSchema2(Schema<Ty, En, Sym, Fk, Att> schema) {
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
Ctx<Chc<En,Ty>, Object> nodes = new Ctx<>();
for (En en : schema.ens) {
Object v1 = graph.insertVertex(parent, null, en.toString(), 20, 20, 80, 30);
nodes.put(Chc.inLeft(en), v1);
}
for (Ty ty : schema.typeSide.tys) {
Object v1 = graph.insertVertex(parent, null, ty.toString(), 20, 20, 80, 30);
nodes.put(Chc.inRight(ty), v1);
}
for (Att att : schema.atts.keySet()) {
graph.insertEdge(parent, null, att.toString(), nodes.get(Chc.inLeft(schema.atts.get(att).first)), nodes.get(Chc.inRight(schema.atts.get(att).second)));
}
for (Fk fk : schema.fks.keySet()) {
graph.insertEdge(parent, null, fk.toString(), nodes.get(Chc.inLeft(schema.fks.get(fk).first)), nodes.get(Chc.inLeft(schema.fks.get(fk).second)));
}
mxGraphLayout layout = new mxOrganicLayout(graph); // or whatever layouting algorithm
layout.execute(parent);
mxGraphComponent graphComponent = new mxGraphComponent(graph);
// Map<String, Object> style = graph.getStylesheet().getDefaultEdgeStyle();
// style.put(mxConstants.STYLE_ROUNDED, true);
// style.put(mxConstants.STYLE_EDGE, mxConstants.EDGESTYLE_ENTITY_RELATION);
return graphComponent;
} */
private static <Ty, En, Sym, Fk, Att, Gen, Sk> JComponent viewDP(DP<Ty, En, Sym, Fk, Att, Gen, Sk> dp, Collage col, AqlJs js) {
CodeTextPanel input = new CodeTextPanel("Input (either equation-in-ctx or term-in-ctx)", "");
CodeTextPanel output = new CodeTextPanel("Output", "");
JButton eq = new JButton("Decide Equation-in-ctx");
JButton nf = new JButton("Normalize Term-in-ctx");
/*
* if (!dp.hasNFs()) { nf.setEnabled(false); }
*/
JButton print = new JButton("Show Info");
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
buttonPanel.add(eq);
buttonPanel.add(nf);
buttonPanel.add(print);
// TODO: aql does not position correctly
Split split = new Split(.5, JSplitPane.VERTICAL_SPLIT);
split.add(input);
split.add(output);
JPanel main = new JPanel(new BorderLayout());
main.add(split, BorderLayout.CENTER);
main.add(buttonPanel, BorderLayout.NORTH);
print.addActionListener(x -> output.setText(dp.toStringProver()));
eq.addActionListener(x -> {
try {
Triple<List<Pair<String, String>>, RawTerm, RawTerm> y = AqlParser.getParser().parseEq(input.getText());
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> z = RawTerm.infer2(y.first, y.second, y.third, col, js);
boolean isEq = dp.eq(z.first, z.second, z.third);
output.setText(Boolean.toString(isEq));
} catch (Exception ex) {
ex.printStackTrace();
output.setText(ex.getMessage());
}
});
nf.addActionListener(x -> {
try {
Pair<List<Pair<String, String>>, RawTerm> y = AqlParser.getParser().parseTermInCtx(input.getText());
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> z = RawTerm.infer2(y.first, y.second, y.second, col, js);
Term<Ty, En, Sym, Fk, Att, Gen, Sk> w = dp.nf(z.first, z.second);
output.setText(w.toString());
} catch (Exception ex) {
ex.printStackTrace();
output.setText(ex.getMessage());
}
});
return main;
}
use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.
the class AqlViewer method viewTransform.
private static <Ty, En, Sym, Fk, Att, Gen1, Sk1, Gen2, Sk2, X1, Y1, X2, Y2> JComponent viewTransform(Transform<Ty, En, Sym, Fk, Att, Gen1, Sk1, Gen2, Sk2, X1, Y1, X2, Y2> t) {
List<JComponent> list = new LinkedList<>();
List<En> ens = Util.alphabetical(t.src().schema().ens);
List<Ty> tys = Util.alphabetical(t.src().schema().typeSide.tys);
for (En en : ens) {
List<String> header = new LinkedList<>();
header.add("Input");
header.add("Output");
int n = t.src().algebra().en(en).size();
Object[][] data = new Object[n][2];
int i = 0;
for (X1 x1 : Util.alphabetical(t.src().algebra().en(en))) {
Object[] row = new Object[2];
row[0] = t.src().algebra().printX(x1);
X2 x2 = t.repr(x1);
row[1] = t.dst().algebra().printX(x2);
data[i] = row;
i++;
}
// TODO: aql boldify attributes
list.add(GuiUtil.makeTable(BorderFactory.createEmptyBorder(), en + " (" + n + ")", data, header.toArray()));
}
Map<Ty, Set<Y1>> z = Util.revS(t.src().algebra().talg().sks.map);
for (Ty ty : tys) {
List<String> header = new LinkedList<>();
header.add("Input");
header.add("Output");
if (!z.containsKey(ty)) {
continue;
}
int n = z.get(ty).size();
Object[][] data = new Object[n][2];
int i = 0;
for (Y1 y1 : z.get(ty)) {
Object[] row = new Object[2];
Term<Ty, En, Sym, Fk, Att, Gen1, Sk1> a = t.src().algebra().reprT_protected(Term.Sk(y1));
row[0] = t.src().algebra().printY(y1);
// t.dst().algebra().intoY(t.reprT(y1));
Term<Ty, En, Sym, Fk, Att, Gen2, Sk2> y0 = t.trans(a);
// t.dst().algebra().pr, Util.voidFn()); //TODO aql viewer printing revisit
row[1] = y0.toString();
data[i] = row;
i++;
}
// TODO: aql boldify attributes
list.add(GuiUtil.makeTable(BorderFactory.createEmptyBorder(), ty + " (" + n + ")", data, header.toArray()));
}
return GuiUtil.makeGrid(list);
}
use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.
the class AqlViewer method viewSchema.
private <Ty, En, Sym, Fk, Att> JComponent viewSchema(Schema<Ty, En, Sym, Fk, Att> schema) {
Graph<Chc<Ty, En>, Chc<Fk, Att>> sgv = new DirectedSparseMultigraph<>();
int i = 0;
boolean triggered = false;
for (En en : schema.ens) {
sgv.addVertex(Chc.inRight(en));
i++;
if (i >= maxrows) {
triggered = true;
break;
}
}
// if (i <= maxrows) {
i = 0;
for (Ty ty : schema.typeSide.tys) {
sgv.addVertex(Chc.inLeft(ty));
i++;
if (i >= maxrows * maxrows) {
triggered = true;
break;
}
}
for (Att att : schema.atts.keySet()) {
sgv.addEdge(Chc.inRight(att), Chc.inRight(schema.atts.get(att).first), Chc.inLeft(schema.atts.get(att).second));
i++;
if (i >= maxrows * maxrows) {
triggered = true;
break;
}
}
for (Fk fk : schema.fks.keySet()) {
sgv.addEdge(Chc.inLeft(fk), Chc.inRight(schema.fks.get(fk).first), Chc.inRight(schema.fks.get(fk).second));
i++;
if (i >= maxrows * maxrows) {
triggered = true;
break;
}
}
if (sgv.getVertexCount() == 0) {
return new JPanel();
}
// Layout<Chc<Ty, En>, Chc<Fk, Att>> layout = new KKLayout<>(sgv);
Layout<Chc<Ty, En>, Chc<Fk, Att>> layout = new FRLayout<>(sgv);
layout.setSize(new Dimension(600, 400));
VisualizationViewer<Chc<Ty, En>, Chc<Fk, Att>> vv = new VisualizationViewer<>(layout);
Function<Chc<Ty, En>, Paint> vertexPaint = x -> x.left ? Color.gray : Color.black;
DefaultModalGraphMouse<Chc<Ty, En>, Chc<Fk, Att>> gm = new DefaultModalGraphMouse<>();
gm.setMode(Mode.TRANSFORMING);
vv.setGraphMouse(gm);
gm.setMode(Mode.PICKING);
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
Function<Chc<Fk, Att>, String> et = Chc::toStringMash;
Function<Chc<Ty, En>, String> vt = Chc::toStringMash;
vv.getRenderContext().setEdgeLabelTransformer(et);
vv.getRenderContext().setVertexLabelTransformer(vt);
GraphZoomScrollPane zzz = new GraphZoomScrollPane(vv);
JPanel ret = new JPanel(new GridLayout(1, 1));
ret.add(zzz);
ret.setBorder(BorderFactory.createEtchedBorder());
vv.getRenderContext().setLabelOffset(16);
// vv.getRenderContext().set
vv.setBackground(Color.white);
if (triggered) {
ret.setBorder(BorderFactory.createTitledBorder("Partial"));
}
return ret;
}
Aggregations