Search in sources :

Example 26 with Att

use of catdata.aql.exp.SchExpRaw.Att in project fql by CategoricalData.

the class AqlViewer method viewSchema.

private <Ty, En, Sym, Fk, Att> JComponent viewSchema(Schema<Ty, En, Sym, Fk, Att> schema) {
    Graph<Chc<Ty, En>, Chc<Fk, Att>> sgv = new DirectedSparseMultigraph<>();
    int i = 0;
    boolean triggered = false;
    for (En en : schema.ens) {
        sgv.addVertex(Chc.inRight(en));
        i++;
        if (i >= maxrows) {
            triggered = true;
            break;
        }
    }
    // if (i <= maxrows) {
    i = 0;
    for (Ty ty : schema.typeSide.tys) {
        sgv.addVertex(Chc.inLeft(ty));
        i++;
        if (i >= maxrows * maxrows) {
            triggered = true;
            break;
        }
    }
    for (Att att : schema.atts.keySet()) {
        sgv.addEdge(Chc.inRight(att), Chc.inRight(schema.atts.get(att).first), Chc.inLeft(schema.atts.get(att).second));
        i++;
        if (i >= maxrows * maxrows) {
            triggered = true;
            break;
        }
    }
    for (Fk fk : schema.fks.keySet()) {
        sgv.addEdge(Chc.inLeft(fk), Chc.inRight(schema.fks.get(fk).first), Chc.inRight(schema.fks.get(fk).second));
        i++;
        if (i >= maxrows * maxrows) {
            triggered = true;
            break;
        }
    }
    if (sgv.getVertexCount() == 0) {
        return new JPanel();
    }
    // Layout<Chc<Ty, En>, Chc<Fk, Att>> layout = new KKLayout<>(sgv);
    Layout<Chc<Ty, En>, Chc<Fk, Att>> layout = new FRLayout<>(sgv);
    layout.setSize(new Dimension(600, 400));
    VisualizationViewer<Chc<Ty, En>, Chc<Fk, Att>> vv = new VisualizationViewer<>(layout);
    Function<Chc<Ty, En>, Paint> vertexPaint = x -> x.left ? Color.gray : Color.black;
    DefaultModalGraphMouse<Chc<Ty, En>, Chc<Fk, Att>> gm = new DefaultModalGraphMouse<>();
    gm.setMode(Mode.TRANSFORMING);
    vv.setGraphMouse(gm);
    gm.setMode(Mode.PICKING);
    vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
    Function<Chc<Fk, Att>, String> et = Chc::toStringMash;
    Function<Chc<Ty, En>, String> vt = Chc::toStringMash;
    vv.getRenderContext().setEdgeLabelTransformer(et);
    vv.getRenderContext().setVertexLabelTransformer(vt);
    GraphZoomScrollPane zzz = new GraphZoomScrollPane(vv);
    JPanel ret = new JPanel(new GridLayout(1, 1));
    ret.add(zzz);
    ret.setBorder(BorderFactory.createEtchedBorder());
    vv.getRenderContext().setLabelOffset(16);
    // vv.getRenderContext().set
    vv.setBackground(Color.white);
    if (triggered) {
        ret.setBorder(BorderFactory.createTitledBorder("Partial"));
    }
    return ret;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Color(java.awt.Color) TypeSide(catdata.aql.TypeSide) Sym(catdata.aql.exp.TyExpRaw.Sym) Comment(catdata.aql.Comment) Constraints(catdata.aql.Constraints) AqlOption(catdata.aql.AqlOptions.AqlOption) Instance(catdata.aql.Instance) Map(java.util.Map) Exp(catdata.aql.exp.Exp) DirectedSparseMultigraph(edu.uci.ics.jung.graph.DirectedSparseMultigraph) DP(catdata.aql.DP) Term(catdata.aql.Term) BorderLayout(java.awt.BorderLayout) Pair(catdata.Pair) Semantics(catdata.aql.Semantics) Function(com.google.common.base.Function) Transform(catdata.aql.Transform) Set(java.util.Set) BorderFactory(javax.swing.BorderFactory) ColimitSchema(catdata.aql.ColimitSchema) Query(catdata.aql.Query) DMG(catdata.graph.DMG) Component(java.awt.Component) Collectors(java.util.stream.Collectors) GraphZoomScrollPane(edu.uci.ics.jung.visualization.GraphZoomScrollPane) Dimension(java.awt.Dimension) List(java.util.List) Paint(java.awt.Paint) Sk(catdata.aql.exp.InstExpRaw.Sk) Triple(catdata.Triple) Algebra(catdata.aql.Algebra) CombinatorParser(catdata.aql.exp.CombinatorParser) GuiUtil(catdata.ide.GuiUtil) JPanel(javax.swing.JPanel) Ty(catdata.aql.exp.TyExpRaw.Ty) Pragma(catdata.aql.Pragma) Ctx(catdata.Ctx) JSplitPane(javax.swing.JSplitPane) RawTerm(catdata.aql.RawTerm) En(catdata.aql.exp.SchExpRaw.En) VisualizationViewer(edu.uci.ics.jung.visualization.VisualizationViewer) Chc(catdata.Chc) AqlParser(catdata.aql.exp.AqlParser) Gen(catdata.aql.exp.InstExpRaw.Gen) GridLayout(java.awt.GridLayout) LinkedHashMap(java.util.LinkedHashMap) Mapping(catdata.aql.Mapping) JTabbedPane(javax.swing.JTabbedPane) Graph(edu.uci.ics.jung.graph.Graph) AqlJs(catdata.aql.AqlJs) Collage(catdata.aql.Collage) LinkedList(java.util.LinkedList) JComponent(javax.swing.JComponent) Morphism(catdata.aql.Morphism) Fk(catdata.aql.exp.SchExpRaw.Fk) Layout(edu.uci.ics.jung.algorithms.layout.Layout) JButton(javax.swing.JButton) CodeTextPanel(catdata.ide.CodeTextPanel) SemanticsVisitor(catdata.aql.SemanticsVisitor) Util(catdata.Util) Unit(catdata.Unit) Att(catdata.aql.exp.SchExpRaw.Att) Split(catdata.ide.Split) AqlEnv(catdata.aql.exp.AqlEnv) Schema(catdata.aql.Schema) Var(catdata.aql.Var) DefaultModalGraphMouse(edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse) FRLayout(edu.uci.ics.jung.algorithms.layout.FRLayout) Mode(edu.uci.ics.jung.visualization.control.ModalGraphMouse.Mode) JPanel(javax.swing.JPanel) FRLayout(edu.uci.ics.jung.algorithms.layout.FRLayout) En(catdata.aql.exp.SchExpRaw.En) GridLayout(java.awt.GridLayout) VisualizationViewer(edu.uci.ics.jung.visualization.VisualizationViewer) DefaultModalGraphMouse(edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) GraphZoomScrollPane(edu.uci.ics.jung.visualization.GraphZoomScrollPane) Dimension(java.awt.Dimension) Paint(java.awt.Paint) Paint(java.awt.Paint) DirectedSparseMultigraph(edu.uci.ics.jung.graph.DirectedSparseMultigraph) Chc(catdata.Chc)

Example 27 with Att

use of catdata.aql.exp.SchExpRaw.Att in project fql by CategoricalData.

the class InstExpImport method eval.

@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> eval(AqlEnv env) {
    Schema<Ty, En, Sym, Fk, Att> sch = schema.eval(env);
    for (Ty ty : sch.typeSide.tys) {
        if (!sch.typeSide.js.java_tys.containsKey(ty)) {
            throw new RuntimeException("Import is only allowed onto java types");
        }
    }
    op = new AqlOptions(options, null, env.defaults);
    import_as_theory = (boolean) op.getOrDefault(AqlOption.import_as_theory);
    isJoined = (boolean) op.getOrDefault(AqlOption.import_joined);
    idCol = (String) op.getOrDefault(AqlOption.id_column_name);
    nullOnErr = (Boolean) op.getOrDefault(AqlOption.import_null_on_err_unsafe);
    prepend_entity_on_ids = (Boolean) op.getOrDefault(AqlOption.prepend_entity_on_ids);
    import_col_seperator = (String) op.getOrDefault(AqlOption.import_col_seperator);
    prefix = (String) op.getOrDefault(AqlOption.csv_import_prefix);
    dont_check_closure = (boolean) op.getOrDefault(AqlOption.import_dont_check_closure_unsafe);
    ens0 = new Ctx<>(Util.newSetsFor0(sch.ens));
    tys0 = new Ctx<>(Util.newSetsFor0(sch.typeSide.tys));
    fks0 = new Ctx<>();
    atts0 = new Ctx<>();
    extraRepr = new Ctx<>();
    En last = null;
    try {
        Handle h = start(sch);
        if (!isJoined) {
            /*
				Map<En, Q> ens = new HashMap<>();
				Map<Ty, Q> tys = new HashMap<>();
				Map<Att, Q> atts = new HashMap<>();
				Map<Fk, Q> fks = new HashMap<>();
	
				for (String o : map.keySet()) {
					assertUnambig(o, sch);
					Q q = map.get(o);
					if (sch.typeSide.tys.contains(new Ty(o))) {
						tys.put(new Ty(o), q);
					} else if (sch.ens.contains(new En(o))) {
						ens.put(new En(o), q);
					} else if (sch.atts.map.containsKey(new Att(o))) {
						atts.put(new Att(o), q);
					} else if (sch.fks.map.containsKey(new Fk(o))) {
						fks.put(new Fk(o), q);
					} //TODO aql
				}
				totalityCheck(sch, ens, tys, atts, fks);
	
				for (En en : ens.keySet()) {
					shreddedEn(h, en, ens.get(en), sch);
				}
				for (Fk fk : fks.keySet()) {
					shreddedFk(h, fk, fks.get(fk), sch);
				}
				for (Att att : atts.keySet()) {
					shreddedAtt(h, att, atts.get(att), sch);
				}
	*/
            throw new RuntimeException("Unjoined form no longer supported.  To request, contact us.");
        } else {
            for (En en : sch.ens) {
                last = en;
                Q z = map.get(en.str);
                // TODO: aql: this check isn't needed
                // if (z == null) {
                // throw new RuntimeException("No binding given for " + en);
                // }
                joinedEn(h, en, z, sch);
            }
        }
        end(h);
    } catch (Exception exn) {
        exn.printStackTrace();
        String pre = "";
        if (last != null) {
            pre = "On entity " + last + ", ";
        }
        throw new RuntimeException(pre + exn.getMessage() + "\n\n" + getHelpStr());
    }
    if (import_as_theory) {
        return forTheory(sch, ens0, tys0, fks0, atts0, op);
    }
    ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>> alg = new ImportAlgebra<>(sch, ens0, tys0, fks0, atts0, Object::toString, Object::toString, dont_check_closure);
    return new SaturatedInstance<>(alg, alg, (Boolean) op.getOrDefault(AqlOption.require_consistency), (Boolean) op.getOrDefault(AqlOption.allow_java_eqs_unsafe), true, extraRepr);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) ImportAlgebra(catdata.aql.ImportAlgebra) Null(catdata.Null) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) En(catdata.aql.exp.SchExpRaw.En) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOptions(catdata.aql.AqlOptions) SaturatedInstance(catdata.aql.fdm.SaturatedInstance)

Example 28 with Att

use of catdata.aql.exp.SchExpRaw.Att in project fql by CategoricalData.

the class InstExpJdbc method joinedEn.

@Override
protected void joinedEn(Connection conn, En en, String s, Schema<Ty, En, Sym, Fk, Att> sch) throws Exception {
    if (s == null) {
        if (!(boolean) op.getOrDefault(AqlOption.import_missing_is_empty)) {
            throw new RuntimeException("Missing query for entity: " + en + ". \n\nPossible options to consider: " + AqlOption.import_missing_is_empty);
        } else {
            return;
        }
    }
    Statement stmt = conn.createStatement();
    stmt.execute(s);
    ResultSet rs = stmt.getResultSet();
    ResultSetMetaData rsmd = rs.getMetaData();
    checkColumns(en, s, sch, rsmd);
    while (rs.next()) {
        Object gen = rs.getObject(idCol);
        if (gen == null) {
            stmt.close();
            rs.close();
            conn.close();
            throw new RuntimeException("Encountered a NULL generator in ID column " + idCol);
        }
        Gen g1 = toGen(en, gen.toString());
        // store strings
        ens0.get(en).add(g1);
        for (Fk fk : sch.fksFrom(en)) {
            Object rhs = rs.getObject(fk.str);
            if (rhs == null) {
                stmt.close();
                rs.close();
                conn.close();
                throw new RuntimeException("ID " + gen + " has a NULL foreign key value on " + fk);
            }
            En en2 = sch.fks.get(fk).second;
            // store strings
            Gen g2 = toGen(en2, rhs.toString());
            if (!fks0.containsKey(g1)) {
                fks0.put(g1, new Ctx<>());
            }
            fks0.get(g1).put(fk, g2);
        }
        for (Att att : sch.attsFrom(en)) {
            Object rhs = rs.getObject(att.str);
            if (!atts0.map.containsKey(g1)) {
                atts0.put(g1, new Ctx<>());
            }
            atts0.get(g1).put(att, objectToSk(sch, rhs, g1, att, tys0, extraRepr, false, nullOnErr));
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Att(catdata.aql.exp.SchExpRaw.Att) Gen(catdata.aql.exp.InstExpRaw.Gen) Fk(catdata.aql.exp.SchExpRaw.Fk) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) En(catdata.aql.exp.SchExpRaw.En)

Example 29 with Att

use of catdata.aql.exp.SchExpRaw.Att 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 30 with Att

use of catdata.aql.exp.SchExpRaw.Att 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)

Aggregations

Att (catdata.aql.exp.SchExpRaw.Att)36 En (catdata.aql.exp.SchExpRaw.En)36 Fk (catdata.aql.exp.SchExpRaw.Fk)36 Sym (catdata.aql.exp.TyExpRaw.Sym)34 Ty (catdata.aql.exp.TyExpRaw.Ty)33 Ctx (catdata.Ctx)26 Pair (catdata.Pair)25 Gen (catdata.aql.exp.InstExpRaw.Gen)22 HashSet (java.util.HashSet)20 Chc (catdata.Chc)17 AqlOptions (catdata.aql.AqlOptions)17 Triple (catdata.Triple)16 Sk (catdata.aql.exp.InstExpRaw.Sk)16 List (java.util.List)15 Collage (catdata.aql.Collage)13 Term (catdata.aql.Term)13 HashMap (java.util.HashMap)13 LinkedList (java.util.LinkedList)12 RawTerm (catdata.aql.RawTerm)11 Set (java.util.Set)10