use of catdata.aql.exp.InstExpRaw.Gen in project fql by CategoricalData.
the class InstExpJdbcAll method toInstance.
private Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> toInstance(AqlEnv env, SqlInstance inst, SqlSchema info) {
AqlOptions ops = new AqlOptions(options, null, env.defaults);
Schema<Ty, En, Sym, Fk, Att> sch = makeSchema(env, info, ops);
Ctx<En, Collection<Gen>> ens0 = new Ctx<>(Util.newSetsFor0(sch.ens));
Ctx<Ty, Collection<Null<?>>> tys0 = new Ctx<>();
Ctx<Gen, Ctx<Fk, Gen>> fks0 = new Ctx<>();
Ctx<Gen, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Null<?>>>> atts0 = new Ctx<>();
Ctx<Null<?>, Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>> extraRepr = new Ctx<>();
for (Ty ty : sch.typeSide.tys) {
tys0.put(ty, new HashSet<>());
}
boolean schemaOnly = (Boolean) ops.getOrDefault(AqlOption.schema_only);
boolean nullOnErr = (Boolean) ops.getOrDefault(AqlOption.import_null_on_err_unsafe);
boolean dontCheckClosure = (Boolean) ops.getOrDefault(AqlOption.import_dont_check_closure_unsafe);
if (!schemaOnly) {
int fr = 0;
Map<SqlTable, Map<Map<SqlColumn, Optional<Object>>, Gen>> iso1 = new HashMap<>();
for (SqlTable table : info.tables) {
Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
Map<Map<SqlColumn, Optional<Object>>, Gen> i1 = new HashMap<>();
SqlColumn thePk = null;
if (table.pk.size() == 1) {
thePk = Util.get0(table.pk);
}
for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
Gen i = new Gen("v" + (fr++));
/* can't do this until Gen need not be unique
if (thePk == null) {
i = new Gen("v" + (fr++));
} else {
Optional<Object> x = tuple.get(thePk);
if (!x.isPresent()) {
throw new RuntimeException("Primary key col is null in " + tuple);
}
i = new Gen(x.get().toString()); //TODO aql
}
*/
i1.put(tuple, i);
// tuple.keySet().
// i2.put(i, tuple);
ens0.get(new En(table.name)).add(i);
for (SqlColumn c : table.columns) {
if (!atts0.containsKey(i)) {
atts0.put(i, new Ctx<>());
}
Optional<Object> val = tuple.get(c);
Term<Ty, Void, Sym, Void, Void, Void, Null<?>> xxx = InstExpJdbc.objectToSk(sch, val.orElse(null), i, new Att(new En(table.name), c.name), tys0, extraRepr, false, nullOnErr);
atts0.get(i).put(new Att(new En(table.name), c.name), xxx);
}
}
iso1.put(table, i1);
// iso2.put(table, i2);
}
for (SqlForeignKey fk : info.fks) {
for (Map<SqlColumn, Optional<Object>> in : inst.get(fk.source)) {
Map<SqlColumn, Optional<Object>> out = inst.follow(in, fk);
Gen tgen = iso1.get(fk.target).get(out);
Gen sgen = iso1.get(fk.source).get(in);
if (!fks0.containsKey(sgen)) {
fks0.put(sgen, new Ctx<>());
}
fks0.get(sgen).put(new Fk(new En(fk.source.name), fk.toString()), tgen);
}
}
}
ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>> alg = new ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>>(sch, ens0, tys0, fks0, atts0, Object::toString, Object::toString, dontCheckClosure);
return new SaturatedInstance<>(alg, alg, (Boolean) ops.getOrDefault(AqlOption.require_consistency), (Boolean) ops.getOrDefault(AqlOption.allow_java_eqs_unsafe), true, extraRepr);
}
use of catdata.aql.exp.InstExpRaw.Gen in project fql by CategoricalData.
the class RawTerm method toHeadNoPrim.
// @SuppressWarnings("unchecked")
// only used for precedences with aql options
public static Head<Ty, En, Sym, Fk, Att, Gen, Sk> toHeadNoPrim(String head, Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col) {
Util.assertNotNull(head);
Util.assertNotNull(col);
assertUnambig(head, col);
if (col.syms.containsKey(new Sym(head))) {
return Head.Sym(new Sym(head));
} else if (col.gens.containsKey(new Gen(head))) {
return Head.Gen(new Gen(head));
} else if (col.sks.containsKey(new Sk(head))) {
return Head.Sk(new Sk(head));
}
for (En en : col.ens) {
// TODO aql won't work with ambig
if (col.fks.containsKey(new Fk(en, head))) {
return Head.Fk(new Fk(en, head));
}
if (col.atts.containsKey(new Att(en, head))) {
return Head.Att(new Att(en, head));
}
}
throw new RuntimeException("Anomaly: please report");
}
use of catdata.aql.exp.InstExpRaw.Gen 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.InstExpRaw.Gen 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.InstExpRaw.Gen in project fql by CategoricalData.
the class InstExpImport method eval.
@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> eval(AqlEnv env) {
Schema<Ty, En, Sym, Fk, Att> sch = schema.eval(env);
for (Ty ty : sch.typeSide.tys) {
if (!sch.typeSide.js.java_tys.containsKey(ty)) {
throw new RuntimeException("Import is only allowed onto java types");
}
}
op = new AqlOptions(options, null, env.defaults);
import_as_theory = (boolean) op.getOrDefault(AqlOption.import_as_theory);
isJoined = (boolean) op.getOrDefault(AqlOption.import_joined);
idCol = (String) op.getOrDefault(AqlOption.id_column_name);
nullOnErr = (Boolean) op.getOrDefault(AqlOption.import_null_on_err_unsafe);
prepend_entity_on_ids = (Boolean) op.getOrDefault(AqlOption.prepend_entity_on_ids);
import_col_seperator = (String) op.getOrDefault(AqlOption.import_col_seperator);
prefix = (String) op.getOrDefault(AqlOption.csv_import_prefix);
dont_check_closure = (boolean) op.getOrDefault(AqlOption.import_dont_check_closure_unsafe);
ens0 = new Ctx<>(Util.newSetsFor0(sch.ens));
tys0 = new Ctx<>(Util.newSetsFor0(sch.typeSide.tys));
fks0 = new Ctx<>();
atts0 = new Ctx<>();
extraRepr = new Ctx<>();
En last = null;
try {
Handle h = start(sch);
if (!isJoined) {
/*
Map<En, Q> ens = new HashMap<>();
Map<Ty, Q> tys = new HashMap<>();
Map<Att, Q> atts = new HashMap<>();
Map<Fk, Q> fks = new HashMap<>();
for (String o : map.keySet()) {
assertUnambig(o, sch);
Q q = map.get(o);
if (sch.typeSide.tys.contains(new Ty(o))) {
tys.put(new Ty(o), q);
} else if (sch.ens.contains(new En(o))) {
ens.put(new En(o), q);
} else if (sch.atts.map.containsKey(new Att(o))) {
atts.put(new Att(o), q);
} else if (sch.fks.map.containsKey(new Fk(o))) {
fks.put(new Fk(o), q);
} //TODO aql
}
totalityCheck(sch, ens, tys, atts, fks);
for (En en : ens.keySet()) {
shreddedEn(h, en, ens.get(en), sch);
}
for (Fk fk : fks.keySet()) {
shreddedFk(h, fk, fks.get(fk), sch);
}
for (Att att : atts.keySet()) {
shreddedAtt(h, att, atts.get(att), sch);
}
*/
throw new RuntimeException("Unjoined form no longer supported. To request, contact us.");
} else {
for (En en : sch.ens) {
last = en;
Q z = map.get(en.str);
// TODO: aql: this check isn't needed
// if (z == null) {
// throw new RuntimeException("No binding given for " + en);
// }
joinedEn(h, en, z, sch);
}
}
end(h);
} catch (Exception exn) {
exn.printStackTrace();
String pre = "";
if (last != null) {
pre = "On entity " + last + ", ";
}
throw new RuntimeException(pre + exn.getMessage() + "\n\n" + getHelpStr());
}
if (import_as_theory) {
return forTheory(sch, ens0, tys0, fks0, atts0, op);
}
ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>> alg = new ImportAlgebra<>(sch, ens0, tys0, fks0, atts0, Object::toString, Object::toString, dont_check_closure);
return new SaturatedInstance<>(alg, alg, (Boolean) op.getOrDefault(AqlOption.require_consistency), (Boolean) op.getOrDefault(AqlOption.allow_java_eqs_unsafe), true, extraRepr);
}
Aggregations