1 /**
2 * UGENE - Integrated Bioinformatics Tools.
3 * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4 * http://ugene.net
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include "PortMapping.h"
23
24 #include <QSet>
25
26 #include <U2Core/U2SafePoints.h>
27
28 namespace U2 {
29
PortMapping(const QString & srcPortId,const QString & dstPortId)30 PortMapping::PortMapping(const QString &srcPortId, const QString &dstPortId)
31 : IdMapping(srcPortId, dstPortId) {
32 }
33
addSlotMapping(const SlotMapping & value)34 void PortMapping::addSlotMapping(const SlotMapping &value) {
35 slotList << value;
36 }
37
getMappings() const38 const QList<SlotMapping> &PortMapping::getMappings() const {
39 return slotList;
40 }
41
validate(const QMap<Descriptor,DataTypePtr> & srcType,const QMap<Descriptor,DataTypePtr> & dstType,U2OpStatus & os) const42 void PortMapping::validate(const QMap<Descriptor, DataTypePtr> &srcType,
43 const QMap<Descriptor, DataTypePtr> &dstType,
44 U2OpStatus &os) const {
45 validateSlotsCount(srcType, dstType, os);
46 CHECK_OP(os, );
47
48 QSet<QString> srcIds;
49 QSet<QString> dstIds;
50 foreach (const SlotMapping &mapping, slotList) {
51 tryAddId(mapping.getSrcId(), srcIds, os);
52 CHECK_OP(os, );
53 tryAddId(mapping.getDstId(), dstIds, os);
54 CHECK_OP(os, );
55 DataTypePtr srcSlotType = validateSlotId(srcId, mapping.getSrcId(), srcType, os);
56 CHECK_OP(os, );
57 DataTypePtr dstSlotType = validateSlotId(dstId, mapping.getDstId(), dstType, os);
58 CHECK_OP(os, );
59 mapping.validate(srcSlotType, dstSlotType, os);
60 CHECK_OP(os, );
61 }
62
63 validateMappingsCount(srcType, os);
64 CHECK_OP(os, );
65 }
66
validateSlotsCount(const QMap<Descriptor,DataTypePtr> & srcType,const QMap<Descriptor,DataTypePtr> & dstType,U2OpStatus & os) const67 void PortMapping::validateSlotsCount(const QMap<Descriptor, DataTypePtr> &srcType,
68 const QMap<Descriptor, DataTypePtr> &dstType,
69 U2OpStatus &os) const {
70 if (srcType.size() != dstType.size()) {
71 os.setError(QObject::tr("Ports can not be mapped: %1, %2. Slots count is different").arg(srcId).arg(dstId));
72 }
73 }
74
validateSlotId(const QString & portId,const QString & slotId,const QMap<Descriptor,DataTypePtr> & type,U2OpStatus & os) const75 DataTypePtr PortMapping::validateSlotId(const QString &portId, const QString &slotId, const QMap<Descriptor, DataTypePtr> &type, U2OpStatus &os) const {
76 if (!type.contains(slotId)) {
77 os.setError(QObject::tr("%1 port does not contain a slot with id: %2").arg(portId).arg(slotId));
78 return DataTypePtr();
79 }
80 return type[slotId];
81 }
82
tryAddId(const QString & id,QSet<QString> & idSet,U2OpStatus & os) const83 void PortMapping::tryAddId(const QString &id,
84 QSet<QString> &idSet,
85 U2OpStatus &os) const {
86 if (idSet.contains(id)) {
87 os.setError(QObject::tr("Duplicated mapping of slot with id: %1").arg(id));
88 return;
89 }
90 idSet << id;
91 }
92
validateMappingsCount(const QMap<Descriptor,DataTypePtr> & srcType,U2OpStatus & os) const93 void PortMapping::validateMappingsCount(const QMap<Descriptor, DataTypePtr> &srcType,
94 U2OpStatus &os) const {
95 if (slotList.count() < srcType.count()) {
96 os.setError(QObject::tr("Not all slots are mapped"));
97 }
98 }
99
getMappingBySrcPort(const QString & srcPortId,const QList<PortMapping> & mappings,U2OpStatus & os)100 PortMapping PortMapping::getMappingBySrcPort(const QString &srcPortId, const QList<PortMapping> &mappings, U2OpStatus &os) {
101 foreach (const PortMapping &m, mappings) {
102 if (m.getSrcId() == srcPortId) {
103 return m;
104 }
105 }
106 os.setError(QString("No mapping for port: %1").arg(srcPortId));
107 return PortMapping("", "");
108 }
109
getDstSlotId(const QString & srcSlotId,U2OpStatus & os) const110 QString PortMapping::getDstSlotId(const QString &srcSlotId, U2OpStatus &os) const {
111 foreach (const SlotMapping &m, slotList) {
112 if (m.getSrcId() == srcSlotId) {
113 return m.getDstId();
114 }
115 }
116 os.setError(QString("No mapping for slot: %1").arg(srcSlotId));
117 return "";
118 }
119
120 } // namespace U2
121