Search in sources :

Example 6 with ModelConstraint

use of easik.model.constraint.ModelConstraint in project fql by CategoricalData.

the class DeletePathAction method actionPerformed.

/**
 * Tests if the removal is valid and then removes the path from the
 * constraint
 *
 * @param e
 *            The action event
 */
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e) {
    // If there is nothing seleceted then just do nothing
    if (_theFrame.getInfoTreeUI().getInfoTree().isSelectionEmpty()) {
        return;
    }
    // cancel operation
    if (_theFrame.getMModel().isSynced()) {
        int choice = JOptionPane.showConfirmDialog(_theFrame, "Warning: this sketch is currently synced with a db; continue and break synchronization?", "Warning!", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
        if (choice == JOptionPane.CANCEL_OPTION) {
            return;
        }
    }
    // Get currently selected object
    DefaultMutableTreeNode curSelected = (DefaultMutableTreeNode) _theFrame.getInfoTreeUI().getInfoTree().getSelectionPath().getLastPathComponent();
    // Selection is a constraint
    if (curSelected instanceof ModelPath) {
        ModelConstraint<F, GM, M, N, E> curConstraint = (ModelConstraint<F, GM, M, N, E>) curSelected.getParent();
        ArrayList<ModelPath<F, GM, M, N, E>> tempPaths = new ArrayList<>();
        tempPaths.remove(curSelected);
        boolean valid = false;
        if (curConstraint instanceof SumConstraint) {
            valid = _theFrame.getMModel().isSumConstraint(tempPaths);
            // Replace previous path array list
            if (valid) {
                ((SumConstraint<F, GM, M, N, E>) curConstraint).setPaths(tempPaths);
            }
        } else if (curConstraint instanceof ProductConstraint) {
            valid = _theFrame.getMModel().isProductConstraint(tempPaths);
            // Replace previous path array list
            if (valid) {
                ((ProductConstraint<F, GM, M, N, E>) curConstraint).setPaths(tempPaths);
            }
        } else if (curConstraint instanceof CommutativeDiagram) {
            valid = _theFrame.getMModel().isCommutativeDiagram(tempPaths);
            // Replace previous path array list
            if (valid) {
                ((CommutativeDiagram<F, GM, M, N, E>) curConstraint).setPaths(tempPaths);
            }
        } else {
            JOptionPane.showMessageDialog(_theFrame, "You don't have a path selected that can be removed. \nPlease select another path and try again.", "No ModelConstraint Selected", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (valid) {
            ModelConstraint<F, GM, M, N, E> myConst = curConstraint;
            // Remove old tree node
            myConst.removeFromParent();
            _theFrame.getInfoTreeUI().addConstraint(myConst);
            // Referesh Tree
            _theFrame.getInfoTreeUI().refreshTree(myConst);
            _theFrame.getMModel().setDirty();
            _theFrame.getMModel().setSynced(false);
        } else {
            JOptionPane.showMessageDialog(_theFrame, "Revoming this path would make the constraint invalid.\nPath was not removed", "Path Not Removed", JOptionPane.ERROR_MESSAGE);
        }
    } else // Selection is not a constraint
    {
        JOptionPane.showMessageDialog(_theFrame, "You don't have a path selected. \nPlease select a path and try again.", "No ModelConstraint Selected", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ModelConstraint(easik.model.constraint.ModelConstraint) ArrayList(java.util.ArrayList) SumConstraint(easik.model.constraint.SumConstraint) SumConstraint(easik.model.constraint.SumConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) ModelPath(easik.model.path.ModelPath) CommutativeDiagram(easik.model.constraint.CommutativeDiagram)

Example 7 with ModelConstraint

use of easik.model.constraint.ModelConstraint in project fql by CategoricalData.

the class ModelVertexRenderer method getRendererComponent.

/**
 * Returns the renderer component after initializing it
 *
 * @param graph
 *            The graph (really a M in disguise)
 * @param view
 *            The cell view
 * @param sel
 *            If the view is selected or not
 * @param focus
 *            If the view has focus or not
 * @param preview
 *            If the graph is in preview mode or not
 * @return The renderer component fully initialized
 */
@Override
@SuppressWarnings("unchecked")
public Component getRendererComponent(JGraph graph, CellView view, boolean sel, boolean focus, boolean preview) {
    this.model = (M) graph;
    this.selected = sel;
    this.preview = preview;
    this.hasFocus = focus;
    ModelVertex<F, GM, M, N, E> v = (ModelVertex<F, GM, M, N, E>) view.getCell();
    // doesn't paint anything
    if ((v instanceof ModelConstraint) && !((ModelConstraint<F, GM, M, N, E>) v).isVisible()) {
        return new JPanel() {

            private static final long serialVersionUID = -8516030326162065848L;

            @Override
            public void paint(Graphics g) {
            }
        };
    }
    // may be because of int casting and off by one)
    if ((int) (view.getBounds().getX()) != v.getX() || (int) (view.getBounds().getY()) != v.getY()) {
        model.setDirty();
    }
    // Initialize panel
    removeAll();
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    // Add in entity/constraint label
    add(_entity = new JPanel(new GridLayout(1, 1)));
    _entity.add(_entityLabel = new JLabel(v.getName(), SwingConstants.CENTER));
    N myNode = (N) v;
    // Add in attributes
    _attributes = new JPanel(new GridLayout(0, 1));
    add(_attributes);
    for (EntityAttribute<F, GM, M, N, E> att : myNode.getEntityAttributes()) {
        _attributes.add(new JLabel(" @ " + att.getName()));
    }
    if ((_attributes.getComponentCount() == 0) || !model.getFrame().getShowAttsVal()) {
        _attributes.setVisible(false);
    }
    // Add in unique keys
    _uniqueKeys = new JPanel(new GridLayout(0, 1));
    add(_uniqueKeys);
    for (UniqueKey<F, GM, M, N, E> key : myNode.getUniqueKeys()) {
        _uniqueKeys.add(new JLabel(" $ " + key.getKeyName()));
    }
    if ((_uniqueKeys.getComponentCount() == 0) || !model.getFrame().getShowAttsVal()) {
        _uniqueKeys.setVisible(false);
    }
    @SuppressWarnings("rawtypes") Map attributes = view.getAllAttributes();
    installAttributes(v, attributes);
    // Set desired size
    setSize(getPreferredSize());
    return this;
}
Also used : JPanel(javax.swing.JPanel) ModelConstraint(easik.model.constraint.ModelConstraint) BoxLayout(javax.swing.BoxLayout) JLabel(javax.swing.JLabel) Graphics(java.awt.Graphics) GridLayout(java.awt.GridLayout) ModelVertex(easik.model.vertex.ModelVertex) Map(java.util.Map)

Example 8 with ModelConstraint

use of easik.model.constraint.ModelConstraint in project fql by CategoricalData.

the class JDBCExporter method createConstraints.

/**
 * Generates db constraints from the sketch constraints. This method is not
 * generally overridden by drivers: the individual createConstraint(...)
 * methods are called for each existing constraint.
 *
 * @return list of queries to create the constraints.
 */
protected List<String> createConstraints() {
    final List<String> constraintSQL = new LinkedList<>();
    final List<ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> constraints = new ArrayList<>(sketch.getConstraints().values());
    int id = 0;
    for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : constraints) {
        id++;
        if (c instanceof CommutativeDiagram) {
            constraintSQL.addAll(createConstraint((CommutativeDiagram<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else if (c instanceof ProductConstraint) {
            constraintSQL.addAll(createConstraint((ProductConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else if (c instanceof PullbackConstraint) {
            constraintSQL.addAll(createConstraint((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else if (c instanceof EqualizerConstraint) {
            constraintSQL.addAll(createConstraint((EqualizerConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else if (c instanceof SumConstraint) {
            constraintSQL.addAll(createConstraint((SumConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else if (c instanceof LimitConstraint) {
            constraintSQL.addAll(createConstraint((LimitConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
        } else {
            System.err.println("Unknown constraint type encountered: " + c.getClass());
        }
    }
    return constraintSQL;
}
Also used : LimitConstraint(easik.model.constraint.LimitConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) ArrayList(java.util.ArrayList) PullbackConstraint(easik.model.constraint.PullbackConstraint) SumConstraint(easik.model.constraint.SumConstraint) LinkedList(java.util.LinkedList) LimitConstraint(easik.model.constraint.LimitConstraint) EqualizerConstraint(easik.model.constraint.EqualizerConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) ProductConstraint(easik.model.constraint.ProductConstraint) SketchEdge(easik.sketch.edge.SketchEdge) EqualizerConstraint(easik.model.constraint.EqualizerConstraint) Sketch(easik.sketch.Sketch) CommutativeDiagram(easik.model.constraint.CommutativeDiagram)

Example 9 with ModelConstraint

use of easik.model.constraint.ModelConstraint in project fql by CategoricalData.

the class Model method removeConstraint.

/**
 * Removes a constraint and guide arrows
 *
 * @param toRemove
 *            The constraint about to be removed
 */
public void removeConstraint(final ModelConstraint<F, GM, M, N, E> toRemove) {
    model.beginUpdate();
    getGraphLayoutCache().remove(toRemove.getGuideEdges().toArray(new GuideEdge[toRemove.getGuideEdges().size()]));
    getGraphLayoutCache().remove(new Object[] { toRemove });
    final int position = toRemove.getID();
    _constraints.remove(toRemove.getID());
    for (N n : toRemove.getEntities()) {
        n.removeConstraint(toRemove);
    }
    model.postEdit(new AbstractUndoableEdit() {

        /**
         */
        private static final long serialVersionUID = 6431577416127308496L;

        @Override
        public void undo() {
            super.undo();
            _constraints.put(position, toRemove);
            for (N n : toRemove.getEntities()) {
                n.addConstraint(toRemove);
            }
        }

        @Override
        public void redo() {
            super.redo();
            _constraints.remove(position);
            for (N n : toRemove.getEntities()) {
                n.removeConstraint(toRemove);
            }
        }
    });
    // Remove Entity from tree
    _Frame.getInfoTreeUI().removeConstraint(toRemove);
    model.endUpdate();
}
Also used : AbstractUndoableEdit(javax.swing.undo.AbstractUndoableEdit) GuideEdge(easik.model.edge.GuideEdge) Point(java.awt.Point) ModelConstraint(easik.model.constraint.ModelConstraint)

Example 10 with ModelConstraint

use of easik.model.constraint.ModelConstraint in project fql by CategoricalData.

the class JDBCUpdateMonitor method insert.

/**
 * Determines if insertion into a given table requires special handling due
 * to constraints it may be in. As of now, special cases that may result
 * from being in multiple constraints are not supported.
 *
 * @param table
 *            The table into which we wish to insert data
 * @return Success of the insertion
 */
@Override
public boolean insert(final EntityNode table) {
    final DialogOptions dOpts = getDialogOptions(table);
    final String lineSep = EasikTools.systemLineSeparator();
    // a set of column-value pairs of which we wish to force a specific
    // value, leaving the user out
    final Set<ColumnEntry> forced = new HashSet<>(10);
    // contstraint. Tighten up?
    for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : table.getConstraints()) {
        if (c instanceof SumConstraint) {
            // of its foreign key, so remove it from the dialog's selection
            for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
                if (sp.getDomain() == table) {
                    // we force the value 0 to avoid out driver to kick back
                    // an error for having a null fKey
                    final String columnName = cn.tableFK(sp.getFirstEdge());
                    dOpts.fKeys.remove(columnName);
                    forced.add(new ColumnEntry(columnName, "0", new Int()));
                    break;
                }
            }
        }
        if (c instanceof CommutativeDiagram) {
            // commute
            if (c.getPaths().get(0).getDomain() == table) {
                JOptionPane.showMessageDialog(null, "Be sure that the following paths commute:" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram", JOptionPane.INFORMATION_MESSAGE);
                try {
                    return promptAndInsert(table, dOpts, forced);
                } catch (SQLException e) {
                    /*
						 * if(e instanceof com.mysql.jdbc.exceptions.jdbc4.
						 * MySQLIntegrityConstraintViolationException){
						 * //injective property violated
						 * JOptionPane.showMessageDialog(null, e.getMessage(),
						 * "Injective property violation",
						 * JOptionPane.ERROR_MESSAGE); }else{
						 */
                    JOptionPane.showMessageDialog(null, "Not all of the following paths commute -- insert aborted!" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram failure", JOptionPane.ERROR_MESSAGE);
                // }
                }
            }
        }
        if (c instanceof PullbackConstraint) {
            // happens, we want to let the user update the new record
            if (((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getTarget() != table) {
                final EntityNode pullback = ((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getSource();
                try {
                    // get row count pre-insert
                    ResultSet result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
                    result.next();
                    final int preRowCount = result.getInt(1);
                    if (!promptAndInsert(table, dOpts)) {
                        return false;
                    }
                    // get row count post-insert
                    result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
                    result.next();
                    final int postRowCount = result.getInt(1);
                    // new row (the one with the highest primary ID)
                    if (postRowCount > preRowCount) {
                        result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(pullback) + ") FROM " + pullback.getName() + " X");
                        result.next();
                        final int pk = result.getInt(1);
                        if (JOptionPane.showConfirmDialog(null, "New record in pullback table '" + pullback.getName() + "'. Enter column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0) {
                            updateRow(pullback, pk);
                        }
                    }
                    return true;
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(null, "Could not execute update. MYSQL Error output:\n" + e.getMessage());
                }
            }
        }
        if (c instanceof ProductConstraint) {
            // inserting into the product.
            for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
                if (sp.getCoDomain() == table) {
                    final EntityNode product = sp.getDomain();
                    try {
                        if (!promptAndInsert(table, dOpts)) {
                            return false;
                        }
                        // get the new records from the product. They are
                        // any record who's fk to our INSERT factor matches
                        // the primary id of the last insert
                        ResultSet result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(table) + ") FROM " + table.getName() + " X");
                        result.next();
                        final int newPK = result.getInt(1);
                        result = dbd.executeQuery("SELECT * FROM " + product.getName() + " WHERE " + cn.tableFK(sp.getFirstEdge()) + " = " + newPK);
                        // get count of new rows as result of INSERT
                        result.last();
                        final int newRows = result.getRow();
                        result.beforeFirst();
                        if ((newRows > 0) && (JOptionPane.showConfirmDialog(null, newRows + " new rows in product table '" + product.getName() + "'. Insert column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0)) {
                            while (result.next()) {
                                updateRow(product, result.getInt(1));
                            }
                        }
                    } catch (SQLException e) {
                        JOptionPane.showMessageDialog(null, e.getMessage());
                    }
                    return true;
                }
            }
        }
        if (c instanceof LimitConstraint) {
        // TRIANGLES TODO CF2012 Incomplete
        }
    }
    try {
        return promptAndInsert(table, dOpts, forced);
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Could not execute update. MYSQL Error output:\n" + e.getMessage());
        System.err.println(e.getMessage());
        return false;
    }
}
Also used : LimitConstraint(easik.model.constraint.LimitConstraint) SQLException(java.sql.SQLException) ColumnEntry(easik.ui.datamanip.ColumnEntry) PullbackConstraint(easik.model.constraint.PullbackConstraint) SumConstraint(easik.model.constraint.SumConstraint) Int(easik.database.types.Int) LimitConstraint(easik.model.constraint.LimitConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) ProductConstraint(easik.model.constraint.ProductConstraint) SketchEdge(easik.sketch.edge.SketchEdge) ResultSet(java.sql.ResultSet) Sketch(easik.sketch.Sketch) CommutativeDiagram(easik.model.constraint.CommutativeDiagram) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

ModelConstraint (easik.model.constraint.ModelConstraint)14 Sketch (easik.sketch.Sketch)9 SketchEdge (easik.sketch.edge.SketchEdge)8 SketchFrame (easik.ui.SketchFrame)8 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)7 EntityNode (easik.sketch.vertex.EntityNode)7 ProductConstraint (easik.model.constraint.ProductConstraint)6 SumConstraint (easik.model.constraint.SumConstraint)6 CommutativeDiagram (easik.model.constraint.CommutativeDiagram)5 LinkedHashSet (java.util.LinkedHashSet)5 LimitConstraint (easik.model.constraint.LimitConstraint)4 PullbackConstraint (easik.model.constraint.PullbackConstraint)4 HashSet (java.util.HashSet)4 GuideEdge (easik.model.edge.GuideEdge)3 ModelPath (easik.model.path.ModelPath)3 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)3 EasikType (easik.database.types.EasikType)2 Int (easik.database.types.Int)2 EqualizerConstraint (easik.model.constraint.EqualizerConstraint)2 TriangleEdge (easik.model.edge.TriangleEdge)2