Search in sources :

Example 21 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class AqlViewer method viewDP.

/*public static <Ty, En, Sym, Fk, Att> JComponent viewSchema2(Schema<Ty, En, Sym, Fk, Att> schema) {
		mxGraph graph = new mxGraph();
		Object parent = graph.getDefaultParent();

		Ctx<Chc<En,Ty>, Object> nodes = new Ctx<>();
		for (En en : schema.ens) {
			Object v1 = graph.insertVertex(parent, null, en.toString(), 20, 20, 80, 30);
			nodes.put(Chc.inLeft(en), v1);
		}
		for (Ty ty : schema.typeSide.tys) {
			Object v1 = graph.insertVertex(parent, null, ty.toString(), 20, 20, 80, 30);
			nodes.put(Chc.inRight(ty), v1);
		}
		for (Att att : schema.atts.keySet()) {
			graph.insertEdge(parent, null, att.toString(), nodes.get(Chc.inLeft(schema.atts.get(att).first)), nodes.get(Chc.inRight(schema.atts.get(att).second)));
		}
		for (Fk fk : schema.fks.keySet()) {
			graph.insertEdge(parent, null, fk.toString(), nodes.get(Chc.inLeft(schema.fks.get(fk).first)), nodes.get(Chc.inLeft(schema.fks.get(fk).second)));
		}
		mxGraphLayout layout = new mxOrganicLayout(graph); // or whatever layouting algorithm
		layout.execute(parent);
		
		mxGraphComponent graphComponent = new mxGraphComponent(graph);
		
		//  Map<String, Object> style = graph.getStylesheet().getDefaultEdgeStyle();
		 // style.put(mxConstants.STYLE_ROUNDED, true);
		//  style.put(mxConstants.STYLE_EDGE, mxConstants.EDGESTYLE_ENTITY_RELATION);
		
		return graphComponent;
	}	*/
private static <Ty, En, Sym, Fk, Att, Gen, Sk> JComponent viewDP(DP<Ty, En, Sym, Fk, Att, Gen, Sk> dp, Collage col, AqlJs js) {
    CodeTextPanel input = new CodeTextPanel("Input (either equation-in-ctx or term-in-ctx)", "");
    CodeTextPanel output = new CodeTextPanel("Output", "");
    JButton eq = new JButton("Decide Equation-in-ctx");
    JButton nf = new JButton("Normalize Term-in-ctx");
    /*
		 * if (!dp.hasNFs()) { nf.setEnabled(false); }
		 */
    JButton print = new JButton("Show Info");
    JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
    buttonPanel.add(eq);
    buttonPanel.add(nf);
    buttonPanel.add(print);
    // TODO: aql does not position correctly
    Split split = new Split(.5, JSplitPane.VERTICAL_SPLIT);
    split.add(input);
    split.add(output);
    JPanel main = new JPanel(new BorderLayout());
    main.add(split, BorderLayout.CENTER);
    main.add(buttonPanel, BorderLayout.NORTH);
    print.addActionListener(x -> output.setText(dp.toStringProver()));
    eq.addActionListener(x -> {
        try {
            Triple<List<Pair<String, String>>, RawTerm, RawTerm> y = AqlParser.getParser().parseEq(input.getText());
            Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> z = RawTerm.infer2(y.first, y.second, y.third, col, js);
            boolean isEq = dp.eq(z.first, z.second, z.third);
            output.setText(Boolean.toString(isEq));
        } catch (Exception ex) {
            ex.printStackTrace();
            output.setText(ex.getMessage());
        }
    });
    nf.addActionListener(x -> {
        try {
            Pair<List<Pair<String, String>>, RawTerm> y = AqlParser.getParser().parseTermInCtx(input.getText());
            Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> z = RawTerm.infer2(y.first, y.second, y.second, col, js);
            Term<Ty, En, Sym, Fk, Att, Gen, Sk> w = dp.nf(z.first, z.second);
            output.setText(w.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
            output.setText(ex.getMessage());
        }
    });
    return main;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) JPanel(javax.swing.JPanel) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Ctx(catdata.Ctx) Sym(catdata.aql.exp.TyExpRaw.Sym) JButton(javax.swing.JButton) En(catdata.aql.exp.SchExpRaw.En) Term(catdata.aql.Term) RawTerm(catdata.aql.RawTerm) RawTerm(catdata.aql.RawTerm) Gen(catdata.aql.exp.InstExpRaw.Gen) GridLayout(java.awt.GridLayout) BorderLayout(java.awt.BorderLayout) CodeTextPanel(catdata.ide.CodeTextPanel) Sk(catdata.aql.exp.InstExpRaw.Sk) List(java.util.List) LinkedList(java.util.LinkedList) Split(catdata.ide.Split)

Example 22 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class AqlViewer method viewMorphism.

private static <Ty, En1, Sym, Fk1, Att1, Gen1, Sk1, En2, Fk2, Att2, Gen2, Sk2> JComponent viewMorphism(Morphism m, AqlJs js) {
    CodeTextPanel input = new CodeTextPanel("Input term-in-ctx", "");
    CodeTextPanel output = new CodeTextPanel("Output term-in-ctx", "");
    JButton nf = new JButton("Translate");
    JPanel buttonPanel = new JPanel(new GridLayout(1, 1));
    buttonPanel.add(nf);
    // TODO: aql does not position correctly
    Split split = new Split(.5, JSplitPane.VERTICAL_SPLIT);
    split.add(input);
    split.add(output);
    JPanel main = new JPanel(new BorderLayout());
    main.add(split, BorderLayout.CENTER);
    main.add(buttonPanel, BorderLayout.NORTH);
    nf.addActionListener(x -> {
        try {
            Pair<List<Pair<String, String>>, RawTerm> y = AqlParser.getParser().parseTermInCtx(input.getText());
            Triple<Ctx<Var, Chc<Ty, En1>>, Term<Ty, En1, Sym, Fk1, Att1, Gen1, Sk1>, Term<Ty, En1, Sym, Fk1, Att1, Gen1, Sk1>> z = RawTerm.infer2(y.first, y.second, y.second, m.src(), js);
            Pair<Ctx<Var, Chc<Ty, En2>>, Term<Ty, En2, Sym, Fk2, Att2, Gen2, Sk2>> a = m.translate(z.first, z.second);
            output.setText(a.first.toString() + a.second);
        } catch (Exception ex) {
            ex.printStackTrace();
            output.setText(ex.getMessage());
        }
    });
    return main;
}
Also used : JPanel(javax.swing.JPanel) Ty(catdata.aql.exp.TyExpRaw.Ty) Ctx(catdata.Ctx) JButton(javax.swing.JButton) Term(catdata.aql.Term) RawTerm(catdata.aql.RawTerm) RawTerm(catdata.aql.RawTerm) GridLayout(java.awt.GridLayout) BorderLayout(java.awt.BorderLayout) CodeTextPanel(catdata.ide.CodeTextPanel) List(java.util.List) LinkedList(java.util.LinkedList) Split(catdata.ide.Split)

Example 23 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class InstExpJdbcQuotient method eval.

@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> eval(AqlEnv env) {
    Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> J = I.eval(env);
    Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col = new Collage<>(J.collage());
    AqlOptions strat = new AqlOptions(options, col, env.defaults);
    String toGet = jdbcString;
    String driver = clazz;
    if (clazz.trim().isEmpty()) {
        driver = (String) strat.getOrDefault(AqlOption.jdbc_default_class);
        Util.checkClass(driver);
    }
    if (jdbcString.trim().isEmpty()) {
        toGet = (String) strat.getOrDefault(AqlOption.jdbc_default_string);
    }
    Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>>> eqs0 = new HashSet<>(J.eqs());
    try (Connection conn = DriverManager.getConnection(toGet)) {
        for (Entry<LocStr, String> q : queries.entrySet()) {
            if (!J.schema().ens.contains(new En(q.getKey().str))) {
                throw new LocException(q.getKey().loc, "Not an entity: " + q.getKey().str + ", expected one of " + J.schema().ens);
            }
            Statement stmt = conn.createStatement();
            stmt.execute(q.getValue());
            ResultSet rs = stmt.getResultSet();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnsNumber = rsmd.getColumnCount();
            if (columnsNumber != 2) {
                stmt.close();
                rs.close();
                throw new LocException(q.getKey().loc, "Expected 2 columns but received " + columnsNumber);
            }
            while (rs.next()) {
                // input instance need not be jdbc, so dont call toGen from import here
                // (Gen) ;
                Gen gen1 = new Gen(rs.getObject(1).toString());
                // (Gen) rs.getObject(2).toString();
                Gen gen2 = new Gen(rs.getObject(2).toString());
                if (gen1 == null || gen2 == null) {
                    stmt.close();
                    rs.close();
                    throw new LocException(q.getKey().loc, "Encountered a NULL generator");
                } else if (!J.gens().containsKey(gen1)) {
                    throw new LocException(q.getKey().loc, "Cannot import record linkage: " + gen1 + " is not a generator in the input instance");
                } else if (!J.gens().containsKey(gen2)) {
                    throw new LocException(q.getKey().loc, "Cannot import record linkage: " + gen2 + " is not a generator in the input instance");
                }
                Term<Ty, En, Sym, Fk, Att, Gen, Sk> l = Term.Gen(gen1);
                Term<Ty, En, Sym, Fk, Att, Gen, Sk> r = Term.Gen(gen2);
                eqs0.add(new Pair<>(l, r));
                col.eqs.add(new Eq<>(new Ctx<>(), l, r));
            }
            stmt.close();
            rs.close();
        }
    // } catch (SQLException exn) {
    // exn.printStackTrace();
    // throw new RuntimeException(/*"JDBC exception: " + */ exn /*.getMessage() */);
    } catch (Throwable thr) {
        // thr.printStackTrace();
        throw new RuntimeException(thr);
    }
    InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Sk, ID> initial0 = new InitialAlgebra<>(strat, J.schema(), col, new It(), Object::toString, Object::toString);
    return new LiteralInstance<>(J.schema(), col.gens.map, col.sks.map, eqs0, initial0.dp(), initial0, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe));
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) It(catdata.aql.It) LiteralInstance(catdata.aql.fdm.LiteralInstance) ResultSetMetaData(java.sql.ResultSetMetaData) InitialAlgebra(catdata.aql.fdm.InitialAlgebra) ResultSet(java.sql.ResultSet) ID(catdata.aql.It.ID) Pair(catdata.Pair) HashSet(java.util.HashSet) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Statement(java.sql.Statement) Connection(java.sql.Connection) Gen(catdata.aql.exp.InstExpRaw.Gen) Sk(catdata.aql.exp.InstExpRaw.Sk) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage)

Example 24 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class InstExpQuotient method eval.

@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Sk, ID, Chc<Sk, Pair<ID, Att>>> eval(AqlEnv env) {
    Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> J = I.eval(env);
    Collage<Ty, En, Sym, Fk, Att, Gen, Sk> col = new Collage<>(J.collage());
    Set<Pair<Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>>> eqs0 = new HashSet<>();
    for (Pair<RawTerm, RawTerm> eq : eqs) {
        try {
            Map<String, Chc<Ty, En>> ctx = Collections.emptyMap();
            Triple<Ctx<Var, Chc<Ty, En>>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>, Term<Ty, En, Sym, Fk, Att, Gen, Sk>> eq0 = RawTerm.infer1x(ctx, eq.first, eq.second, null, col, "", J.schema().typeSide.js).first3();
            if (J.type(eq0.second).left) {
                throw new RuntimeException("Attempt to equate values at type in quotient: " + eq0.second + " at type " + J.type(eq0.second).l);
            }
            if (J.type(eq0.third).left) {
                throw new RuntimeException("Attempt to equate values at type in quotient: " + eq0.third + " at type " + J.type(eq0.third).l);
            }
            eqs0.add(new Pair<>(eq0.second, eq0.third));
            col.eqs.add(new Eq<>(new Ctx<>(), eq0.second, eq0.third));
        } catch (RuntimeException ex) {
            ex.printStackTrace();
            throw new LocException(find("equations", eq), "In equation " + eq.first + " = " + eq.second + ", " + ex.getMessage());
        }
    }
    AqlOptions strat = new AqlOptions(options, col, env.defaults);
    InitialAlgebra<Ty, En, Sym, Fk, Att, Gen, Sk, ID> initial = new InitialAlgebra<>(strat, J.schema(), col, new It(), Object::toString, Object::toString);
    return new LiteralInstance<>(J.schema(), col.gens.map, col.sks.map, eqs0, initial.dp(), initial, (Boolean) strat.getOrDefault(AqlOption.require_consistency), (Boolean) strat.getOrDefault(AqlOption.allow_java_eqs_unsafe));
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) It(catdata.aql.It) LiteralInstance(catdata.aql.fdm.LiteralInstance) InitialAlgebra(catdata.aql.fdm.InitialAlgebra) ID(catdata.aql.It.ID) Pair(catdata.Pair) HashSet(java.util.HashSet) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) RawTerm(catdata.aql.RawTerm) Term(catdata.aql.Term) RawTerm(catdata.aql.RawTerm) Gen(catdata.aql.exp.InstExpRaw.Gen) Sk(catdata.aql.exp.InstExpRaw.Sk) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage) Chc(catdata.Chc)

Example 25 with Ctx

use of catdata.Ctx in project fql by CategoricalData.

the class InstExpRandom method eval.

// not exactly the smartest way
@Override
public SaturatedInstance<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>, Pair<Integer, En>, Pair<Integer, Att>> eval(AqlEnv env) {
    int seed = (Integer) new AqlOptions(options, null, env.defaults).getOrDefault(AqlOption.random_seed);
    Random rand = new Random(seed);
    Schema<Ty, En, Sym, Fk, Att> schema = sch.eval(env);
    Ctx<En, Collection<Pair<Integer, En>>> ens0 = new Ctx<>();
    Ctx<Ty, Collection<Pair<Integer, Att>>> tys = new Ctx<>();
    Ctx<Pair<Integer, En>, Ctx<Fk, Pair<Integer, En>>> fks = new Ctx<>();
    Ctx<Pair<Integer, En>, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Pair<Integer, Att>>>> atts = new Ctx<>();
    for (Ty ty : schema.typeSide.tys) {
        tys.put(ty, new LinkedList<>());
    }
    for (String en : ens.keySet()) {
        List<Pair<Integer, En>> l = new LinkedList<>();
        int size = ens.get(en);
        for (int i = 0; i < size; i++) {
            l.add(new Pair<>(i, new En(en)));
            Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Pair<Integer, Att>>> ctx = new Ctx<>();
            for (Att att : schema.attsFrom(new En(en))) {
                ctx.put(att, Term.Sk(new Pair<>(i, att)));
                tys.get(schema.atts.get(att).second).add(new Pair<>(i, att));
            }
            atts.put(new Pair<>(i, new En(en)), ctx);
            Ctx<Fk, Pair<Integer, En>> ctx0 = new Ctx<>();
            for (Fk fk : schema.fksFrom(new En(en))) {
                int size0 = ens.get(schema.fks.get(fk).second.str);
                Integer k = rand.nextInt(size0);
                ctx0.put(fk, new Pair<>(k, schema.fks.get(fk).second));
            }
            fks.put(new Pair<>(i, new En(en)), ctx0);
        }
        ens0.put(new En(en), l);
    }
    ImportAlgebra<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> alg = new ImportAlgebra<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>>(schema, ens0, tys, fks, atts, x -> x.toString(), x -> x.toString(), true);
    DP<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> dp = new DP<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>>() {

        @Override
        public String toStringProver() {
            return "Random";
        }

        @Override
        public boolean eq(Ctx<Var, Chc<Ty, En>> ctx, Term<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> lhs, Term<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>> rhs) {
            if (!ctx.isEmpty()) {
                Util.anomaly();
            }
            return lhs.equals(rhs);
        }
    };
    return new SaturatedInstance<Ty, En, Sym, Fk, Att, Pair<Integer, En>, Pair<Integer, Att>, Pair<Integer, En>, Pair<Integer, Att>>(alg, dp, false, true, false, new Ctx<>());
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) DP(catdata.aql.DP) Random(java.util.Random) Pair(catdata.Pair) ImportAlgebra(catdata.aql.ImportAlgebra) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Term(catdata.aql.Term) LinkedList(java.util.LinkedList) Collection(java.util.Collection) AqlOptions(catdata.aql.AqlOptions) SaturatedInstance(catdata.aql.fdm.SaturatedInstance)

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