use of com.structurizr.view.DynamicView in project dsl by structurizr.
the class DynamicViewContentParser method parseRelationship.
void parseRelationship(DynamicViewDslContext context, Tokens tokens) {
// <identifier> -> <identifier> [description] [technology]
DynamicView view = context.getView();
if (tokens.hasMoreThan(TECHNOLOGY_INDEX)) {
throw new RuntimeException("Too many tokens, expected: " + GRAMMAR);
}
if (!tokens.includes(DESTINATION_IDENTIFIER_INDEX)) {
throw new RuntimeException("Expected: " + GRAMMAR);
}
String sourceId = tokens.get(SOURCE_IDENTIFIER_INDEX);
String destinationId = tokens.get(DESTINATION_IDENTIFIER_INDEX);
Element sourceElement = context.getElement(sourceId);
if (sourceElement == null) {
throw new RuntimeException("The source element \"" + sourceId + "\" does not exist");
}
if (!(sourceElement instanceof StaticStructureElement)) {
throw new RuntimeException("The source element \"" + sourceId + "\" should be a static structure element");
}
Element destinationElement = context.getElement(destinationId);
if (destinationElement == null) {
throw new RuntimeException("The destination element \"" + destinationId + "\" does not exist");
}
if (!(destinationElement instanceof StaticStructureElement)) {
throw new RuntimeException("The destination element \"" + destinationId + "\" should be a static structure element");
}
String description = "";
if (tokens.includes(DESCRIPTION_INDEX)) {
description = tokens.get(DESCRIPTION_INDEX);
}
String technology = "";
if (tokens.includes(TECHNOLOGY_INDEX)) {
technology = tokens.get(TECHNOLOGY_INDEX);
}
if (!sourceElement.hasEfferentRelationshipWith(destinationElement) && !destinationElement.hasEfferentRelationshipWith(sourceElement)) {
new ExplicitRelationshipParser().parse(context, tokens);
}
view.add((StaticStructureElement) sourceElement, description, technology, (StaticStructureElement) destinationElement);
}
use of com.structurizr.view.DynamicView in project dsl by structurizr.
the class DynamicViewParser method parse.
DynamicView parse(DslContext context, Tokens tokens) {
// dynamic <*|software system identifier|container identifier> [key] [description] {
Workspace workspace = context.getWorkspace();
String key = "";
String description = "";
DecimalFormat format = new DecimalFormat("000");
if (tokens.hasMoreThan(DESCRIPTION_INDEX)) {
throw new RuntimeException("Too many tokens, expected: " + GRAMMAR);
}
if (!tokens.includes(SCOPE_IDENTIFIER_INDEX)) {
throw new RuntimeException("Expected: " + GRAMMAR);
}
if (tokens.includes(DESCRIPTION_INDEX)) {
description = tokens.get(DESCRIPTION_INDEX);
}
DynamicView view;
String scopeIdentifier = tokens.get(SCOPE_IDENTIFIER_INDEX);
if (WILDCARD.equals(scopeIdentifier)) {
if (tokens.includes(KEY_INDEX)) {
key = tokens.get(KEY_INDEX);
} else {
key = VIEW_TYPE + "-" + format.format(workspace.getViews().getDynamicViews().size() + 1);
}
validateViewKey(key);
view = workspace.getViews().createDynamicView(key, description);
} else {
Element element = context.getElement(scopeIdentifier);
if (element == null) {
throw new RuntimeException("The software system or container \"" + scopeIdentifier + "\" does not exist");
}
if (element instanceof SoftwareSystem) {
if (tokens.includes(KEY_INDEX)) {
key = tokens.get(KEY_INDEX);
} else {
key = removeNonWordCharacters(element.getName()) + "-" + VIEW_TYPE + "-" + format.format(workspace.getViews().getDynamicViews().size() + 1);
}
validateViewKey(key);
view = workspace.getViews().createDynamicView((SoftwareSystem) element, key, description);
} else if (element instanceof Container) {
Container container = (Container) element;
if (tokens.includes(KEY_INDEX)) {
key = tokens.get(KEY_INDEX);
} else {
key = removeNonWordCharacters(container.getSoftwareSystem().getName()) + "-" + removeNonWordCharacters(container.getName()) + "-" + VIEW_TYPE + "-" + format.format(workspace.getViews().getDynamicViews().size() + 1);
}
validateViewKey(key);
view = workspace.getViews().createDynamicView((Container) element, key, description);
} else {
throw new RuntimeException("The element \"" + scopeIdentifier + "\" is not a software system or container");
}
}
view.setExternalBoundariesVisible(true);
return view;
}
use of com.structurizr.view.DynamicView in project dsl by structurizr.
the class DynamicViewContentParserTests method test_parseRelationship_AddsTheRelationshipToTheViewWithAnOverriddenDescription_WhenItAlreadyExistsInTheModel.
@Test
void test_parseRelationship_AddsTheRelationshipToTheViewWithAnOverriddenDescription_WhenItAlreadyExistsInTheModel() {
Person user = model.addPerson("User", "Description");
SoftwareSystem softwareSystem = model.addSoftwareSystem("Software System", "Description");
user.uses(softwareSystem, "Uses");
DynamicView view = views.createDynamicView("key", "Description");
DynamicViewDslContext context = new DynamicViewDslContext(view);
IdentifiersRegister elements = new IdentifiersRegister();
elements.register("source", user);
elements.register("destination", softwareSystem);
context.setIdentifierRegister(elements);
parser.parseRelationship(context, tokens("source", "->", "destination", "Does something with"));
assertEquals(1, view.getRelationships().size());
RelationshipView rv = view.getRelationships().iterator().next();
assertSame(user, rv.getRelationship().getSource());
assertSame(softwareSystem, rv.getRelationship().getDestination());
assertEquals("Does something with", rv.getDescription());
assertEquals("1", rv.getOrder());
}
use of com.structurizr.view.DynamicView in project dsl by structurizr.
the class DynamicViewContentParserTests method test_parseRelationship_AddsTheRelationshipToTheModelAndView_WhenItDoesNotAlreadyExistInTheModel.
@Test
void test_parseRelationship_AddsTheRelationshipToTheModelAndView_WhenItDoesNotAlreadyExistInTheModel() {
Person user = model.addPerson("User", "Description");
SoftwareSystem softwareSystem = model.addSoftwareSystem("Software System", "Description");
DynamicView view = views.createDynamicView("key", "Description");
DynamicViewDslContext context = new DynamicViewDslContext(view);
IdentifiersRegister elements = new IdentifiersRegister();
elements.register("source", user);
elements.register("destination", softwareSystem);
context.setIdentifierRegister(elements);
assertEquals(0, model.getRelationships().size());
parser.parseRelationship(context, tokens("source", "->", "destination", "Uses"));
assertEquals(1, model.getRelationships().size());
Relationship r = model.getRelationships().iterator().next();
assertSame(user, r.getSource());
assertSame(softwareSystem, r.getDestination());
assertEquals("Uses", r.getDescription());
assertEquals(1, view.getRelationships().size());
RelationshipView rv = view.getRelationships().iterator().next();
assertSame(user, rv.getRelationship().getSource());
assertSame(softwareSystem, rv.getRelationship().getDestination());
assertEquals("Uses", rv.getDescription());
assertEquals("1", rv.getOrder());
}
use of com.structurizr.view.DynamicView in project dsl by structurizr.
the class DynamicViewParserTests method test_parse_CreatesADynamicViewWithSoftwareSystemScopeAndKey.
@Test
void test_parse_CreatesADynamicViewWithSoftwareSystemScopeAndKey() {
DslContext context = context();
IdentifiersRegister elements = new IdentifiersRegister();
SoftwareSystem softwareSystem = model.addSoftwareSystem("Name", "Description");
elements.register("softwaresystem", softwareSystem);
context.setIdentifierRegister(elements);
parser.parse(context, tokens("dynamic", "softwareSystem", "key"));
List<DynamicView> views = new ArrayList<>(this.views.getDynamicViews());
assertEquals(1, views.size());
assertEquals("key", views.get(0).getKey());
assertEquals("", views.get(0).getDescription());
assertSame(softwareSystem, views.get(0).getElement());
}
Aggregations