-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathConnectorUtil.java
More file actions
197 lines (173 loc) · 7.57 KB
/
ConnectorUtil.java
File metadata and controls
197 lines (173 loc) · 7.57 KB
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
/*******************************************************************************
* SysML 2 Pilot Implementation
* Copyright (c) 2021-2022 Model Driven Solutions, Inc.
* Copyright (c) 2023 Mgnite Inc.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 theGNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later>
*
*******************************************************************************/
package org.omg.sysml.util;
import java.util.List;
import java.util.Optional;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.BasicInternalEList;
import org.omg.sysml.lang.sysml.BindingConnector;
import org.omg.sysml.lang.sysml.Connector;
import org.omg.sysml.lang.sysml.Feature;
import org.omg.sysml.lang.sysml.FeatureMembership;
import org.omg.sysml.lang.sysml.Flow;
import org.omg.sysml.lang.sysml.Namespace;
import org.omg.sysml.lang.sysml.ReferenceSubsetting;
import org.omg.sysml.lang.sysml.SysMLFactory;
import org.omg.sysml.lang.sysml.SysMLPackage;
import org.omg.sysml.lang.sysml.Type;
import org.omg.sysml.lang.sysml.util.SysMLLibraryUtil;
public class ConnectorUtil {
private ConnectorUtil() {
}
// Creation
public static BindingConnector createBindingConnector(Feature source, Feature target) {
BindingConnector connector = SysMLFactory.eINSTANCE.createBindingConnector();
addConnectorEndTo(connector, source);
addConnectorEndTo(connector, target);
return connector;
}
public static void transformBindingConnector(BindingConnector connector, Type owner) {
TypeUtil.addImplicitGeneralTypeTo(connector, SysMLPackage.eINSTANCE.getSubsetting(),
SysMLLibraryUtil.getLibraryType(owner, ImplicitGeneralizationMap.getDefaultSupertypeFor(connector.getClass(), "binary")));
for (Feature end: connector.getConnectorEnd()) {
ElementUtil.transform(end);
}
}
// Connector ends
public static Feature addConnectorEndTo(Connector connector, Feature relatedFeature) {
Feature endFeature = SysMLFactory.eINSTANCE.createFeature();
ReferenceSubsetting subsetting = SysMLFactory.eINSTANCE.createReferenceSubsetting();
if (relatedFeature.getOwner() == null) {
subsetting.getOwnedRelatedElement().add(relatedFeature);
}
subsetting.setReferencedFeature(relatedFeature);
endFeature.getOwnedRelationship().add(subsetting);
endFeature.setIsEnd(true);
FeatureMembership membership = SysMLFactory.eINSTANCE.createFeatureMembership();
membership.setOwnedMemberFeature(endFeature);
connector.getOwnedRelationship().add(membership);
return endFeature;
}
public static void transformConnectorEndsOf(Flow flow) {
Namespace owner = flow.getOwningNamespace();
if (owner instanceof Feature) {
EList<Feature> ends = flow.getConnectorEnd();
if (ends.size() >= 2) {
EList<Feature> endFeatures = ends.get(1).getOwnedFeature();
if (!endFeatures.isEmpty()) {
Feature flowEndFeature = endFeatures.get(0);
if (flowEndFeature.getOwnedRedefinition().isEmpty()) {
TypeUtil.addImplicitGeneralTypeTo(flowEndFeature, SysMLPackage.eINSTANCE.getRedefinition(), (Feature)owner);
}
}
}
}
}
// Related Features
public static Feature getRelatedFeatureOfEnd(Feature end) {
return FeatureUtil.getReferencedFeatureOf(end);
}
public static EList<Feature> getRelatedFeaturesOf(Connector connector) {
EList<Feature> relatedFeatures = new BasicInternalEList<Feature>(Feature.class);
for (Object end: connector.getConnectorEnd().toArray()) {
if (end != null) {
Feature referencedFeature = getRelatedFeatureOfEnd((Feature) end);
if (referencedFeature != null) {
relatedFeatures.add(referencedFeature);
}
}
}
return relatedFeatures;
}
public static void setRelatedFeatureOf(Connector connector, int index, Feature relatedFeature) {
EList<Feature> connectorEnds = connector.getConnectorEnd();
if (index < connectorEnds.size()) {
setRelatedFeatureOf(connector, connectorEnds.get(index), relatedFeature);
}
}
public static void setRelatedFeatureOf(Connector connector, Feature connectorEnd, Feature relatedFeature) {
FeatureUtil.getFirstOwnedSubsettingOf(connector).
orElseGet(()->FeatureUtil.addSubsettingTo(connectorEnd)).
setSubsettedFeature(relatedFeature);
}
public static Feature getSourceFeatureOf(Connector connector) {
EList<Feature> relatedFeatures = connector.getRelatedFeature();
return relatedFeatures.isEmpty()? null: relatedFeatures.get(0);
}
public static void addTargetFeatures(Connector connector, EList<Feature> targetFeatures) {
EList<Feature> relatedFeatures = connector.getRelatedFeature();
if (relatedFeatures.size() >= 2) {
targetFeatures.addAll(relatedFeatures.subList(1, relatedFeatures.size()));
}
}
// Path
public static void getPath(EList<Feature> path, Namespace start, Feature feature) {
if (feature != null) {
Namespace owningNamespace = feature.getOwningNamespace();
if (owningNamespace instanceof Feature && owningNamespace != start) {
getPath(path, start, (Feature)owningNamespace);
}
path.add(feature);
}
}
// Context Type
public static Type getContextTypeFor(Connector connector) {
List<Type> commonFeaturingTypes = null;
List<Feature> relatedFeatures = connector.getRelatedFeature();
Type relatedFeatureContext = relatedFeatures.stream().
filter(f1->relatedFeatures.stream().anyMatch(f2->f2 != f1 && f2.getFeaturingType().contains(f1))).
findFirst().orElse(null);
if (relatedFeatureContext != null) {
// If one of the relatedFeatures is a featuringType of all the other relatedFeatures,
// then return that as the context.
return relatedFeatureContext;
} else {
for (Feature relatedFeature: connector.getRelatedFeature()) {
// getAllFeaturingTypesOf returns a breadth-first transitive traversal of all the
// the featuring type hierarchy for relatedFeatures.
List<Type> featuringTypes = FeatureUtil.getAllFeaturingTypesOf(relatedFeature);
if (commonFeaturingTypes == null) {
commonFeaturingTypes = featuringTypes;
} else {
int i = 0;
while (i < commonFeaturingTypes.size()) {
Type type = commonFeaturingTypes.get(i);
Optional<Type> subtype = featuringTypes.stream().filter(f->TypeUtil.isCompatible(f, type)).findFirst();
if (subtype.isPresent()) {
// If the featuringTypes of the next relatedFeature include a subtype of this type,
// then replace this type with the subtype in the list of commonFeaturingTypes.
commonFeaturingTypes.set(i, subtype.get());
} else if (featuringTypes.stream().noneMatch(f->TypeUtil.isCompatible(type, f))) {
// Otherwise, if this type doesn't conform to any of the featuringTypes of the
// next relatedFeature, remove it from the list of commonFeaturingTypes.
commonFeaturingTypes.remove(i);
i--;
}
i++;
}
}
}
// If any commonFeaturingTypes have been found across all relatedFeatures, then return the first (innermost) one.
return commonFeaturingTypes == null || commonFeaturingTypes.isEmpty()? null: commonFeaturingTypes.get(0);
}
}
}