Skip to content

Implement support for relationship!= expressions #378

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ bin
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# MacOS files
.DS_Store

structurizr-dsl/src/test/resources/dsl/spring-petclinic/.structurizr
structurizr-dsl/src/test/resources/dsl/spring-petclinic/workspace.json

**/structurizr.properties
**/structurizr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class ExpressionParser {
static boolean isExpression(String token) {
token = token.toLowerCase();

return
token.startsWith(ELEMENT_EQUALS_EXPRESSION.toLowerCase()) ||
return token.startsWith(ELEMENT_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(ELEMENT_NOT_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(ELEMENT_TYPE_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(ELEMENT_TAG_EQUALS_EXPRESSION.toLowerCase()) ||
Expand All @@ -32,11 +31,13 @@ static boolean isExpression(String token) {
token.startsWith(RELATIONSHIP_TAG_NOT_EQUALS_EXPRESSION.toLowerCase()) ||
token.matches(RELATIONSHIP_PROPERTY_EQUALS_EXPRESSION) ||
token.startsWith(RELATIONSHIP_SOURCE_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP_SOURCE_NOT_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP_EQUALS_EXPRESSION);
token.startsWith(RELATIONSHIP_DESTINATION_NOT_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP_EQUALS_EXPRESSION) ||
token.startsWith(RELATIONSHIP_NOT_EQUALS_EXPRESSION);
}


final Set<ModelItem> parseExpression(String expr, DslContext context) {
if (expr.contains(" && ")) {
String[] expressions = expr.split(" && ");
Expand Down Expand Up @@ -90,15 +91,20 @@ private Set<ModelItem> evaluateExpression(String expr, DslContext context) {
}
});
}
} else if (expr.startsWith(RELATIONSHIP_EQUALS_EXPRESSION)) {
} else if (expr.startsWith(RELATIONSHIP_EQUALS_EXPRESSION)
|| expr.startsWith(RELATIONSHIP_NOT_EQUALS_EXPRESSION)) {
Boolean negate = expr.startsWith(RELATIONSHIP_NOT_EQUALS_EXPRESSION);
expr = expr.substring(RELATIONSHIP_EQUALS_EXPRESSION.length());

if (WILDCARD.equals(expr)) {
if (negate) {
throw new RuntimeException("The relationship \"*\" cannot be negated");
}
expr = WILDCARD + RELATIONSHIP + WILDCARD;
}

if (isExpression(expr)) {
modelItems.addAll(evaluateExpression(expr, context));
modelItems.addAll(evaluateExpression(negate ? "!" + expr : expr, context));
} else {
modelItems.addAll(parseIdentifier(expr, context));
}
Expand Down Expand Up @@ -145,12 +151,15 @@ private Set<ModelItem> evaluateExpression(String expr, DslContext context) {
}
}
} else if (expr.contains(RELATIONSHIP)) {
Boolean negate = expr.startsWith("!");
String[] identifiers = expr.split(RELATIONSHIP);
String sourceIdentifier = identifiers[0].trim();
String sourceIdentifier = identifiers[0].trim().replaceFirst("^!", "");
String destinationIdentifier = identifiers[1].trim();

String sourceExpression = RELATIONSHIP_SOURCE_EQUALS_EXPRESSION + sourceIdentifier;
String destinationExpression = RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION + destinationIdentifier;
String sourceExpression = (negate ? RELATIONSHIP_SOURCE_NOT_EQUALS_EXPRESSION
: RELATIONSHIP_SOURCE_EQUALS_EXPRESSION) + sourceIdentifier;
String destinationExpression = (negate ? RELATIONSHIP_DESTINATION_NOT_EQUALS_EXPRESSION
: RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION) + destinationIdentifier;

if (WILDCARD.equals(sourceIdentifier) && WILDCARD.equals(destinationIdentifier)) {
modelItems.addAll(context.getWorkspace().getModel().getRelationships());
Expand Down Expand Up @@ -235,7 +244,8 @@ private Set<ModelItem> evaluateExpression(String expr, DslContext context) {
modelItems.add(relationship);
}
});
} else if (expr.startsWith(RELATIONSHIP_SOURCE_EQUALS_EXPRESSION)) {
} else if (expr.startsWith(RELATIONSHIP_SOURCE_EQUALS_EXPRESSION) ||
expr.startsWith(RELATIONSHIP_SOURCE_NOT_EQUALS_EXPRESSION)) {
String identifier = expr.substring(RELATIONSHIP_SOURCE_EQUALS_EXPRESSION.length());
Set<Element> sourceElements = new HashSet<>();

Expand All @@ -259,12 +269,15 @@ private Set<ModelItem> evaluateExpression(String expr, DslContext context) {
}
}

Boolean negate = expr.startsWith(RELATIONSHIP_SOURCE_NOT_EQUALS_EXPRESSION);
context.getWorkspace().getModel().getRelationships().forEach(relationship -> {
if (sourceElements.contains(relationship.getSource())) {
Boolean condition = sourceElements.contains(relationship.getSource());
if (negate ? !condition : condition) {
modelItems.add(relationship);
}
});
} else if (expr.startsWith(RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION)) {
} else if (expr.startsWith(RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION) ||
expr.startsWith(RELATIONSHIP_DESTINATION_NOT_EQUALS_EXPRESSION)) {
String identifier = expr.substring(RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION.length());
Set<Element> destinationElements = new HashSet<>();

Expand All @@ -288,8 +301,10 @@ private Set<ModelItem> evaluateExpression(String expr, DslContext context) {
}
}

Boolean negate = expr.startsWith(RELATIONSHIP_DESTINATION_NOT_EQUALS_EXPRESSION);
context.getWorkspace().getModel().getRelationships().forEach(relationship -> {
if (destinationElements.contains(relationship.getDestination())) {
Boolean condition = destinationElements.contains(relationship.getDestination());
if (negate ? !condition : condition) {
modelItems.add(relationship);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ class StructurizrDslExpressions {
static final String RELATIONSHIP_PROPERTY_EQUALS_EXPRESSION = "relationship\\.properties\\[.*]==.*";

static final String RELATIONSHIP_SOURCE_EQUALS_EXPRESSION = "relationship.source==";
static final String RELATIONSHIP_SOURCE_NOT_EQUALS_EXPRESSION = "relationship.source!=";
static final String RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION = "relationship.destination==";
static final String RELATIONSHIP_DESTINATION_NOT_EQUALS_EXPRESSION = "relationship.destination!=";

static final String RELATIONSHIP_EQUALS_EXPRESSION = "relationship==";
static final String RELATIONSHIP_NOT_EQUALS_EXPRESSION = "relationship!=";

static final String RELATIONSHIP = "->";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.structurizr.view.SystemLandscapeView;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -53,6 +54,19 @@ void test_parseExpression_ThrowsAnException_WhenTheRelationshipDestinationIsSpec
}
}

@Test
void test_parseExpression_ThrowsAnException_WhenTheWildcardRelationshipIsNegated() {
try {
SystemLandscapeViewDslContext context = new SystemLandscapeViewDslContext(null);
context.setWorkspace(workspace);

parser.parseExpression("relationship!=*", context);
fail();
} catch (Exception e) {
assertEquals("The relationship \"*\" cannot be negated", e.getMessage());
}
}

@Test
void test_parseExpression_ThrowsAnException_WhenTheRelationshipDestinationIsSpecifiedUsingShortSyntaxButDoesNotExist() {
try {
Expand Down Expand Up @@ -137,6 +151,10 @@ void test_parseExpression_ReturnsARelationship_WhenTheRelationshipSourceIsSpecif
assertEquals(1, relationships.size());
assertTrue(relationships.contains(aToB));

relationships = parser.parseExpression("relationship!=a->*", context);
assertEquals(1, relationships.size());
assertTrue(relationships.contains(bToC));

relationships = parser.parseExpression("a -> *", context);
assertEquals(1, relationships.size());
assertTrue(relationships.contains(aToB));
Expand All @@ -149,6 +167,7 @@ void test_parseExpression_ReturnsARelationship_WhenTheRelationshipDestinationIsS
SoftwareSystem c = model.addSoftwareSystem("C");
Relationship aToB = a.uses(b, "Uses");
Relationship bToC = b.uses(c, "Uses");
Relationship aToC = a.uses(c, "Uses");

SystemLandscapeViewDslContext context = new SystemLandscapeViewDslContext(null);
context.setWorkspace(workspace);
Expand All @@ -162,6 +181,10 @@ void test_parseExpression_ReturnsARelationship_WhenTheRelationshipDestinationIsS
Set<ModelItem> relationships = parser.parseExpression("relationship.destination==b", context);
assertEquals(1, relationships.size());
assertTrue(relationships.contains(aToB));

relationships = parser.parseExpression("relationship.destination!=b", context);
assertEquals(2, relationships.size());
assertTrue(relationships.containsAll(Arrays.asList(aToC, bToC)));
}

@Test
Expand All @@ -185,6 +208,10 @@ void test_parseExpression_ReturnsARelationship_WhenTheRelationshipDestinationIsS
assertEquals(1, relationships.size());
assertTrue(relationships.contains(aToB));

relationships = parser.parseExpression("relationship!=*->b", context);
assertEquals(1, relationships.size());
assertTrue(relationships.contains(bToC));

relationships = parser.parseExpression("* -> b", context);
assertEquals(1, relationships.size());
assertTrue(relationships.contains(aToB));
Expand All @@ -207,9 +234,15 @@ void test_parseExpression_ReturnsARelationship_WhenTheRelationshipSourceAndDesti
elements.register("c", c);
context.setIdentifierRegister(elements);

Set<ModelItem> relationships = parser.parseExpression("relationship.source==a && relationship.destination==b", context);
Set<ModelItem> relationships = parser.parseExpression("relationship.source==a && relationship.destination==b",
context);
assertEquals(1, relationships.size());
assertTrue(relationships.contains(aToB));

relationships = parser.parseExpression("relationship.source!=a && relationship.destination!=b",
context);
assertEquals(1, relationships.size());
assertTrue(relationships.contains(bToC));
}

@Test
Expand All @@ -232,6 +265,10 @@ void test_parseExpression_ReturnsARelationship_WhenTheRelationshipSourceAndDesti
Set<ModelItem> relationships = parser.parseExpression("relationship==a->b", context);
assertEquals(1, relationships.size());
assertTrue(relationships.contains(aToB));

relationships = parser.parseExpression("relationship!=a->b", context);
assertEquals(1, relationships.size());
assertTrue(relationships.contains(bToC));
}

@Test
Expand Down Expand Up @@ -584,4 +621,4 @@ void test_parseExpression_ReturnsElements_WhenUsingElementNotEqualsExpression()
assertTrue(elements.contains(c));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,56 @@ void test_parseExclude_RemovesTheRelationshipFromAView_WhenAnExpressionIsSpecifi
assertEquals(0, view.getRelationships().size());
}

@Test
void test_parseExclude_RemovesAllRelationshipsExceptSpecificSourceWhenExpressionIsSpecifiedWithSource() {
Person user = model.addPerson("User", "Description");
SoftwareSystem softwareSystem1 = model.addSoftwareSystem("Software System", "Description");
user.uses(softwareSystem1, "Uses");
SoftwareSystem softwareSystem2 = model.addSoftwareSystem("Software System 2", "Description");
user.uses(softwareSystem2, "Uses");
softwareSystem1.uses(softwareSystem2, "Uses");

SystemContextView view = views.createSystemContextView(softwareSystem1, "key", "Description");
view.addDefaultElements();
SystemContextViewDslContext context = new SystemContextViewDslContext(view);
context.setWorkspace(workspace);

IdentifiersRegister elements = new IdentifiersRegister();
elements.register("user", user);
elements.register("softwaresystem1", softwareSystem1);
elements.register("softwaresystem2", softwareSystem2);
context.setIdentifierRegister(elements);

parser.parseExclude(context, tokens("exclude", "relationship.source!=user"));
assertEquals(3, view.getElements().size());
assertEquals(2, view.getRelationships().size());
}

@Test
void test_parseExclude_RemovesAllRelationshipsExceptSpecificSourceWhenExpressionIsSpecifiedWithWildcard() {
Person user = model.addPerson("User", "Description");
SoftwareSystem softwareSystem1 = model.addSoftwareSystem("Software System", "Description");
user.uses(softwareSystem1, "Uses");
SoftwareSystem softwareSystem2 = model.addSoftwareSystem("Software System 2", "Description");
user.uses(softwareSystem2, "Uses");
softwareSystem1.uses(softwareSystem2, "Uses");

SystemContextView view = views.createSystemContextView(softwareSystem1, "key", "Description");
view.addDefaultElements();
SystemContextViewDslContext context = new SystemContextViewDslContext(view);
context.setWorkspace(workspace);

IdentifiersRegister elements = new IdentifiersRegister();
elements.register("user", user);
elements.register("softwaresystem1", softwareSystem1);
elements.register("softwaresystem2", softwareSystem2);
context.setIdentifierRegister(elements);

parser.parseExclude(context, tokens("exclude", "relationship!=user->*"));
assertEquals(3, view.getElements().size());
assertEquals(2, view.getRelationships().size());
}

@Test
void test_parseExclude_RemovesTheRelationshipFromAView_WhenAnExpressionIsSpecifiedWithWildcardAndDestination() {
Person user = model.addPerson("User", "Description");
Expand All @@ -407,6 +457,56 @@ void test_parseExclude_RemovesTheRelationshipFromAView_WhenAnExpressionIsSpecifi
assertEquals(0, view.getRelationships().size());
}

@Test
void test_parseExclude_RemovesAllRelationshipsExceptSpecificDestinationWhenExpressionIsSpecifiedWithDestination() {
Person user = model.addPerson("User", "Description");
SoftwareSystem softwareSystem1 = model.addSoftwareSystem("Software System", "Description");
user.uses(softwareSystem1, "Uses");
SoftwareSystem softwareSystem2 = model.addSoftwareSystem("Software System 2", "Description");
user.uses(softwareSystem2, "Uses");
softwareSystem1.uses(softwareSystem2, "Uses");

SystemContextView view = views.createSystemContextView(softwareSystem1, "key", "Description");
view.addDefaultElements();
SystemContextViewDslContext context = new SystemContextViewDslContext(view);
context.setWorkspace(workspace);

IdentifiersRegister elements = new IdentifiersRegister();
elements.register("user", user);
elements.register("softwaresystem1", softwareSystem1);
elements.register("softwaresystem2", softwareSystem2);
context.setIdentifierRegister(elements);

parser.parseExclude(context, tokens("exclude", "relationship.destination!=softwareSystem2"));
assertEquals(3, view.getElements().size());
assertEquals(2, view.getRelationships().size());
}

@Test
void test_parseExclude_RemovesAllRelationshipsExceptSpecificDestinationWhenExpressionIsSpecifiedWithWildcard() {
Person user = model.addPerson("User", "Description");
SoftwareSystem softwareSystem1 = model.addSoftwareSystem("Software System", "Description");
user.uses(softwareSystem1, "Uses");
SoftwareSystem softwareSystem2 = model.addSoftwareSystem("Software System 2", "Description");
user.uses(softwareSystem2, "Uses");
softwareSystem1.uses(softwareSystem2, "Uses");

SystemContextView view = views.createSystemContextView(softwareSystem1, "key", "Description");
view.addDefaultElements();
SystemContextViewDslContext context = new SystemContextViewDslContext(view);
context.setWorkspace(workspace);

IdentifiersRegister elements = new IdentifiersRegister();
elements.register("user", user);
elements.register("softwaresystem1", softwareSystem1);
elements.register("softwaresystem2", softwareSystem2);
context.setIdentifierRegister(elements);

parser.parseExclude(context, tokens("exclude", "relationship!=*->softwareSystem2"));
assertEquals(3, view.getElements().size());
assertEquals(2, view.getRelationships().size());
}

@Test
void test_parseExclude_RemovesTheRelationshipFromAView_WhenAnExpressionIsSpecifiedWithWildcardAndWildcard() {
Person user = model.addPerson("User", "Description");
Expand Down