|
| 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 | +} |
0 commit comments