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;
}
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]));
}
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;
}
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;
}
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));
}
Aggregations