Search in sources :

Example 1 with EqualizerConstraint

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

the class ModelInfoTreeUI method _addConstraint.

/**
 * Internal method that actually does the work of adding the constraint to
 * the info tree; this is separate from the public method so that the public
 * method can handle undo/redo controlling.
 *
 * @param constraint
 */
private void _addConstraint(ModelConstraint<F, GM, M, N, E> constraint) {
    DefaultMutableTreeNode node = constraint.getTreeNode();
    if (constraint instanceof ProductConstraint) {
        _tree_constraints_product.add(node);
    } else if (constraint instanceof SumConstraint) {
        _tree_constraints_sum.add(node);
    } else if (constraint instanceof PullbackConstraint) {
        _tree_constraints_pullback.add(node);
    } else if (constraint instanceof EqualizerConstraint) {
        _tree_constraints_equalizer.add(node);
    } else if (constraint instanceof CommutativeDiagram) {
        _tree_constraints_commutative.add(node);
    } else if (constraint instanceof LimitConstraint) {
        _tree_constraints_limit.add(node);
    }
    // Add the paths to the constraint
    for (ModelPath<F, GM, M, N, E> path : constraint.getPaths()) {
        node.add(path);
    }
    refreshTree((DefaultMutableTreeNode) node.getParent());
}
Also used : LimitConstraint(easik.model.constraint.LimitConstraint) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) PullbackConstraint(easik.model.constraint.PullbackConstraint) SumConstraint(easik.model.constraint.SumConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) EqualizerConstraint(easik.model.constraint.EqualizerConstraint) CommutativeDiagram(easik.model.constraint.CommutativeDiagram)

Example 2 with EqualizerConstraint

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

the class ModelInfoTreeUI method _removeConstraint.

/**
 * Internal method that actually does the work of adding the constraint to
 * the info tree; this is separate from the public method so that the public
 * method can handle undo/redo controlling.
 *
 * @param c
 */
private void _removeConstraint(ModelConstraint<F, GM, M, N, E> c) {
    DefaultMutableTreeNode constraint = c.getTreeNode();
    if (c instanceof CommutativeDiagram) {
        _tree_constraints_commutative.remove(constraint);
    } else if (c instanceof ProductConstraint) {
        _tree_constraints_product.remove(constraint);
    } else if (c instanceof PullbackConstraint) {
        _tree_constraints_pullback.remove(constraint);
    } else if (c instanceof EqualizerConstraint) {
        _tree_constraints_equalizer.remove(constraint);
    } else if (c instanceof SumConstraint) {
        _tree_constraints_sum.remove(constraint);
    } else if (c instanceof LimitConstraint) {
        _tree_constraints_limit.remove(constraint);
    }
    refreshTree();
}
Also used : ProductConstraint(easik.model.constraint.ProductConstraint) LimitConstraint(easik.model.constraint.LimitConstraint) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) EqualizerConstraint(easik.model.constraint.EqualizerConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) SumConstraint(easik.model.constraint.SumConstraint) CommutativeDiagram(easik.model.constraint.CommutativeDiagram)

Example 3 with EqualizerConstraint

use of easik.model.constraint.EqualizerConstraint 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 4 with EqualizerConstraint

use of easik.model.constraint.EqualizerConstraint 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 5 with EqualizerConstraint

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

the class SketchHandler method startElement.

/**
 * Overloaded method that is called any time the start of an element is
 * found
 *
 * @param namespace
 * 			@see org.xml.sax.helpers.DefaultHandler
 * @param localName
 * 			@see org.xml.sax.helpers.DefaultHandler
 * @param qName
 * 			@see org.xml.sax.helpers.DefaultHandler
 * @param atts
 * 			@see org.xml.sax.helpers.DefaultHandler
 */
@Override
public void startElement(String namespace, String localName, String qName, Attributes atts) {
    _currNode = qName;
    if (qName.equals("entity")) {
        String name = atts.getValue("name");
        int x = Integer.parseInt(atts.getValue("x"));
        int y = Integer.parseInt(atts.getValue("y"));
        if (_entityNodes.containsKey(name)) {
            System.err.println("Duplicate nodes found in XML");
            return;
        }
        _newNode = new EntityNode(name, x, y, _theFrame.getMModel());
        _entityNodes.put(name, _newNode);
        _curNodeAtts = new LinkedHashMap<>();
    } else if (qName.equals("attribute")) {
        EasikType type;
        // attributeType was created by old Easik versions, and is the SQL
        // type signature
        // (such as "VARCHAR(255)"). Easik now uses attributeTypeClass,
        // containing the
        // class name, and any number of extra attributes which
        // EasikType.newType() uses to
        // recreate the appropriate EasikType object.
        String typesig = atts.getValue("attributeType");
        if (typesig != null) {
            type = EasikType.typeFromSignature(typesig);
        } else {
            String typename = atts.getValue("attributeTypeClass");
            try {
                type = EasikType.newType(typename, attributeMap(atts, "attributeType", "attributeTypeClass", "name"));
            } catch (ClassNotFoundException e) {
                System.err.println("Invalid type found in XML: '" + typename + "' (" + e.getMessage() + ")");
                return;
            }
        }
        EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> myAtt = new EntityAttribute<>(atts.getValue("name"), type, _newNode);
        _curNodeAtts.put(atts.getValue("name"), myAtt);
        _newNode.addEntityAttribute(myAtt);
    } else if (qName.equals("uniqueKey")) {
        // New EASIK has noderef, telling us what we refer to. In old easik,
        // uniqueKey is under
        // the node itself (but as a result, cannot contain edge
        // references).
        String noderef = atts.getValue("noderef");
        if (noderef != null) {
            // Restore _newNode and _curNodeAtts, since we're going to need
            // them:
            _newNode = _entityNodes.get(noderef);
            _curNodeAtts = new LinkedHashMap<>();
            for (EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> att : _newNode.getEntityAttributes()) {
                _curNodeAtts.put(att.getName(), att);
            }
        }
        _curUniqueKeyName = atts.getValue("name");
        _curUniqueKeyElems = new LinkedList<>();
    } else if (qName.equals("attref")) {
        _curUniqueKeyElems.add(_curNodeAtts.get(atts.getValue("name")));
    } else if (qName.equals("edgekeyref")) {
        SketchEdge e = _edges.get(atts.getValue("id"));
        if (e instanceof UniqueIndexable) {
            _curUniqueKeyElems.add((UniqueIndexable) e);
        } else {
            System.err.println("Encountered an non-unique-indexable <edgekeyref> " + e);
        }
    } else if (qName.equals("edge")) {
        SketchEdge newEdge;
        String edgeType = atts.getValue("type");
        // injective is an old EASIK attribute:
        String injective = atts.getValue("injective");
        if (injective != null) {
            edgeType = atts.getValue("injective").equals("true") ? "injective" : "normal";
        }
        EntityNode source = _entityNodes.get(atts.getValue("source"));
        EntityNode target = _entityNodes.get(atts.getValue("target"));
        String id = atts.getValue("id");
        String cascadeAtt = atts.getValue("cascade");
        if (cascadeAtt == null) {
            // This is from an export before Easik had per-edge cascading
            // (in other words, before r583)
            // We use the global preferences for cascading
            String key = "sql_cascade", def = "restrict";
            if (edgeType.equals("partial")) {
                key = "sql_cascade_partial";
                def = "set_null";
            }
            cascadeAtt = Easik.getInstance().getSettings().getProperty(key, def);
        }
        SketchEdge.Cascade cascade = cascadeAtt.equals("set_null") ? SketchEdge.Cascade.SET_NULL : cascadeAtt.equals("cascade") ? SketchEdge.Cascade.CASCADE : SketchEdge.Cascade.RESTRICT;
        if (edgeType.equals("injective")) {
            newEdge = new InjectiveEdge(source, target, id, cascade);
        } else if (edgeType.equals("partial")) {
            newEdge = new PartialEdge(source, target, id, cascade);
        } else {
            newEdge = new NormalEdge(source, target, id, cascade);
        }
        _edges.put(id, newEdge);
    } else if (qName.equals("path")) {
        _curPath = new LinkedList<>();
        _curPathId = atts.getValue("id");
        _curDomain = _entityNodes.get(atts.getValue("domain"));
    } else if (qName.equals("edgeref")) {
        _curPath.add(_edges.get(atts.getValue("id")));
    } else if (// TRIANGLES
    qName.equals("sumconstraint") || qName.equals("pullbackconstraint") || qName.equals("productconstraint") || qName.equals("commutativediagram") || qName.equals("equalizerconstraint") || qName.equals("limitconstraint")) // CF2012
    {
        _curConstraintX = Integer.parseInt(atts.getValue("x"));
        _curConstraintY = Integer.parseInt(atts.getValue("y"));
        _curConstraintVisible = atts.getValue("isVisible").equals("true");
        _curConstraintPaths = new ArrayList<>();
        _allConstraintsVisible = atts.getValue("isVisible").equals("true");
    } else if (qName.equals("pathref")) {
        // This is for compatibility with old versions of Easik (pre-2.0);
        // new versions
        // put <path> elements directly inside the various constraint
        // elements.
        _curConstraintPaths.add(_allPaths.get(atts.getValue("id")));
    } else if (qName.equals("connectionParam")) {
        _connParams.put(atts.getValue("name"), atts.getValue("value"));
    } else if (qName.equals("synchronized")) {
        // The existance of this tag tells us the sketch is synchronized
        _curSketchSync = true;
    }
}
Also used : EntityAttribute(easik.model.attribute.EntityAttribute) PartialEdge(easik.sketch.edge.PartialEdge) 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) 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) NormalEdge(easik.sketch.edge.NormalEdge) Sketch(easik.sketch.Sketch) EasikType(easik.database.types.EasikType) UniqueIndexable(easik.model.keys.UniqueIndexable)

Aggregations

EqualizerConstraint (easik.model.constraint.EqualizerConstraint)6 LimitConstraint (easik.model.constraint.LimitConstraint)6 ProductConstraint (easik.model.constraint.ProductConstraint)6 PullbackConstraint (easik.model.constraint.PullbackConstraint)6 SumConstraint (easik.model.constraint.SumConstraint)6 Sketch (easik.sketch.Sketch)4 SketchEdge (easik.sketch.edge.SketchEdge)4 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)4 SketchFrame (easik.ui.SketchFrame)4 CommutativeDiagram (easik.model.constraint.CommutativeDiagram)3 ModelConstraint (easik.model.constraint.ModelConstraint)3 EntityNode (easik.sketch.vertex.EntityNode)3 LinkedList (java.util.LinkedList)3 ModelPath (easik.model.path.ModelPath)2 InjectiveEdge (easik.sketch.edge.InjectiveEdge)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 EasikType (easik.database.types.EasikType)1 EntityAttribute (easik.model.attribute.EntityAttribute)1 UniqueIndexable (easik.model.keys.UniqueIndexable)1 NormalEdge (easik.sketch.edge.NormalEdge)1