use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class Sketch method addNewNode.
/**
* Add a new, empty entity at point X, Y
*
* @param name
* The name of the new entity being added
* @param x
* X Coordinate of new entity
* @param y
* Y Coordinate of new entity
*/
@Override
public void addNewNode(final String name, final double x, final double y) {
final EntityNode newEntity;
newEntity = new EntityNode(name, (int) x, (int) y, this);
addEntity(newEntity);
}
use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class Sketch method addEntity.
/**
* Adds a collection (set, list, etc.) of EntityNodes to the graph.
*
* @param theEntities
* the collection of entities to be added.
*/
public void addEntity(final Collection<EntityNode> theEntities) {
// Push loading state
_stateManager.pushState(new LoadingState<>(this));
final GraphLayoutCache glc = getGraphLayoutCache();
model.beginUpdate();
for (final EntityNode node : theEntities) {
// Set the on-screen position of our entity to the attributes of the
// entity
final AttributeMap nAttribs = node.getAttributes();
GraphConstants.setAutoSize(nAttribs, true);
GraphConstants.setBounds(nAttribs, new Rectangle2D.Double(node.getX(), node.getY(), 0, 0));
if (_nodes.containsKey(node.getName())) {
node.setName(node.getName());
}
// Add our entity to the graph
glc.insert(node);
// Add our entity to our table of entities
_nodes.put(node.getName(), node);
// Add Entity to tree
_Frame.getInfoTreeUI().addNode(node);
}
model.postEdit(new AbstractUndoableEdit() {
/**
*/
private static final long serialVersionUID = -74767611415529681L;
@Override
public void undo() {
super.undo();
for (final EntityNode node : theEntities) {
_nodes.remove(node.getName());
}
}
@Override
public void redo() {
super.redo();
for (final EntityNode node : theEntities) {
_nodes.put(node.getName(), node);
}
}
});
model.endUpdate();
// Reload the graph to respect the new changes
glc.reload();
// Pop state
_stateManager.popState();
}
use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class Sketch method removeEdge.
/**
* Removes an edge, also cascades to remove all constraints using it.
*
* @param toRemove
* The edge about to be removed
*/
public void removeEdge(final SketchEdge toRemove) {
model.beginUpdate();
// Check for constraints that need these edges
for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : _constraints.values()) {
if (c.hasEdge(toRemove)) {
removeConstraint(c);
}
}
if (toRemove instanceof UniqueIndexable) {
final EntityNode source = toRemove.getSourceEntity();
boolean needCleanup = false;
for (final UniqueKey<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> q : source.getUniqueKeys()) {
if (q.removeElement((UniqueIndexable) toRemove)) {
needCleanup = true;
}
}
if (needCleanup) {
source.cleanup();
}
// FIXME: undo: unique key cleanup stuff
}
_edges.remove(toRemove.getName());
toRemove.getSourceEntity().removeDepend(toRemove.getTargetEntity());
model.postEdit(new AbstractUndoableEdit() {
/**
*/
private static final long serialVersionUID = 711022413236293938L;
@Override
public void undo() {
super.undo();
_edges.put(toRemove.getName(), toRemove);
toRemove.getSourceEntity().addDepend(toRemove.getTargetEntity());
}
@Override
public void redo() {
super.redo();
_edges.remove(toRemove.getName());
toRemove.getSourceEntity().removeDepend(toRemove.getTargetEntity());
}
});
getGraphLayoutCache().remove(new Object[] { toRemove });
model.endUpdate();
KosarajuSCC s = new KosarajuSCC(this);
strongConnected = s.getSCC();
}
use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class Sketch method multPathWarning.
/**
* Method that checks if there are paths with the same domain and codomain
* and then sees if they mismatch. We only care about when one of the paths
* is all cascade and the other path contains an edge which is not cascade.
* In this case we will warn the user.
*
* @return String warning
* @author Federico Mora
*/
private String multPathWarning() {
boolean cascade = false;
boolean other = false;
String warning = "";
// want to look at paths from every node to every other node
for (EntityNode s : _nodes.values()) {
for (EntityNode t : _nodes.values()) {
if (s != t) {
cascade = false;
other = false;
// System.out.println("Finding paths from " + s.getName() +
// " to " + t.getName());
ArrayList<SketchEdge> startEdges = new ArrayList<>();
ArrayList<SketchEdge> endEdges = new ArrayList<>();
for (SketchEdge edge : _edges.values()) {
if (edge.getSourceEntity() == s) {
startEdges.add(edge);
}
if (edge.getTargetEntity() == t) {
endEdges.add(edge);
}
}
// care
if (startEdges.size() < 2 || endEdges.size() < 2) {
// + t.getName());
continue;
}
ArrayList<ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> paths = new ArrayList<>();
ArrayList<LinkedList<SketchEdge>> temp = buildpath(s, t);
for (LinkedList<SketchEdge> q : temp) {
if (q.peekLast().getTargetEntity() != t) {
q.clear();
} else {
paths.add(new ModelPath<>(q));
}
}
if (paths.size() > 1) {
for (ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p : paths) {
if (p.isCompositeCascade()) {
cascade = true;
} else {
other = true;
}
}
if (cascade && other) {
warning += this.getName() + " contains multiple paths from " + s.getName() + " to " + t.getName() + "\n";
for (ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p : paths) {
if (p.isCompositeCascade()) {
warning += " " + p.toString() + " is Aggregate Cascade\n";
} else {
warning += " " + p.toString() + " is Aggregate Other\n";
}
}
}
}
}
}
}
return warning;
}
use of easik.sketch.vertex.EntityNode 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;
}
}
Aggregations