use of catdata.Pair in project fql by CategoricalData.
the class CfgToOpl method toCfg.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static OplExp toCfg(Object o) {
Map<String, List<List<String>>> ret = new HashMap<>();
List<Tuple3> l = (List<Tuple3>) o;
for (Tuple3 p : l) {
String x = (String) p.a;
if (ret.containsKey(x)) {
throw new RuntimeException("Duplicate production name: " + x);
}
ret.put(x, (List<List<String>>) p.c);
}
Map<String, Pair<List<String>, String>> symbols = new HashMap<>();
int i = 0;
for (String k : ret.keySet()) {
List<List<String>> v = ret.get(k);
for (List<String> u : v) {
List<String> pre = new LinkedList<>();
List<String> tys = new LinkedList<>();
for (String z : u) {
if (ret.keySet().contains(z)) {
tys.add(z);
} else {
pre.add(z);
}
}
String name0 = Util.sep(pre, "_");
String xxx = symbols.keySet().contains(name0) ? "_" + (i++) : "";
String name = "\"" + name0 + xxx + "\"";
symbols.put(name, new Pair<>(tys, k));
i++;
}
}
return new OplSig(null, new HashMap<>(), ret.keySet(), symbols, new LinkedList<>());
}
use of catdata.Pair in project fql by CategoricalData.
the class ColimSchExpModify method eval.
// TODO aql add options
@Override
public ColimitSchema<N> eval(AqlEnv env) {
boolean checkJava = !(Boolean) env.defaults.getOrDefault(options, AqlOption.allow_java_eqs_unsafe);
ColimitSchema<N> colim0 = colim.eval(env);
// colim0.schemaStr.co
for (Pair<String, String> k : ens) {
colim0 = colim0.renameEntity(new En(k.first), new En(k.second), checkJava);
}
for (Pair<Pair<String, String>, String> k : fks0) {
colim0 = colim0.renameFk(new Fk(new En(k.first.first), k.first.second), new Fk(new En(k.first.first), k.second), checkJava);
}
for (Pair<Pair<String, String>, String> k : atts0) {
colim0 = colim0.renameAtt(new Att(new En(k.first.first), k.first.second), new Att(new En(k.first.first), k.second), checkJava);
}
for (Pair<Pair<String, String>, List<String>> k : fks) {
if (!colim0.schemaStr.fks.containsKey(new Fk(new En(k.first.first), k.first.second))) {
throw new RuntimeException("Not an fk: " + k.first + " in\n\n" + colim0.schemaStr);
}
String pre = "In processing " + k.first + " -> " + k.second + ", ";
Collage<Ty, En, Sym, Fk, Att, Void, Void> xxx = colim0.schemaStr.collage();
RawTerm term = RawTerm.fold(k.second, "v");
En tr = colim0.schemaStr.fks.get(new Fk(new En(k.first.first), k.first.second)).second;
Ctx<String, Chc<Ty, En>> ctx = new Ctx<>("v", Chc.inRight(new En(k.first.first)));
Term<Ty, En, Sym, Fk, Att, Gen, Sk> t = RawTerm.infer1x(ctx.map, term, null, Chc.inRight(tr), xxx.convert(), pre, colim0.schemaStr.typeSide.js).second;
// colim0 = colim0.removeAtt(new Att(k.first), new Var(k.second.first), t.convert(), checkJava);
colim0 = colim0.removeFk(new Fk(new En(k.first.first), k.first.second), t.toFkList(), checkJava);
}
for (Pair<Pair<String, String>, Triple<String, String, RawTerm>> k : atts) {
if (!colim0.schemaStr.atts.containsKey(new Att(new En(k.first.first), k.first.second))) {
throw new RuntimeException("Not an attribute: " + k.first + " in\n\n" + colim0.schemaStr);
}
String pre = "In processing " + k.first + " -> lambda " + k.second.first + "." + k.second.third + ", ";
Pair<En, Ty> r = colim0.schemaStr.atts.get(new Att(new En(k.first.first), k.first.second));
if (k.second.second != null && !k.second.second.equals(r.first)) {
throw new RuntimeException(pre + " given type is " + k.second.second + " but expected " + r.first);
}
Collage<Ty, En, Sym, Fk, Att, Void, Void> xxx = colim0.schemaStr.collage();
Ctx<String, Chc<Ty, En>> ctx = new Ctx<>(k.second.first, Chc.inRight(r.first));
Term<Ty, En, Sym, Fk, Att, Gen, Sk> t = RawTerm.infer1x(ctx.map, k.second.third, null, Chc.inLeft(r.second), xxx.convert(), pre, colim0.schemaStr.typeSide.js).second;
colim0 = colim0.removeAtt(new Att(new En(k.first.first), k.first.second), new Var(k.second.first), t.convert(), checkJava);
}
return colim0;
}
use of catdata.Pair in project fql by CategoricalData.
the class EasikAql method easikToAql.
/*
public static String javaClassFor(String s) {
switch (s) {
case "BigInt":
return "java.lang.Long";
case "Boolean":
return "java.lang.Boolean";
case "Char":
return "java.lang.Character";
case "DoublePrecision":
return "java.lang.Double";
case "Float":
return "java.lang.Float";
case "Int":
return "java.lang.Integer";
case "SmallInt":
return "java.lang.Short";
case "Text":
return "java.lang.String";
case "Varchar":
return "java.lang.String";
case "Custom":
return "java.lang.Object";
// case "Blob" : return "Blob"; //byte[]
// case "Date" : return "java.sql.Date";
// case "Decimal" : return "java.math.BigDecimal";
// case "Time" : return "java.sql.time";
// case "TimeStamp" : return "java.sql.TimeStamp";
default:
return "java.lang.Object";
}
}
public static String javaParserFor(String s) {
switch (s) {
case "BigInt":
return "return new java.lang.Long(input[0])";
case "Boolean":
return "return new java.lang.Boolean(input[0])";
case "Char":
return "return input[0].charAt(0)";
case "DoublePrecision":
return "return new java.lang.Double(input[0])";
case "Float":
return "return new java.lang.Float(input[0])"; // Float
case "Int":
return "return new java.lang.Integer(input[0])"; // Integer
case "SmallInt":
return "return new java.lang.Short(input[0])"; // Short
case "Text":
return "return input[0]"; // String
case "Varchar":
return "return input[0]";
case "Custom":
return "return input[0]"; // Object
// case "Blob" : return "Blob"; //byte[]
// case "Date" : return "Date";
// case "Decimal" : return "Decimal";
// case "Time" : return "Time"; //java.sql.time
// case "TimeStamp" : return "TimeStamp"; //java.sql.timestamp
default:
return "return input[0]";
}
} */
/*
// TODO: AQL add operations here
public static TyExpRaw sql(Set<String> used) {
List<Pair<String, String>> java_tys = new LinkedList<>();
List<Pair<String, String>> java_parsers = new LinkedList<>();
for (String s0 : used) {
String s = easikTypeToString(s0);
java_tys.add(new Pair<>(s, javaClassFor(s)));
java_parsers.add(new Pair<>(s, javaParserFor(s)));
}
return new TyExpRaw(new LinkedList<>(), new LinkedList<>(), new LinkedList<>(), new LinkedList<>(), java_tys, java_parsers, new LinkedList<>(), new LinkedList<>());
} */
public static String easikToAql(String in) {
String ret = "";
Set<String> tys = new HashSet<>();
Set<String> warnings = new HashSet<>();
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
InputStream stream = new ByteArrayInputStream(in.getBytes(StandardCharsets.UTF_8));
Document doc = dBuilder.parse(stream);
doc.getDocumentElement().normalize();
NodeList sketchesNodes = doc.getElementsByTagName("sketches");
if (sketchesNodes.getLength() != 1) {
throw new RuntimeException("multiple sketches tags");
}
Node sketchesNode = sketchesNodes.item(0);
NodeList nList = sketchesNode.getChildNodes();
String ret2 = "";
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (!nNode.getNodeName().equals("easketch")) {
continue;
}
String s0 = nNode.getAttributes().getNamedItem("name").getTextContent().replace(" ", "_") + "_schema";
Pair<SchExp<?, ?, ?, ?, ?>, List<Pair<String, EdsExpRaw>>> schExp0 = translate1(nNode, tys, warnings, s0);
SchExp<?, ?, ?, ?, ?> schExp = schExp0.first;
String s1 = "schema " + s0 + " = " + schExp + "\n\n";
List<Pair<String, EdsExpRaw>> edsExps = schExp0.second;
edsExps.addAll(translateC(nNode, warnings, new SchExpVar(s0)));
ret2 += s1;
int i = 0;
List<String> imports = new LinkedList<>();
for (Pair<String, EdsExpRaw> edsExp : edsExps) {
String x = nNode.getAttributes().getNamedItem("name").getTextContent().replace(" ", "_") + "_" + edsExp.first + i;
ret2 += "constraints " + x + " = " + edsExp.second + "\n\n";
imports.add(x);
i++;
}
if (!imports.isEmpty()) {
ret2 += "constraints " + nNode.getAttributes().getNamedItem("name").getTextContent().replace(" ", "_") + "_constraints = literal : " + s0 + " {\n\timports\n\t\t" + Util.sep(imports, "\n\t\t") + "\n}\n\n";
}
}
NodeList views = doc.getElementsByTagName("views");
if (views != null && views.getLength() != 0 && views.item(0).hasChildNodes()) {
warnings.add("Cannot export views - AQL does not currently support views ");
}
ret = "typeside SqlTypeSide = sql \n\n" + ret2;
if (!warnings.isEmpty()) {
ret += "/* Warnings:\n\n" + Util.sep(warnings, "\n") + "\n*/";
}
return ret;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
use of catdata.Pair in project fql by CategoricalData.
the class EasikAql method translate1.
private static Pair<SchExp<?, ?, ?, ?, ?>, List<Pair<String, EdsExpRaw>>> translate1(Node sketch, Set<String> used, Set<String> warnings, String sname) {
List<String> ens = new LinkedList<>();
List<Pair<String, Pair<String, Ty>>> atts = new LinkedList<>();
List<Pair<String, Pair<String, String>>> fks = new LinkedList<>();
List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
// there shouldn't be observation equations in easik
// List<Quad<String, String, RawTerm, RawTerm>> eqs2 = new
// LinkedList<>();
List<Pair<String, EdsExpRaw>> edsExps = 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 = safe(m.getAttributes().getNamedItem("name").getTextContent());
ens.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 = safe(w.getAttributes().getNamedItem("name").getTextContent());
String tyName = w.getAttributes().getNamedItem("attributeTypeClass").getTextContent();
used.add(tyName);
atts.add(new Pair<>(nodeName + "_" + attName.replace(" ", "_"), new Pair<>(nodeName, new Ty(easikTypeToString(tyName)))));
}
}
} else if (m.getNodeName().equals("edge")) {
String ename = safe(m.getAttributes().getNamedItem("id").getTextContent());
String esrc = safe(m.getAttributes().getNamedItem("source").getTextContent());
fks.add(new Pair<>(ename, new Pair<>(esrc, safe(m.getAttributes().getNamedItem("target").getTextContent()))));
if (m.getAttributes().getNamedItem("type").getTextContent().equals("injective")) {
List<EdExpRaw> eds = new LinkedList<>();
List<Pair<String, String>> As = new LinkedList<>();
As.add(new Pair<>("x", esrc));
As.add(new Pair<>("y", esrc));
List<Pair<RawTerm, RawTerm>> Aeqs = new LinkedList<>();
Aeqs.add(new Pair<>(new RawTerm(ename, Util.singList(new RawTerm("x"))), new RawTerm(ename, Util.singList(new RawTerm("y")))));
List<Pair<String, String>> Es = new LinkedList<>();
List<Pair<RawTerm, RawTerm>> Eeqs = new LinkedList<>();
Eeqs.add(new Pair<>(new RawTerm("x"), new RawTerm("y")));
EdExpRaw ed = new EdExpRaw(As, Aeqs, Es, Eeqs, false, null);
eds.add(ed);
edsExps.add(new Pair<>("injective", new EdsExpRaw(new SchExpVar(sname), new LinkedList<>(), eds, null)));
}
if (m.getAttributes().getNamedItem("type").getTextContent().equals("partial")) {
warnings.add("Not exported - partial edges. Their AQL semantics is unclear");
}
} else if (m.getNodeName().equals("uniqueKey")) {
String esrc = safe(m.getAttributes().getNamedItem("noderef").getTextContent());
List<String> atts0 = new LinkedList<>();
for (int w = 0; w < m.getChildNodes().getLength(); w++) {
Node node = m.getChildNodes().item(w);
if (!node.getNodeName().equals("attref")) {
continue;
}
String att = safe(node.getAttributes().getNamedItem("name").getTextContent());
atts0.add(att);
}
List<EdExpRaw> eds = new LinkedList<>();
List<Pair<String, String>> As = new LinkedList<>();
As.add(new Pair<>("x", esrc));
As.add(new Pair<>("y", esrc));
List<Pair<RawTerm, RawTerm>> Aeqs = new LinkedList<>();
for (String att : atts0) {
Aeqs.add(new Pair<>(new RawTerm(esrc + "_" + att, Util.singList(new RawTerm("x"))), new RawTerm(esrc + "_" + att, Util.singList(new RawTerm("y")))));
}
List<Pair<String, String>> Es = new LinkedList<>();
List<Pair<RawTerm, RawTerm>> Eeqs = new LinkedList<>();
Eeqs.add(new Pair<>(new RawTerm("x"), new RawTerm("y")));
EdExpRaw ed = new EdExpRaw(As, Aeqs, Es, Eeqs, false, null);
eds.add(ed);
edsExps.add(new Pair<>("key", new EdsExpRaw(new SchExpVar(sname), new LinkedList<>(), eds, null)));
} 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 AQL internal error");
}
String cod1 = safe(w1.getAttributes().getNamedItem("domain").getTextContent());
String cod2 = safe(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 = safe(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 = safe(rhsX.item(temp3).getAttributes().getNamedItem("id").getTextContent());
rhs.add(toAdd);
}
eqs.add(new Pair<>(lhs, rhs));
}
}
}
SchExp<?, ?, ?, ?, ?> schExp = new SchExpRaw(new TyExpVar<>("sql"), new LinkedList<>(), ens, fks, eqs, atts, new LinkedList<>(), new LinkedList<>(), null);
return new Pair<>(schExp, edsExps);
}
use of catdata.Pair in project fql by CategoricalData.
the class EasikAql method path.
private static Pair<List<String>, String> path(Node w1) {
String dom = safe(w1.getAttributes().getNamedItem("domain").getTextContent());
List<String> lhs = new LinkedList<>();
lhs.add(dom);
NodeList lhsX = w1.getChildNodes();
for (int temp3 = 0; temp3 < lhsX.getLength(); temp3++) {
if (!lhsX.item(temp3).getNodeName().equals("edgeref")) {
continue;
}
String toAdd = safe(lhsX.item(temp3).getAttributes().getNamedItem("id").getTextContent());
lhs.add(toAdd);
}
return new Pair<>(lhs, safe(w1.getAttributes().getNamedItem("codomain").getTextContent()));
}
Aggregations