aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/modeleditor/packageviewcontroller.cpp
blob: c74090c7c68548d705cdf4a31ee8d1e668aec4e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (C) 2018 Jochen Becher
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "packageviewcontroller.h"

#include "modelutilities.h"

#include "qmt/model/mpackage.h"
#include "qmt/model/mdependency.h"
#include "qmt/model_controller/modelcontroller.h"

#include <utils/algorithm.h>

#include <QSet>

namespace ModelEditor {
namespace Internal {

class PackageViewController::PackageViewControllerPrivate {
public:
    qmt::ModelController *m_modelController = nullptr;
    ModelUtilities *m_modelUtilities = nullptr;
};

PackageViewController::PackageViewController(QObject *parent)
    : QObject(parent),
      d(new PackageViewControllerPrivate)
{
}

PackageViewController::~PackageViewController()
{
    delete d;
}

void PackageViewController::setModelController(qmt::ModelController *modelController)
{
    d->m_modelController = modelController;
}

void PackageViewController::setModelUtilities(ModelUtilities *modelUtilities)
{
    d->m_modelUtilities = modelUtilities;
}

void PackageViewController::createAncestorDependencies(qmt::MObject *object1, qmt::MObject *object2)
{
    // search ancestors sharing a common owner
    QList<qmt::MPackage *> componentAncestors;
    auto ancestor = dynamic_cast<qmt::MPackage *>(object1->owner());
    while (ancestor) {
        componentAncestors.append(ancestor);
        ancestor = dynamic_cast<qmt::MPackage *>(ancestor->owner());
    }
    QList<qmt::MPackage *> includeComponentAncestors;
    ancestor = dynamic_cast<qmt::MPackage *>(object2->owner());
    while (ancestor) {
        includeComponentAncestors.append(ancestor);
        ancestor = dynamic_cast<qmt::MPackage *>(ancestor->owner());
    }

    int componentHighestAncestorIndex = componentAncestors.size() - 1;
    int includeComponentHighestAncestorIndex = includeComponentAncestors.size() - 1;
    QMT_ASSERT(componentAncestors.at(componentHighestAncestorIndex) == includeComponentAncestors.at(includeComponentHighestAncestorIndex), return);
    while (componentHighestAncestorIndex > 0 && includeComponentHighestAncestorIndex > 0) {
        if (componentAncestors.at(componentHighestAncestorIndex) != includeComponentAncestors.at(includeComponentHighestAncestorIndex))
            break;
        --componentHighestAncestorIndex;
        --includeComponentHighestAncestorIndex;
    }

    // add dependency between parent packages with same stereotype
    int index1 = 0;
    int includeComponentLowestIndex = 0;
    while (index1 <= componentHighestAncestorIndex
           && includeComponentLowestIndex <= includeComponentHighestAncestorIndex) {
        if (!componentAncestors.at(index1)->stereotypes().isEmpty()) {
            int index2 = includeComponentLowestIndex;
            while (index2 <= includeComponentHighestAncestorIndex) {
                if (haveMatchingStereotypes(componentAncestors.at(index1), includeComponentAncestors.at(index2))) {
                    if (!d->m_modelUtilities->haveDependency(componentAncestors.at(index1), includeComponentAncestors.at(index2))) {
                        auto dependency = new qmt::MDependency;
                        dependency->setFlags(qmt::MElement::ReverseEngineered);
                        // TODO set stereotype for testing purpose
                        dependency->setStereotypes({"same stereotype"});
                        dependency->setDirection(qmt::MDependency::AToB);
                        dependency->setSource(componentAncestors.at(index1)->uid());
                        dependency->setTarget(includeComponentAncestors.at(index2)->uid());
                        d->m_modelController->addRelation(componentAncestors.at(index1), dependency);
                    }
                    includeComponentLowestIndex = index2 + 1;
                    break;
                }
                ++index2;
            }
        }
        ++index1;
    }

    // add dependency between topmost packages with common owner
    if (componentAncestors.at(componentHighestAncestorIndex) != includeComponentAncestors.at(includeComponentHighestAncestorIndex)) {
        if (!d->m_modelUtilities->haveDependency(componentAncestors.at(componentHighestAncestorIndex), includeComponentAncestors)) {
            auto dependency = new qmt::MDependency;
            dependency->setFlags(qmt::MElement::ReverseEngineered);
            // TODO set stereotype for testing purpose
            dependency->setStereotypes({"ancestor"});
            dependency->setDirection(qmt::MDependency::AToB);
            dependency->setSource(componentAncestors.at(componentHighestAncestorIndex)->uid());
            dependency->setTarget(includeComponentAncestors.at(includeComponentHighestAncestorIndex)->uid());
            d->m_modelController->addRelation(componentAncestors.at(componentHighestAncestorIndex), dependency);
        }
    }

    // add dependency between parent packages
    if (componentHighestAncestorIndex > 0 && includeComponentHighestAncestorIndex > 0) { // check for valid parents
        if (componentAncestors.at(0) != includeComponentAncestors.at(0)) {
            if (!d->m_modelUtilities->haveDependency(componentAncestors.at(0), includeComponentAncestors)) {
                auto dependency = new qmt::MDependency;
                dependency->setFlags(qmt::MElement::ReverseEngineered);
                // TODO set stereotype for testing purpose
                dependency->setStereotypes({"parents"});
                dependency->setDirection(qmt::MDependency::AToB);
                dependency->setSource(componentAncestors.at(0)->uid());
                dependency->setTarget(includeComponentAncestors.at(0)->uid());
                d->m_modelController->addRelation(componentAncestors.at(0), dependency);
            }
        }
    }
}

bool PackageViewController::haveMatchingStereotypes(const qmt::MObject *object1,
                                                    const qmt::MObject *object2)
{
    return !(Utils::toSet(object1->stereotypes()) & Utils::toSet(object2->stereotypes())).isEmpty();
}


} // namespace Internal
} // namespace ModelEditor