use of easik.sketch.edge.InjectiveEdge 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.sketch.edge.InjectiveEdge in project fql by CategoricalData.
the class KosarajuSCC method transposeDFS.
/**
* Depth first search of the transpose
*
* @param v
*/
public void transposeDFS(EntityNode v) {
visited.add(v);
sccList.add(v);
for (SketchEdge sk : graph.getEdges().values()) {
if (sk instanceof NormalEdge || sk instanceof InjectiveEdge) {
if (sk.getTargetEntity().equals(v)) {
EntityNode t = sk.getSourceEntity();
if (!visited.contains(t)) {
transposeDFS(t);
}
}
}
}
}
use of easik.sketch.edge.InjectiveEdge in project fql by CategoricalData.
the class KosarajuSCC method dfs.
/**
* Depth first search
*/
public void dfs(EntityNode v) {
visited.add(v);
for (SketchEdge sk : v.getOutgoingEdges()) {
if (sk instanceof NormalEdge || sk instanceof InjectiveEdge) {
EntityNode t = sk.getTargetEntity();
if (!visited.contains(t)) {
dfs(t);
}
}
}
stack.push(v);
}
use of easik.sketch.edge.InjectiveEdge 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);
}
use of easik.sketch.edge.InjectiveEdge 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;
}
}
Aggregations