Search in sources :

Example 1 with SchExpVar

use of catdata.aql.exp.SchExp.SchExpVar 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();
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) SchExpVar(catdata.aql.exp.SchExp.SchExpVar) NodeList(org.w3c.dom.NodeList) List(java.util.List) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Pair(catdata.Pair)

Example 2 with SchExpVar

use of catdata.aql.exp.SchExp.SchExpVar 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);
}
Also used : Node(org.w3c.dom.Node) EdExpRaw(catdata.aql.exp.EdsExpRaw.EdExpRaw) NodeList(org.w3c.dom.NodeList) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) Ty(catdata.aql.exp.TyExpRaw.Ty) NodeList(org.w3c.dom.NodeList) RawTerm(catdata.aql.RawTerm) LinkedList(java.util.LinkedList) SchExpVar(catdata.aql.exp.SchExp.SchExpVar)

Aggregations

Pair (catdata.Pair)2 SchExpVar (catdata.aql.exp.SchExp.SchExpVar)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 RawTerm (catdata.aql.RawTerm)1 EdExpRaw (catdata.aql.exp.EdsExpRaw.EdExpRaw)1 Ty (catdata.aql.exp.TyExpRaw.Ty)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.w3c.dom.Document)1