diff --git a/docs/changelog.md b/docs/changelog.md index 89d4ebd..6197a7c 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,9 @@ # Changelog +## (unreleased to Maven Central) + +- Adds support for more easily including/excluding implied relationships in view definitions (https://github.com/structurizr/dsl/issues/303). + ## 1.30.3 (4th July 2023) - Fixes https://github.com/structurizr/dsl/issues/289 (Cannot invoke "Object.equals(Object)" because "r" is null). diff --git a/src/main/java/com/structurizr/dsl/AbstractExpressionParser.java b/src/main/java/com/structurizr/dsl/AbstractExpressionParser.java index 5785ef6..07f7a36 100644 --- a/src/main/java/com/structurizr/dsl/AbstractExpressionParser.java +++ b/src/main/java/com/structurizr/dsl/AbstractExpressionParser.java @@ -313,6 +313,9 @@ protected Set parseIdentifier(String identifier, DslContext context) Relationship relationship = context.getRelationship(identifier); if (relationship != null) { modelItems.add(relationship); + + // and also find all relationships linked to it (i.e. implied and replicated relationships) + relationship.getModel().getRelationships().stream().filter(r -> relationship.getId().equals(r.getLinkedRelationshipId())).forEach(modelItems::add); } if (modelItems.isEmpty()) { diff --git a/src/test/dsl/exclude-implied-relationship.dsl b/src/test/dsl/exclude-implied-relationship.dsl new file mode 100644 index 0000000..ec8370d --- /dev/null +++ b/src/test/dsl/exclude-implied-relationship.dsl @@ -0,0 +1,22 @@ +workspace { + + model { + softwareSystem "A" { + a = container "A" + } + + softwareSystem "B" { + b = container "B" + } + + r = a -> b + } + + views { + systemLandscape { + include * + exclude r + } + } + +} \ No newline at end of file diff --git a/src/test/dsl/include-implied-relationship.dsl b/src/test/dsl/include-implied-relationship.dsl new file mode 100644 index 0000000..86757f9 --- /dev/null +++ b/src/test/dsl/include-implied-relationship.dsl @@ -0,0 +1,23 @@ +workspace { + + model { + softwareSystem "A" { + a = container "A" + } + + softwareSystem "B" { + b = container "B" + } + + r = a -> b + } + + views { + systemLandscape { + include * + exclude *->* + include r + } + } + +} \ No newline at end of file diff --git a/src/test/java/com/structurizr/dsl/DslTests.java b/src/test/java/com/structurizr/dsl/DslTests.java index aab3151..369c703 100644 --- a/src/test/java/com/structurizr/dsl/DslTests.java +++ b/src/test/java/com/structurizr/dsl/DslTests.java @@ -1096,4 +1096,22 @@ void test_RelationshipAlreadyExists() throws Exception { } } + @Test + void test_ExcludeImpliedRelationship() throws Exception { + StructurizrDslParser parser = new StructurizrDslParser(); + parser.parse(new File("src/test/dsl/exclude-implied-relationship.dsl")); + + // check the system landscape view doesn't include any relationships + assertEquals(0, parser.getWorkspace().getViews().getSystemLandscapeViews().iterator().next().getRelationships().size()); + } + + @Test + void test_IncludeImpliedRelationship() throws Exception { + StructurizrDslParser parser = new StructurizrDslParser(); + parser.parse(new File("src/test/dsl/include-implied-relationship.dsl")); + + // check the system landscape view includes a relationship + assertEquals(1, parser.getWorkspace().getViews().getSystemLandscapeViews().iterator().next().getRelationships().size()); + } + } \ No newline at end of file