use of catdata.Chc in project fql by CategoricalData.
the class OplParser method fromBlock2.
private static Block<String, String, String, String, String, String> fromBlock2(Object o) {
Tuple3<List, List, List> t = (Tuple3<List, List, List>) o;
LinkedHashMap<String, String> from = new LinkedHashMap<>();
Set<Pair<OplTerm<String, String>, OplTerm<String, String>>> where = new HashSet<>();
Map<String, Chc<Agg<String, String, String, String, String, String>, OplTerm<String, String>>> attrs = new HashMap<>();
Map<String, Pair<Object, Map<String, OplTerm<String, String>>>> edges = new HashMap<>();
// from
for (Object x : (Iterable) ((org.jparsec.functors.Pair) t.b).b) {
org.jparsec.functors.Pair l = (org.jparsec.functors.Pair) x;
String gen;
String ty;
if (l.b == null) {
gen = (String) l.a;
ty = (String) l.a;
} else {
org.jparsec.functors.Pair g = (org.jparsec.functors.Pair) l.b;
gen = (String) g.b;
ty = (String) l.a;
}
if (from.containsKey(gen)) {
throw new DoNotIgnore("In from clause, duplicate for: " + gen);
}
from.put(gen, ty);
}
// Object z = ((org.jparsec.functors.Pair)t.c).b;
if (t.c != null) {
for (Object x : (Iterable) ((org.jparsec.functors.Pair) t.c).b) {
Tuple3 l = (Tuple3) x;
where.add(new Pair(toTerm(from.keySet(), null, l.a, true), toTerm(from.keySet(), null, l.c, true)));
}
}
// return
for (Object x : (Iterable) ((org.jparsec.functors.Pair) t.a).b) {
Tuple3 l = (Tuple3) x;
String dst = (String) l.c;
if (attrs.containsKey(dst) || edges.containsKey(dst)) {
throw new DoNotIgnore("In select clause, duplicate for: " + dst);
}
if (l.a instanceof Tuple3 && ((org.jparsec.functors.Pair) l.a).a.toString().equals("(")) {
edges.put(dst, new Pair(null, fromBlockHelper2(from.keySet(), l.a)));
} else {
attrs.put(dst, Chc.inRight(toTerm(from.keySet(), null, l.a, true)));
}
}
Block bl = new Block<>(from, where, attrs, edges);
return bl;
}
use of catdata.Chc in project fql by CategoricalData.
the class SqlToOpl method convert.
// : happy medium between CNF and non-CNF? ignore as many columns as possible?
// ////////////////////////////////////////////////////////////////////////////////////////////////////
private static Pair<OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String>> convert(SqlSchema info, SqlInstance inst, String S0, String I0) {
Set<Chc<SqlType, SqlTable>> sorts = new HashSet<>();
Set<Chc<SqlType, SqlTable>> entities = new HashSet<>();
Map<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, Pair<List<Chc<SqlType, SqlTable>>, Chc<SqlType, SqlTable>>> symbols = new HashMap<>();
List<Triple<OplCtx<Chc<SqlType, SqlTable>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>>> equations = new LinkedList<>();
for (SqlType type : info.types) {
sorts.add(Chc.inLeft(type));
}
for (SqlTable table : info.tables) {
sorts.add(Chc.inRight(table));
entities.add(Chc.inRight(table));
for (SqlColumn col : table.columns) {
symbols.put(Chc.inRight(Chc.inLeft(col)), new Pair<>(Util.singList(Chc.inRight(table)), Chc.inLeft(col.type)));
}
}
for (SqlForeignKey fk : info.fks) {
symbols.put(Chc.inRight(Chc.inRight(fk)), new Pair<>(Util.singList(Chc.inRight(fk.source)), Chc.inRight(fk.target)));
OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> head = new OplTerm<>("x");
OplCtx<Chc<SqlType, SqlTable>, String> ctx = new OplCtx<>(Util.singList(new Pair<>("x", Chc.inRight(fk.source))));
OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> rhs0 = new OplTerm<>(Chc.inRight(Chc.inRight(fk)), Util.singList(head));
for (SqlColumn tcol : fk.map.keySet()) {
SqlColumn scol = fk.map.get(tcol);
Chc<SqlColumn, SqlForeignKey> l = Chc.inLeft(scol);
Chc<SqlColumn, SqlForeignKey> r = Chc.inLeft(tcol);
OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> lhs = new OplTerm<>(Chc.inRight(l), Util.singList(head));
OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> rhs = new OplTerm<>(Chc.inRight(r), Util.singList(rhs0));
equations.add(new Triple<>(ctx, lhs, rhs));
}
}
OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sch = new OplSchema<>(S0, entities);
OplSig<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sig = new OplSig<>(VIt.vit, new HashMap<>(), sorts, symbols, equations);
sch.validate(sig);
OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> I = new OplInst<>(S0, I0, "none");
Map<String, Chc<SqlType, SqlTable>> gens = new HashMap<>();
List<Pair<OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>, OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>>> eqs = new LinkedList<>();
int fr = 0;
if (inst != null) {
Map<SqlTable, Map<Map<SqlColumn, Optional<Object>>, String>> iso1 = new HashMap<>();
for (SqlTable table : info.tables) {
Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
Map<Map<SqlColumn, Optional<Object>>, String> i1 = new HashMap<>();
// Map<String, Map<SqlColumn, Optional<Object>>> i2 = new HashMap<>();
for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
String i = "v" + (fr++);
i1.put(tuple, i);
// i2.put(i, tuple);
gens.put(i, Chc.inRight(table));
for (SqlColumn col : table.columns) {
SqlType ty = col.type;
Optional<Object> val = tuple.get(col);
if (!val.isPresent()) {
continue;
}
symbols.put(Chc.inLeft(val.get()), new Pair<>(new LinkedList<>(), Chc.inLeft(ty)));
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inLeft(Chc.inLeft(val.get())), new LinkedList<>());
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inLeft(col))), Util.singList(new OplTerm<>(Chc.inRight(i), new LinkedList<>())));
eqs.add(new Pair<>(lhs, rhs));
}
}
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);
String tgen = iso1.get(fk.target).get(out);
String sgen = iso1.get(fk.source).get(in);
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inRight(tgen), new LinkedList<>());
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inRight(fk))), Util.singList(new OplTerm<>(Chc.inRight(sgen), new LinkedList<>())));
eqs.add(new Pair<>(lhs, rhs));
}
}
}
OplPres<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> P = new OplPres<>(new HashMap<>(), S0, sig, gens, eqs);
P.toSig();
I.validate(sch, P, null);
return new Pair<>(sch, I);
}
use of catdata.Chc in project fql by CategoricalData.
the class SqlToOpl method convertCnf.
// : code formatter should not wrap lines ever
private static Pair<OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String>> convertCnf(SqlSchema info, SqlInstance inst, String S0, String I0) {
if (!info.isCnf()) {
throw new RuntimeException("Schema not in categorical normal form");
}
Set<Chc<SqlType, SqlTable>> sorts = new HashSet<>();
Set<Chc<SqlType, SqlTable>> entities = new HashSet<>();
Map<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, Pair<List<Chc<SqlType, SqlTable>>, Chc<SqlType, SqlTable>>> symbols = new HashMap<>();
List<Triple<OplCtx<Chc<SqlType, SqlTable>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, OplTerm<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>>> equations = new LinkedList<>();
for (SqlType type : info.types) {
sorts.add(Chc.inLeft(type));
}
for (SqlTable table : info.tables) {
sorts.add(Chc.inRight(table));
entities.add(Chc.inRight(table));
for (SqlColumn col : table.columns) {
if (col.equals(table.getCnfId())) {
continue;
}
if (isFk(info, table, col)) {
continue;
}
symbols.put(Chc.inRight(Chc.inLeft(col)), new Pair<>(Util.singList(Chc.inRight(table)), Chc.inLeft(col.type)));
}
}
for (SqlForeignKey fk : info.fks) {
symbols.put(Chc.inRight(Chc.inRight(fk)), new Pair<>(Util.singList(Chc.inRight(fk.source)), Chc.inRight(fk.target)));
}
OplSchema<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sch = new OplSchema<>(S0, entities);
OplSig<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String> sig = new OplSig<>(VIt.vit, new HashMap<>(), sorts, symbols, equations);
sch.validate(sig);
OplInst<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> I = new OplInst<>(S0, I0, "none");
Map<String, Chc<SqlType, SqlTable>> gens = new HashMap<>();
List<Pair<OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>, OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String>>> eqs = new LinkedList<>();
int fr = 0;
if (inst != null) {
Map<SqlTable, Map<Object, String>> iso1 = new HashMap<>();
for (SqlTable table : info.tables) {
Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
Map<Object, String> i1 = new HashMap<>();
// Map<String, Object> i2 = new HashMap<>();
for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
String i = "v" + (fr++);
if (!tuple.get(table.getCnfId()).isPresent()) {
throw new RuntimeException("Anomly: please report");
}
i1.put(tuple.get(table.getCnfId()).get(), i);
// i2.put(i, tuple.get(table.getCnfId()).get());
gens.put(i, Chc.inRight(table));
for (SqlColumn col : table.columns) {
if (col.equals(table.getCnfId())) {
continue;
}
if (isFk(info, table, col)) {
continue;
}
SqlType ty = col.type;
Optional<Object> val = tuple.get(col);
if (!val.isPresent()) {
continue;
}
symbols.put(Chc.inLeft(val.get()), new Pair<>(new LinkedList<>(), Chc.inLeft(ty)));
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inLeft(Chc.inLeft(val.get())), new LinkedList<>());
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inLeft(col))), Util.singList(new OplTerm<>(Chc.inRight(i), new LinkedList<>())));
eqs.add(new Pair<>(lhs, rhs));
}
}
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);
if (!out.get(fk.target.getCnfId()).isPresent() || !in.get(fk.source.getCnfId()).isPresent()) {
throw new RuntimeException("Anomaly: please report");
}
String tgen = iso1.get(fk.target).get(out.get(fk.target.getCnfId()).get());
String sgen = iso1.get(fk.source).get(in.get(fk.source.getCnfId()).get());
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> rhs = new OplTerm<>(Chc.inRight(tgen), new LinkedList<>());
OplTerm<Chc<Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String>, String> lhs = new OplTerm<>(Chc.inLeft(Chc.inRight(Chc.inRight(fk))), Util.singList(new OplTerm<>(Chc.inRight(sgen), new LinkedList<>())));
eqs.add(new Pair<>(lhs, rhs));
}
}
}
OplPres<Chc<SqlType, SqlTable>, Chc<Object, Chc<SqlColumn, SqlForeignKey>>, String, String> P = new OplPres<>(new HashMap<>(), S0, sig, gens, eqs);
P.toSig();
I.validate(sch, P, null);
return new Pair<>(sch, I);
}
use of catdata.Chc in project fql by CategoricalData.
the class XMapping method buildFromSig.
private Graph<Chc<C, D>, Object> buildFromSig() {
String pre = "_38u5n";
int i = 0;
Graph<Chc<C, D>, Object> G = new DirectedSparseMultigraph<>();
for (C n : src.ids) {
G.addVertex(Chc.inLeft(n));
G.addEdge(pre + i++, Chc.inLeft(n), Chc.inRight(em.get(n).get(0)));
}
for (D n : dst.ids) {
G.addVertex(Chc.inRight(n));
}
for (C e : src.terms()) {
if (src.allIds().contains(e)) {
continue;
}
if (e.toString().startsWith("!")) {
continue;
}
Pair<C, C> t = src.type(e);
if (src.global.ids.contains(t.first) || src.global.ids.contains(t.second)) {
continue;
}
G.addEdge(Chc.inLeft(e), Chc.inLeft(t.first), Chc.inLeft(t.second));
}
for (D e : dst.terms()) {
if (dst.allIds().contains(e)) {
continue;
}
if (e.toString().startsWith("!")) {
continue;
}
Pair<D, D> t = dst.type(e);
if (dst.global.ids.contains(t.first) || dst.global.ids.contains(t.second)) {
continue;
}
G.addEdge(Chc.inRight(e), Chc.inRight(t.first), Chc.inRight(t.second));
}
return G;
}
use of catdata.Chc in project fql by CategoricalData.
the class XProd method inl.
public static <X, Y> XMapping<X, Chc<X, Y>> inl(XCtx<X> I, XCtx<Y> J) {
XCtx<Chc<X, Y>> IJ = coprod(I, J);
Map<X, List<Chc<X, Y>>> em = new HashMap<>();
for (X x : I.types.keySet()) {
List<Chc<X, Y>> l = new LinkedList<>();
l.add(Chc.inLeft(x));
em.put(x, l);
}
for (X x : I.allTerms()) {
if (em.containsKey(x)) {
continue;
}
List l = new LinkedList<>();
l.add(x);
em.put(x, l);
}
return new XMapping<>(I, IJ, em, "homomorphism");
}
Aggregations