use of catdata.Triple in project fql by CategoricalData.
the class XCtx method extend.
// mutate paths in place
private static <C> void extend(Thue<C> kb, Collection<Triple<C, C, List<C>>> paths, Map<C, Pair<C, C>> t, Collection<Triple<C, C, List<C>>> consts) {
int iter = 0;
for (; iter < DefunctGlobalOptions.debug.fpql.MAX_PATH_LENGTH; iter++) {
Set<Triple<C, C, List<C>>> newPaths = new HashSet<>();
for (Triple<C, C, List<C>> p : paths) {
for (C e : outEdges(t, p.second)) {
List<C> p0 = new LinkedList<>(p.third);
p0.add(e);
Triple<C, C, List<C>> toAdd = new Triple<>(p.first, t.get(e).second, p0);
Triple<C, C, List<C>> found = find_old(kb, toAdd, paths);
if (found == null) {
found = find_old(kb, toAdd, newPaths);
if (found == null) {
find_old(kb, toAdd, consts);
newPaths.add(toAdd);
}
}
}
}
if (paths.containsAll(newPaths)) {
break;
}
paths.addAll(newPaths);
}
if (iter == DefunctGlobalOptions.debug.fpql.MAX_PATH_LENGTH) {
throw new RuntimeException("Exceeded maximum path length");
}
}
use of catdata.Triple in project fql by CategoricalData.
the class XEasikToFQL method translate1.
private static String translate1(Node sketch) {
List<String> ns = new LinkedList<>();
List<Triple<String, String, String>> es = new LinkedList<>();
List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
NodeList l = sketch.getChildNodes();
for (int temp = 0; temp < l.getLength(); temp++) {
Node n = l.item(temp);
NodeList j = n.getChildNodes();
for (int temp2 = 0; temp2 < j.getLength(); temp2++) {
Node m = j.item(temp2);
if (m.getNodeName().equals("entity")) {
String nodeName = m.getAttributes().getNamedItem("name").getTextContent();
ns.add(nodeName);
NodeList k = m.getChildNodes();
for (int temp3 = 0; temp3 < k.getLength(); temp3++) {
Node w = k.item(temp3);
if (w.getNodeName().equals("attribute")) {
String attName = w.getAttributes().getNamedItem("name").getTextContent();
es.add(new Triple<>(nodeName + "_" + attName.replace(" ", "_"), nodeName, "dom"));
}
}
} else if (m.getNodeName().equals("edge")) {
es.add(new Triple<>(m.getAttributes().getNamedItem("id").getTextContent(), m.getAttributes().getNamedItem("source").getTextContent(), m.getAttributes().getNamedItem("target").getTextContent()));
} else if (m.getNodeName().equals("commutativediagram")) {
NodeList k = m.getChildNodes();
Node w1 = null;
Node w2 = null;
for (int temp4 = 0; temp4 < k.getLength(); temp4++) {
Node wX = k.item(temp4);
if (wX.getNodeName().equals("path") && w1 == null) {
w1 = wX;
} else if (wX.getNodeName().equals("path") && w2 == null) {
w2 = wX;
}
}
if (w1 == null || w2 == null) {
throw new RuntimeException("Easik to FQL internal error");
}
String cod1 = w1.getAttributes().getNamedItem("domain").getTextContent();
String cod2 = w2.getAttributes().getNamedItem("domain").getTextContent();
List<String> lhs = new LinkedList<>();
List<String> rhs = new LinkedList<>();
lhs.add(cod1);
rhs.add(cod2);
NodeList lhsX = w1.getChildNodes();
for (int temp3 = 0; temp3 < lhsX.getLength(); temp3++) {
if (!lhsX.item(temp3).getNodeName().equals("edgeref")) {
continue;
}
String toAdd = lhsX.item(temp3).getAttributes().getNamedItem("id").getTextContent();
lhs.add(toAdd);
}
NodeList rhsX = w2.getChildNodes();
for (int temp3 = 0; temp3 < rhsX.getLength(); temp3++) {
if (!rhsX.item(temp3).getNodeName().equals("edgeref")) {
continue;
}
String toAdd = rhsX.item(temp3).getAttributes().getNamedItem("id").getTextContent();
rhs.add(toAdd);
}
eqs.add(new Pair<>(lhs, rhs));
}
}
}
XSchema sch = new XSchema(ns, es, eqs);
return sketch.getAttributes().getNamedItem("name").getTextContent().replace(" ", "_") + " = " + sch;
}
use of catdata.Triple in project fql by CategoricalData.
the class XChaser method massage.
private static Object massage(Triple<String, List<List<String>>, List<String>> x, XCtx I) {
if (x.first == null) {
return x.third;
}
List y = x.second.stream().map(z -> new Triple<>(I.type(z).first, I.type(z).second, z)).collect(Collectors.toList());
List z = new LinkedList<>();
z.add(new Pair<>(x.first, y));
if (x.third != null) {
z.addAll(x.third);
}
return z;
}
use of catdata.Triple in project fql by CategoricalData.
the class SchExpRaw method eval.
// TODO: aql printing of contexts broken when conitain choices
@SuppressWarnings("unused")
@Override
public synchronized Schema<Ty, En, Sym, Fk, Att> eval(AqlEnv env) {
TypeSide<Ty, Sym> ts = typeSide.eval(env);
Collage<Ty, En, Sym, Fk, Att, Void, Void> col = new Collage<>(ts.collage());
Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs0 = new HashSet<>();
for (String k : imports) {
@SuppressWarnings("unchecked") Schema<Ty, En, Sym, Fk, Att> v = env.defs.schs.get(k);
col.addAll(v.collage());
eqs0.addAll(v.eqs);
}
col.ens.addAll(ens.stream().map(x -> new En(x)).collect(Collectors.toList()));
col.fks.putAll(conv1(fks));
col.atts.putAll(conv2(atts));
for (Quad<String, String, RawTerm, RawTerm> eq : t_eqs) {
try {
Map<String, Chc<Ty, En>> ctx = Util.singMap(eq.first, eq.second == null ? null : Chc.inRight(new En(eq.second)));
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> eq0 = RawTerm.infer1x(ctx, eq.third, eq.fourth, null, col.convert(), "", ts.js).first3();
Chc<Ty, En> v = eq0.first.get(new Var(eq.first));
if (v.left) {
throw new RuntimeException(eq.first + " has type " + v.l + " which is not an entity");
}
En t = v.r;
eqs0.add(new Triple<>(new Pair<>(new Var(eq.first), t), eq0.second.convert(), eq0.third.convert()));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(find("obs equations", eq), "In equation " + eq.third + " = " + eq.fourth + ", " + ex.getMessage());
}
}
for (Pair<List<String>, List<String>> eq : p_eqs) {
try {
String vv = "v";
Var var = new Var(vv);
Map<String, Chc<Ty, En>> ctx = Util.singMap(vv, null);
RawTerm lhs = RawTerm.fold(col.fks.keySet(), col.ens, eq.first, vv);
RawTerm rhs = RawTerm.fold(col.fks.keySet(), col.ens, eq.second, vv);
Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> eq0 = RawTerm.infer1x(ctx, lhs, rhs, null, col.convert(), "", ts.js).first3();
Chc<Ty, En> v = eq0.first.get(var);
if (v.left) {
throw new RuntimeException("the equation's source " + eq.first + " is type " + v.l + " which is not an entity");
}
En t = v.r;
if (eq0.first.size() != 1) {
throw new RuntimeException("java constants cannot be used ");
}
eqs0.add(new Triple<>(new Pair<>(var, t), eq0.second.convert(), eq0.third.convert()));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(find("path equations", eq), "In equation " + Util.sep(eq.first, ".") + " = " + Util.sep(eq.second, ".") + ", " + ex.getMessage());
}
}
for (Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> eq : eqs0) {
col.eqs.add(new Eq<>(new Ctx<>(eq.first).inRight(), eq.second, eq.third));
}
AqlOptions strat = new AqlOptions(options, col, env.defaults);
AqlOptions s = new AqlOptions(Util.singMap(AqlOption.prover.toString(), ProverName.fail.toString()), col, env.defaults);
// forces type checking before prover construction
new Schema<>(ts, col.ens, col.atts.map, col.fks.map, eqs0, AqlProver.create(s, col, ts.js), false);
Schema<Ty, En, Sym, Fk, Att> ret = new Schema<>(ts, col.ens, col.atts.map, col.fks.map, eqs0, AqlProver.create(strat, col, ts.js), !((Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe)));
return ret;
}
use of catdata.Triple in project fql by CategoricalData.
the class QueryExpRaw method eval.
@Override
public Query<Ty, En, Sym, Fk, Att, En, Fk, Att> eval(AqlEnv env) {
Schema<Ty, En, Sym, Fk, Att> src0 = src.eval(env);
Schema<Ty, En, Sym, Fk, Att> dst0 = dst.eval(env);
Ctx<En, Triple<Ctx<Var, En>, Collection<Eq<Ty, En, Sym, Fk, Att, Var, Var>>, AqlOptions>> ens0 = new Ctx<>();
Ctx<Att, Term<Ty, En, Sym, Fk, Att, Var, Var>> atts0 = new Ctx<>();
Ctx<Fk, Pair<Ctx<Var, Term<Void, En, Void, Fk, Void, Var, Void>>, Boolean>> fks0 = new Ctx<>();
Ctx<Var, Ty> xxx = new Ctx<>();
Ctx<Var, Term<Ty, Void, Sym, Void, Void, Void, Void>> yyy = new Ctx<>();
for (String k : imports) {
@SuppressWarnings("unchecked") Query<Ty, En, Sym, Fk, Att, En, Fk, Att> v = env.defs.qs.get(k);
for (Var var : v.params.keySet()) {
// allow benign collisions
xxx.map.put(var, v.params.get(var));
}
for (Var var : v.consts.keySet()) {
yyy.map.put(var, v.consts.get(var));
}
for (En En : v.ens.keySet()) {
ens0.put(En, new Triple<>(v.ens.get(En).gens, v.ens.get(En).eqs, v.ens.get(En).options));
}
for (Att Att : v.atts.keySet()) {
atts0.put(Att, v.atts.get(Att));
}
for (Fk Fk : v.fks.keySet()) {
fks0.put(Fk, new Pair<>(v.fks.get(Fk).gens(), v.doNotValidate.get(Fk)));
}
}
Ctx<En, Collage<Ty, En, Sym, Fk, Att, Var, Var>> cols = new Ctx<>();
for (Block p : blocks) {
try {
if (!dst0.ens.contains(p.en)) {
throw new RuntimeException("the proposed target entity " + p.en + " does not actually appear in the target schema");
}
processBlock(options, env, src0, ens0, cols, p, params);
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(b1.get(p.en), "In block for target entity " + p.en + ", " + ex.getMessage());
}
for (Pair<catdata.aql.exp.SchExpRaw.Att, RawTerm> pp : p.atts) {
try {
processAtt(src0, dst0, ens0, atts0, cols, pp, params);
} catch (Exception ex) {
ex.printStackTrace();
throw new LocException(b3.get(pp.first), "In return clause for " + pp.first + ", " + ex.getMessage());
}
}
}
// two loops bc need stuff in en to do this part
for (Block p : blocks) {
for (Pair<catdata.aql.exp.SchExpRaw.Fk, Trans> pp : p.fks) {
try {
Ctx<Var, Term<Void, En, Void, Fk, Void, Var, Void>> trans = new Ctx<>();
for (Pair<Var, RawTerm> v : pp.second.gens) {
Ctx<String, Chc<Ty, En>> ctx = unVar(ens0.get(dst0.fks.get(pp.first).first).first.inRight());
Collage<Ty, En, Sym, Fk, Att, Var, Var> col = cols.get(dst0.fks.get(pp.first).first);
Chc<Ty, En> required = Chc.inRight(ens0.get(dst0.fks.get(pp.first).second).first.get(v.first));
Term<catdata.aql.exp.TyExpRaw.Ty, catdata.aql.exp.SchExpRaw.En, catdata.aql.exp.TyExpRaw.Sym, catdata.aql.exp.SchExpRaw.Fk, catdata.aql.exp.SchExpRaw.Att, Gen, Sk> term = RawTerm.infer1x(ctx.map, v.second, null, required, col.convert(), "in foreign key " + pp.first.str + ", ", src0.typeSide.js).second;
trans.put(v.first, freeze(term.convert(), params).convert());
}
boolean doNotCheckEqs = (Boolean) new AqlOptions(pp.second.options, null, env.defaults).getOrDefault(AqlOption.dont_validate_unsafe);
fks0.put(pp.first, new Pair<>(trans, doNotCheckEqs));
} catch (RuntimeException ex) {
ex.printStackTrace();
throw new LocException(b2.get(pp.first), ex.getMessage());
}
}
}
boolean doNotCheckEqs = (Boolean) new AqlOptions(options, null, env.defaults).getOrDefault(AqlOption.dont_validate_unsafe);
boolean elimRed = (Boolean) new AqlOptions(options, null, env.defaults).getOrDefault(AqlOption.query_remove_redundancy);
for (String s : params.keySet()) {
xxx.put(new Var(s), new Ty(params.get(s)));
}
for (String s : consts.keySet()) {
Chc<Ty, En> required = Chc.inLeft(xxx.get(new Var(s)));
Term<catdata.aql.exp.TyExpRaw.Ty, catdata.aql.exp.SchExpRaw.En, catdata.aql.exp.TyExpRaw.Sym, catdata.aql.exp.SchExpRaw.Fk, catdata.aql.exp.SchExpRaw.Att, Gen, Sk> term = RawTerm.infer1x(new HashMap<>(), consts.get(s), null, required, src0.collage().convert(), "", src0.typeSide.js).second;
yyy.put(new Var(s), term.convert());
}
return Query.makeQuery2(xxx, yyy, ens0, atts0, fks0, src0, dst0, doNotCheckEqs, elimRed);
}
Aggregations