Search in sources :

Example 6 with ModelPath

use of easik.model.path.ModelPath 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 ModelPath

use of easik.model.path.ModelPath in project fql by CategoricalData.

the class MySQLExporter method createConstraint.

/**
 * @param cd
 * @param id
 *
 * @return
 */
@Override
public List<String> createConstraint(final CommutativeDiagram<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> cd, final String id) {
    final List<String> sql = new LinkedList<>();
    final List<ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> paths = cd.getPaths();
    final EntityNode dom = paths.get(0).getDomain();
    final StringBuilder proc = new StringBuilder("");
    final List<String> declarations = new LinkedList<>();
    final List<String> values = new LinkedList<>();
    final List<String> args = new LinkedList<>();
    final List<String> params = new LinkedList<>();
    int targetNum = 0;
    for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : paths) {
        ++targetNum;
        final LinkedList<SketchEdge> tmpPath = new LinkedList<>(path.getEdges());
        tmpPath.removeFirst();
        final String fk = "_path" + targetNum + "fk";
        args.add(fk + ' ' + pkType());
        params.add("NEW." + quoteId(tableFK(path.getFirstEdge())));
        declarations.add("_cdTarget" + targetNum);
        if (tmpPath.size() == 0) {
            values.add("    SELECT " + fk + " INTO _cdTarget" + targetNum + ';' + lineSep);
        } else {
            values.add("    SELECT " + qualifiedFK(path.getLastEdge()) + " INTO _cdTarget" + targetNum + " FROM " + joinPath(tmpPath, false) + " WHERE " + qualifiedPK(tmpPath.getFirst().getSourceEntity()) + " = " + fk + ';' + lineSep);
        }
    }
    proc.append("CREATE PROCEDURE ").append(quoteId("commutativeDiagram" + id)).append('(').append(EasikTools.join(", ", args)).append(") BEGIN").append(lineSep).append("       DECLARE ").append(EasikTools.join(", ", declarations)).append(' ').append(pkType()).append(';').append(lineSep).append(EasikTools.join("", values)).append("       IF").append(lineSep);
    for (int i = 2; i <= targetNum; i++) {
        proc.append("               NOT (_cdTarget1 <=> _cdTarget").append(i).append(')');
        if (i < targetNum) {
            proc.append(" OR");
        }
        proc.append(lineSep);
    }
    proc.append("       THEN CALL constraint_failure('Commutative diagram constraint failure.');").append(lineSep).append("       END IF;").append(lineSep).append("END");
    addFail = true;
    sql.addAll(delimit("$$", proc));
    addTrigger(dom, "BEFORE INSERT", "CALL " + quoteId("commutativeDiagram" + id) + '(' + EasikTools.join(", ", params) + ')');
    return sql;
}
Also used : 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) EntityNode(easik.sketch.vertex.EntityNode) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) ModelPath(easik.model.path.ModelPath) Sketch(easik.sketch.Sketch)

Example 8 with ModelPath

use of easik.model.path.ModelPath in project fql by CategoricalData.

the class MySQLExporter method createConstraint.

/**
 * @param constraint
 * @param id
 *
 * @return
 */
@Override
public List<String> createConstraint(final EqualizerConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> constraint, final String id) {
    final List<String> sql = new LinkedList<>();
    final EntityNode eq = constraint.getEqualizerEntity();
    final EntityNode source = constraint.getSourceEntity();
    final EntityNode target = constraint.getTargetEntity();
    final InjectiveEdge projEdge = (InjectiveEdge) constraint.getProjection().getFirstEdge();
    final List<ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> paths = constraint.getEqualizerPaths();
    final String insConName = "eqCon" + id + "InsUpd" + cleanId(source);
    final Collection<String> args = new LinkedList<>();
    final Collection<String> params = new LinkedList<>();
    args.add("_newId " + pkType());
    params.add("NEW." + quoteId(tablePK(source)));
    // commented out by Sarah van der Laan -- caused error in generated SQL
    // file (invalid foreign keys)
    /**
     * for (final SketchEdge shadow : source.getShadowEdges()) {
     * args.add(quoteId("NEW_shadow_" + tableFK(shadow)) + ' ' + pkType());
     * params.add("NEW." + quoteId(tableFK(shadow))); }
     */
    final StringBuilder proc = new StringBuilder(500);
    proc.append("CREATE PROCEDURE ").append(quoteId(insConName)).append('(').append(EasikTools.join(", ", args)).append(") BEGIN").append(lineSep);
    proc.append("   DECLARE _path0Id");
    // Add a variable for each path, _path0Id, _path1Id, etc.:
    for (int i = 1; i < paths.size(); i++) {
        proc.append(", _path").append(i).append("Id");
    }
    proc.append(' ').append(pkType()).append(';').append(lineSep);
    final String sourcePK = qualifiedPK(source);
    final String targetPK = qualifiedPK(target);
    final String newPK = "_newId";
    int i = 0;
    for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p : paths) {
        proc.append("   SELECT ").append(targetPK).append(" INTO _path").append(i).append("Id").append(lineSep).append("           FROM ").append(joinPath(p)).append(lineSep).append("           WHERE ").append(sourcePK).append(" = ").append(newPK).append(';').append(lineSep);
        ++i;
    }
    // Build equality clause, such as: _path0Id IS DISTINCT FROM _path1Id OR
    // _path0Id IS DISTINCT FROM _path2Id OR ...
    proc.append("   IF NOT (_path0Id <=> _path1Id)");
    for (int j = 2; j < paths.size(); j++) {
        proc.append(" OR NOT (_path0Id <=> _path").append(j).append("Id)");
    }
    proc.append(" THEN").append(lineSep).append(// delete it from the equalizer (if it's there)
    "               DELETE FROM ").append(quoteId(eq)).append(" WHERE ").append(qualifiedFK(projEdge)).append(" = ").append(newPK).append(';').append(lineSep).append("       ELSEIF (SELECT COUNT(*) FROM ").append(quoteId(eq)).append(" WHERE ").append(qualifiedFK(projEdge)).append(" = ").append(newPK).append(") = 0 THEN").append(lineSep).append(// add it:
    "               ");
    /*
		 * Deal with intermediate nodes in projection Federico Mora
		 */
    ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path = constraint.getProjection();
    final SketchEdge[] edges = path.getEdges().toArray(new SketchEdge[path.getEdges().size()]);
    for (int j = edges.length - 1; j > 0; j--) {
        final EntityNode s = edges[j].getSourceEntity();
        if (j == edges.length - 1) {
            // after s there used to be dest.getShadowEdges();
            proc.append(insertInto(false, s, null, null, Collections.singletonList(quoteId(tableFK(edges[j]))), Collections.singletonList("_newId")));
        } else {
            // after source there used to be dest.getShadowEdges();
            proc.append(insertInto(false, s, null, null, Collections.singletonList(quoteId(tableFK(edges[j]))), Collections.singletonList("LAST_INSERT_ID()")));
        }
    }
    // done - deal with intermediate nodes in projection
    // after eq there used to be dest.getShadowEdges();
    proc.append("               ").append(insertInto(true, eq, null, null, Collections.singletonList(quoteId(tableFK(projEdge))), Collections.singletonList("LAST_INSERT_ID()"))).append("       END IF;").append(lineSep).append("END").append(lineSep);
    sql.add(proc.toString());
    // Create the trigger for inserting into the source table:
    final String call = "CALL " + quoteId(insConName) + '(' + EasikTools.join(", ", params) + ')';
    addTrigger(source, "AFTER INSERT", call);
    addTrigger(source, "AFTER UPDATE", call);
    // If the projection isn't a cascading edge, cascade:
    if (projEdge.getCascading() != SketchEdge.Cascade.CASCADE) {
        addTrigger(source, "BEFORE DELETE", "DELETE FROM " + quoteId(eq) + " WHERE " + qualifiedFK(projEdge) + " = OLD." + quoteId(tablePK(source)));
    }
    // Federico Mora
    ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p = constraint.getProjection();
    StringBuilder proc1 = new StringBuilder(500);
    proc1.append("CREATE PROCEDURE ").append("Update" + constraint.getID() + "Proj").append("() BEGIN").append(lineSep);
    // making the update string which is used by all the delete
    // procedures.
    final LinkedList<SketchEdge> egs = p.getEdges();
    if (egs.size() >= 2) {
        Iterator<SketchEdge> iter = egs.iterator();
        SketchEdge e = iter.next();
        proc1.append("   UPDATE ").append(leftJoinPath(p)).append(lineSep);
        proc1.append("   SET");
        while (iter.hasNext()) {
            SketchEdge ske = iter.next();
            proc1.append(" " + quoteId(ske.getSourceEntity()) + ".BC" + constraint.getID() + " = " + false + ",");
        }
        proc1.delete(proc1.length() - 1, proc1.length());
        proc1.append(" WHERE " + qualifiedFK(e) + " IS NULL;" + lineSep);
        proc1.append("END");
        sql.add(proc1.toString());
        // Now create the triggers to call the new procedure
        addTrigger(p.getCoDomain(), "AFTER UPDATE", "CALL " + quoteId("Update" + constraint.getID() + "Proj") + "()");
    }
    return delimit("$$", sql);
}
Also used : 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) EntityNode(easik.sketch.vertex.EntityNode) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) SketchFrame(easik.ui.SketchFrame) InjectiveEdge(easik.sketch.edge.InjectiveEdge) SketchEdge(easik.sketch.edge.SketchEdge) ModelPath(easik.model.path.ModelPath) Sketch(easik.sketch.Sketch)

Example 9 with ModelPath

use of easik.model.path.ModelPath in project fql by CategoricalData.

the class XSDExporter method createConstraint.

/**
 * Add an annotation explaining the pullback.
 * <p/>
 * Today, this is simply done by creating an annotation. For example in the
 * standard pullback constraint in constraints.easik gives this annotation:
 *
 * <pre>
 * &lt;xs:annotation>
 *  &lt;xs:documentation>
 *     ForAll.elemA in B, ForAll.elemB in C :
 *     elemA.f1 in A=elemB.isA_1 in A
 *     ==> Exists.p=(elemA,elemB) in Pullback
 *   &lt;/xs:documentation>
 * &lt;/xs:annotation>
 * </pre>
 *
 * @param pb
 *            the product diagram constraint.
 */
private void createConstraint(final PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> pb) {
    final List<ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> paths = pb.getPaths();
    final EntityNode dom = paths.get(0).getDomain();
    final XSDType domType = dom.getXsdType();
    final List<String> elts = new ArrayList<>(paths.size());
    @SuppressWarnings("unused") final String keyrefName = Easik.getInstance().getSettings().getProperty("xml_keyref_name");
    final List<String> values = new ArrayList<>();
    final List<String> equalities = new ArrayList<>();
    // WPBEDIT CF2012
    for (int i = 0; i < pb.getWidth(); i++) {
        final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path = pb.getFullPath(i);
        final LinkedList<SketchEdge> tmpPath = new LinkedList<>(path.getEdges());
        tmpPath.removeFirst();
        values.add("ForAll.elem" + i + " in " + xmlPBJoinPath(tmpPath, false));
        equalities.add("elem" + i + '.' + xmlPBelemJoinPath(tmpPath, true));
        elts.add("elem" + i);
    }
    final String valdocumentation = EasikTools.join(", ", values);
    final String equalDoc = EasikTools.join("=", equalities);
    final String elements = "==> Exists.p=(" + EasikTools.join(",", elts) + ") in " + dom.getName();
    domType.addAnnotation(new XSDAnnotation(valdocumentation + " : " + lineSep + equalDoc + lineSep + elements));
}
Also used : ArrayList(java.util.ArrayList) 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) LinkedList(java.util.LinkedList) EntityNode(easik.sketch.vertex.EntityNode) XSDType(easik.xml.xsd.nodes.types.XSDType) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) ModelPath(easik.model.path.ModelPath) Sketch(easik.sketch.Sketch) XSDAnnotation(easik.xml.xsd.nodes.XSDAnnotation)

Example 10 with ModelPath

use of easik.model.path.ModelPath in project fql by CategoricalData.

the class XSDExporter method createConstraint.

/**
 * Add an annotation explaining the product.
 * <p/>
 * Today, this is simply done by creating an annotation. For example in the
 * standard product constraint in constraints.easik gives this annotation:
 *
 * <pre>
 * &lt;xs:annotation>
 *  &lt;xs:documentation>
 *     ForAll.elem1 in (P1), ForAll.elem2 in (P2)
 *     Exists.p=(elem1,elem2) in Product
 *   &lt;/xs:documentation>
 * &lt;/xs:annotation>
 * </pre>
 *
 * @param prod
 *            the product diagram constraint.
 */
private void createConstraint(final ProductConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> prod) {
    final List<ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> paths = prod.getPaths();
    final EntityNode dom = paths.get(0).getDomain();
    final XSDType domType = dom.getXsdType();
    final List<String> elts = new ArrayList<>(paths.size());
    int id = 0;
    @SuppressWarnings("unused") final String keyrefName = Easik.getInstance().getSettings().getProperty("xml_keyref_name");
    final List<String> values = new ArrayList<>(paths.size());
    for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : paths) {
        id++;
        final LinkedList<SketchEdge> tmpPath = new LinkedList<>(path.getEdges());
        tmpPath.removeFirst();
        final String elem = "elem" + id;
        elts.add(elem);
        if (tmpPath.size() == 0) {
            values.add("ForAll." + elem + " in (" + path.getCoDomain().getName() + ')');
        } else {
            values.add("ForAll." + elem + " in " + xmlJoinPath(tmpPath, true));
        }
    }
    final String documentation = EasikTools.join(", ", values);
    final String elements = "Exists.p=(" + EasikTools.join(",", elts) + ") in " + dom.getName();
    domType.addAnnotation(new XSDAnnotation(documentation + lineSep + elements));
}
Also used : ArrayList(java.util.ArrayList) 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) LinkedList(java.util.LinkedList) EntityNode(easik.sketch.vertex.EntityNode) XSDType(easik.xml.xsd.nodes.types.XSDType) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) ModelPath(easik.model.path.ModelPath) Sketch(easik.sketch.Sketch) XSDAnnotation(easik.xml.xsd.nodes.XSDAnnotation)

Aggregations

ModelPath (easik.model.path.ModelPath)14 ArrayList (java.util.ArrayList)10 Sketch (easik.sketch.Sketch)9 SketchEdge (easik.sketch.edge.SketchEdge)9 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)9 SketchFrame (easik.ui.SketchFrame)9 LinkedList (java.util.LinkedList)9 ModelConstraint (easik.model.constraint.ModelConstraint)8 ProductConstraint (easik.model.constraint.ProductConstraint)8 SumConstraint (easik.model.constraint.SumConstraint)8 EntityNode (easik.sketch.vertex.EntityNode)8 EqualizerConstraint (easik.model.constraint.EqualizerConstraint)6 LimitConstraint (easik.model.constraint.LimitConstraint)6 PullbackConstraint (easik.model.constraint.PullbackConstraint)6 XSDAnnotation (easik.xml.xsd.nodes.XSDAnnotation)4 XSDType (easik.xml.xsd.nodes.types.XSDType)4 Point (java.awt.Point)3 CommutativeDiagram (easik.model.constraint.CommutativeDiagram)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 EntityAttribute (easik.model.attribute.EntityAttribute)1