Search in sources :

Example 6 with ModelItem

use of com.structurizr.model.ModelItem 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)

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