Skip to content

Commit d69dbff

Browse files
authored
Merge pull request #815 from Systems-Modeling/ST6RI-947
ST6RI-947 Type.getFeature() mutates Usage models by materializing default Multiplicity
2 parents e7a57ab + b6ec20f commit d69dbff

17 files changed

Lines changed: 343 additions & 61 deletions

File tree

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/*******************************************************************************
2+
* SysML 2 Pilot Implementation
3+
* Copyright (c) 2026 Obeo
4+
* Copyright (c) 2026 Model Driven Solutions, Inc.
5+
*
6+
* This program and the accompanying materials are made available under the
7+
* terms of the Eclipse Public License 2.0 which is available at
8+
* http://www.eclipse.org/legal/epl-2.0.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0
11+
*******************************************************************************/
12+
package org.omg.sysml.interactive.tests;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertSame;
16+
import static org.junit.Assert.assertTrue;
17+
18+
import java.nio.file.Path;
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
import org.eclipse.emf.ecore.EObject;
23+
import org.eclipse.emf.ecore.resource.Resource;
24+
import org.junit.After;
25+
import org.junit.BeforeClass;
26+
import org.junit.Test;
27+
import org.omg.sysml.interactive.SysMLInteractive;
28+
import org.omg.sysml.interactive.SysMLInteractiveResult;
29+
import org.omg.sysml.lang.sysml.Element;
30+
import org.omg.sysml.lang.sysml.Multiplicity;
31+
import org.omg.sysml.lang.sysml.MultiplicityRange;
32+
import org.omg.sysml.lang.sysml.OwningMembership;
33+
import org.omg.sysml.lang.sysml.Relationship;
34+
import org.omg.sysml.lang.sysml.Subsetting;
35+
import org.omg.sysml.lang.sysml.SysMLPackage;
36+
import org.omg.sysml.lang.sysml.Type;
37+
import org.omg.sysml.lang.sysml.Usage;
38+
import org.omg.sysml.util.ElementUtil;
39+
import org.omg.sysml.util.TypeUtil;
40+
import org.omg.sysml.xtext.postprocessing.SysMLParserPostProcessorFactory;
41+
42+
/** Tests the boundary between parser preparation of multiplicities and derived consultation. */
43+
public class MultiplicityPreparationTest {
44+
45+
protected static SysMLInteractive interactive;
46+
47+
/** Loads and resolves the standard library once for the textual test scenarios. */
48+
@BeforeClass
49+
public static void loadLibraries() {
50+
interactive = SysMLInteractive.createInstance();
51+
interactive.setVerbose(false);
52+
interactive.loadLibrary(Path.of(System.getProperty("libraryPath")).toAbsolutePath().toString());
53+
}
54+
55+
/** Removes each test model while retaining the shared resolved libraries. */
56+
@After
57+
public void removeResource() {
58+
interactive.removeResource();
59+
}
60+
61+
/**
62+
* Parsing end usages prepares one multiplicity before any feature consultation;
63+
* subsequent reads must preserve that multiplicity and its owning relationships.
64+
*/
65+
@Test
66+
public void parserPreparesEndMultiplicitiesBeforeConsultation() throws Exception {
67+
// Each end has an implicit multiplicity, supplied by parser post-processing.
68+
Resource resource = parse("endMultiplicities.sysml", """
69+
part def Holder {
70+
end attribute attributeEnd;
71+
end item itemEnd;
72+
end port portEnd;
73+
end ref referenceEnd;
74+
end connection connectionEnd;
75+
}
76+
""");
77+
for (String name : List.of("attributeEnd", "itemEnd", "portEnd", "referenceEnd", "connectionEnd")) {
78+
Usage usage = findByName(resource, name, Usage.class);
79+
List<Multiplicity> multiplicities = TypeUtil.getOwnedMultiplicitiesOf(usage);
80+
assertEquals(name, 1, multiplicities.size());
81+
List<Relationship> relationships = new ArrayList<>(usage.getOwnedRelationship());
82+
83+
usage.getFeature();
84+
usage.getFeature();
85+
assertEquals(name, relationships, usage.getOwnedRelationship());
86+
assertSame(name, multiplicities.get(0), TypeUtil.getOwnedMultiplicitiesOf(usage).get(0));
87+
}
88+
}
89+
90+
/**
91+
* Explicit multiplicities are retained by repeated parser preparation, without
92+
* creating a second multiplicity or replacing the existing model element.
93+
*/
94+
@Test
95+
public void repeatedPreparationPreservesExplicitMultiplicity() throws Exception {
96+
// The explicit range must remain the sole owned multiplicity.
97+
Resource resource = parse("explicitMultiplicity.sysml", """
98+
part def Holder { end item endpoint[2..4]; }
99+
""");
100+
Usage usage = findByName(resource, "endpoint", Usage.class);
101+
assertEquals(1, TypeUtil.getOwnedMultiplicitiesOf(usage).size());
102+
Multiplicity multiplicity = TypeUtil.getOwnedMultiplicitiesOf(usage).get(0);
103+
assertTrue(multiplicity instanceof MultiplicityRange);
104+
List<Relationship> relationships = new ArrayList<>(usage.getOwnedRelationship());
105+
106+
// Re-entering preparation must not duplicate or replace the explicit range.
107+
SysMLParserPostProcessorFactory.getPostProcessor(usage).postProcess();
108+
SysMLParserPostProcessorFactory.getPostProcessor(usage).postProcess();
109+
assertEquals(relationships, usage.getOwnedRelationship());
110+
assertEquals(1, TypeUtil.getOwnedMultiplicitiesOf(usage).size());
111+
assertSame(multiplicity, TypeUtil.getOwnedMultiplicitiesOf(usage).get(0));
112+
}
113+
114+
/**
115+
* Removing a prepared multiplicity simulates an edited model without that owned
116+
* element; feature consultation must not recreate it, even after clearing caches.
117+
*/
118+
@Test
119+
public void consultationDoesNotRecreateRemovedMultiplicity() throws Exception {
120+
Resource resource = parse("removedMultiplicity.sysml", """
121+
part def Holder { end item endpoint; }
122+
""");
123+
Usage usage = findByName(resource, "endpoint", Usage.class);
124+
assertEquals(1, TypeUtil.getOwnedMultiplicitiesOf(usage).size());
125+
126+
// This intermediate edited state cannot be obtained by parsing alone: remove
127+
// the multiplicity created by the parser, while retaining the end usage.
128+
usage.getOwnedRelationship().removeIf(relationship -> relationship instanceof OwningMembership
129+
&& ((OwningMembership) relationship).getOwnedMemberElement() instanceof Multiplicity);
130+
ElementUtil.clearCachesOf(usage);
131+
List<Relationship> relationships = new ArrayList<>(usage.getOwnedRelationship());
132+
133+
// A cold feature query must leave zero owned multiplicities, not materialize one.
134+
usage.getFeature();
135+
usage.getFeatureMembership();
136+
assertTrue(TypeUtil.getOwnedMultiplicitiesOf(usage).isEmpty());
137+
assertEquals(relationships, usage.getOwnedRelationship());
138+
}
139+
140+
/**
141+
* Eligible type-owned usages receive a plain default multiplicity even with
142+
* explicit subsettings or redefinitions, including chain and package targets.
143+
*/
144+
@Test
145+
public void explicitSpecializationsReceiveDefaultMultiplicity() throws Exception {
146+
Resource resource = parse("specializedMultiplicities.sysml", """
147+
attribute globalValues[0..*];
148+
part def Base { attribute values[0..*]; }
149+
part def Derived :> Base {
150+
attribute redefinedValues redefines values;
151+
attribute subsetValues subsets values;
152+
part nested : Base;
153+
attribute chainValues subsets nested.values;
154+
attribute packageValues subsets globalValues;
155+
attribute plainValues;
156+
}
157+
""");
158+
for (String name : List.of("redefinedValues", "subsetValues", "chainValues", "packageValues")) {
159+
Usage usage = findByName(resource, name, Usage.class);
160+
// ST6RI-774 allows the default even when the target has [0..*].
161+
assertEquals(name, 1, TypeUtil.getOwnedMultiplicitiesOf(usage).size());
162+
Multiplicity multiplicity = TypeUtil.getOwnedMultiplicitiesOf(usage).get(0);
163+
assertEquals(SysMLPackage.Literals.MULTIPLICITY, multiplicity.eClass());
164+
usage.getFeature();
165+
SysMLParserPostProcessorFactory.getPostProcessor(usage).postProcess();
166+
assertEquals(name, 1, TypeUtil.getOwnedMultiplicitiesOf(usage).size());
167+
}
168+
assertEquals(1, TypeUtil.getOwnedMultiplicitiesOf(findByName(resource, "plainValues", Usage.class)).size());
169+
170+
// Explicit transformation must retain the same multiplicity choices.
171+
ElementUtil.transformAll(resource, true);
172+
for (String name : List.of("redefinedValues", "subsetValues", "chainValues", "packageValues")) {
173+
assertEquals(name, 1, TypeUtil.getOwnedMultiplicitiesOf(findByName(resource, name, Usage.class)).size());
174+
}
175+
}
176+
177+
/**
178+
* Cyclic explicit subsettings still receive default multiplicities; repeated parser
179+
* preparation must terminate without resolving either target.
180+
*/
181+
@Test
182+
public void cyclicSubsettingsDoNotResolveTargets() throws Exception {
183+
Resource resource = parse("cyclicMultiplicities.sysml", """
184+
part def Holder {
185+
attribute left subsets right;
186+
attribute right subsets left;
187+
}
188+
""");
189+
for (String name : List.of("left", "right")) {
190+
Usage usage = findByName(resource, name, Usage.class);
191+
Subsetting subsetting = usage.getOwnedRelationship().stream()
192+
.filter(Subsetting.class::isInstance).map(Subsetting.class::cast).findFirst().orElseThrow();
193+
// Non-resolving reads verify that multiplicity preparation leaves the proxy intact.
194+
Object target = subsetting.eGet(SysMLPackage.Literals.SUBSETTING__SUBSETTED_FEATURE, false);
195+
assertTrue(target instanceof EObject);
196+
assertTrue(((EObject) target).eIsProxy());
197+
SysMLParserPostProcessorFactory.getPostProcessor(usage).postProcess();
198+
assertSame(target, subsetting.eGet(SysMLPackage.Literals.SUBSETTING__SUBSETTED_FEATURE, false));
199+
assertTrue(((EObject) target).eIsProxy());
200+
assertEquals(name, 1, TypeUtil.getOwnedMultiplicitiesOf(usage).size());
201+
}
202+
}
203+
204+
/**
205+
* Only eligible type-owned usages receive a default; package-owned usages,
206+
* ordinary references and non-end connections retain no owned multiplicity.
207+
*/
208+
@Test
209+
public void defaultMultiplicityDependsOnKindAndOwner() throws Exception {
210+
Resource resource = parse("multiplicityKinds.sysml", """
211+
attribute packageAttribute;
212+
item packageItem;
213+
part packagePart;
214+
port packagePort;
215+
part def Holder {
216+
attribute ownedAttribute;
217+
item ownedItem;
218+
part ownedPart;
219+
port ownedPort;
220+
ref reference;
221+
connection link;
222+
}
223+
""");
224+
// Eligible usages receive Multiplicity, never a synthetic MultiplicityRange.
225+
for (String name : List.of("ownedAttribute", "ownedItem", "ownedPart", "ownedPort")) {
226+
List<Multiplicity> multiplicities = TypeUtil.getOwnedMultiplicitiesOf(findByName(resource, name, Usage.class));
227+
assertEquals(name, 1, multiplicities.size());
228+
assertEquals(name, SysMLPackage.Literals.MULTIPLICITY, multiplicities.get(0).eClass());
229+
}
230+
// Neither package ownership nor an ineligible kind may introduce a default.
231+
for (String name : List.of("packageAttribute", "packageItem", "packagePart", "packagePort", "reference", "link")) {
232+
assertTrue(name, TypeUtil.getOwnedMultiplicitiesOf(findByName(resource, name, Usage.class)).isEmpty());
233+
}
234+
}
235+
236+
private Resource parse(String name, String text) throws Exception {
237+
interactive.setResource(name);
238+
interactive.parse(text);
239+
Element rootElement = interactive.getRootElement();
240+
SysMLInteractiveResult result = new SysMLInteractiveResult(rootElement, Collections.emptyList());
241+
assertTrue("'" + text + "':\n" + result.formatIssues(), result.getIssues().isEmpty());
242+
return interactive.getResource();
243+
}
244+
245+
private static <T extends Type> T findByName(Resource resource, String name, Class<T> kind) {
246+
for (var contents = resource.getAllContents(); contents.hasNext();) {
247+
EObject object = contents.next();
248+
if (kind.isInstance(object) && name.equals(kind.cast(object).getDeclaredName())) {
249+
return kind.cast(object);
250+
}
251+
}
252+
throw new AssertionError("Missing " + name);
253+
}
254+
255+
}

‎org.omg.sysml.interactive/src/org/omg/sysml/interactive/SysMLInteractive.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,15 @@ public String getApiBasePath() {
164164
}
165165

166166
public int next(String extension) {
167-
this.resource = this.createResource(counter + extension);
167+
this.setResource(counter + extension);
168168
this.addInputResource(this.resource);
169169
return this.counter++;
170170
}
171171

172+
public void setResource(String path) {
173+
this.resource = this.createResource(path);
174+
}
175+
172176
public Resource getResource() {
173177
return this.resource;
174178
}
Binary file not shown.

‎org.omg.sysml.logic/src/main/java/org/omg/sysml/adapter/AttributeUsageAdapter.java‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*******************************************************************************
22
* SysML 2 Pilot Implementation
33
* Copyright (c) 2021 Model Driven Solutions, Inc.
4+
* Copyright (c) 2026 Obeo
45
*
56
* This program is free software: you can redistribute it and/or modify
67
* it under the terms of the Eclipse Public License as published by
@@ -31,10 +32,4 @@ public AttributeUsageAdapter(AttributeUsage element) {
3132
public AttributeUsage getTarget() {
3233
return (AttributeUsage)super.getTarget();
3334
}
34-
35-
@Override
36-
protected boolean isAddMultiplicity() {
37-
return isAddDefaultMultiplicity();
38-
}
39-
4035
}

‎org.omg.sysml.logic/src/main/java/org/omg/sysml/adapter/ConnectionUsageAdapter.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*******************************************************************************
22
* SysML 2 Pilot Implementation
33
* Copyright (c) 2021, 2025 Model Driven Solutions, Inc.
4+
* Copyright (c) 2026 Obeo
45
*
56
* This program is free software: you can redistribute it and/or modify
67
* it under the terms of the Eclipse Public License as published by
@@ -61,12 +62,7 @@ protected String getDefaultSupertype() {
6162
getDefaultSupertype("base"):
6263
getDefaultSupertype("binary");
6364
}
64-
65-
@Override
66-
protected boolean isAddMultiplicity() {
67-
return getTarget().isEnd();
68-
}
69-
65+
7066
@Override
7167
public void doTransform() {
7268
ConnectionUsage target = getTarget();

‎org.omg.sysml.logic/src/main/java/org/omg/sysml/adapter/ItemUsageAdapter.java‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*******************************************************************************
22
* SysML 2 Pilot Implementation
33
* Copyright (c) 2021, 2024 Model Driven Solutions, Inc.
4+
* Copyright (c) 2026 Obeo
45
*
56
* This program is free software: you can redistribute it and/or modify
67
* it under the terms of the Eclipse Public License as published by
@@ -65,12 +66,4 @@ public boolean isSubitem() {
6566
return target.isComposite() &&
6667
(owningType instanceof ItemDefinition || owningType instanceof ItemUsage);
6768
}
68-
69-
// Transformation
70-
71-
@Override
72-
protected boolean isAddMultiplicity() {
73-
return isAddDefaultMultiplicity();
74-
}
75-
7669
}

‎org.omg.sysml.logic/src/main/java/org/omg/sysml/adapter/PortUsageAdapter.java‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,4 @@ public boolean isSubport() {
7676
Type owningType = target.getOwningType();
7777
return target.isComposite() && (owningType instanceof PortDefinition || owningType instanceof PortUsage);
7878
}
79-
80-
// Transformation
81-
82-
@Override
83-
protected boolean isAddMultiplicity() {
84-
return isAddDefaultMultiplicity();
85-
}
86-
8779
}

‎org.omg.sysml.logic/src/main/java/org/omg/sysml/adapter/UsageAdapter.java‎

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
/*******************************************************************************
22
* SysML 2 Pilot Implementation
3-
<<<<<<< HEAD
4-
* Copyright (c) 2021-2025, 2026 Model Driven Solutions, Inc.
5-
* Copyright (c) 2026 Obeo
6-
=======
73
* Copyright (c) 2021-2026 Model Driven Solutions, Inc.
8-
>>>>>>> refs/remotes/origin/HEAD
4+
* Copyright (c) 2026 Obeo
95
*
106
* This program is free software: you can redistribute it and/or modify
117
* it under the terms of the Eclipse Public License as published by
@@ -145,30 +141,6 @@ public void addDefaultGeneralType() {
145141

146142
// Transformation
147143

148-
// Used to check for default multiplicity for AttributeUsages, ItemUsages and PortUsages.
149-
protected boolean isAddDefaultMultiplicity() {
150-
Usage target = getTarget();
151-
return target.isEnd() ||
152-
target.getOwningType() != null &&
153-
target.getOwnedSubsetting().stream().
154-
map(Subsetting::getSubsettedFeature).
155-
filter(f->f != null).
156-
map(FeatureUtil::getBasicFeatureOf).
157-
noneMatch(f->f != null && f.getOwningType() != null);
158-
}
159-
160-
// Multiplicity of 1..1 is always the default for an end usage.
161-
protected boolean isAddMultiplicity() {
162-
return getTarget().isEnd();
163-
}
164-
165-
@Override
166-
public void addAdditionalMembers() {
167-
if (isAddMultiplicity()) {
168-
TypeUtil.addMultiplicityTo(getTarget());
169-
}
170-
}
171-
172144
/**
173145
* Return the relevant subject parameter to which a Usage should be bound.
174146
*/

0 commit comments

Comments
 (0)