Search in sources :

Example 16 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class Constraints method step.

// TODO aql needs to be over all eds
public <Gen, Sk, X, Y> Instance<Ty, En, Sym, Fk, Att, ?, ?, ?, ?> step(Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> I, AqlOptions options) {
    Collection<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>> T = triggers(I, options);
    if (T.isEmpty()) {
        return null;
    }
    DMG<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Void> shape = new DMG<>(T, new HashMap<>());
    Ctx<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>>> nodesA = new Ctx<>();
    Ctx<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>>> nodesE = new Ctx<>();
    Map<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Term<Void, En, Void, Fk, Void, Gen, Void>> aaa = new HashMap<>();
    Map<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Term<Void, En, Void, Fk, Void, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Void>> xxx = new HashMap<>();
    for (Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>> t : T) {
        Query<Ty, En, Sym, Fk, Att, WHICH, Unit, Void> Q = t.first.Q;
        Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> A = Q.ens.get(WHICH.FRONT);
        Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> E = Q.ens.get(WHICH.BACK);
        Transform<Ty, En, Sym, Fk, Att, Var, Var, Var, Var, ID, Chc<Var, Pair<ID, Att>>, ID, Chc<Var, Pair<ID, Att>>> AE = Q.fks.get(new Unit());
        nodesA.put(t, A);
        nodesE.put(t, E);
        for (Var v : AE.src().gens().keySet()) {
            // revisit after colimit fixed
            xxx.put(new Pair<>(t, v), Term.Gen(new Pair<>(t, v)));
            aaa.put(new Pair<>(t, v), I.algebra().repr(t.second.get(v)));
        }
    /*for (Void v : AE.src().sks().keySet()) {
				yyy.put(v, Util.abort(v)); // revisit after colimit fixed
				bbb.put(v, Util.abort(v));
			}*/
    }
    ColimitInstance<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Void, Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> A0 = new ColimitInstance<>(schema, shape, nodesA, new Ctx<>(), options);
    ColimitInstance<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Void, Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> E0 = new ColimitInstance<>(schema, shape, nodesE, new Ctx<>(), options);
    LiteralTransform<Ty, En, Sym, Fk, Att, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, ID, Chc<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<ID, Att>>, ID, Chc<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<ID, Att>>> A0E0 = new LiteralTransform<>(xxx, new HashMap<>(), A0, E0, false);
    LiteralTransform<Ty, En, Sym, Fk, Att, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Gen, Sk, ID, Chc<Pair<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>, Var>, Pair<ID, Att>>, X, Y> A0I = new LiteralTransform<>(aaa, new HashMap<>(), A0, I, false);
    return pushout(A0E0, A0I, options);
// TODO aql disable checking for speed
}
Also used : EvalInstance(catdata.aql.fdm.EvalInstance) ColimitInstance(catdata.aql.fdm.ColimitInstance) HashMap(java.util.HashMap) Ctx(catdata.Ctx) ColimitInstance(catdata.aql.fdm.ColimitInstance) Unit(catdata.Unit) WHICH(catdata.aql.ED.WHICH) ID(catdata.aql.It.ID) Pair(catdata.Pair) DMG(catdata.graph.DMG) LiteralTransform(catdata.aql.fdm.LiteralTransform) Row(catdata.aql.fdm.EvalAlgebra.Row) Chc(catdata.Chc)

Example 17 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class AqlTester method runMany.

private static Ctx<String, Throwable> runMany(Ctx<String, String> progs) {
    Ctx<String, Throwable> result = new Ctx<>();
    for (String k : progs.keySet()) {
        try {
            System.out.println(k);
            Program<Exp<?>> prog = AqlParser.getParser().parseProgram(progs.get(k));
            String[] toUpdate = new String[] { "" };
            AqlMultiDriver driver = new AqlMultiDriver(prog, toUpdate, null);
            // blocks
            driver.start();
            AqlEnv env = driver.env;
            if (env.exn != null) {
                result.put(k, env.exn);
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
            result.put(k, ex);
        }
    }
    return result;
}
Also used : AqlEnv(catdata.aql.exp.AqlEnv) Ctx(catdata.Ctx) Exp(catdata.aql.exp.Exp) AqlMultiDriver(catdata.aql.exp.AqlMultiDriver)

Example 18 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class AqlTester method doSelfTests.

public static void doSelfTests() {
    int c = JOptionPane.showConfirmDialog(null, message, "Run Self-Test?", JOptionPane.YES_NO_OPTION);
    if (c != JOptionPane.YES_OPTION) {
        return;
    }
    Ctx<String, String> exs = new Ctx<>();
    for (Example e : Examples.getExamples(Language.AQL)) {
        exs.put(e.getName(), e.getText());
    }
    Ctx<String, Throwable> result = runMany(exs);
    if (result.isEmpty()) {
        JOptionPane.showMessageDialog(null, "OK: Tested Passed");
        return;
    }
    JTabbedPane t = new JTabbedPane();
    for (String k : result.keySet()) {
        t.addTab(k, new CodeTextPanel("Error", result.get(k).getMessage()));
    }
    JOptionPane.showMessageDialog(null, t);
}
Also used : CodeTextPanel(catdata.ide.CodeTextPanel) Ctx(catdata.Ctx) Example(catdata.ide.Example) JTabbedPane(javax.swing.JTabbedPane)

Example 19 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class RawTerm method infer2.

public static Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> infer2(List<Pair<String, String>> l, RawTerm a, RawTerm b, Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col, AqlJs<Ty, Sym> js) {
    Map<String, Chc<Ty, En>> ctx = new HashMap<>();
    for (Pair<String, String> p : l) {
        if (ctx.containsKey(p.first)) {
            throw new RuntimeException("Duplicate variable " + p.first + " in context " + Ctx.toString(l));
        }
        if (p.second != null) {
            if (col.tys.contains(p.second) && col.ens.contains(p.second)) {
                throw new RuntimeException("Ambiguous: " + p.second + " is an entity and a type");
            } else if (col.tys.contains(p.second)) {
                Ty tt = new Ty(p.second);
                // TODO aql remove for
                ctx.put(p.first, Chc.inLeft(tt));
            // loops for other ones
            } else if (col.ens.contains(p.second)) {
                En tt = new En(p.second);
                ctx.put(p.first, Chc.inRight(tt));
            } else {
                throw new RuntimeException(p.second + " is neither a type nor entity");
            }
        } else {
            ctx.put(p.first, null);
        }
    }
    Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> eq0 = infer1x(ctx, a, b, null, col, "", js).first3();
    LinkedHashMap<Var, Chc<Ty, En>> map = new LinkedHashMap<>();
    for (String k : ctx.keySet()) {
        Chc<Ty, En> v = eq0.first.get(new Var(k));
        map.put(new Var(k), v);
    }
    Ctx<Var, Chc<Ty, En>> ctx2 = new Ctx<>(map);
    Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> tr = new Triple<>(ctx2, eq0.second, eq0.third);
    return tr;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Ty(catdata.aql.exp.TyExpRaw.Ty) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) LinkedHashMap(java.util.LinkedHashMap) Triple(catdata.Triple) Chc(catdata.Chc)

Example 20 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class RawTerm method infer_good.

private static Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> infer_good(RawTerm e, Chc<Ty, En> expected, Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col, String pre, AqlJs<Ty, Sym> js, Map<Var, Chc<Ty, En>> vars) {
    if (e.annotation != null && !col.tys.contains(new Ty(e.annotation))) {
        throw new RuntimeException(pre + "Annotation " + e.annotation + " is not a type (" + col.tys + ").");
    }
    Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> ret = new HashSet<>();
    if (vars.keySet().contains(new Var((String) e.head)) && e.annotation == null) {
        Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Var(new Var((String) e.head));
        if (expected != null) {
            Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
            ret2.put(new Var((String) e.head), expected);
            if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
                ret.add(new Triple<>(ret1, ret2, expected));
            }
        } else {
            for (En en : col.ens) {
                Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
                ret2.put(new Var((String) e.head), Chc.inRight(en));
                if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
                    ret.add(new Triple<>(ret1, ret2, Chc.inRight(en)));
                }
            }
            for (Ty ty : col.tys) {
                Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
                if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
                    ret2.put(new Var((String) e.head), Chc.inLeft(ty));
                }
                ret.add(new Triple<>(ret1, ret2, Chc.inLeft(ty)));
            }
        }
    }
    if (col.syms.containsKey(new Sym(e.head)) && e.annotation == null) {
        // //System.out.println("a " + e);
        List<List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>>> l = new LinkedList<>();
        l.add(new LinkedList<>());
        for (int i = 0; i < e.args.size(); i++) {
            RawTerm arg = e.args.get(i);
            // //System.out.println("arg " + arg);
            Ty ty = col.syms.get(new Sym(e.head)).first.get(i);
            Set<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> z = infer_good(arg, Chc.inLeft(ty), col, pre, js, vars);
            List<List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>>> l2 = new LinkedList<>();
            for (List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> old : l) {
                // //System.out.println("old " + old);
                for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> y : z) {
                    if (y.third.equals(Chc.inLeft(ty))) {
                        // //System.out.println("z z");
                        l2.add(Util.append(old, Util.singList(y)));
                    }
                }
            }
            l = l2;
        }
        outer: for (List<Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>>> outcome : l) {
            // //System.out.println("outcome " + outcome);
            List<Term<Ty, En, Sym, Fk, Att, Gen, Sk>> w = outcome.stream().map(x -> x.first).collect(Collectors.toList());
            Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Sym(new Sym(e.head), w);
            Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>();
            for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> ctx0 : outcome) {
                if (!ctx0.second.agreeOnOverlap(ret2) || !ctx0.second.agreeOnOverlap(Ctx.fromNullable(vars))) {
                    // //System.out.println("xxx ");
                    continue outer;
                }
                // //System.out.println("yyy");
                ret2.map.putAll(ctx0.second.map);
            }
            for (int i = 0; i < e.args.size(); i++) {
                RawTerm arg = e.args.get(i);
                // //System.out.println("2arx " + arg);
                Chc<Ty, En> ty = Chc.inLeft(col.syms.get(new Sym(e.head)).first.get(i));
                Var v = new Var((String) arg.head);
                if (vars.keySet().contains(v)) {
                    // //System.out.println("a " + v);
                    if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
                        // //System.out.println("b " + v);
                        continue;
                    } else if (!ret2.containsKey(v)) {
                        // //System.out.println("c " + v);
                        ret2.put(new Var(e.args.get(i).head), ty);
                    }
                }
            }
            Chc<Ty, En> ret3 = Chc.inLeft(col.syms.get(new Sym(e.head)).second);
            if (expected != null && !expected.equals(ret3)) {
            // //System.out.println("d " );
            } else {
                // //System.out.println("e " );
                if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
                    ret.add(new Triple<>(ret1, ret2, ret3));
                }
            }
        }
    }
    for (En en : col.ens) {
        if (col.fks.containsKey(new Fk(en, e.head)) && e.args.size() == 1 && e.annotation == null) {
            for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> outcome : infer_good(e.args.get(0), Chc.inRight(col.fks.get(new Fk(en, e.head)).first), col, pre, js, vars)) {
                Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Fk(new Fk(en, e.head), outcome.first);
                // System.out.println("trying " + en + " and " + e.head);
                Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>(outcome.second.map);
                Var v = new Var(e.args.get(0).head);
                Chc<Ty, En> ty = Chc.inRight(col.fks.get(new Fk(en, e.head)).first);
                if (vars.keySet().contains(v)) {
                    if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
                        // System.out.println("no1");
                        continue;
                    } else if (!ret2.containsKey(v)) {
                        ret2.put(new Var(e.args.get(0).head), ty);
                    }
                }
                Chc<Ty, En> ret3 = Chc.inRight(col.fks.get(new Fk(en, e.head)).second);
                Chc<Ty, En> argt = Chc.inRight(col.fks.get(new Fk(en, e.head)).first);
                if (expected != null && !expected.equals(ret3)) {
                } else {
                    if (argt.equals(outcome.third)) {
                        if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
                            ret.add(new Triple<>(ret1, ret2, ret3));
                        } else {
                        // System.out.println("b3");
                        }
                    } else {
                    // System.out.println("c3");
                    }
                }
            }
        }
        if (col.atts.containsKey(new Att(en, e.head)) && e.args.size() == 1 && e.annotation == null) {
            // System.out.println("x " + e);
            for (Triple<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Ctx<Var, Chc<Ty, En>>, Chc<Ty, En>> outcome : infer_good(e.args.get(0), Chc.inRight(col.atts.get(new Att(en, e.head)).first), col, pre, js, vars)) {
                // System.out.println("y " + outcome);
                Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Att(new Att(en, e.head), outcome.first);
                Ctx<Var, Chc<Ty, En>> ret2 = new Ctx<>(outcome.second.map);
                Var v = new Var(e.args.get(0).head);
                Chc<Ty, En> ty = Chc.inRight(col.atts.get(new Att(en, e.head)).first);
                if (vars.keySet().contains(v)) {
                    if (ret2.containsKey(v) && !ret2.get(v).equals(ty)) {
                        continue;
                    } else if (!ret2.containsKey(v)) {
                        // System.out.println("b " + v);
                        ret2.put(v, ty);
                    }
                }
                Chc<Ty, En> ret3 = Chc.inLeft(col.atts.get(new Att(en, e.head)).second);
                Chc<Ty, En> argt = Chc.inRight(col.atts.get(new Att(en, e.head)).first);
                if (expected != null && !expected.equals(ret3)) {
                // System.out.println("d " + v);
                } else {
                    // System.out.println("e " + v);
                    if (argt.equals(outcome.third)) {
                        // System.out.println("f " + v);
                        if (ret2.agreeOnOverlap(Ctx.fromNullable(vars))) {
                            ret.add(new Triple<>(ret1, ret2, ret3));
                        }
                    }
                }
            }
        }
    }
    if (col.gens.containsKey(new Gen(e.head)) && e.args.isEmpty() && e.annotation == null) {
        Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Gen(new Gen(e.head));
        Chc<Ty, En> ret3 = Chc.inRight(col.gens.get(new Gen(e.head)));
        if (expected != null && !expected.equals(ret3)) {
        } else {
            ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
        }
    }
    if (col.sks.containsKey(new Sk(e.head)) && e.args.isEmpty() && e.annotation == null) {
        Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Sk(new Sk(e.head));
        Chc<Ty, En> ret3 = Chc.inLeft(col.sks.get(new Sk(e.head)));
        if (expected != null && !expected.equals(ret3)) {
        } else {
            ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
        }
    }
    if (e.args.isEmpty() && e.annotation != null) {
        Ty ty = new Ty(e.annotation);
        Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Obj(js.parse(ty, e.head), ty);
        Chc<Ty, En> ret3 = Chc.inLeft(ty);
        if (expected != null && !expected.equals(ret3)) {
        } else {
            ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
        }
    }
    // as primitive - only if not a variable/generator/etc in scope i.e. none above fired
    if (e.args.isEmpty() && e.annotation == null && ret.isEmpty()) {
        for (Ty ty : col.tys) {
            if (expected != null && !expected.equals(Chc.inLeft(ty))) {
                continue;
            }
            try {
                Term<Ty, En, Sym, Fk, Att, Gen, Sk> ret1 = Term.Obj(js.parse(ty, e.head), ty);
                Chc<Ty, En> ret3 = Chc.inLeft(ty);
                if (expected != null && !expected.equals(ret3)) {
                // System.out.println("zzz");
                } else {
                    ret.add(new Triple<>(ret1, new Ctx<>(), ret3));
                // System.out.println("added " + ret + " and " + ret3);
                }
            } catch (Exception ex) {
                if (expected != null) {
                    ex.printStackTrace();
                // throw ex;
                }
            // //ex.printStackTrace();
            }
        }
    }
    return ret;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) List(java.util.List) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) LinkedList(java.util.LinkedList) Triple(catdata.Triple) Gen(catdata.aql.exp.InstExpRaw.Gen) Sk(catdata.aql.exp.InstExpRaw.Sk) Chc(catdata.Chc)

Aggregations

Ctx (catdata.Ctx)31 Ty (catdata.aql.exp.TyExpRaw.Ty)27 En (catdata.aql.exp.SchExpRaw.En)26 Sym (catdata.aql.exp.TyExpRaw.Sym)25 Att (catdata.aql.exp.SchExpRaw.Att)24 Fk (catdata.aql.exp.SchExpRaw.Fk)24 Pair (catdata.Pair)22 Chc (catdata.Chc)19 HashSet (java.util.HashSet)19 Triple (catdata.Triple)17 Gen (catdata.aql.exp.InstExpRaw.Gen)16 HashMap (java.util.HashMap)15 AqlOptions (catdata.aql.AqlOptions)14 List (java.util.List)14 Term (catdata.aql.Term)13 Sk (catdata.aql.exp.InstExpRaw.Sk)13 Collage (catdata.aql.Collage)12 RawTerm (catdata.aql.RawTerm)11 LinkedList (java.util.LinkedList)10 Var (catdata.aql.Var)9