Search in sources :

Example 21 with Element

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

the class DslContext method getElement.

Element getElement(String identifier) {
    Element element = identifiersRegister.getElement(identifier.toLowerCase());
    if (element == null && identifiersRegister.getIdentifierScope() == IdentifierScope.Hierarchical) {
        if (this instanceof ModelItemDslContext) {
            ModelItemDslContext modelItemDslContext = (ModelItemDslContext) this;
            if (modelItemDslContext.getModelItem() instanceof Element) {
                Element parent = (Element) modelItemDslContext.getModelItem();
                while (parent != null && element == null) {
                    String parentIdentifier = identifiersRegister.findIdentifier(parent);
                    element = identifiersRegister.getElement(parentIdentifier + "." + identifier);
                    parent = parent.getParent();
                }
            }
        } else if (this instanceof DeploymentEnvironmentDslContext) {
            DeploymentEnvironmentDslContext deploymentEnvironmentDslContext = (DeploymentEnvironmentDslContext) this;
            DeploymentEnvironment deploymentEnvironment = new DeploymentEnvironment(deploymentEnvironmentDslContext.getEnvironment());
            String parentIdentifier = identifiersRegister.findIdentifier(deploymentEnvironment);
            element = identifiersRegister.getElement(parentIdentifier + "." + identifier);
        }
    }
    return element;
}
Also used : Element(com.structurizr.model.Element)

Example 22 with Element

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

the class CustomViewAnimationStepParser method parse.

void parse(DslContext context, CustomView view, Tokens tokens, int startIndex) {
    List<CustomElement> elements = new ArrayList<>();
    for (int i = startIndex; i < tokens.size(); i++) {
        String elementIdentifier = tokens.get(i);
        Element element = context.getElement(elementIdentifier);
        if (element == null) {
            throw new RuntimeException("The element \"" + elementIdentifier + "\" does not exist");
        }
        if (element instanceof CustomElement) {
            elements.add((CustomElement) element);
        }
    }
    view.addAnimation(elements.toArray(new CustomElement[0]));
}
Also used : CustomElement(com.structurizr.model.CustomElement) Element(com.structurizr.model.Element) ArrayList(java.util.ArrayList) CustomElement(com.structurizr.model.CustomElement)

Example 23 with Element

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

the class AbstractExpressionParser method getElements.

private Set<Element> getElements(String identifier, DslContext context) {
    Set<Element> elements = new HashSet<>();
    Element element = context.getElement(identifier);
    if (element != null) {
        if (element instanceof ElementGroup) {
            ElementGroup group = (ElementGroup) element;
            elements.addAll(group.getElements());
        } else {
            elements.add(element);
        }
    }
    return elements;
}
Also used : Element(com.structurizr.model.Element) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 24 with Element

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

the class AbstractExpressionParser method evaluateExpression.

private Set<ModelItem> evaluateExpression(String expr, DslContext context) {
    Set<ModelItem> modelItems = new LinkedHashSet<>();
    if (expr.startsWith(ELEMENT_EQUALS_EXPRESSION)) {
        expr = expr.substring(ELEMENT_EQUALS_EXPRESSION.length());
        if (isExpression(expr)) {
            modelItems.addAll(evaluateExpression(expr, context));
        } else {
            modelItems.addAll(parseIdentifier(expr, context));
        }
    } else if (expr.startsWith(RELATIONSHIP_EQUALS_EXPRESSION)) {
        expr = expr.substring(RELATIONSHIP_EQUALS_EXPRESSION.length());
        if (WILDCARD.equals(expr)) {
            expr = WILDCARD + RELATIONSHIP + WILDCARD;
        }
        if (isExpression(expr)) {
            modelItems.addAll(evaluateExpression(expr, context));
        } else {
            modelItems.addAll(parseIdentifier(expr, context));
        }
    } else if (RELATIONSHIP.equals(expr)) {
        throw new RuntimeException("Unexpected identifier \"->\"");
    } else if (expr.startsWith(RELATIONSHIP) || expr.endsWith(RELATIONSHIP)) {
        // this is an element expression: ->identifier identifier-> ->identifier->
        boolean includeAfferentCouplings = false;
        boolean includeEfferentCouplings = false;
        String identifier = expr;
        if (identifier.startsWith(RELATIONSHIP)) {
            includeAfferentCouplings = true;
            identifier = identifier.substring(RELATIONSHIP.length());
        }
        if (identifier.endsWith(RELATIONSHIP)) {
            includeEfferentCouplings = true;
            identifier = identifier.substring(0, identifier.length() - RELATIONSHIP.length());
        }
        identifier = identifier.trim();
        Set<Element> elements;
        if (isExpression(identifier)) {
            elements = parseExpression(identifier, context).stream().filter(mi -> mi instanceof Element).map(mi -> (Element) mi).collect(Collectors.toSet());
        } else {
            elements = getElements(identifier, context);
        }
        if (elements.isEmpty()) {
            throw new RuntimeException("The element \"" + identifier + "\" does not exist");
        }
        for (Element element : elements) {
            modelItems.add(element);
            if (includeAfferentCouplings) {
                modelItems.addAll(findAfferentCouplings(element));
            }
            if (includeEfferentCouplings) {
                modelItems.addAll(findEfferentCouplings(element));
            }
        }
    } else if (expr.contains(RELATIONSHIP)) {
        String[] identifiers = expr.split(RELATIONSHIP);
        String sourceIdentifier = identifiers[0].trim();
        String destinationIdentifier = identifiers[1].trim();
        String sourceExpression = RELATIONSHIP_SOURCE_EQUALS_EXPRESSION + sourceIdentifier;
        String destinationExpression = RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION + destinationIdentifier;
        if (WILDCARD.equals(sourceIdentifier) && WILDCARD.equals(destinationIdentifier)) {
            modelItems.addAll(context.getWorkspace().getModel().getRelationships());
        } else if (WILDCARD.equals(destinationIdentifier)) {
            modelItems.addAll(parseExpression(sourceExpression, context));
        } else if (WILDCARD.equals(sourceIdentifier)) {
            modelItems.addAll(parseExpression(destinationExpression, context));
        } else {
            modelItems.addAll(parseExpression(sourceExpression + " && " + destinationExpression, context));
        }
    } else if (expr.toLowerCase().startsWith(ELEMENT_PARENT_EQUALS_EXPRESSION)) {
        String parentIdentifier = expr.substring(ELEMENT_PARENT_EQUALS_EXPRESSION.length());
        Element parentElement = context.getElement(parentIdentifier);
        if (parentElement == null) {
            throw new RuntimeException("The parent element \"" + parentIdentifier + "\" does not exist");
        } else {
            context.getWorkspace().getModel().getElements().forEach(element -> {
                if (element.getParent() == parentElement) {
                    modelItems.add(element);
                }
            });
        }
    } else if (expr.toLowerCase().startsWith(ELEMENT_TYPE_EQUALS_EXPRESSION)) {
        modelItems.addAll(evaluateElementTypeExpression(expr, context));
    } else if (expr.toLowerCase().startsWith(ELEMENT_TAG_EQUALS_EXPRESSION.toLowerCase())) {
        String[] tags = expr.substring(ELEMENT_TAG_EQUALS_EXPRESSION.length()).split(",");
        context.getWorkspace().getModel().getElements().forEach(element -> {
            if (hasAllTags(element, tags)) {
                modelItems.add(element);
            }
        });
    } else if (expr.toLowerCase().startsWith(ELEMENT_TAG_NOT_EQUALS_EXPRESSION)) {
        String[] tags = expr.substring(ELEMENT_TAG_NOT_EQUALS_EXPRESSION.length()).split(",");
        context.getWorkspace().getModel().getElements().forEach(element -> {
            if (!hasAllTags(element, tags)) {
                modelItems.add(element);
            }
        });
    } else if (expr.startsWith(RELATIONSHIP_TAG_EQUALS_EXPRESSION)) {
        String[] tags = expr.substring(RELATIONSHIP_TAG_EQUALS_EXPRESSION.length()).split(",");
        context.getWorkspace().getModel().getRelationships().forEach(relationship -> {
            if (hasAllTags(relationship, tags)) {
                modelItems.add(relationship);
            }
        });
    } else if (expr.startsWith(RELATIONSHIP_TAG_NOT_EQUALS_EXPRESSION)) {
        String[] tags = expr.substring(RELATIONSHIP_TAG_NOT_EQUALS_EXPRESSION.length()).split(",");
        context.getWorkspace().getModel().getRelationships().forEach(relationship -> {
            if (!hasAllTags(relationship, tags)) {
                modelItems.add(relationship);
            }
        });
    } else if (expr.startsWith(RELATIONSHIP_SOURCE_EQUALS_EXPRESSION)) {
        String identifier = expr.substring(RELATIONSHIP_SOURCE_EQUALS_EXPRESSION.length());
        Set<Element> sourceElements = new HashSet<>();
        if (isExpression(identifier)) {
            Set<ModelItem> set = parseExpression(identifier, context);
            for (ModelItem modelItem : set) {
                if (modelItem instanceof Element) {
                    sourceElements.add((Element) modelItem);
                }
            }
        } else {
            Element source = context.getElement(identifier);
            if (source == null) {
                throw new RuntimeException("The element \"" + identifier + "\" does not exist");
            }
            if (source instanceof ElementGroup) {
                sourceElements.addAll(((ElementGroup) source).getElements());
            } else {
                sourceElements.add(source);
            }
        }
        context.getWorkspace().getModel().getRelationships().forEach(relationship -> {
            if (sourceElements.contains(relationship.getSource())) {
                modelItems.add(relationship);
            }
        });
    } else if (expr.startsWith(RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION)) {
        String identifier = expr.substring(RELATIONSHIP_DESTINATION_EQUALS_EXPRESSION.length());
        Set<Element> destinationElements = new HashSet<>();
        if (isExpression(identifier)) {
            Set<ModelItem> set = parseExpression(identifier, context);
            for (ModelItem modelItem : set) {
                if (modelItem instanceof Element) {
                    destinationElements.add((Element) modelItem);
                }
            }
        } else {
            Element destination = context.getElement(identifier);
            if (destination == null) {
                throw new RuntimeException("The element \"" + identifier + "\" does not exist");
            }
            if (destination instanceof ElementGroup) {
                destinationElements.addAll(((ElementGroup) destination).getElements());
            } else {
                destinationElements.add(destination);
            }
        }
        context.getWorkspace().getModel().getRelationships().forEach(relationship -> {
            if (destinationElements.contains(relationship.getDestination())) {
                modelItems.add(relationship);
            }
        });
    }
    return modelItems;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Relationship(com.structurizr.model.Relationship) StructurizrDslExpressions(com.structurizr.dsl.StructurizrDslExpressions) Set(java.util.Set) StringUtils(com.structurizr.util.StringUtils) StaticStructureElementInstance(com.structurizr.model.StaticStructureElementInstance) Collectors(java.util.stream.Collectors) Element(com.structurizr.model.Element) ModelItem(com.structurizr.model.ModelItem) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) Element(com.structurizr.model.Element) ModelItem(com.structurizr.model.ModelItem) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 25 with Element

use of com.structurizr.model.Element in project java by structurizr.

the class StaticView method addAnimation.

/**
 * Adds an animation step, with the specified elements.
 *
 * @param elements      the elements that should be shown in the animation step
 */
public void addAnimation(Element... elements) {
    if (elements == null || elements.length == 0) {
        throw new IllegalArgumentException("One or more elements must be specified.");
    }
    Set<String> elementIdsInPreviousAnimationSteps = new HashSet<>();
    for (Animation animationStep : animations) {
        elementIdsInPreviousAnimationSteps.addAll(animationStep.getElements());
    }
    Set<Element> elementsInThisAnimationStep = new HashSet<>();
    Set<Relationship> relationshipsInThisAnimationStep = new HashSet<>();
    for (Element element : elements) {
        if (isElementInView(element)) {
            if (!elementIdsInPreviousAnimationSteps.contains(element.getId())) {
                elementIdsInPreviousAnimationSteps.add(element.getId());
                elementsInThisAnimationStep.add(element);
            }
        }
    }
    if (elementsInThisAnimationStep.size() == 0) {
        throw new IllegalArgumentException("None of the specified elements exist in this view.");
    }
    for (RelationshipView relationshipView : this.getRelationships()) {
        if ((elementsInThisAnimationStep.contains(relationshipView.getRelationship().getSource()) && elementIdsInPreviousAnimationSteps.contains(relationshipView.getRelationship().getDestination().getId())) || (elementIdsInPreviousAnimationSteps.contains(relationshipView.getRelationship().getSource().getId()) && elementsInThisAnimationStep.contains(relationshipView.getRelationship().getDestination()))) {
            relationshipsInThisAnimationStep.add(relationshipView.getRelationship());
        }
    }
    animations.add(new Animation(animations.size() + 1, elementsInThisAnimationStep, relationshipsInThisAnimationStep));
}
Also used : Element(com.structurizr.model.Element) Relationship(com.structurizr.model.Relationship) HashSet(java.util.HashSet)

Aggregations

Element (com.structurizr.model.Element)27 SoftwareSystem (com.structurizr.model.SoftwareSystem)8 Workspace (com.structurizr.Workspace)7 Relationship (com.structurizr.model.Relationship)7 Collectors (java.util.stream.Collectors)7 Container (com.structurizr.model.Container)6 File (java.io.File)6 IOException (java.io.IOException)6 StaticStructureElement (com.structurizr.model.StaticStructureElement)5 ArrayList (java.util.ArrayList)5 LinkedHashSet (java.util.LinkedHashSet)5 Map (java.util.Map)5 Inject (javax.inject.Inject)5 OutputBuilder (org.ndx.agile.architecture.base.OutputBuilder)5 Component (com.structurizr.model.Component)4 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4 Optional (java.util.Optional)4 Set (java.util.Set)4 Logger (java.util.logging.Logger)4