<commit_before>package com.structurizr.view;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.structurizr.model.*;
import com.structurizr.util.StringUtils;

import javax.annotation.Nonnull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * A dynamic view, used to describe behaviour between static elements at runtime.
 */
public final class DynamicView extends View {

    private Model model;

    private Element element;
    private String elementId;

    private boolean externalBoundariesVisible = false;

    private SequenceNumber sequenceNumber = new SequenceNumber();

    DynamicView() {
    }

    DynamicView(Model model, String key, String description) {
        super(null, key, description);

        setModel(model);
        setElement(null);
    }

    DynamicView(SoftwareSystem softwareSystem, String key, String description) {
        super(softwareSystem, key, description);

        setModel(softwareSystem.getModel());
        setElement(softwareSystem);
    }

    DynamicView(Container container, String key, String description) {
        super(container.getSoftwareSystem(), key, description);

        setModel(container.getModel());
        setElement(container);
    }

    @JsonIgnore
    @Override
    public Model getModel() {
        return this.model;
    }

    void setModel(Model model) {
        this.model = model;
    }

    @Override
    @JsonIgnore
    public String getSoftwareSystemId() {
        return super.getSoftwareSystemId();
    }

    /**
     * Gets the ID of the software system or container associated with this view.
     *
     * @return the ID, as a String, or null if not set
     */
    public String getElementId() {
        if (this.element != null) {
            return element.getId();
        } else {
            return this.elementId;
        }
    }

    void setElementId(String elementId) {
        this.elementId = elementId;
    }

    @JsonIgnore
    public Element getElement() {
        return element;
    }

    void setElement(Element element) {
        this.element = element;

        if (element instanceof SoftwareSystem) {
            setSoftwareSystem((SoftwareSystem)element);
        }
    }

    public RelationshipView add(@Nonnull Element source, @Nonnull Element destination) {
        return add(source, "", destination);
    }

    public RelationshipView add(@Nonnull Element source, String description, @Nonnull Element destination) {
        return add(source, description, "", destination);
    }

    public RelationshipView add(@Nonnull Element source, String description, String technology, @Nonnull Element destination) {
        if (source == null) {
            throw new IllegalArgumentException("A source element must be specified.");
        }

        if (destination == null) {
            throw new IllegalArgumentException("A destination element must be specified.");
        }

        checkElementCanBeAdded(source);
        checkElementCanBeAdded(destination);

        // check that the relationship is in the model before adding it
        Relationship relationship = null;

        if (StringUtils.isNullOrEmpty(technology)) {
            // no technology is specified, so just pick the first relationship we find
            relationship = source.getEfferentRelationshipWith(destination);
        } else {
            Set<Relationship> relationships = source.getEfferentRelationshipsWith(destination);
            for (Relationship rel : relationships) {
                if (technology.equals(rel.getTechnology())) {
                    relationship = rel;
                }
            }
        }

        if (relationship != null) {
            addElement(source, false);
            addElement(destination, false);

            return addRelationship(relationship, description, sequenceNumber.getNext(), false);
        } else {
            // perhaps model this as a return/reply/response message instead, if the reverse relationship exists

            if (StringUtils.isNullOrEmpty(technology)) {
                // no technology is specified, so just pick the first relationship we find
                relationship = destination.getEfferentRelationshipWith(source);
            } else {
                Set<Relationship> relationships = destination.getEfferentRelationshipsWith(source);
                for (Relationship rel : relationships) {
                    if (technology.equals(rel.getTechnology())) {
                        relationship = rel;
                    }
                }
            }
<commit_msg>Dynamic views now try to find a relationship with the same description.<commit_after>package com.structurizr.view;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.structurizr.model.*;
import com.structurizr.util.StringUtils;

import javax.annotation.Nonnull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * A dynamic view, used to describe behaviour between static elements at runtime.
 */
public final class DynamicView extends View {

    private Model model;

    private Element element;
    private String elementId;

    private boolean externalBoundariesVisible = false;

    private SequenceNumber sequenceNumber = new SequenceNumber();

    DynamicView() {
    }

    DynamicView(Model model, String key, String description) {
        super(null, key, description);

        setModel(model);
        setElement(null);
    }

    DynamicView(SoftwareSystem softwareSystem, String key, String description) {
        super(softwareSystem, key, description);

        setModel(softwareSystem.getModel());
        setElement(softwareSystem);
    }

    DynamicView(Container container, String key, String description) {
        super(container.getSoftwareSystem(), key, description);

        setModel(container.getModel());
        setElement(container);
    }

    @JsonIgnore
    @Override
    public Model getModel() {
        return this.model;
    }

    void setModel(Model model) {
        this.model = model;
    }

    @Override
    @JsonIgnore
    public String getSoftwareSystemId() {
        return super.getSoftwareSystemId();
    }

    /**
     * Gets the ID of the software system or container associated with this view.
     *
     * @return the ID, as a String, or null if not set
     */
    public String getElementId() {
        if (this.element != null) {
            return element.getId();
        } else {
            return this.elementId;
        }
    }

    void setElementId(String elementId) {
        this.elementId = elementId;
    }

    @JsonIgnore
    public Element getElement() {
        return element;
    }

    void setElement(Element element) {
        this.element = element;

        if (element instanceof SoftwareSystem) {
            setSoftwareSystem((SoftwareSystem)element);
        }
    }

    public RelationshipView add(@Nonnull Element source, @Nonnull Element destination) {
        return add(source, "", destination);
    }

    public RelationshipView add(@Nonnull Element source, String description, @Nonnull Element destination) {
        return add(source, description, "", destination);
    }

    public RelationshipView add(@Nonnull Element source, String description, String technology, @Nonnull Element destination) {
        if (source == null) {
            throw new IllegalArgumentException("A source element must be specified.");
        }

        if (destination == null) {
            throw new IllegalArgumentException("A destination element must be specified.");
        }

        checkElementCanBeAdded(source);
        checkElementCanBeAdded(destination);

        // check that the relationship is in the model before adding it
        Relationship relationship = null;

        if (StringUtils.isNullOrEmpty(technology)) {
            // no technology is specified, so just pick the first relationship we find
            relationship = source.getEfferentRelationshipWith(destination, description);
            if (relationship == null) {
                relationship = source.getEfferentRelationshipWith(destination);
            }
        } else {
            Set<Relationship> relationships = source.getEfferentRelationshipsWith(destination);
            for (Relationship rel : relationships) {
                if (technology.equals(rel.getTechnology())) {
                    relationship = rel;
                }
            }
        }

        if (relationship != null) {
            addElement(source, false);
            addElement(destination, false);

            return addRelationship(relationship, description, sequenceNumber.getNext(), false);
        } else {
            // perhaps model this as a return/reply/response message instead, if the reverse relationship exists

            if (StringUtils.isNullOrEmpty(technology)) {
                // no technology is specified, so just pick the first relationship we find
                relationship = destination.getEfferentRelationshipWith(source);
            } else {
                Set<Relationship> relationships = destination.getEfferentRelationshipsWith(source);
                for (Relationship rel : relationships) {
                    if (technology.equals(rel.getTechnology())) {
                        relationship = rel;
                    }
                }
            }

{'fraction_non_alphanumeric': 0.08282569199529764, 'fraction_numerical': 0.0, 'mean_word_length': 2.385672937771346, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1460304', 'n_tokens_mistral': 2303, 'n_tokens_neox': 2153, 'n_words': 649}