-
-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds an
!elements
keyword that can be used to find a set of element…
…s via an expression.
- Loading branch information
1 parent
52ec9be
commit e30f5e8
Showing
8 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
structurizr-dsl/src/main/java/com/structurizr/dsl/ElementsDslContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.structurizr.dsl; | ||
|
||
import com.structurizr.model.Element; | ||
import com.structurizr.model.ModelItem; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
class ElementsDslContext extends ModelItemsDslContext { | ||
|
||
private final Set<Element> elements; | ||
|
||
ElementsDslContext(DslContext parentDslContext, Set<Element> elements) { | ||
super(parentDslContext); | ||
|
||
this.elements = elements; | ||
} | ||
|
||
Set<Element> getElements() { | ||
return elements; | ||
} | ||
|
||
@Override | ||
Set<ModelItem> getModelItems() { | ||
return elements.stream().map(e -> (ModelItem)e).collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
protected String[] getPermittedTokens() { | ||
return new String[0]; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
structurizr-dsl/src/main/java/com/structurizr/dsl/ElementsParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.structurizr.dsl; | ||
|
||
import com.structurizr.model.Element; | ||
import com.structurizr.model.ModelItem; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
final class ElementsParser extends AbstractParser { | ||
|
||
private static final String GRAMMAR = "!elements <expression>"; | ||
|
||
private final static int EXPRESSION_INDEX = 1; | ||
|
||
Set<Element> parse(DslContext context, Tokens tokens) { | ||
// !elements <expression> | ||
|
||
if (tokens.hasMoreThan(EXPRESSION_INDEX)) { | ||
throw new RuntimeException("Too many tokens, expected: " + GRAMMAR); | ||
} | ||
|
||
String expression = tokens.get(1); | ||
Set<ModelItem> modelItems = new ExpressionParser().parseExpression(expression, context); | ||
|
||
return modelItems.stream().filter(mi -> mi instanceof Element).map(mi -> (Element)mi).collect(Collectors.toSet()); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
structurizr-dsl/src/main/java/com/structurizr/dsl/ModelItemsDslContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.structurizr.dsl; | ||
|
||
import com.structurizr.model.ModelItem; | ||
|
||
import java.util.Set; | ||
|
||
abstract class ModelItemsDslContext extends DslContext { | ||
|
||
private final DslContext parentDslContext; | ||
|
||
ModelItemsDslContext(DslContext parentDslContext) { | ||
this.parentDslContext = parentDslContext; | ||
} | ||
|
||
DslContext getParentDslContext() { | ||
return parentDslContext; | ||
} | ||
|
||
abstract Set<ModelItem> getModelItems(); | ||
|
||
@Override | ||
protected String[] getPermittedTokens() { | ||
return new String[0]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
structurizr-dsl/src/test/java/com/structurizr/dsl/ElementsParserTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.structurizr.dsl; | ||
|
||
import com.structurizr.model.Component; | ||
import com.structurizr.model.Container; | ||
import com.structurizr.model.Element; | ||
import com.structurizr.model.SoftwareSystem; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class ElementsParserTests extends AbstractTests { | ||
|
||
private final ElementsParser parser = new ElementsParser(); | ||
|
||
@Test | ||
void test_parse_ThrowsAnException_WhenThereAreTooManyTokens() { | ||
try { | ||
parser.parse(context(), tokens("!elements", "expression", "tokens")); | ||
fail(); | ||
} catch (Exception e) { | ||
assertEquals("Too many tokens, expected: !elements <expression>", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
void test_parse_FindsElementsByExpression() { | ||
Container application = model.addSoftwareSystem("Software System").addContainer("Application"); | ||
Component componentA = application.addComponent("A"); | ||
Component componentB = application.addComponent("B"); | ||
Component componentC = application.addComponent("C"); | ||
|
||
ModelItemDslContext context = new ContainerDslContext(application); | ||
context.setWorkspace(workspace); | ||
IdentifiersRegister register = new IdentifiersRegister(); | ||
register.register("application", application); | ||
context.setIdentifierRegister(register); | ||
|
||
Set<Element> elements = parser.parse(context, tokens("!elements", "element.parent==application")); | ||
assertTrue(elements.contains(componentA)); | ||
assertTrue(elements.contains(componentB)); | ||
assertTrue(elements.contains(componentC)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters