Skip to content

Commit

Permalink
structurizr-dsl: Adds support for element!= expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Nov 23, 2024
1 parent 0351c2d commit 69b7d39
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ subprojects { proj ->

description = 'Structurizr'
group = 'com.structurizr'
version = '3.1.0'
version = '3.2.0'

repositories {
mavenCentral()
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.2.0 (unreleased)

- structurizr-dsl: Adds support for `element!=` expressions.

## 3.1.0 (4th November 2024)

- structurizr-client: Workspace archive file now includes the branch name in the filename.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ static boolean isExpression(String token) {
token = token.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()) ||
token.startsWith(ELEMENT_TAG_NOT_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(ELEMENT_TECHNOLOGY_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(ELEMENT_TECHNOLOGY_NOT_EQUALS_EXPRESSION.toLowerCase()) ||
token.matches(ELEMENT_PROPERTY_EQUALS_EXPRESSION) ||
token.startsWith(ELEMENT_PARENT_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP) || token.endsWith(RELATIONSHIP) || token.contains(RELATIONSHIP) ||
token.startsWith(ELEMENT_EQUALS_EXPRESSION) ||
token.startsWith(RELATIONSHIP_TAG_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP_TAG_NOT_EQUALS_EXPRESSION.toLowerCase()) ||
token.matches(RELATIONSHIP_PROPERTY_EQUALS_EXPRESSION) ||
token.startsWith(RELATIONSHIP_SOURCE_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP_EQUALS_EXPRESSION);
token.startsWith(ELEMENT_TAG_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(ELEMENT_TAG_NOT_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(ELEMENT_TECHNOLOGY_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(ELEMENT_TECHNOLOGY_NOT_EQUALS_EXPRESSION.toLowerCase()) ||
token.matches(ELEMENT_PROPERTY_EQUALS_EXPRESSION) ||
token.startsWith(ELEMENT_PARENT_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP) || token.endsWith(RELATIONSHIP) || token.contains(RELATIONSHIP) ||
token.startsWith(RELATIONSHIP_TAG_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP_TAG_NOT_EQUALS_EXPRESSION.toLowerCase()) ||
token.matches(RELATIONSHIP_PROPERTY_EQUALS_EXPRESSION) ||
token.startsWith(RELATIONSHIP_SOURCE_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION.toLowerCase()) ||
token.startsWith(RELATIONSHIP_EQUALS_EXPRESSION);
}


Expand Down Expand Up @@ -71,6 +72,24 @@ private Set<ModelItem> evaluateExpression(String expr, DslContext context) {
} else {
modelItems.addAll(parseIdentifier(expr, context));
}
} else if (expr.startsWith(ELEMENT_NOT_EQUALS_EXPRESSION)) {
expr = expr.substring(ELEMENT_NOT_EQUALS_EXPRESSION.length());

if (isExpression(expr)) {
Set<ModelItem> mi = evaluateExpression(expr, context);
context.getWorkspace().getModel().getElements().forEach(element -> {
if (!mi.contains(element)) {
modelItems.add(element);
}
});
} else {
Set<ModelItem> mi = parseIdentifier(expr, context);
context.getWorkspace().getModel().getElements().forEach(element -> {
if (!mi.contains(element)) {
modelItems.add(element);
}
});
}
} else if (expr.startsWith(RELATIONSHIP_EQUALS_EXPRESSION)) {
expr = expr.substring(RELATIONSHIP_EQUALS_EXPRESSION.length());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class StructurizrDslExpressions {
static final String ELEMENT_PROPERTY_EQUALS_EXPRESSION = "element\\.properties\\[.*]==.*";

static final String ELEMENT_EQUALS_EXPRESSION = "element==";
static final String ELEMENT_NOT_EQUALS_EXPRESSION = "element!=";
static final String ELEMENT_PARENT_EQUALS_EXPRESSION = "element.parent==";

static final String RELATIONSHIP_TAG_EQUALS_EXPRESSION = "relationship.tag==";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,47 @@ void test_parseExpression_ReturnsRelationshipsAndImpliedRelationships_WhenUsingA
assertTrue(relationships.contains(impliedRelationship));
}

@Test
void test_parseExpression_ReturnsElements_WhenUsingElementNotEqualsIdentifier() {
SoftwareSystem a = model.addSoftwareSystem("A");
SoftwareSystem b = model.addSoftwareSystem("B");
SoftwareSystem c = model.addSoftwareSystem("C");

SystemLandscapeViewDslContext context = new SystemLandscapeViewDslContext(null);
context.setWorkspace(workspace);

IdentifiersRegister map = new IdentifiersRegister();
map.register("a", a);
map.register("b", b);
map.register("c", c);
context.setIdentifierRegister(map);

Set<ModelItem> elements = parser.parseExpression("element!=c", context);
assertEquals(2, elements.size());
assertTrue(elements.contains(a));
assertTrue(elements.contains(b));
}

@Test
void test_parseExpression_ReturnsElements_WhenUsingElementNotEqualsExpression() {
SoftwareSystem a = model.addSoftwareSystem("A");
SoftwareSystem b = model.addSoftwareSystem("B");
SoftwareSystem c = model.addSoftwareSystem("C");
Relationship aToB = a.uses(b, "Uses");
Relationship bToC = b.uses(c, "Uses");

SystemLandscapeViewDslContext context = new SystemLandscapeViewDslContext(null);
context.setWorkspace(workspace);

IdentifiersRegister map = new IdentifiersRegister();
map.register("a", a);
map.register("b", b);
map.register("c", c);
context.setIdentifierRegister(map);

Set<ModelItem> elements = parser.parseExpression("element!=->b", context);
assertEquals(1, elements.size());
assertTrue(elements.contains(c));
}

}

0 comments on commit 69b7d39

Please sign in to comment.