Search in sources :

Example 1 with Flower

use of catdata.fpql.XExp.Flower in project fql by CategoricalData.

the class XDriver method makeEnv.

@SuppressWarnings({ "rawtypes" })
public static XEnvironment makeEnv(String str, XProgram init, String... toUpdate) {
    if (DefunctGlobalOptions.debug.fpql.x_typing) {
        init.doTypes();
    }
    XEnvironment ret = new XEnvironment(init, str);
    Map<String, Integer> extra = new HashMap<>();
    int i = 0;
    for (String k : init.order) {
        XExp se = init.exps.get(k);
        try {
            XObject xxx = se.accept(init, new XOps(ret));
            if (xxx == null) {
                throw new RuntimeException();
            }
            if (se instanceof Flower) {
                Flower f = (Flower) se;
                if (ret.objs.containsKey(f.ty)) {
                    throw new RuntimeException("Duplicate: " + f.ty);
                }
                XCtx c = (XCtx) xxx;
                if (f.ty != null) {
                    ret.objs.put(f.ty, c.schema);
                    extra.put(f.ty, i);
                }
            }
            if (ret.objs.containsKey(k)) {
                throw new RuntimeException("Duplicate: " + k);
            }
            ret.objs.put(k, xxx);
            toUpdate[0] = "Last Processed: " + k;
            i++;
        } catch (Throwable t) {
            t.printStackTrace();
            throw new LineException(t.getLocalizedMessage(), k, "");
        }
    }
    int j = 0;
    for (Entry<String, Integer> e : extra.entrySet()) {
        init.order.add(e.getValue() + j, e.getKey());
        j++;
    }
    // : add to order
    return ret;
}
Also used : Flower(catdata.fpql.XExp.Flower) HashMap(java.util.HashMap) LineException(catdata.LineException)

Example 2 with Flower

use of catdata.fpql.XExp.Flower in project fql by CategoricalData.

the class XOps method visit.

@Override
public XObject visit(XProgram env, XToQuery e) {
    Object o = e.inst.accept(env, this);
    if (!(o instanceof XCtx)) {
        throw new RuntimeException("Not instance: " + o);
    }
    XCtx c = (XCtx) o;
    int i = 0;
    // Map m1 = new HashMap();
    Map m2 = new HashMap();
    // Map mty = new HashMap();
    Map from = new HashMap<>();
    for (Object t : c.terms()) {
        // m1.put("v_v"+i, t);
        m2.put(t, "v_v" + i);
        from.put("v_v" + i, c.type(t).second);
        // from.put("v"+i, c.type(((Pair)t).second);
        i++;
    }
    List where = new LinkedList<>();
    Function f = x -> {
        Object j = m2.get(x);
        if (j == null) {
            return x;
        }
        return j;
    };
    for (Object k : c.eqs) {
        List l = (List) ((Pair) k).first;
        List r = (List) ((Pair) k).second;
        // lookup m2
        where.add(new Pair<>(l.stream().map(f).collect(Collectors.toList()), r.stream().map(f).collect(Collectors.toList())));
    }
    Flower iii = new Flower(new HashMap<>(), from, where, e.applyTo);
    return iii.accept(env, this);
}
Also used : Var(catdata.fpql.XExp.Var) XCoApply(catdata.fpql.XExp.XCoApply) XToQuery(catdata.fpql.XExp.XToQuery) XTy(catdata.fpql.XExp.XTy) XRel(catdata.fpql.XExp.XRel) XInst(catdata.fpql.XExp.XInst) Map(java.util.Map) XPi(catdata.fpql.XExp.XPi) Pair(catdata.Pair) XPushout(catdata.fpql.XExp.XPushout) XGrothLabels(catdata.fpql.XExp.XGrothLabels) XLabel(catdata.fpql.XExp.XLabel) XExpVisitor(catdata.fpql.XExp.XExpVisitor) XSigma(catdata.fpql.XExp.XSigma) Collectors(java.util.stream.Collectors) Apply(catdata.fpql.XExp.Apply) List(java.util.List) XTT(catdata.fpql.XExp.XTT) Flower(catdata.fpql.XExp.Flower) Triple(catdata.Triple) XSchema(catdata.fpql.XExp.XSchema) XCoprod(catdata.fpql.XExp.XCoprod) XOne(catdata.fpql.XExp.XOne) XSuperED(catdata.fpql.XExp.XSuperED) FLOWER2(catdata.fpql.XExp.FLOWER2) XInj(catdata.fpql.XExp.XInj) XUnit(catdata.fpql.XExp.XUnit) XMapConst(catdata.fpql.XExp.XMapConst) HashMap(java.util.HashMap) Function(java.util.function.Function) Compose(catdata.fpql.XExp.Compose) XConst(catdata.fpql.XExp.XConst) XFn(catdata.fpql.XExp.XFn) XCounit(catdata.fpql.XExp.XCounit) XIdPoly(catdata.fpql.XExp.XIdPoly) XVoid(catdata.fpql.XExp.XVoid) LinkedList(java.util.LinkedList) XMatch(catdata.fpql.XExp.XMatch) XProj(catdata.fpql.XExp.XProj) XPair(catdata.fpql.XExp.XPair) XSOED(catdata.fpql.XExp.XSOED) XTransConst(catdata.fpql.XExp.XTransConst) Util(catdata.Util) XEq(catdata.fpql.XExp.XEq) Id(catdata.fpql.XExp.Id) Iter(catdata.fpql.XExp.Iter) FOED(catdata.fpql.XExp.XSOED.FOED) XDelta(catdata.fpql.XExp.XDelta) XUberPi(catdata.fpql.XExp.XUberPi) XFF(catdata.fpql.XExp.XFF) XTimes(catdata.fpql.XExp.XTimes) Flower(catdata.fpql.XExp.Flower) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Function(java.util.function.Function) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with Flower

use of catdata.fpql.XExp.Flower in project fql by CategoricalData.

the class XParser method toProgHelper.

private static void toProgHelper(String z, String s, List<Triple<String, Integer, XExp>> ret, Tuple3 decl) {
    String txt = z;
    int idx = s.indexOf(txt);
    if (idx < 0) {
        throw new RuntimeException();
    }
    if (decl.a instanceof Tuple3) {
        Tuple3 t = (Tuple3) decl.a;
        Object ooo = toExp(decl.c);
        if (ooo instanceof Flower) {
            Flower f = (Flower) toExp(decl.c);
            f.ty = t.c.toString();
            ret.add(new Triple<>(t.a.toString(), idx, f));
        } else if (ooo instanceof FLOWER2) {
            FLOWER2 f = (FLOWER2) toExp(decl.c);
            f.ty = t.c.toString();
            ret.add(new Triple<>(t.a.toString(), idx, f));
        } else {
            throw new RuntimeException("Can only use (v:T) for flowers");
        }
    } else {
        String name = decl.a.toString();
        if (decl.b.toString().equals(":")) {
            ret.add(new Triple<>(name, idx, newToExp(decl.c)));
        } else {
            ret.add(new Triple<>(name, idx, toExp(decl.c)));
        }
    }
}
Also used : Triple(catdata.Triple) FLOWER2(catdata.fpql.XExp.FLOWER2) Flower(catdata.fpql.XExp.Flower) Tuple3(org.jparsec.functors.Tuple3)

Example 4 with Flower

use of catdata.fpql.XExp.Flower in project fql by CategoricalData.

the class XParser method toExp.

private static XExp toExp(Object c) {
    if (c instanceof String) {
        return new Var((String) c);
    }
    try {
        return fromPoly((Tuple4) c);
    } catch (Exception e) {
    }
    try {
        return fromSoed(c);
    } catch (Exception e) {
    }
    try {
        return toCatConst(c);
    } catch (Exception e) {
    }
    try {
        if (c.toString().contains("variables")) {
            return toInstConst(c);
        }
    } catch (Exception e) {
    }
    try {
        return toMapping(c);
    } catch (Exception e) {
    }
    try {
        return toTrans(c);
    } catch (Exception e) {
    }
    if (c instanceof Tuple5) {
        Tuple5 p = (Tuple5) c;
        if (p.c.toString().equals("+")) {
            return new XCoprod(toExp(p.b), toExp(p.d));
        }
        if (p.c.toString().equals("*")) {
            return new XTimes(toExp(p.b), toExp(p.d));
        }
        if (p.c.toString().equals(";")) {
            return new Compose(toExp(p.b), toExp(p.d));
        }
        if (p.a.toString().equals("return") && p.b.toString().equals("sigma")) {
            return new XUnit("sigma", toExp(p.d), toExp(p.e));
        }
        if (p.a.toString().equals("coreturn") && p.b.toString().equals("sigma")) {
            return new XCounit("sigma", toExp(p.d), toExp(p.e));
        }
        if (p.a.toString().equals("return") && p.b.toString().equals("delta")) {
            return new XUnit("pi", toExp(p.d), toExp(p.e));
        }
        if (p.a.toString().equals("coreturn") && p.b.toString().equals("delta")) {
            return new XCounit("pi", toExp(p.d), toExp(p.e));
        }
        return new XFn((String) p.b, (String) p.d, (String) p.e);
    }
    if (c instanceof Tuple4) {
        Tuple4 p = (Tuple4) c;
        return new XEq((List<String>) p.b, (List<String>) p.d);
    }
    if (c instanceof Tuple3) {
        Tuple3 p = (Tuple3) c;
        if (p.a.toString().equals("flower")) {
            XExp I = toExp(p.c);
            Tuple3 q = (Tuple3) p.b;
            // list of tuple3 of (path, string)
            List s = (List) ((org.jparsec.functors.Pair) q.a).b;
            // list of tuple3 of (string, string)
            List f = (List) ((org.jparsec.functors.Pair) q.b).b;
            // list of tuple3 of (path, path)
            List w = (List) ((org.jparsec.functors.Pair) q.c).b;
            Map<Object, List<Object>> select = new HashMap<>();
            Map<Object, Object> from = new HashMap<>();
            List<Pair<List<Object>, List<Object>>> where = new LinkedList<>();
            Set<String> seen = new HashSet<>();
            for (Object o : w) {
                Tuple3 t = (Tuple3) o;
                List lhs = (List) t.a;
                List rhs = (List) t.c;
                where.add(new Pair<>(rhs, lhs));
            }
            for (Object o : s) {
                Tuple3 t = (Tuple3) o;
                List lhs = (List) t.a;
                String rhs = t.c.toString();
                if (seen.contains(rhs)) {
                    throw new RuntimeException("Duplicate AS name: " + rhs + " (note: AS names can't be used in the schema either)");
                }
                seen.add(rhs);
                select.put(rhs, lhs);
            }
            for (Object o : f) {
                Tuple3 t = (Tuple3) o;
                String lhs = t.a.toString();
                String rhs = t.c.toString();
                if (seen.contains(rhs)) {
                    throw new RuntimeException("Duplicate AS name: " + rhs + " (note: AS names can't be used in the schema either)");
                }
                seen.add(rhs);
                from.put(rhs, lhs);
            }
            return new Flower(select, from, where, I);
        }
        if (p.a.toString().equals("FLOWER")) {
            XExp I = toExp(p.c);
            Tuple3 q = (Tuple3) p.b;
            // list of tuple3 of (path, string)
            List s = (List) ((org.jparsec.functors.Pair) q.a).b;
            // list of tuple3 of (string, string)
            List f = (List) ((org.jparsec.functors.Pair) q.b).b;
            // list of tuple3 of (path, path)
            Object w = ((org.jparsec.functors.Pair) q.c).b;
            Map<Object, List<Object>> select = new HashMap<>();
            Map<Object, Object> from = new HashMap<>();
            // List<Pair<List<String>, List<String>>> where = new LinkedList<>();
            Set<String> seen = new HashSet<>();
            for (Object o : s) {
                Tuple3 t = (Tuple3) o;
                List lhs = (List) t.a;
                String rhs = t.c.toString();
                if (seen.contains(rhs)) {
                    throw new RuntimeException("Duplicate AS name: " + rhs + " (note: AS names can't be used in the schema either)");
                }
                seen.add(rhs);
                select.put(rhs, lhs);
            }
            for (Object o : f) {
                Tuple3 t = (Tuple3) o;
                String lhs = t.a.toString();
                String rhs = t.c.toString();
                if (seen.contains(rhs)) {
                    throw new RuntimeException("Duplicate AS name: " + rhs + " (note: AS names can't be used in the schema either)");
                }
                seen.add(rhs);
                from.put(rhs, lhs);
            }
            return new FLOWER2(select, from, toWhere(w), I);
        }
        if (p.a.toString().equals("pushout")) {
            return new XPushout(toExp(p.b), toExp(p.c));
        }
        if (p.a.toString().equals("hom")) {
            return new XToQuery(toExp(p.b), toExp(p.c));
        }
        if (p.a.toString().equals("sigma")) {
            return new XSigma(toExp(p.b), toExp(p.c));
        }
        if (p.a.toString().equals("delta")) {
            return new XDelta(toExp(p.b), toExp(p.c));
        }
        if (p.a.toString().equals("pi")) {
            return new XPi(toExp(p.b), toExp(p.c));
        }
        if (p.a.toString().equals("inl")) {
            return new XInj(toExp(p.b), toExp(p.c), true);
        }
        if (p.a.toString().equals("inr")) {
            return new XInj(toExp(p.b), toExp(p.c), false);
        }
        if (p.a.toString().equals("case")) {
            return new XMatch(toExp(p.b), toExp(p.c));
        }
        if (p.a.toString().equals("fst")) {
            return new XProj(toExp(p.b), toExp(p.c), true);
        }
        if (p.a.toString().equals("snd")) {
            return new XProj(toExp(p.b), toExp(p.c), false);
        }
        if (p.a.toString().equals("pair")) {
            return new XPair(toExp(p.b), toExp(p.c));
        }
        if (p.a.toString().equals("apply")) {
            return new Apply(toExp(p.b), toExp(p.c));
        }
        if (p.a.toString().equals("coapply")) {
            return new XCoApply(toExp(p.b), toExp(p.c));
        }
        return new XConst((String) p.b, (String) p.c);
    }
    if (c instanceof org.jparsec.functors.Pair) {
        org.jparsec.functors.Pair p = (org.jparsec.functors.Pair) c;
        if (p.a.toString().equals("idpoly")) {
            return new XIdPoly(toExp(p.b));
        }
        if (p.a.toString().equals("uberpi")) {
            return new XUberPi(toExp(p.b));
        }
        if (p.a.toString().equals("labels")) {
            return new XLabel(toExp(p.b));
        }
        if (p.a.toString().equals("grothlabels")) {
            return new XGrothLabels(toExp(p.b));
        }
        if (p.a.toString().equals("relationalize")) {
            return new XRel(toExp(p.b));
        }
        if (p.a.toString().equals("void")) {
            return new XVoid(toExp(p.b));
        }
        if (p.a.toString().equals("ff")) {
            return new XFF(toExp(p.b));
        }
        if (p.a.toString().equals("unit")) {
            return new XOne(toExp(p.b));
        }
        if (p.a.toString().equals("tt")) {
            return new XTT(toExp(p.b));
        }
        if (p.a.toString().equals("id")) {
            return new Id(false, toExp(p.b));
        }
        if (p.a.toString().equals("ID")) {
            return new Id(true, toExp(p.b));
        } else // if (p.a.toString().equals("query")) {
        // Tuple3 t = (Tuple3) p.b;
        // return new XExp.XQueryExp(toExp(t.a), toExp(t.b), toExp(t.c));
        // }
        {
            try {
                return fromSuperSoed(c);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new XTy((String) p.b);
        }
    }
    throw new RuntimeException("x: " + c.getClass() + " " + c);
}
Also used : HashMap(java.util.HashMap) Var(catdata.fpql.XExp.Var) XCoApply(catdata.fpql.XExp.XCoApply) Apply(catdata.fpql.XExp.Apply) XRel(catdata.fpql.XExp.XRel) XVoid(catdata.fpql.XExp.XVoid) FLOWER2(catdata.fpql.XExp.FLOWER2) Compose(catdata.fpql.XExp.Compose) XFF(catdata.fpql.XExp.XFF) List(java.util.List) LinkedList(java.util.LinkedList) XToQuery(catdata.fpql.XExp.XToQuery) HashSet(java.util.HashSet) XIdPoly(catdata.fpql.XExp.XIdPoly) XCounit(catdata.fpql.XExp.XCounit) XEq(catdata.fpql.XExp.XEq) XProj(catdata.fpql.XExp.XProj) LinkedList(java.util.LinkedList) XPair(catdata.fpql.XExp.XPair) XCoApply(catdata.fpql.XExp.XCoApply) XTimes(catdata.fpql.XExp.XTimes) Id(catdata.fpql.XExp.Id) XSigma(catdata.fpql.XExp.XSigma) XFn(catdata.fpql.XExp.XFn) XInj(catdata.fpql.XExp.XInj) XOne(catdata.fpql.XExp.XOne) XUberPi(catdata.fpql.XExp.XUberPi) XDelta(catdata.fpql.XExp.XDelta) XMatch(catdata.fpql.XExp.XMatch) XTT(catdata.fpql.XExp.XTT) XGrothLabels(catdata.fpql.XExp.XGrothLabels) Pair(catdata.Pair) XPair(catdata.fpql.XExp.XPair) Flower(catdata.fpql.XExp.Flower) XConst(catdata.fpql.XExp.XConst) Tuple4(org.jparsec.functors.Tuple4) XPi(catdata.fpql.XExp.XPi) Tuple5(org.jparsec.functors.Tuple5) XCoprod(catdata.fpql.XExp.XCoprod) Tuple3(org.jparsec.functors.Tuple3) XPushout(catdata.fpql.XExp.XPushout) XLabel(catdata.fpql.XExp.XLabel) XUnit(catdata.fpql.XExp.XUnit) XTy(catdata.fpql.XExp.XTy)

Aggregations

Flower (catdata.fpql.XExp.Flower)4 FLOWER2 (catdata.fpql.XExp.FLOWER2)3 HashMap (java.util.HashMap)3 Pair (catdata.Pair)2 Triple (catdata.Triple)2 Apply (catdata.fpql.XExp.Apply)2 Compose (catdata.fpql.XExp.Compose)2 Id (catdata.fpql.XExp.Id)2 Var (catdata.fpql.XExp.Var)2 XCoApply (catdata.fpql.XExp.XCoApply)2 XConst (catdata.fpql.XExp.XConst)2 XCoprod (catdata.fpql.XExp.XCoprod)2 XCounit (catdata.fpql.XExp.XCounit)2 XDelta (catdata.fpql.XExp.XDelta)2 XEq (catdata.fpql.XExp.XEq)2 XFF (catdata.fpql.XExp.XFF)2 XFn (catdata.fpql.XExp.XFn)2 XGrothLabels (catdata.fpql.XExp.XGrothLabels)2 XIdPoly (catdata.fpql.XExp.XIdPoly)2 XInj (catdata.fpql.XExp.XInj)2