Search in sources :

Example 11 with Tuple4

use of org.jparsec.functors.Tuple4 in project fql by CategoricalData.

the class CombinatorParser method schExpRaw.

private static Parser<SchExpRaw> schExpRaw() {
    Parser<List<LocStr>> entities = Parsers.tuple(token("entities"), locstr.many()).map(x -> x.b);
    Parser<Pair<Token, List<Tuple5<List<LocStr>, Token, String, Token, String>>>> fks = Parsers.tuple(token("foreign_keys"), Parsers.tuple(locstr.many1(), token(":"), ident, token("->"), ident).many());
    Parser<List<catdata.Pair<LocStr, catdata.Pair<String, String>>>> fks0 = fks.map(x -> {
        List<catdata.Pair<LocStr, catdata.Pair<String, String>>> ret = new LinkedList<>();
        for (Tuple5<List<LocStr>, Token, String, Token, String> a : x.b) {
            for (LocStr b : a.a) {
                ret.add(new catdata.Pair<>(b, new catdata.Pair<>(a.c, a.e)));
            }
        }
        return ret;
    });
    Parser<Pair<Token, List<Tuple5<List<LocStr>, Token, String, Token, String>>>> atts = Parsers.tuple(token("attributes"), Parsers.tuple(locstr.many1(), token(":"), ident, token("->"), ident).many());
    Parser<List<catdata.Pair<LocStr, catdata.Pair<String, String>>>> atts0 = atts.map(x -> {
        List<catdata.Pair<LocStr, catdata.Pair<String, String>>> ret = new LinkedList<>();
        for (Tuple5<List<LocStr>, Token, String, Token, String> a : x.b) {
            for (LocStr b : a.a) {
                ret.add(new catdata.Pair<>(b, new catdata.Pair<>(a.c, a.e)));
            }
        }
        return ret;
    });
    Parser<catdata.Pair<Integer, catdata.Pair<List<String>, List<String>>>> p_eq = Parsers.tuple(Parsers.INDEX, Parsers.tuple(ident.sepBy(token(".")), token("="), ident.sepBy(token(".")))).map(x -> new catdata.Pair<>(x.a, new catdata.Pair<>(x.b.a, x.b.c)));
    Parser<Pair<Token, List<catdata.Pair<Integer, catdata.Pair<List<String>, List<String>>>>>> p_eqs = Parsers.tuple(token("path_equations"), p_eq.many());
    Parser<List<catdata.Pair<Integer, catdata.Pair<List<String>, List<String>>>>> p_eqs0 = p_eqs.map(x -> x.b);
    Parser<catdata.Pair<Integer, Quad<String, String, RawTerm, RawTerm>>> o_eq_from_p_eq = p_eq.map(x -> new catdata.Pair<>(x.first, new Quad<>("_x", null, RawTerm.fold(x.second.first, "_x"), RawTerm.fold(x.second.second, "_x"))));
    Parser<catdata.Pair<Integer, Quad<String, String, RawTerm, RawTerm>>> o_eq_old = Parsers.tuple(Parsers.INDEX, Parsers.tuple(token("forall"), ident, Parsers.tuple(token(":"), ident).optional().followedBy(token(".")), term().followedBy(token("=")), term())).map(x -> new catdata.Pair<>(x.a, new Quad<>(x.b.b, x.b.c == null ? null : x.b.c.b, x.b.d, x.b.e)));
    Parser<catdata.Pair<Integer, Quad<String, String, RawTerm, RawTerm>>> o_eq = Parsers.or(o_eq_old, o_eq_from_p_eq);
    Parser<Pair<Token, List<catdata.Pair<Integer, Quad<String, String, RawTerm, RawTerm>>>>> o_eqs = Parsers.tuple(token("observation_equations"), o_eq.many());
    Parser<List<catdata.Pair<Integer, Quad<String, String, RawTerm, RawTerm>>>> o_eqs0 = o_eqs.map(x -> x.b);
    Parser<Tuple4<List<LocStr>, List<LocStr>, List<catdata.Pair<LocStr, catdata.Pair<String, String>>>, List<catdata.Pair<Integer, catdata.Pair<List<String>, List<String>>>>>> pa = Parsers.tuple(imports, entities.optional(), fks0.optional(), p_eqs0.optional());
    Parser<Tuple3<List<catdata.Pair<LocStr, catdata.Pair<String, String>>>, List<catdata.Pair<Integer, Quad<String, String, RawTerm, RawTerm>>>, List<catdata.Pair<String, String>>>> pb = Parsers.tuple(atts0.optional(), o_eqs0.optional(), options);
    Parser<Tuple4<Token, Token, TyExp<?, ?>, Token>> l = Parsers.tuple(token("literal"), token(":"), ty_ref.lazy(), // .map(x -> x.c);
    token("{"));
    // needs tyexp
    Parser<SchExpRaw> ret = Parsers.tuple(l, pa, pb, token("}")).map(x -> new SchExpRaw((TyExp<Ty, Sym>) x.a.c, x.b.a, Util.newIfNull(x.b.b), Util.newIfNull(x.b.c), Util.newIfNull(x.b.d), Util.newIfNull(x.c.a), Util.newIfNull(x.c.b), x.c.c));
    return ret;
}
Also used : Quad(catdata.Quad) Token(org.jparsec.Token) List(java.util.List) LinkedList(java.util.LinkedList) Pair(org.jparsec.functors.Pair) RawTerm(catdata.aql.RawTerm) LinkedList(java.util.LinkedList) Tuple4(org.jparsec.functors.Tuple4) Tuple5(org.jparsec.functors.Tuple5) Tuple3(org.jparsec.functors.Tuple3) ColimSchExpRaw(catdata.aql.exp.ColimSchExp.ColimSchExpRaw)

Example 12 with Tuple4

use of org.jparsec.functors.Tuple4 in project fql by CategoricalData.

the class XNeo4jToFQL method fromNodes.

/*			Parser<?> q = Parsers.tuple(ident(), term("="), string());
		Parser<?> p = Parsers.tuple(ident(), term(":"), ident(), Parsers.tuple(term("{"), q.sepBy(term(",")), term("}")));
		return Parsers.tuple(term("CREATE"), Parsers.tuple(term("("), p, term(")")).sepBy(term(",")));
	*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private static Map<String, Map<String, Object>> fromNodes(Object oo) {
    Map<String, Map<String, Object>> ret = new HashMap<>();
    org.jparsec.functors.Pair o = (org.jparsec.functors.Pair) oo;
    List<Tuple3> xx = (List<Tuple3>) o.b;
    for (Tuple3 tt : xx) {
        Tuple4 t = (Tuple4) tt.b;
        String n = (String) t.a;
        String l = (String) t.c;
        if (ret.containsKey(n)) {
            throw new RuntimeException("Duplicate node: " + n);
        }
        Map<String, Object> m = new HashMap<>();
        m.put("label", l);
        Tuple3 pp = (Tuple3) t.d;
        List<Tuple3> p = (List<Tuple3>) pp.b;
        for (Tuple3 q : p) {
            String k = (String) q.a;
            String v = (String) q.c;
            if (m.containsKey(k)) {
                throw new RuntimeException("Duplicate property: " + k);
            }
            m.put(k, v);
        }
        ret.put(n, m);
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) Tuple4(org.jparsec.functors.Tuple4) Tuple3(org.jparsec.functors.Tuple3) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Pair(catdata.Pair)

Example 13 with Tuple4

use of org.jparsec.functors.Tuple4 in project fql by CategoricalData.

the class XNeo4jToFQL method fromEdges.

/*		Parser<?> n = ident().between(term("("), term(")"));
		Parser<?> e = Parsers.tuple(term("["), term(":"), ident(), term("]"));
		Parser<?> p = Parsers.tuple(n, term("-"), e, term("->"), n);
		return Parsers.tuple(term("CREATE"), p.sepBy(term(","))); */
@SuppressWarnings({ "rawtypes", "unchecked" })
private static Map<String, Set<Pair<String, String>>> fromEdges(Object oo) {
    Map<String, Set<Pair<String, String>>> ret = new HashMap<>();
    org.jparsec.functors.Pair o = (org.jparsec.functors.Pair) oo;
    List<Tuple5> xx = (List<Tuple5>) o.b;
    for (Tuple5 tt : xx) {
        String s = (String) tt.a;
        String t = (String) tt.e;
        Tuple4 e0 = (Tuple4) tt.c;
        String e = (String) e0.c;
        Set<Pair<String, String>> set = ret.computeIfAbsent(e, k -> new HashSet<>());
        set.add(new Pair<>(s, t));
    }
    return ret;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Tuple4(org.jparsec.functors.Tuple4) Tuple5(org.jparsec.functors.Tuple5) LinkedList(java.util.LinkedList) List(java.util.List) Pair(catdata.Pair)

Example 14 with Tuple4

use of org.jparsec.functors.Tuple4 in project fql by CategoricalData.

the class XParser method toInstConst.

/* public static final Parser<?> instance(Reference ref) {
		Parser<?> node = Parsers.tuple(ident(), term(":"), ident());
		Parser<?> p3 = Parsers.tuple(path(), term("="), path());
		Parser<?> xxx = Parsers.tuple(section("variables", node), 
				section("equations", p3));
		Parser kkk = ((Parser)term("INSTANCE")).or((Parser) term("instance"));
		Parser<?> constant = Parsers
				.tuple(kkk, xxx.between(term("{"), term("}")), term(":"),
						ref.lazy());
		return constant;  */
private static XInst toInstConst(Object decl) {
    Tuple4 y = (Tuple4) decl;
    org.jparsec.functors.Pair x = (org.jparsec.functors.Pair) y.b;
    Tuple3 nodes = (Tuple3) x.a;
    Tuple3 arrows = (Tuple3) x.b;
    List nodes0 = (List) nodes.b;
    List arrows0 = (List) arrows.b;
    List<Pair<String, String>> nodesX = new LinkedList<>();
    for (Object o : nodes0) {
        Tuple3 u = (Tuple3) o;
        List<String> n2 = (List) u.a;
        String l = (String) u.c;
        for (String n : n2) {
            // String n = (String) u.a;
            nodesX.add(new Pair<>(n, l));
        }
    }
    List<Pair<List<String>, List<String>>> eqsX = new LinkedList<>();
    for (Object o : arrows0) {
        Tuple3 u = (Tuple3) o;
        List<String> n = (List<String>) u.a;
        List<String> m = (List<String>) u.c;
        eqsX.add(new Pair<>(n, m));
    }
    XInst ret = new XInst(toExp(y.d), nodesX, eqsX);
    if (y.a.toString().equals("INSTANCE")) {
        ret.saturated = true;
    }
    return ret;
}
Also used : XInst(catdata.fpql.XExp.XInst) LinkedList(java.util.LinkedList) Tuple4(org.jparsec.functors.Tuple4) Tuple3(org.jparsec.functors.Tuple3) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) XPair(catdata.fpql.XExp.XPair)

Example 15 with Tuple4

use of org.jparsec.functors.Tuple4 in project fql by CategoricalData.

the class XParser method fromSuperSoed.

/*
	J = soed {
		exists f:A->B, g:C->D;
		forall a:A, a.f = p.q, a.g = p.f;
		forall b:B, p = q; 
	} : X -> Y on I
		 */
private static XSuperED fromSuperSoed(Object ooo) {
    org.jparsec.functors.Pair ooo1 = (org.jparsec.functors.Pair) ooo;
    Tuple4 a = (Tuple4) ooo1.a;
    // List<Triple<String, List<String>, String>> es = new LinkedList<>();
    List<SuperFOED> as = new LinkedList<>();
    Map<String, List<String>> dom = new HashMap<>();
    Map<String, String> cod = new HashMap<>();
    List<Tuple5> es0 = (List<Tuple5>) a.b;
    for (Tuple5 t : es0) {
        if (dom.keySet().contains(t.a)) {
            throw new RuntimeException("Duplicate function name " + t.a);
        }
        dom.put((String) t.a, (List<String>) t.c);
        cod.put((String) t.a, (String) t.e);
    }
    List<Tuple4> as0 = (List<Tuple4>) a.d;
    for (Tuple4 t : as0) {
        List<Tuple3> aas = (List<Tuple3>) t.b;
        Map<String, String> aa = new HashMap<>();
        for (Tuple3 xxx : aas) {
            if (aa.containsKey(xxx.a)) {
                throw new RuntimeException("Duplicate var " + xxx.a);
            }
            aa.put((String) xxx.a, (String) xxx.c);
        }
        Tuple3 td = (Tuple3) t.d;
        List<Tuple3> lhss = (List<Tuple3>) td.a;
        List<Tuple3> rhss = (List<Tuple3>) td.c;
        List<Pair<Triple<String, List<List<String>>, List<String>>, Triple<String, List<List<String>>, List<String>>>> cc = new LinkedList<>();
        List<Pair<Triple<String, List<List<String>>, List<String>>, Triple<String, List<List<String>>, List<String>>>> bb = new LinkedList<>();
        for (Tuple3 o : lhss) {
            bb.add(new Pair<>(fromBulb(o.a), fromBulb(o.c)));
        }
        for (Tuple3 o : rhss) {
            cc.add(new Pair<>(fromBulb(o.a), fromBulb(o.c)));
        }
        as.add(new SuperFOED(aa, bb, cc));
    }
    Tuple4 b = (Tuple4) ooo1.b;
    String src = (String) b.b;
    String dst = (String) b.c;
    String i = (String) b.d;
    // es, as, src, dst, i);
    XSuperED ret = new XSuperED(dom, cod, as, src, dst, i);
    return ret;
}
Also used : HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) XSuperED(catdata.fpql.XExp.XSuperED) Tuple4(org.jparsec.functors.Tuple4) Tuple5(org.jparsec.functors.Tuple5) SuperFOED(catdata.fpql.XExp.XSuperED.SuperFOED) Tuple3(org.jparsec.functors.Tuple3) List(java.util.List) LinkedList(java.util.LinkedList) Pair(catdata.Pair) XPair(catdata.fpql.XExp.XPair)

Aggregations

Tuple4 (org.jparsec.functors.Tuple4)36 Pair (catdata.Pair)30 Tuple3 (org.jparsec.functors.Tuple3)30 List (java.util.List)25 LinkedList (java.util.LinkedList)24 Tuple5 (org.jparsec.functors.Tuple5)18 HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)8 HashSet (java.util.HashSet)6 XPair (catdata.fpql.XExp.XPair)5 Map (java.util.Map)4 Triple (catdata.Triple)3 Pair (org.jparsec.functors.Pair)3 Chc (catdata.Chc)2 RawTerm (catdata.aql.RawTerm)2 ColimSchExpRaw (catdata.aql.exp.ColimSchExp.ColimSchExpRaw)2 Delta (catdata.fql.decl.FullQueryExp.Delta)2 Pi (catdata.fql.decl.FullQueryExp.Pi)2 Sigma (catdata.fql.decl.FullQueryExp.Sigma)2 External (catdata.fql.decl.InstExp.External)2