use of easik.model.constraint.LimitConstraint 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());
}
use of easik.model.constraint.LimitConstraint in project fql by CategoricalData.
the class JDBCViewUpdateMonitor 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) {
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: " + 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: " + e.getMessage());
return false;
}
}
use of easik.model.constraint.LimitConstraint in project fql by CategoricalData.
the class SketchFileIO method sketchToElement.
/**
* Converts a sketch to an Element.
*
* @param document
* The Document in which our information is being placed.
* @param sketch
* @return All of the information needed to rebuild our sketch containted in
* an Element. Returns null in the event that the element could not
* be created.
*/
public static Element sketchToElement(Document document, Sketch sketch) {
try {
Element rootElement = document.createElement("easketch");
Element header = document.createElement("header");
// Add Header info to document
DocumentInfo d = sketch.getDocInfo();
Element name = document.createElement("title");
name.appendChild(document.createTextNode(d.getName()));
header.appendChild(name);
for (String aut : d.getAuthors()) {
Element author = document.createElement("author");
author.appendChild(document.createTextNode(aut));
header.appendChild(author);
}
Element desc = document.createElement("description");
desc.appendChild(document.createTextNode(d.getDesc()));
header.appendChild(desc);
Element creationDate = document.createElement("creationDate");
creationDate.appendChild(document.createTextNode(EasikConstants.XML_DATETIME.format(d.getCreationDate())));
header.appendChild(creationDate);
Element modDate = document.createElement("lastModificationDate");
modDate.appendChild(document.createTextNode(EasikConstants.XML_DATETIME.format(d.getModificationDate())));
header.appendChild(modDate);
Map<String, String> connParams = sketch.getConnectionParams();
for (String key : connParams.keySet()) {
Element connParam = document.createElement("connectionParam");
connParam.setAttribute("name", key);
connParam.setAttribute("value", connParams.get(key));
header.appendChild(connParam);
}
if (sketch.isSynced()) {
header.appendChild(document.createElement("synchronized"));
}
rootElement.appendChild(header);
Element entities = document.createElement("entities");
// Loop through entities, add them to the document
for (EntityNode currentEntity : sketch.getEntities()) {
if (currentEntity == null) {
continue;
}
Element thisEntity = document.createElement("entity");
thisEntity.setAttribute("name", currentEntity.toString());
thisEntity.setAttribute("x", currentEntity.getX() + "");
thisEntity.setAttribute("y", currentEntity.getY() + "");
entities.appendChild(thisEntity);
// Loop through attributes, add them to the document
for (EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> curAttribute : currentEntity.getEntityAttributes()) {
Element attributeElmt = document.createElement("attribute");
attributeElmt.setAttribute("name", curAttribute.getName());
EasikType attType = curAttribute.getType();
attributeElmt.setAttribute("attributeTypeClass", attType.getClass().getName());
Map<String, String> typeAttribs = attType.attributes();
for (String key : typeAttribs.keySet()) {
attributeElmt.setAttribute(key, typeAttribs.get(key));
}
thisEntity.appendChild(attributeElmt);
}
// We can't go through unique keys yet: they have to come
// *after* edges
}
rootElement.appendChild(entities);
Element edges = document.createElement("edges");
for (SketchEdge currentEdge : sketch.getEdges().values()) {
Element thisEdge = document.createElement("edge");
thisEdge.setAttribute("id", currentEdge.getName());
thisEdge.setAttribute("source", currentEdge.getSourceEntity().getName());
thisEdge.setAttribute("target", currentEdge.getTargetEntity().getName());
thisEdge.setAttribute("type", (currentEdge instanceof PartialEdge) ? "partial" : (currentEdge instanceof InjectiveEdge) ? "injective" : "normal");
thisEdge.setAttribute("cascade", (currentEdge.getCascading() == SketchEdge.Cascade.SET_NULL) ? "set_null" : (currentEdge.getCascading() == SketchEdge.Cascade.CASCADE) ? "cascade" : "restrict");
edges.appendChild(thisEdge);
}
rootElement.appendChild(edges);
Element keys = document.createElement("keys");
// Loop through unique keys for every node, add them to the document
for (EntityNode currentEntity : sketch.getEntities()) {
for (UniqueKey<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> curKey : currentEntity.getUniqueKeys()) {
Element uniqueKeyElmt = document.createElement("uniqueKey");
uniqueKeyElmt.setAttribute("name", curKey.getKeyName());
uniqueKeyElmt.setAttribute("noderef", currentEntity.toString());
keys.appendChild(uniqueKeyElmt);
for (UniqueIndexable curElem : curKey.getElements()) {
if (curElem instanceof EntityAttribute) {
Element attributeElmt = document.createElement("attref");
attributeElmt.setAttribute("name", curElem.getName());
uniqueKeyElmt.appendChild(attributeElmt);
} else if (curElem instanceof SketchEdge) {
Element edgeElmt = document.createElement("edgekeyref");
edgeElmt.setAttribute("id", curElem.getName());
uniqueKeyElmt.appendChild(edgeElmt);
} else {
System.err.println("Unknown unique key item encountered: element '" + curElem.getName() + "' is neither EntityAttribute nor SketchEdge");
}
}
}
}
rootElement.appendChild(keys);
Element constraints = document.createElement("constraints");
// Now add the constraints
for (ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> curConstraint : sketch.getConstraints().values()) {
Element thisConstraint = document.createElement(curConstraint.getType());
thisConstraint.setAttribute("x", curConstraint.getX() + "");
thisConstraint.setAttribute("y", curConstraint.getY() + "");
thisConstraint.setAttribute("isVisible", curConstraint.isVisible() ? "true" : "false");
if (curConstraint instanceof LimitConstraint) {
LimitConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> lc = (LimitConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) curConstraint;
// TODO A better way? really long
// cone - AB
Element pathElem = document.createElement("path");
pathElem.setAttribute("domain", lc.getCone().AB.getDomain().getName());
pathElem.setAttribute("codomain", lc.getCone().AB.getCoDomain().getName());
for (SketchEdge edge : lc.getCone().AB.getEdges()) {
Element edgeElem = document.createElement("edgeref");
edgeElem.setAttribute("id", edge.getName());
pathElem.appendChild(edgeElem);
}
thisConstraint.appendChild(pathElem);
// cone - BC
pathElem = document.createElement("path");
pathElem.setAttribute("domain", lc.getCone().BC.getDomain().getName());
pathElem.setAttribute("codomain", lc.getCone().BC.getCoDomain().getName());
for (SketchEdge edge : lc.getCone().BC.getEdges()) {
Element edgeElem = document.createElement("edgeref");
edgeElem.setAttribute("id", edge.getName());
pathElem.appendChild(edgeElem);
}
thisConstraint.appendChild(pathElem);
// cone - AC
pathElem = document.createElement("path");
pathElem.setAttribute("domain", lc.getCone().AC.getDomain().getName());
pathElem.setAttribute("codomain", lc.getCone().AC.getCoDomain().getName());
for (SketchEdge edge : lc.getCone().AC.getEdges()) {
Element edgeElem = document.createElement("edgeref");
edgeElem.setAttribute("id", edge.getName());
pathElem.appendChild(edgeElem);
}
thisConstraint.appendChild(pathElem);
// limit cone 1 - AB
pathElem = document.createElement("path");
pathElem.setAttribute("domain", lc.getLimitCone1().AB.getDomain().getName());
pathElem.setAttribute("codomain", lc.getLimitCone1().AB.getCoDomain().getName());
for (SketchEdge edge : lc.getLimitCone1().AB.getEdges()) {
Element edgeElem = document.createElement("edgeref");
edgeElem.setAttribute("id", edge.getName());
pathElem.appendChild(edgeElem);
}
thisConstraint.appendChild(pathElem);
// limit cone 1 - BC
pathElem = document.createElement("path");
pathElem.setAttribute("domain", lc.getLimitCone1().BC.getDomain().getName());
pathElem.setAttribute("codomain", lc.getLimitCone1().BC.getCoDomain().getName());
for (SketchEdge edge : lc.getLimitCone1().BC.getEdges()) {
Element edgeElem = document.createElement("edgeref");
edgeElem.setAttribute("id", edge.getName());
pathElem.appendChild(edgeElem);
}
thisConstraint.appendChild(pathElem);
// limit cone 1 - AC
pathElem = document.createElement("path");
pathElem.setAttribute("domain", lc.getLimitCone1().AC.getDomain().getName());
pathElem.setAttribute("codomain", lc.getLimitCone1().AC.getCoDomain().getName());
for (SketchEdge edge : lc.getLimitCone1().AC.getEdges()) {
Element edgeElem = document.createElement("edgeref");
edgeElem.setAttribute("id", edge.getName());
pathElem.appendChild(edgeElem);
}
thisConstraint.appendChild(pathElem);
// limit cone 2 - AB
pathElem = document.createElement("path");
pathElem.setAttribute("domain", lc.getLimitCone2().AB.getDomain().getName());
pathElem.setAttribute("codomain", lc.getLimitCone2().AB.getCoDomain().getName());
for (SketchEdge edge : lc.getLimitCone2().AB.getEdges()) {
Element edgeElem = document.createElement("edgeref");
edgeElem.setAttribute("id", edge.getName());
pathElem.appendChild(edgeElem);
}
thisConstraint.appendChild(pathElem);
// limit cone 2 - BC
pathElem = document.createElement("path");
pathElem.setAttribute("domain", lc.getLimitCone2().BC.getDomain().getName());
pathElem.setAttribute("codomain", lc.getLimitCone2().BC.getCoDomain().getName());
for (SketchEdge edge : lc.getLimitCone2().BC.getEdges()) {
Element edgeElem = document.createElement("edgeref");
edgeElem.setAttribute("id", edge.getName());
pathElem.appendChild(edgeElem);
}
thisConstraint.appendChild(pathElem);
// limit cone 2 - AC
pathElem = document.createElement("path");
pathElem.setAttribute("domain", lc.getLimitCone2().AC.getDomain().getName());
pathElem.setAttribute("codomain", lc.getLimitCone2().AC.getCoDomain().getName());
for (SketchEdge edge : lc.getLimitCone2().AC.getEdges()) {
Element edgeElem = document.createElement("edgeref");
edgeElem.setAttribute("id", edge.getName());
pathElem.appendChild(edgeElem);
}
thisConstraint.appendChild(pathElem);
// Add constraint to constraints
constraints.appendChild(thisConstraint);
continue;
}
for (ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : curConstraint.getPaths()) {
// Add pathref to constraint
Element pathElem = document.createElement("path");
pathElem.setAttribute("domain", path.getDomain().getName());
pathElem.setAttribute("codomain", path.getCoDomain().getName());
for (SketchEdge edge : path.getEdges()) {
Element edgeElem = document.createElement("edgeref");
edgeElem.setAttribute("id", edge.getName());
pathElem.appendChild(edgeElem);
}
thisConstraint.appendChild(pathElem);
}
// Add constraint to constraints
constraints.appendChild(thisConstraint);
}
rootElement.appendChild(constraints);
return rootElement;
} catch (Exception e) {
return null;
}
}
use of easik.model.constraint.LimitConstraint in project fql by CategoricalData.
the class MySQLExporter method createConstraint.
/**
* @param constraint
* @param id
*
* @return
*/
@Override
public List<String> createConstraint(final LimitConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> constraint, final String id) {
final List<String> sql = new LinkedList<>();
StringBuilder proc = new StringBuilder(500);
final List<String> declarations = new LinkedList<>();
final List<String> values = new LinkedList<>();
final List<String> args = new LinkedList<>();
final List<String> params = new LinkedList<>();
// getting the paths involved in this limit constraint
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> coneAB = constraint.getCone().AB;
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> coneBC = constraint.getCone().BC;
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> coneAC = constraint.getCone().AC;
// TODO this should be in the LimitConstraint
List<SketchEdge> tmpEdges = coneAB.getEdges();
tmpEdges.addAll(coneBC.getEdges());
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> coneABC = new ModelPath<>(tmpEdges);
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> limitConeAB = constraint.getLimitCone1().AB;
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> limitCone1BC = constraint.getLimitCone1().BC;
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> limitCone2BC = constraint.getLimitCone2().BC;
tmpEdges = limitConeAB.getEdges();
tmpEdges.addAll(limitCone1BC.getEdges());
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> limitCone1ABC = new ModelPath<>(tmpEdges);
tmpEdges = limitConeAB.getEdges();
tmpEdges.addAll(limitCone2BC.getEdges());
ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> limitCone2ABC = new ModelPath<>(tmpEdges);
ArrayList<ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> paths = new ArrayList<>();
paths.add(coneABC);
paths.add(coneAC);
paths.add(limitCone1ABC);
paths.add(coneAB);
paths.add(limitCone2ABC);
// would add coneAC, but it's already been added
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("limitConstraint" + 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);
// cone commutativity check
proc.append(" NOT (_cdTarget1 <=> _cdTarget2) OR");
proc.append(lineSep);
// limitCone1 commutativity check
proc.append(" NOT (_cdTarget3 <=> _cdTarget4) OR");
proc.append(lineSep);
// limitCone2 commutativity check
proc.append(" NOT (_cdTarget5 <=> _cdTarget2)");
proc.append(lineSep);
proc.append(" THEN CALL constraint_failure('Limit constraint failure.');").append(lineSep).append(" END IF;").append(lineSep).append("END");
addFail = true;
sql.addAll(delimit("$$", proc));
// cast because we will only do this in sketches
addTrigger(constraint.getCone().getA(), "BEFORE INSERT", "CALL " + quoteId("limitConstraint" + id) + '(' + EasikTools.join(", ", params) + ')');
return sql;
}
use of easik.model.constraint.LimitConstraint 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();
}
Aggregations