use of catdata.fql.sql.Flower in project fql by CategoricalData.
the class InstOps method visit.
@Override
public List<PSM> visit(String env, Return e) {
// String xxx = "return_temp_xxx";
List<PSM> ret = new LinkedList<>();
InstExp i1 = prog.insts.get(e.inst);
if (i1 instanceof Delta) {
String middle = ((Delta) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((Delta) i1).F.toMap(prog);
if (i2 instanceof Sigma) {
Sigma input0 = ((Sigma) i2);
String input = input0.I;
for (Node n : f.source.nodes) {
ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(input + "_" + n.string, middle + "_" + f.nm.get(n) + "_subst", e.inst + "_" + n.string + "_subst"), "c0", "c1"));
}
} else if (i2 instanceof FullSigma) {
FullSigma input0 = ((FullSigma) i2);
String input = input0.I;
for (Node n : f.source.nodes) {
ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(input + "_" + n.string, middle + "_" + n.string + "_e", e.inst + "_" + n.string + "_subst"), "c0", "c1"));
}
} else {
throw new RuntimeException();
}
} else if (i1 instanceof Pi) {
String middle = ((Pi) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((Pi) i1).F.toMap(prog);
if (i2 instanceof Delta) {
Delta input0 = ((Delta) i2);
String input = input0.I;
for (Node n : f.target.nodes) {
try {
Map<String, Triple<Node, Node, Arr<Node, Path>>[]> colmap = PSMGen.pi(f, middle, e.inst).second;
Triple<Node, Node, Arr<Node, Path>>[] col = colmap.get(n.string);
if (col.length == 0) {
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
Map<String, String> from = new HashMap<>();
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
from.put("lim", e.inst + "_" + n.string + "_limit");
from.put("middle", input + "_" + n.string);
select.put("c0", new Pair<>("middle", "c0"));
select.put("c1", new Pair<>("lim", "guid"));
Flower flower = new Flower(select, from, where);
ret.add(new InsertSQL(env + "_" + n.string, flower, "c0", "c1"));
return ret;
}
// LinkedHashMap<String, String> attrs = new LinkedHashMap<>();
// attrs.put("guid", PSM.VARCHAR());
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
Map<String, String> from = new HashMap<>();
from.put("lim", e.inst + "_" + n.string + "_limit");
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
int i = 0;
for (Triple<Node, Node, Arr<Node, Path>> col0 : col) {
from.put("c" + i + "_subst_inv", middle + "_" + col0.second.string + "_subst_inv");
where.add(new Pair<>(new Pair<>("lim", "c" + i), new Pair<>("c" + i + "_subst_inv", "c0")));
// attrs.put("c" + i, PSM.VARCHAR());
i++;
}
// if (col.length > 1) {
for (int j = 0; j < col.length; j++) {
if (col[j].third.arr.equals(f.target.toCategory2().second.of(new Path(f.target, n)).arr)) {
where.add(new Pair<>(new Pair<>("c" + 0 + "_subst_inv", "c1"), new Pair<>("c" + j + "_subst_inv", "c1")));
}
}
select.put("c" + 0, new Pair<>("c" + 0 + "_subst_inv", "c1"));
select.put("c1", new Pair<>("lim", "guid"));
// }
// ret.add(new CreateTable(xxx, attrs, false));
Flower flower = new Flower(select, from, where);
ret.add(new InsertSQL(env + "_" + n.string, flower, "c0", "c1"));
} catch (FQLException fe) {
fe.printStackTrace();
throw new RuntimeException(fe.getMessage());
}
}
} else {
throw new RuntimeException();
}
} else {
throw new RuntimeException();
}
return ret;
}
use of catdata.fql.sql.Flower in project fql by CategoricalData.
the class InstOps method visit.
@Override
public List<PSM> visit(String dst, TT e) {
try {
List<PSM> ret = new LinkedList<>();
Signature s = prog.insts.get(e.obj).type(prog).toSig(prog);
String temp1 = next();
ret.addAll(PSMGen.makeTables(temp1, s, false));
String temp2 = next();
ret.addAll(PSMGen.makeTables(temp2, s, false));
Pair<Map<Node, List<Pair<Path, Attribute<Node>>>>, List<PSM>> xxx = Relationalizer.observations(s, temp1, e.tgt, false);
Pair<Map<Node, List<Pair<Path, Attribute<Node>>>>, List<PSM>> yyy = Relationalizer.observations(s, temp2, e.obj, false);
if (!xxx.first.equals(yyy.first)) {
throw new RuntimeException("not equal: " + xxx + " and " + yyy);
}
ret.addAll(xxx.second);
ret.addAll(yyy.second);
for (Node n : s.nodes) {
List<?> cols = xxx.first.get(n);
Map<String, String> from = new HashMap<>();
from.put("t1", e.tgt + "_" + n);
from.put("t1_obs", temp1 + "_" + n + "_" + "observables");
from.put("t2", e.obj + "_" + n);
from.put("t2_obs", temp2 + "_" + n + "_" + "observables");
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
where.add(new Pair<>(new Pair<>("t1", "c0"), new Pair<>("t1_obs", "id")));
where.add(new Pair<>(new Pair<>("t2", "c0"), new Pair<>("t2_obs", "id")));
for (int i = 0; i < cols.size(); i++) {
where.add(new Pair<>(new Pair<>("t1_obs", "c" + i), new Pair<>("t2_obs", "c" + i)));
}
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
select.put("c0", new Pair<>("t1", "c0"));
select.put("c1", new Pair<>("t2", "c0"));
Flower f = new Flower(select, from, where);
ret.add(new InsertSQL(dst + "_" + n, f, "c0", "c1"));
}
ret.addAll(PSMGen.dropTables(temp1, s));
ret.addAll(PSMGen.dropTables(temp2, s));
return ret;
} catch (FQLException fe) {
fe.printStackTrace();
throw new RuntimeException(fe.getLocalizedMessage());
}
}
use of catdata.fql.sql.Flower in project fql by CategoricalData.
the class Chase method chaseEgd.
private static List<Pair<Object, Object>> chaseEgd(Map<String, Set<Pair<Object, Object>>> i, List<String> forall, List<Triple<String, String, String>> where, List<Pair<String, String>> t) {
List<Pair<Object, Object>> ret = new LinkedList<>();
EmbeddedDependency xxx0 = conv(forall, where, t);
ED xxx = ED.from(xxx0);
Flower front = xxx.front();
Flower back = xxx.back();
Set<Map<Object, Object>> frontX = front.eval(ED.conv(i));
Set<Map<Object, Object>> backX = back.eval(ED.conv(i));
if (frontX.equals(backX)) {
return ret;
}
for (Pair<String, String> eq : t) {
int a = getColNo(xxx0.forall, xxx0.where, eq.first);
int b = getColNo(xxx0.forall, xxx0.where, eq.second);
for (Map<Object, Object> row : frontX) {
if (row.get("c" + a).toString().startsWith("_")) {
ret.add(new Pair<>(row.get("c" + a), row.get("c" + b)));
} else {
ret.add(new Pair<>(row.get("c" + b), row.get("c" + a)));
}
}
}
return ret;
}
use of catdata.fql.sql.Flower in project fql by CategoricalData.
the class Chase method chaseTgd.
private static Map<String, Set<Pair<Object, Object>>> chaseTgd(Map<String, Set<Pair<Object, Object>>> i, List<String> forall, List<Triple<String, String, String>> where, List<Triple<String, String, String>> t) {
Map<String, Set<Pair<Object, Object>>> ret = new HashMap<>();
for (String k : i.keySet()) {
ret.put(k, new HashSet<>());
}
EmbeddedDependency xxx0 = conv2(forall, where, t);
ED xxx = ED.from(xxx0);
Flower front = xxx.front();
Flower back = xxx.back();
Set<Map<Object, Object>> frontX = front.eval(ED.conv(i));
Set<Map<Object, Object>> backX = back.eval(ED.conv(i));
if (frontX.equals(backX)) {
return ret;
}
for (Map<Object, Object> eq : frontX) {
Map<String, String> map = new HashMap<>();
for (String v : xxx0.exists) {
String v0 = "_" + (fresh++);
map.put(v, v0);
}
for (Triple<String, String, String> fact : xxx0.tgd) {
Object a;
try {
a = eq.get("c" + getColNo(xxx0.forall, xxx0.where, fact.second));
} catch (Exception ee) {
a = map.get(fact.second);
}
Object b;
try {
b = eq.get("c" + getColNo(xxx0.forall, xxx0.where, fact.third));
} catch (Exception ee) {
b = map.get(fact.third);
}
ret.get(fact.first).add(new Pair<>(a, b));
}
}
return ret;
}
use of catdata.fql.sql.Flower in project fql by CategoricalData.
the class Chase method obeys.
private static boolean obeys(Map<String, Set<Pair<Object, Object>>> i0, List<Triple<List<String>, List<Triple<String, String, String>>, List<Triple<String, String, String>>>> tgds, List<Triple<List<String>, List<Triple<String, String, String>>, List<Pair<String, String>>>> egds) {
for (Triple<List<String>, List<Triple<String, String, String>>, List<Pair<String, String>>> egd : egds) {
EmbeddedDependency xxx0 = conv(egd.first, egd.second, egd.third);
ED xxx = ED.from(xxx0);
Flower front = xxx.front();
Flower back = xxx.back();
Set<Map<Object, Object>> frontX = front.eval(ED.conv(i0));
Set<Map<Object, Object>> backX = back.eval(ED.conv(i0));
if (!frontX.equals(backX)) {
return false;
}
}
for (Triple<List<String>, List<Triple<String, String, String>>, List<Triple<String, String, String>>> tgd : tgds) {
EmbeddedDependency xxx0 = conv2(tgd.first, tgd.second, tgd.third);
ED xxx = ED.from(xxx0);
Flower front = xxx.front();
Flower back = xxx.back();
Set<Map<Object, Object>> frontX = front.eval(ED.conv(i0));
Set<Map<Object, Object>> backX = back.eval(ED.conv(i0));
if (!frontX.equals(backX)) {
return false;
}
}
return true;
}
Aggregations