Search in sources :

Example 1 with DynamicView

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);
}
Also used : StaticStructureElement(com.structurizr.model.StaticStructureElement) StaticStructureElement(com.structurizr.model.StaticStructureElement) Element(com.structurizr.model.Element) DynamicView(com.structurizr.view.DynamicView)

Example 2 with DynamicView

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;
}
Also used : Container(com.structurizr.model.Container) DecimalFormat(java.text.DecimalFormat) Element(com.structurizr.model.Element) SoftwareSystem(com.structurizr.model.SoftwareSystem) DynamicView(com.structurizr.view.DynamicView) Workspace(com.structurizr.Workspace)

Example 3 with DynamicView

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());
}
Also used : RelationshipView(com.structurizr.view.RelationshipView) SoftwareSystem(com.structurizr.model.SoftwareSystem) DynamicView(com.structurizr.view.DynamicView) Person(com.structurizr.model.Person) Test(org.junit.jupiter.api.Test)

Example 4 with DynamicView

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());
}
Also used : RelationshipView(com.structurizr.view.RelationshipView) Relationship(com.structurizr.model.Relationship) SoftwareSystem(com.structurizr.model.SoftwareSystem) DynamicView(com.structurizr.view.DynamicView) Person(com.structurizr.model.Person) Test(org.junit.jupiter.api.Test)

Example 5 with DynamicView

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());
}
Also used : ArrayList(java.util.ArrayList) SoftwareSystem(com.structurizr.model.SoftwareSystem) DynamicView(com.structurizr.view.DynamicView) Test(org.junit.jupiter.api.Test)

Aggregations

DynamicView (com.structurizr.view.DynamicView)16 Test (org.junit.jupiter.api.Test)13 SoftwareSystem (com.structurizr.model.SoftwareSystem)8 ArrayList (java.util.ArrayList)6 Container (com.structurizr.model.Container)4 Person (com.structurizr.model.Person)4 RelationshipView (com.structurizr.view.RelationshipView)4 Element (com.structurizr.model.Element)2 Relationship (com.structurizr.model.Relationship)2 Workspace (com.structurizr.Workspace)1 StructurizrDslParser (com.structurizr.dsl.StructurizrDslParser)1 StructurizrDslParserException (com.structurizr.dsl.StructurizrDslParserException)1 Diagram (com.structurizr.export.Diagram)1 StructurizrPlantUMLExporter (com.structurizr.export.plantuml.StructurizrPlantUMLExporter)1 StaticStructureElement (com.structurizr.model.StaticStructureElement)1 ComponentView (com.structurizr.view.ComponentView)1 ContainerView (com.structurizr.view.ContainerView)1 DeploymentView (com.structurizr.view.DeploymentView)1 SystemContextView (com.structurizr.view.SystemContextView)1 SystemLandscapeView (com.structurizr.view.SystemLandscapeView)1