Skip to content

Commit 87fccb8

Browse files
committed
[ST6RI-881] Refactored computation and caching of types of a Feature.
- Ensured that implicit types are computed when the types are derived.
1 parent 6d52042 commit 87fccb8

3 files changed

Lines changed: 58 additions & 48 deletions

File tree

kerml/src/examples/Simple Tests/MetadataTest.kerml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,18 @@ package MetadataTest {
3838
}
3939
}
4040

41+
class CC;
42+
struct SS {
43+
feature cc : CC;
44+
}
45+
46+
metaclass M :> Metaobjects::SemanticMetadata {
47+
:>> annotatedElement : KerML::Class;
48+
:>> baseType = if annotatedElement istype KerML::Structure ?
49+
SS meta KerML::Type else CC meta KerML::Class;
50+
}
51+
52+
#M struct T {
53+
feature :>> cc;
54+
}
4155
}

org.omg.sysml/src/org/omg/sysml/adapter/FeatureAdapter.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import org.eclipse.emf.common.util.EList;
3636
import org.eclipse.emf.ecore.EClass;
37+
import org.eclipse.emf.ecore.InternalEObject;
3738
import org.omg.sysml.lang.sysml.Association;
3839
import org.omg.sysml.lang.sysml.BindingConnector;
3940
import org.omg.sysml.lang.sysml.Connector;
@@ -61,6 +62,7 @@
6162
import org.omg.sysml.util.ElementUtil;
6263
import org.omg.sysml.util.ExpressionUtil;
6364
import org.omg.sysml.util.FeatureUtil;
65+
import org.omg.sysml.util.NonNotifyingEObjectEList;
6466
import org.omg.sysml.util.TypeUtil;
6567

6668
public class FeatureAdapter extends TypeAdapter {
@@ -112,15 +114,6 @@ public String getEffectiveShortName() {
112114
return storedEffectiveShortName;
113115
}
114116

115-
public EList<Type> getTypes() {
116-
return types;
117-
}
118-
119-
public EList<Type> setTypes(EList<Type> types) {
120-
this.types = types;
121-
return types;
122-
}
123-
124117
Set<Feature> allRedefinedFeatures = null;
125118

126119
@Override
@@ -549,6 +542,45 @@ public boolean isComputeRedefinitions() {
549542
target.getOwnedRedefinition().isEmpty());
550543
}
551544

545+
public EList<Type> getAllTypes() {
546+
if (types == null) {
547+
types = new NonNotifyingEObjectEList<Type>(Type.class, (InternalEObject)getTarget(), SysMLPackage.FEATURE__TYPE);
548+
getTypes(types, new HashSet<Feature>());
549+
removeRedundantTypes(types);
550+
}
551+
return types;
552+
}
553+
554+
public void getTypes(List<Type> types, Set<Feature> visitedFeatures) {
555+
Feature feature = getTarget();
556+
visitedFeatures.add(feature);
557+
computeImplicitGeneralTypes();
558+
getFeatureTypes(types, visitedFeatures);
559+
for (Feature typingFeature : feature.typingFeatures()) {
560+
if (!visitedFeatures.contains(typingFeature)) {
561+
FeatureUtil.getTypesOf(typingFeature, types, visitedFeatures);
562+
}
563+
}
564+
}
565+
566+
public void getFeatureTypes(List<Type> types, Set<Feature> visitedFeatures) {
567+
Feature feature = getTarget();
568+
feature.getOwnedTyping().stream().
569+
map(typing->typing.getType()).
570+
filter(type->type != null).
571+
forEachOrdered(types::add);
572+
types.addAll(getImplicitGeneralTypes(SysMLPackage.eINSTANCE.getFeatureTyping()));
573+
}
574+
575+
protected static void removeRedundantTypes(List<Type> types) {
576+
for (int i = types.size() - 1; i >= 0 ; i--) {
577+
Type type = types.get(i);
578+
if (types.stream().anyMatch(otherType->otherType != type && TypeUtil.specializes(otherType, type))) {
579+
types.remove(i);
580+
}
581+
}
582+
}
583+
552584
/**
553585
* Compute relevant implicit Redefinitions, as appropriate.
554586
*/

org.omg.sysml/src/org/omg/sysml/util/FeatureUtil.java

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.Optional;
3030
import java.util.Set;
3131
import java.util.function.Consumer;
32-
import java.util.function.Supplier;
3332
import java.util.stream.Collectors;
3433
import java.util.stream.Stream;
3534

@@ -58,7 +57,6 @@
5857
import org.omg.sysml.lang.sysml.Step;
5958
import org.omg.sysml.lang.sysml.Subsetting;
6059
import org.omg.sysml.lang.sysml.SysMLFactory;
61-
import org.omg.sysml.lang.sysml.SysMLPackage;
6260
import org.omg.sysml.lang.sysml.Type;
6361
import org.omg.sysml.lang.sysml.TypeFeaturing;
6462

@@ -120,12 +118,6 @@ public static boolean checkIsOrdered(Feature feature, Set<Feature> visited) {
120118

121119
// Typing
122120

123-
public static EList<Type> cacheTypesOf(Feature feature, Supplier<EList<Type>> supplier) {
124-
FeatureAdapter adapter = getFeatureAdapter(feature);
125-
EList<Type> types = adapter.getTypes();
126-
return types == null? adapter.setTypes(supplier.get()): types;
127-
}
128-
129121
public static <T> T getFirstTypeOf(Feature feature, Class<T> kind) {
130122
return FeatureUtil.getAllTypesOf(feature).stream().
131123
filter(kind::isInstance).
@@ -144,39 +136,11 @@ public static <T> EList<T> getAllTypesOf(Feature feature, Class<T> kind, int fea
144136
}
145137

146138
public static EList<Type> getAllTypesOf(Feature feature) {
147-
return FeatureUtil.cacheTypesOf(feature, ()->{
148-
EList<Type> types = new NonNotifyingEObjectEList<Type>(Type.class, (InternalEObject)feature, SysMLPackage.FEATURE__TYPE);
149-
getTypesOf(feature, types, new HashSet<Feature>());
150-
removeRedundantTypes(types);
151-
return types;
152-
});
139+
return getFeatureAdapter(feature).getAllTypes();
153140
}
154141

155-
private static void getTypesOf(Feature feature, List<Type> types, Set<Feature> visitedFeatures) {
156-
visitedFeatures.add(feature);
157-
getFeatureTypesOf(feature, types, visitedFeatures);
158-
for (Feature typingFeature : feature.typingFeatures()) {
159-
if (!visitedFeatures.contains(typingFeature)) {
160-
getTypesOf(typingFeature, types, visitedFeatures);
161-
}
162-
}
163-
}
164-
165-
private static void getFeatureTypesOf(Feature feature, List<Type> types, Set<Feature> visitedFeatures) {
166-
feature.getOwnedTyping().stream().
167-
map(typing->typing.getType()).
168-
filter(type->type != null).
169-
forEachOrdered(types::add);
170-
types.addAll(TypeUtil.getImplicitGeneralTypesFor(feature, SysMLPackage.eINSTANCE.getFeatureTyping()));
171-
}
172-
173-
protected static void removeRedundantTypes(List<Type> types) {
174-
for (int i = types.size() - 1; i >= 0 ; i--) {
175-
Type type = types.get(i);
176-
if (types.stream().anyMatch(otherType->otherType != type && TypeUtil.specializes(otherType, type))) {
177-
types.remove(i);
178-
}
179-
}
142+
public static void getTypesOf(Feature feature, List<Type> types, Set<Feature> visitedFeatures) {
143+
getFeatureAdapter(feature).getTypes(types, visitedFeatures);
180144
}
181145

182146
public static FeatureTyping addFeatureTypingTo(Feature feature) {

0 commit comments

Comments
 (0)