Search in sources :

Example 1 with ModelItem

use of com.structurizr.model.ModelItem in project dsl by structurizr.

the class RefParser method parse.

ModelItem parse(DslContext context, Tokens tokens) {
    if (tokens.hasMoreThan(IDENTIFIER_INDEX)) {
        throw new RuntimeException("Too many tokens, expected: " + GRAMMAR);
    }
    if (!tokens.includes(IDENTIFIER_INDEX)) {
        throw new RuntimeException("Expected: " + GRAMMAR);
    }
    String s = tokens.get(IDENTIFIER_INDEX);
    ModelItem modelItem;
    if (s.contains("://")) {
        modelItem = context.getWorkspace().getModel().getElementWithCanonicalName(s);
    } else {
        modelItem = context.getElement(s);
        if (modelItem == null) {
            modelItem = context.getRelationship(s);
        }
    }
    if (modelItem == null) {
        throw new RuntimeException("An element/relationship referenced by \"" + s + "\" could not be found");
    }
    return modelItem;
}
Also used : ModelItem(com.structurizr.model.ModelItem)

Example 2 with ModelItem

use of com.structurizr.model.ModelItem in project dsl by structurizr.

the class RefParserTests method test_parse_FindsAnElementByCanonicalName.

@Test
void test_parse_FindsAnElementByCanonicalName() {
    Person user = workspace.getModel().addPerson("User");
    ModelItem element = parser.parse(context(), tokens("ref", "Person://User"));
    assertSame(user, element);
}
Also used : ModelItem(com.structurizr.model.ModelItem) Person(com.structurizr.model.Person) Test(org.junit.jupiter.api.Test)

Example 3 with ModelItem

use of com.structurizr.model.ModelItem in project dsl by structurizr.

the class RefParserTests method test_parse_FindsARelationshipByIdentifier.

@Test
void test_parse_FindsARelationshipByIdentifier() {
    Person user = workspace.getModel().addPerson("User");
    Relationship relationship = user.interactsWith(user, "Description");
    ModelDslContext context = context();
    IdentifiersRegister register = new IdentifiersRegister();
    register.register("rel", relationship);
    context.setIdentifierRegister(register);
    ModelItem modelItem = parser.parse(context, tokens("ref", "rel"));
    assertSame(modelItem, relationship);
}
Also used : Relationship(com.structurizr.model.Relationship) ModelItem(com.structurizr.model.ModelItem) Person(com.structurizr.model.Person) Test(org.junit.jupiter.api.Test)

Example 4 with ModelItem

use of com.structurizr.model.ModelItem in project dsl by structurizr.

the class AbstractExpressionParser method parseIdentifier.

protected Set<ModelItem> parseIdentifier(String identifier, DslContext context) {
    Set<ModelItem> modelItems = new LinkedHashSet<>();
    Element element = context.getElement(identifier);
    if (element != null) {
        modelItems.addAll(getElements(identifier, context));
    }
    Relationship relationship = context.getRelationship(identifier);
    if (relationship != null) {
        modelItems.add(relationship);
    }
    if (modelItems.isEmpty()) {
        throw new RuntimeException("The element/relationship \"" + identifier + "\" does not exist");
    } else {
        return modelItems;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Element(com.structurizr.model.Element) Relationship(com.structurizr.model.Relationship) ModelItem(com.structurizr.model.ModelItem)

Example 5 with ModelItem

use of com.structurizr.model.ModelItem in project dsl by structurizr.

the class RefParserTests method test_parse_FindsAnElementByIdentifier.

@Test
void test_parse_FindsAnElementByIdentifier() {
    Person user = workspace.getModel().addPerson("User");
    ModelDslContext context = context();
    IdentifiersRegister register = new IdentifiersRegister();
    register.register("user", user);
    context.setIdentifierRegister(register);
    ModelItem modelItem = parser.parse(context, tokens("ref", "user"));
    assertSame(modelItem, user);
}
Also used : ModelItem(com.structurizr.model.ModelItem) Person(com.structurizr.model.Person) Test(org.junit.jupiter.api.Test)

Aggregations

ModelItem (com.structurizr.model.ModelItem)6 Person (com.structurizr.model.Person)3 Relationship (com.structurizr.model.Relationship)3 Test (org.junit.jupiter.api.Test)3 Element (com.structurizr.model.Element)2 LinkedHashSet (java.util.LinkedHashSet)2 StructurizrDslExpressions (com.structurizr.dsl.StructurizrDslExpressions)1 StaticStructureElementInstance (com.structurizr.model.StaticStructureElementInstance)1 StringUtils (com.structurizr.util.StringUtils)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1