Skip to content

Commit

Permalink
Bugfixes for 2.0 (#219)
Browse files Browse the repository at this point in the history
Fix GModelDeleteOperationHandler by updating the collectDependents method.
- Dont add child elements of elements that should be deleted to the dependent set. They will be deleted indirectly anyways because
the parent container is deleted.
- Correctly add incoming/outgoing edges to the dependet set instead of their targets/sources
  • Loading branch information
tortmayr authored Oct 13, 2023
1 parent 9e11a0b commit 93a9a18
Showing 1 changed file with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,49 +73,46 @@ protected boolean delete(final String elementId, final GModelIndex index) {
return false;
}

// Always delete the top-level node
GModelElement nodeToDelete = findTopLevelElement(element.get());
if (nodeToDelete.getParent() == null) {
// Always delete the top-level element
GModelElement elementToDelete = findTopLevelElement(element.get());
if (elementToDelete.getParent() == null) {
LOGGER.warn("The requested node doesn't have a parent; it can't be deleted");
return false; // Can't delete the root, or an element that doesn't belong to the model
}

Set<GModelElement> dependents = new LinkedHashSet<>();
collectDependents(dependents, nodeToDelete, modelState);
collectDependents(dependents, elementToDelete, modelState, false);

dependents.forEach(EcoreUtil::delete);
allDependantsIds.addAll(dependents.stream().map(GModelElement::getId).collect(Collectors.toSet()));
dependents.forEach(EcoreUtil::delete);
return true;
}

protected void collectDependents(final Set<GModelElement> dependents, final GModelElement nodeToDelete,
final GModelState modelState) {
final GModelState modelState, final boolean isChild) {

if (dependents.contains(nodeToDelete)) {
return;
}

// First, children
if (nodeToDelete.getChildren() != null) {
for (GModelElement child : nodeToDelete.getChildren()) {
collectDependents(dependents, child, modelState);
collectDependents(dependents, child, modelState, true);
}
}

// Then, incoming/outgoing edges for nodes
if (nodeToDelete instanceof GNode) {
GModelIndex index = modelState.getIndex();

// Then, incoming/outgoing links
for (GModelElement incoming : index.getIncomingEdges(nodeToDelete)) {
collectDependents(dependents, incoming, modelState);
}
for (GModelElement outgoing : index.getOutgoingEdges(nodeToDelete)) {
collectDependents(dependents, outgoing, modelState);
}
dependents.addAll(index.getIncomingEdges(nodeToDelete));
dependents.addAll(index.getOutgoingEdges(nodeToDelete));
}

// Finally, the node to delete
dependents.add(nodeToDelete);
if (!isChild) {
dependents.add(nodeToDelete);
}
}

protected GModelElement findTopLevelElement(final GModelElement element) {
Expand Down

0 comments on commit 93a9a18

Please sign in to comment.