11/*******************************************************************************
22 * SysML 2 Pilot Implementation
33 * Copyright (c) 2026 Obeo
4+ * Copyright (c) 2026 Model Driven Solutions, Inc.
45 *
56 * This program and the accompanying materials are made available under the
67 * terms of the Eclipse Public License 2.0 which is available at
1415import static org .junit .Assert .assertSame ;
1516import static org .junit .Assert .assertTrue ;
1617
17- import java .io .ByteArrayInputStream ;
18- import java .nio .charset .StandardCharsets ;
1918import java .nio .file .Path ;
2019import java .util .ArrayList ;
20+ import java .util .Collections ;
2121import java .util .List ;
22- import java .util .Map ;
23-
24- import org .eclipse .emf .common .util .URI ;
2522import org .eclipse .emf .ecore .EObject ;
2623import org .eclipse .emf .ecore .resource .Resource ;
27- import org .eclipse .emf .ecore .resource .ResourceSet ;
28- import org .eclipse .xtext .EcoreUtil2 ;
2924import org .junit .After ;
3025import org .junit .BeforeClass ;
3126import org .junit .Test ;
3227import org .omg .sysml .interactive .SysMLInteractive ;
28+ import org .omg .sysml .interactive .SysMLInteractiveResult ;
29+ import org .omg .sysml .lang .sysml .Element ;
3330import org .omg .sysml .lang .sysml .Multiplicity ;
3431import org .omg .sysml .lang .sysml .MultiplicityRange ;
3532import org .omg .sysml .lang .sysml .OwningMembership ;
3936import org .omg .sysml .lang .sysml .Type ;
4037import org .omg .sysml .lang .sysml .Usage ;
4138import org .omg .sysml .util .ElementUtil ;
42- import org .omg .sysml .util .NamespaceUtil ;
39+ import org .omg .sysml .util .TypeUtil ;
4340import org .omg .sysml .xtext .postprocessing .SysMLParserPostProcessorFactory ;
4441
4542/** Tests the boundary between parser preparation of multiplicities and derived consultation. */
4643public class MultiplicityPreparationTest {
4744
48- private static ResourceSet resourceSet ;
49- private final List <Resource > models = new ArrayList <>();
45+ protected static SysMLInteractive interactive ;
5046
5147 /** Loads and resolves the standard library once for the textual test scenarios. */
5248 @ BeforeClass
5349 public static void loadLibraries () {
54- SysMLInteractive interactive = SysMLInteractive .createInstance ();
50+ interactive = SysMLInteractive .createInstance ();
5551 interactive .setVerbose (false );
56- interactive .getLibraryIndexCache ().setIndexDisabled (true );
5752 interactive .loadLibrary (Path .of (System .getProperty ("libraryPath" )).toAbsolutePath ().toString ());
58- resourceSet = interactive .getResourceSet ();
59- for (int i = 0 ; i < resourceSet .getResources ().size (); i ++) {
60- EcoreUtil2 .resolveLazyCrossReferences (resourceSet .getResources ().get (i ), null );
61- }
6253 }
63-
54+
6455 /** Removes each test model while retaining the shared resolved libraries. */
6556 @ After
66- public void removeModels () {
67- for (Resource resource : models ) {
68- resource .unload ();
69- resourceSet .getResources ().remove (resource );
70- }
57+ public void removeResource () {
58+ interactive .removeResource ();
7159 }
7260
7361 /**
@@ -88,14 +76,14 @@ public void parserPreparesEndMultiplicitiesBeforeConsultation() throws Exception
8876 """ );
8977 for (String name : List .of ("attributeEnd" , "itemEnd" , "portEnd" , "referenceEnd" , "connectionEnd" )) {
9078 Usage usage = findByName (resource , name , Usage .class );
91- List <Multiplicity > multiplicities = ownedMultiplicities (usage );
79+ List <Multiplicity > multiplicities = TypeUtil . getOwnedMultiplicitiesOf (usage );
9280 assertEquals (name , 1 , multiplicities .size ());
9381 List <Relationship > relationships = new ArrayList <>(usage .getOwnedRelationship ());
9482
9583 usage .getFeature ();
9684 usage .getFeature ();
9785 assertEquals (name , relationships , usage .getOwnedRelationship ());
98- assertSame (name , multiplicities .get (0 ), ownedMultiplicities (usage ).get (0 ));
86+ assertSame (name , multiplicities .get (0 ), TypeUtil . getOwnedMultiplicitiesOf (usage ).get (0 ));
9987 }
10088 }
10189
@@ -110,17 +98,17 @@ public void repeatedPreparationPreservesExplicitMultiplicity() throws Exception
11098 part def Holder { end item endpoint[2..4]; }
11199 """ );
112100 Usage usage = findByName (resource , "endpoint" , Usage .class );
113- assertEquals (1 , ownedMultiplicities (usage ).size ());
114- Multiplicity multiplicity = ownedMultiplicities (usage ).get (0 );
101+ assertEquals (1 , TypeUtil . getOwnedMultiplicitiesOf (usage ).size ());
102+ Multiplicity multiplicity = TypeUtil . getOwnedMultiplicitiesOf (usage ).get (0 );
115103 assertTrue (multiplicity instanceof MultiplicityRange );
116104 List <Relationship > relationships = new ArrayList <>(usage .getOwnedRelationship ());
117105
118106 // Re-entering preparation must not duplicate or replace the explicit range.
119107 SysMLParserPostProcessorFactory .getPostProcessor (usage ).postProcess ();
120108 SysMLParserPostProcessorFactory .getPostProcessor (usage ).postProcess ();
121109 assertEquals (relationships , usage .getOwnedRelationship ());
122- assertEquals (1 , ownedMultiplicities (usage ).size ());
123- assertSame (multiplicity , ownedMultiplicities (usage ).get (0 ));
110+ assertEquals (1 , TypeUtil . getOwnedMultiplicitiesOf (usage ).size ());
111+ assertSame (multiplicity , TypeUtil . getOwnedMultiplicitiesOf (usage ).get (0 ));
124112 }
125113
126114 /**
@@ -133,7 +121,7 @@ public void consultationDoesNotRecreateRemovedMultiplicity() throws Exception {
133121 part def Holder { end item endpoint; }
134122 """ );
135123 Usage usage = findByName (resource , "endpoint" , Usage .class );
136- assertEquals (1 , ownedMultiplicities (usage ).size ());
124+ assertEquals (1 , TypeUtil . getOwnedMultiplicitiesOf (usage ).size ());
137125
138126 // This intermediate edited state cannot be obtained by parsing alone: remove
139127 // the multiplicity created by the parser, while retaining the end usage.
@@ -145,7 +133,7 @@ public void consultationDoesNotRecreateRemovedMultiplicity() throws Exception {
145133 // A cold feature query must leave zero owned multiplicities, not materialize one.
146134 usage .getFeature ();
147135 usage .getFeatureMembership ();
148- assertTrue (ownedMultiplicities (usage ).isEmpty ());
136+ assertTrue (TypeUtil . getOwnedMultiplicitiesOf (usage ).isEmpty ());
149137 assertEquals (relationships , usage .getOwnedRelationship ());
150138 }
151139
@@ -170,19 +158,19 @@ public void explicitSpecializationsReceiveDefaultMultiplicity() throws Exception
170158 for (String name : List .of ("redefinedValues" , "subsetValues" , "chainValues" , "packageValues" )) {
171159 Usage usage = findByName (resource , name , Usage .class );
172160 // ST6RI-774 allows the default even when the target has [0..*].
173- assertEquals (name , 1 , ownedMultiplicities (usage ).size ());
174- Multiplicity multiplicity = ownedMultiplicities (usage ).get (0 );
161+ assertEquals (name , 1 , TypeUtil . getOwnedMultiplicitiesOf (usage ).size ());
162+ Multiplicity multiplicity = TypeUtil . getOwnedMultiplicitiesOf (usage ).get (0 );
175163 assertEquals (SysMLPackage .Literals .MULTIPLICITY , multiplicity .eClass ());
176164 usage .getFeature ();
177165 SysMLParserPostProcessorFactory .getPostProcessor (usage ).postProcess ();
178- assertEquals (name , 1 , ownedMultiplicities (usage ).size ());
166+ assertEquals (name , 1 , TypeUtil . getOwnedMultiplicitiesOf (usage ).size ());
179167 }
180- assertEquals (1 , ownedMultiplicities (findByName (resource , "plainValues" , Usage .class )).size ());
168+ assertEquals (1 , TypeUtil . getOwnedMultiplicitiesOf (findByName (resource , "plainValues" , Usage .class )).size ());
181169
182170 // Explicit transformation must retain the same multiplicity choices.
183171 ElementUtil .transformAll (resource , true );
184172 for (String name : List .of ("redefinedValues" , "subsetValues" , "chainValues" , "packageValues" )) {
185- assertEquals (name , 1 , ownedMultiplicities (findByName (resource , name , Usage .class )).size ());
173+ assertEquals (name , 1 , TypeUtil . getOwnedMultiplicitiesOf (findByName (resource , name , Usage .class )).size ());
186174 }
187175 }
188176
@@ -209,7 +197,7 @@ public void cyclicSubsettingsDoNotResolveTargets() throws Exception {
209197 SysMLParserPostProcessorFactory .getPostProcessor (usage ).postProcess ();
210198 assertSame (target , subsetting .eGet (SysMLPackage .Literals .SUBSETTING__SUBSETTED_FEATURE , false ));
211199 assertTrue (((EObject ) target ).eIsProxy ());
212- assertEquals (name , 1 , ownedMultiplicities (usage ).size ());
200+ assertEquals (name , 1 , TypeUtil . getOwnedMultiplicitiesOf (usage ).size ());
213201 }
214202 }
215203
@@ -235,22 +223,23 @@ public void defaultMultiplicityDependsOnKindAndOwner() throws Exception {
235223 """ );
236224 // Eligible usages receive Multiplicity, never a synthetic MultiplicityRange.
237225 for (String name : List .of ("ownedAttribute" , "ownedItem" , "ownedPart" , "ownedPort" )) {
238- List <Multiplicity > multiplicities = ownedMultiplicities (findByName (resource , name , Usage .class ));
226+ List <Multiplicity > multiplicities = TypeUtil . getOwnedMultiplicitiesOf (findByName (resource , name , Usage .class ));
239227 assertEquals (name , 1 , multiplicities .size ());
240228 assertEquals (name , SysMLPackage .Literals .MULTIPLICITY , multiplicities .get (0 ).eClass ());
241229 }
242230 // Neither package ownership nor an ineligible kind may introduce a default.
243231 for (String name : List .of ("packageAttribute" , "packageItem" , "packagePart" , "packagePort" , "reference" , "link" )) {
244- assertTrue (name , ownedMultiplicities (findByName (resource , name , Usage .class )).isEmpty ());
232+ assertTrue (name , TypeUtil . getOwnedMultiplicitiesOf (findByName (resource , name , Usage .class )).isEmpty ());
245233 }
246234 }
247235
248236 private Resource parse (String name , String text ) throws Exception {
249- Resource resource = resourceSet .createResource (URI .createURI ("memory:/" + name ));
250- models .add (resource );
251- resource .load (new ByteArrayInputStream (text .getBytes (StandardCharsets .UTF_8 )), Map .of ());
252- assertTrue (resource .getErrors ().toString (), resource .getErrors ().isEmpty ());
253- return resource ;
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 ();
254243 }
255244
256245 private static <T extends Type > T findByName (Resource resource , String name , Class <T > kind ) {
@@ -263,8 +252,4 @@ private static <T extends Type> T findByName(Resource resource, String name, Cla
263252 throw new AssertionError ("Missing " + name );
264253 }
265254
266- private static List <Multiplicity > ownedMultiplicities (Usage usage ) {
267- return NamespaceUtil .getOwnedMembersOf (usage ).filter (Multiplicity .class ::isInstance )
268- .map (Multiplicity .class ::cast ).toList ();
269- }
270255}
0 commit comments