Search in sources :

Example 31 with Tuple3

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

the class FQLParser method toMapConst.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static MapExp toMapConst(Object decl, SigExp t1, SigExp t2) {
    Tuple3 x = (Tuple3) decl;
    List<Pair<String, String>> objs = new LinkedList<>();
    List<Pair<String, String>> attrs = new LinkedList<>();
    List<Pair<String, List<String>>> arrows = new LinkedList<>();
    Tuple3 a = (Tuple3) x.a;
    Tuple3 b = (Tuple3) x.b;
    Tuple3 c = (Tuple3) x.c;
    List a0 = (List) a.b;
    for (Object o : a0) {
        Tuple3 z = (Tuple3) o;
        String p = (String) z.a;
        String q = (String) z.c;
        objs.add(new Pair<>(p, q));
    }
    if (b.b.toString().equals("ASWRITTEN")) {
        for (Pair<String, String> k : objs) {
            attrs.add(new Pair<>(k.first + "_att", k.second + "_att"));
        }
    } else {
        List b0 = (List) b.b;
        for (Object o : b0) {
            Tuple3 z = (Tuple3) o;
            String p = (String) z.a;
            String q = (String) z.c;
            attrs.add(new Pair<>(p, q));
        }
    }
    List c0 = (List) c.b;
    for (Object o : c0) {
        Tuple3 z = (Tuple3) o;
        String p = (String) z.a;
        List<String> q = (List<String>) z.c;
        arrows.add(new Pair<>(p, q));
    }
    try {
        MapExp.Const ret = new MapExp.Const(objs, attrs, arrows, t1, t2);
        return ret;
    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    }
}
Also used : Const(catdata.fql.decl.SigExp.Const) MapExp(catdata.fql.decl.MapExp) Tuple3(org.jparsec.functors.Tuple3) Pair(catdata.Pair)

Example 32 with Tuple3

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

the class FQLParser method toMatch.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Set<Pair<String, String>> toMatch(Object b) {
    List<Tuple3> l = (List<Tuple3>) b;
    Set<Pair<String, String>> ret = new HashSet<>();
    for (Tuple3 k : l) {
        ret.add(new Pair<>(k.a.toString(), k.c.toString()));
    }
    return ret;
}
Also used : Tuple3(org.jparsec.functors.Tuple3) Pair(catdata.Pair)

Example 33 with Tuple3

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

the class FQLParser method toMapping.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static MapExp toMapping(Object o) {
    try {
        Tuple3 p = (Tuple3) o;
        Object p2 = p.b;
        Object p3 = p.c;
        Object o1 = p.a;
        String p1 = p.a.toString();
        if (p1.equals("fst")) {
            return new Fst(toSchema(p2), toSchema(p3));
        } else if (p1.equals("snd")) {
            return new Snd(toSchema(p2), toSchema(p3));
        } else if (p1.equals("unit")) {
            return new TT(toSchema(p3), new HashSet<>((Collection<String>) p2));
        } else if (p1.equals("subschema")) {
            return new Sub(toSchema(p2), toSchema(p3));
        } else if (p1.equals("inl")) {
            return new Inl(toSchema(p2), toSchema(p3));
        } else if (p1.equals("inr")) {
            return new Inr(toSchema(p2), toSchema(p3));
        } else if (p1.equals("iso1")) {
            return new Iso(true, toSchema(p2), toSchema(p3));
        } else if (p1.equals("iso2")) {
            return new Iso(false, toSchema(p2), toSchema(p3));
        } else if (p1.equals("eval")) {
            return new Apply(toSchema(p2), toSchema(p3));
        } else if (p2.toString().equals("then")) {
            return new MapExp.Comp(toMapping(o1), toMapping(p3));
        } else if (p2.toString().equals("*")) {
            return new Prod(toMapping(o1), toMapping(p3));
        } else if (p2.toString().equals("+")) {
            return new Case(toMapping(o1), toMapping(p3));
        }
    } catch (RuntimeException re) {
    }
    if (o instanceof Tuple5) {
        Tuple5 p = (Tuple5) o;
        Object p2 = p.c;
        Object p3 = p.e;
        Object o1 = p.a;
        return toMapConst(o1, toSchema(p2), toSchema(p3));
    }
    try {
        org.jparsec.functors.Pair p = (org.jparsec.functors.Pair) o;
        String p1 = p.a.toString();
        Object p2 = p.b;
        switch(p1) {
            case "id":
                return new Id(toSchema(p2));
            case "curry":
                return new Curry(toMapping(p2));
            case "void":
                return new FF(toSchema(p2));
            case "opposite":
                return new Opposite(toMapping(p2));
            default:
                break;
        }
    } catch (RuntimeException re) {
    }
    if (o instanceof String) {
        return new MapExp.Var(o.toString());
    }
    throw new RuntimeException("Cannot parse " + o);
}
Also used : TT(catdata.fql.decl.MapExp.TT) TransCurry(catdata.fql.decl.TransExp.TransCurry) Curry(catdata.fql.decl.MapExp.Curry) FF(catdata.fql.decl.MapExp.FF) Inl(catdata.fql.decl.MapExp.Inl) Apply(catdata.fql.decl.MapExp.Apply) Var(catdata.fql.decl.FullQueryExp.Var) Snd(catdata.fql.decl.MapExp.Snd) Inr(catdata.fql.decl.MapExp.Inr) Case(catdata.fql.decl.MapExp.Case) Prod(catdata.fql.decl.MapExp.Prod) Pair(catdata.Pair) Opposite(catdata.fql.decl.MapExp.Opposite) Sub(catdata.fql.decl.MapExp.Sub) TransIso(catdata.fql.decl.TransExp.TransIso) Iso(catdata.fql.decl.MapExp.Iso) Fst(catdata.fql.decl.MapExp.Fst) MapExp(catdata.fql.decl.MapExp) Tuple5(org.jparsec.functors.Tuple5) Tuple3(org.jparsec.functors.Tuple3) Id(catdata.fql.decl.MapExp.Id)

Example 34 with Tuple3

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

the class FQLParser method toInstConst.

@SuppressWarnings("rawtypes")
private static InstExp toInstConst(Object decl) {
    Tuple3 y = (Tuple3) decl;
    Tuple3 x = (Tuple3) y.a;
    // List<Pair<String, List<Pair<Object, Object>>>> data = new
    // LinkedList<>();
    Tuple3 nodes = (Tuple3) x.a;
    Tuple3 arrows = (Tuple3) x.c;
    Tuple3 attrs = (Tuple3) x.b;
    List nodes0 = (List) nodes.b;
    List arrows0 = (List) arrows.b;
    // List<Object> seen = new LinkedList<>();
    List<Pair<String, List<Pair<Object, Object>>>> nodesX = new LinkedList<>();
    for (Object o : nodes0) {
        Tuple3 u = (Tuple3) o;
        String n = (String) u.a;
        List m = (List) u.c;
        List<Pair<Object, Object>> l = new LinkedList<>();
        for (Object h : m) {
            l.add(new Pair<>(h, h));
        }
        // if (seen.contains(n)) {
        // throw new RuntimeException("duplicate field: " + o);
        // }
        // seen.add(n);
        nodesX.add(new Pair<>(n, l));
    }
    // RuntimeException toThrow = null;
    List<Pair<String, List<Pair<Object, Object>>>> attrsX = new LinkedList<>();
    if (attrs.b.toString().equals("ASWRITTEN")) {
        for (Pair<String, List<Pair<Object, Object>>> k : nodesX) {
            attrsX.add(new Pair<>(k.first + "_att", k.second));
        }
    } else {
        List attrs0 = (List) attrs.b;
        for (Object o : attrs0) {
            Tuple3 u = (Tuple3) o;
            String n = (String) u.a;
            List m = (List) u.c;
            List<Pair<Object, Object>> l = new LinkedList<>();
            for (Object h : m) {
                Tuple3 k = (Tuple3) h;
                l.add(new Pair<>(k.a, k.c));
            }
            // if (seen.contains(n)) {
            // toThrow = new RuntimeException("duplicate field: " + n );
            // throw toThrow;
            // }
            // seen.add(n);
            attrsX.add(new Pair<>(n, l));
        }
    }
    List<Pair<String, List<Pair<Object, Object>>>> arrowsX = new LinkedList<>();
    for (Object o : arrows0) {
        Tuple3 u = (Tuple3) o;
        String n = (String) u.a;
        List m = (List) u.c;
        List<Pair<Object, Object>> l = new LinkedList<>();
        for (Object h : m) {
            Tuple3 k = (Tuple3) h;
            l.add(new Pair<>(k.a, k.c));
        }
        // if (seen.contains(n)) {
        // throw new RuntimeException("duplicate field: " + o);
        // }
        // seen.add(n);
        arrowsX.add(new Pair<>(n, l));
    }
    InstExp.Const ret = new InstExp.Const(nodesX, attrsX, arrowsX, toSchema(y.c));
    return ret;
}
Also used : Const(catdata.fql.decl.SigExp.Const) InstExp(catdata.fql.decl.InstExp) Tuple3(org.jparsec.functors.Tuple3) Pair(catdata.Pair)

Example 35 with Tuple3

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

the class FQLParser method toSchema.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static SigExp toSchema(Object o) {
    try {
        Tuple3<?, ?, ?> t = (Tuple3<?, ?, ?>) o;
        Token z = (Token) t.b;
        String y = z.toString();
        switch(y) {
            case "+":
                return new SigExp.Plus(toSchema(t.a), toSchema(t.c));
            case "*":
                return new SigExp.Times(toSchema(t.a), toSchema(t.c));
            case "^":
                return new SigExp.Exp(toSchema(t.a), toSchema(t.c));
            case "union":
                return new Union(toSchema(t.a), toSchema(t.c));
            default:
                break;
        }
    } catch (RuntimeException cce) {
    }
    try {
        org.jparsec.functors.Pair p = (org.jparsec.functors.Pair) o;
        if (p.a.toString().equals("unit")) {
            return new SigExp.One(new HashSet<>((Collection<String>) p.b));
        } else if (p.a.toString().equals("opposite")) {
            return new SigExp.Opposite(toSchema(p.b));
        }
    } catch (RuntimeException cce) {
    }
    try {
        if (o.toString().equals("void")) {
            return new SigExp.Zero();
        } else if (o.toString().equals("?")) {
            return new Unknown("?" + unknown_idx++);
        }
        throw new RuntimeException();
    } catch (RuntimeException cce) {
    }
    try {
        return toSchemaConst(o);
    } catch (RuntimeException cce) {
    }
    return new SigExp.Var(o.toString());
}
Also used : Zero(catdata.fql.decl.InstExp.Zero) Var(catdata.fql.decl.FullQueryExp.Var) One(catdata.fql.decl.InstExp.One) Token(org.jparsec.Token) Union(catdata.fql.decl.SigExp.Union) Unknown(catdata.fql.decl.SigExp.Unknown) SigExp(catdata.fql.decl.SigExp) Tuple3(org.jparsec.functors.Tuple3) Times(catdata.fql.decl.InstExp.Times) Plus(catdata.fql.decl.InstExp.Plus) FullQueryExp(catdata.fql.decl.FullQueryExp) Exp(catdata.fql.decl.InstExp.Exp) MapExp(catdata.fql.decl.MapExp) TransExp(catdata.fql.decl.TransExp) QueryExp(catdata.fql.decl.QueryExp) SigExp(catdata.fql.decl.SigExp) InstExp(catdata.fql.decl.InstExp) Pair(catdata.Pair)

Aggregations

Tuple3 (org.jparsec.functors.Tuple3)90 Pair (catdata.Pair)69 List (java.util.List)57 LinkedList (java.util.LinkedList)56 Tuple5 (org.jparsec.functors.Tuple5)41 HashMap (java.util.HashMap)33 Tuple4 (org.jparsec.functors.Tuple4)32 LinkedHashMap (java.util.LinkedHashMap)17 Triple (catdata.Triple)15 HashSet (java.util.HashSet)15 XPair (catdata.fpql.XExp.XPair)11 Map (java.util.Map)10 Set (java.util.Set)6 Chc (catdata.Chc)5 Var (catdata.fqlpp.FunctorExp.Var)5 Program (catdata.Program)4 Const (catdata.fql.decl.SigExp.Const)4 Plus (catdata.fqlpp.CatExp.Plus)4 Apply (catdata.fqlpp.FunctorExp.Apply)4 Zero (catdata.fqlpp.FunctorExp.Zero)4