Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfixes for 2.0 #219

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading