File tree Expand file tree Collapse file tree 4 files changed +85
-2
lines changed
main/java/com/structurizr
test/java/com/structurizr Expand file tree Collapse file tree 4 files changed +85
-2
lines changed Original file line number Diff line number Diff line change @@ -310,6 +310,36 @@ void remove(DeploymentNode deploymentNode) {
310310 }
311311 }
312312
313+ /**
314+ * Removes a relationship from the workspace.
315+ *
316+ * @param relationship the Relationship to remove
317+ */
318+ public void remove (Relationship relationship ) {
319+ if (relationship == null ) {
320+ throw new IllegalArgumentException ("A relationship must be specified." );
321+ }
322+
323+ // remove the relationship from views
324+ for (View view : viewSet .getViews ()) {
325+ if (view instanceof ModelView ) {
326+ ModelView modelView = (ModelView )view ;
327+ if (modelView .isRelationshipInView (relationship )) {
328+ modelView .remove (relationship );
329+ }
330+ }
331+ }
332+
333+ // now remove the relationship itself
334+ try {
335+ Method method = Model .class .getDeclaredMethod ("remove" , Relationship .class );
336+ method .setAccessible (true );
337+ method .invoke (model , relationship );
338+ } catch (Exception e ) {
339+ throw new RuntimeException (e );
340+ }
341+ }
342+
313343 private boolean isElementAssociatedWithAnyViews (Element element ) {
314344 boolean result = false ;
315345
Original file line number Diff line number Diff line change @@ -1171,13 +1171,22 @@ private void removeElement(Element element) {
11711171 // remove any relationships to/from the element
11721172 for (Relationship relationship : getRelationships ()) {
11731173 if (relationship .getSource () == element || relationship .getDestination () == element ) {
1174- removeRelationshipFromInternalStructures (relationship );
1175- relationship .getSource ().remove (relationship );
1174+ remove (relationship );
11761175 }
11771176 }
11781177
11791178 elementsById .remove (element .getId ());
11801179 elements .remove (element );
11811180 }
11821181
1182+ /**
1183+ * Removes a relationship from the model.
1184+ *
1185+ * @param relationship the Relationship to remove
1186+ */
1187+ void remove (Relationship relationship ) {
1188+ removeRelationshipFromInternalStructures (relationship );
1189+ relationship .getSource ().remove (relationship );
1190+ }
1191+
11831192}
Original file line number Diff line number Diff line change @@ -272,10 +272,26 @@ protected RelationshipView addRelationship(Relationship relationship) {
272272 return null ;
273273 }
274274
275+ /**
276+ * Determines whether the specified element exists in this view.
277+ *
278+ * @param element the Element to look for
279+ * @return true if the element exists in the view, false otherwise
280+ */
275281 public boolean isElementInView (Element element ) {
276282 return this .elementViews .stream ().anyMatch (ev -> ev .getElement ().equals (element ));
277283 }
278284
285+ /**
286+ * Determines whether the specified relationship exists in this view.
287+ *
288+ * @param relationship the Relationship to look for
289+ * @return true if the relationship exists in the view, false otherwise
290+ */
291+ public boolean isRelationshipInView (Relationship relationship ) {
292+ return this .relationshipViews .stream ().anyMatch (rv -> rv .getRelationship ().equals (relationship ));
293+ }
294+
279295 /**
280296 * Removes a relationship from this view.
281297 *
Original file line number Diff line number Diff line change @@ -300,4 +300,32 @@ void trim_WhenTheDestinationOfAnElementIsRemoved() {
300300 assertEquals (0 , a .getRelationships ().size ());
301301 }
302302
303+ @ Test
304+ void removeRelationship_ThrowsAnException_WhenNoRelationshipIsSpecified () {
305+ Workspace workspace = new Workspace ("Name" , "Description" );
306+ try {
307+ workspace .remove ((Relationship )null );
308+ fail ();
309+ } catch (Exception e ) {
310+ assertEquals ("A relationship must be specified." , e .getMessage ());
311+ }
312+ }
313+
314+ @ Test
315+ void removeRelationship () {
316+ Workspace workspace = new Workspace ("Name" , "Description" );
317+ SoftwareSystem a = workspace .getModel ().addSoftwareSystem ("A" );
318+ SoftwareSystem b = workspace .getModel ().addSoftwareSystem ("B" );
319+ Relationship relationship = a .uses (b , "Uses" );
320+
321+ SystemLandscapeView view = workspace .getViews ().createSystemLandscapeView ("key" , "Description" );
322+ view .addDefaultElements ();
323+
324+ workspace .remove (relationship );
325+
326+ assertEquals (0 , a .getRelationships ().size ());
327+ assertFalse (a .hasEfferentRelationshipWith (b ));
328+ assertFalse (view .isRelationshipInView (relationship ));
329+ }
330+
303331}
You can’t perform that action at this time.
0 commit comments