File size: 7,848 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
 *   Copyright (c) 2023 David Carter <dcarter@david.carter.ca>             *
 *                                                                         *
 *   This file is part of FreeCAD.                                         *
 *                                                                         *
 *   FreeCAD is free software: you can redistribute it and/or modify it    *
 *   under the terms of the GNU Lesser General Public License as           *
 *   published by the Free Software Foundation, either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   FreeCAD is distributed in the hope that it will be useful, but        *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with FreeCAD. If not, see                               *
 *   <https://www.gnu.org/licenses/>.                                      *
 *                                                                         *
 **************************************************************************/

#include <string>


#include <App/Application.h>

#include "Exceptions.h"
#include "Model.h"
#include "ModelLibrary.h"


using namespace Materials;

TYPESYSTEM_SOURCE(Materials::ModelProperty, Base::BaseClass)

ModelProperty::ModelProperty()
{}

ModelProperty::ModelProperty(const QString& name,
                             const QString& header,
                             const QString& type,
                             const QString& units,
                             const QString& url,
                             const QString& description)
    : _name(name)
    , _displayName(header)
    , _propertyType(type)
    , _units(units)
    , _url(url)
    , _description(description)
{}

ModelProperty::ModelProperty(const ModelProperty& other)
    : _name(other._name)
    , _displayName(other._displayName)
    , _propertyType(other._propertyType)
    , _units(other._units)
    , _url(other._url)
    , _description(other._description)
    , _inheritance(other._inheritance)
{
    for (auto it = other._columns.begin(); it != other._columns.end(); it++) {
        _columns.push_back(*it);
    }
}

const QString ModelProperty::getDisplayName() const
{
    if (_displayName.isEmpty()) {
        return getName();
    }
    return _displayName;
}

ModelProperty& ModelProperty::operator=(const ModelProperty& other)
{
    if (this == &other) {
        return *this;
    }

    _name = other._name;
    _displayName = other._displayName;
    _propertyType = other._propertyType;
    _units = other._units;
    _url = other._url;
    _description = other._description;
    _inheritance = other._inheritance;
    _columns.clear();
    for (auto it = other._columns.begin(); it != other._columns.end(); it++) {
        _columns.push_back(*it);
    }

    return *this;
}

bool ModelProperty::operator==(const ModelProperty& other) const
{
    if (this == &other) {
        return true;
    }

    return (_name == other._name) && (_displayName == other._displayName)
        && (_propertyType == other._propertyType) && (_units == other._units)
        && (_url == other._url) && (_description == other._description)
        && (_inheritance == other._inheritance);
}

void ModelProperty::validate(const ModelProperty& other) const
{
    if (_name != other._name) {
        throw InvalidProperty("Model names don't match");
    }
    if (getDisplayName() != other.getDisplayName()) {
        Base::Console().log("Local display name '%s'\n", getDisplayName().toStdString().c_str());
        Base::Console().log("Remote display name '%s'\n",
                            other.getDisplayName().toStdString().c_str());
        throw InvalidProperty("Model display names don't match");
    }
    if (_propertyType != other._propertyType) {
        throw InvalidProperty("Model property types don't match");
    }
    if (_units != other._units) {
        throw InvalidProperty("Model units don't match");
    }
    if (_url != other._url) {
        throw InvalidProperty("Model URLs don't match");
    }
    if (_description != other._description) {
        throw InvalidProperty("Model descriptions don't match");
    }
    if (_inheritance != other._inheritance) {
        throw InvalidProperty("Model inheritance don't match");
    }

    if (_columns.size() != other._columns.size()) {
        Base::Console().log("Local property column count %d\n", _columns.size());
        Base::Console().log("Remote property column count %d\n", other._columns.size());
        throw InvalidProperty("Model property column counts don't match");
    }
    for (size_t i = 0; i < _columns.size(); i++) {
        _columns[i].validate(other._columns[i]);
    }
}

TYPESYSTEM_SOURCE(Materials::Model, Base::BaseClass)

Model::Model()
{}

Model::Model(std::shared_ptr<ModelLibrary> library,
             ModelType type,
             const QString& name,
             const QString& directory,
             const QString& uuid,
             const QString& description,
             const QString& url,
             const QString& doi)
    : _library(library)
    , _type(type)
    , _name(name)
    , _directory(directory)
    , _uuid(uuid)
    , _description(description)
    , _url(url)
    , _doi(doi)
{}

QString Model::getDirectory() const
{
    return _directory;
}

void Model::setDirectory(const QString& directory)
{
    _directory = directory;
}

QString Model::getFilename() const
{
    return _filename;
}

void Model::setFilename(const QString& filename)
{
    _filename = filename;
}

QString Model::getFilePath() const
{
    return QDir(_directory + QStringLiteral("/") + _filename).absolutePath();
}

ModelProperty& Model::operator[](const QString& key)
{
    try {
        return _properties.at(key);
    }
    catch (std::out_of_range const&) {
        throw PropertyNotFound();
    }
}

void Model::validate(Model& other) const
{
    try {
        _library->validate(*(other._library));
    }
    catch (const InvalidLibrary& e)
    {
        throw InvalidModel(e.what());
    }

    // std::map<QString, ModelProperty> _properties;
    if (_type != other._type) {
        throw InvalidModel("Model types don't match");
    }
    if (_name != other._name) {
        throw InvalidModel("Model names don't match");
    }
    if (_directory != other._directory) {
        throw InvalidModel("Model directories don't match");
    }
    if (!other._filename.isEmpty()) {
        throw InvalidModel("Remote filename is not empty");
    }
    if (_uuid != other._uuid) {
        throw InvalidModel("Model UUIDs don't match");
    }
    if (_description != other._description) {
        throw InvalidModel("Model descriptions don't match");
    }
    if (_url != other._url) {
        throw InvalidModel("Model URLs don't match");
    }
    if (_doi != other._doi) {
        throw InvalidModel("Model DOIs don't match");
    }
    if (_inheritedUuids != other._inheritedUuids) {
        throw InvalidModel("Model inherited UUIDs don't match");
    }

    // Need to compare properties
    if (_properties.size() != other._properties.size()) {
        throw InvalidModel("Model property counts don't match");
    }
    for (auto& property : _properties) {
        auto& remote = other._properties[property.first];
        property.second.validate(remote);
    }
}