Skip to content

Commit fbfbc75

Browse files
committed
ST6RI-926 Move isLibraryResource to SysMLLibraryUtil
Signed-off-by: Axel RICHARD <axel.richard@obeo.fr>
1 parent a4fcc1c commit fbfbc75

4 files changed

Lines changed: 124 additions & 48 deletions

File tree

org.omg.kerml.xtext/src/org/omg/kerml/xtext/validation/KerMLValidator.xtend

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* Copyright (c) 2018 IncQuery Labs Ltd.
44
* Copyright (c) 2018-2026 Model Driven Solutions, Inc.
55
* Copyright (c) 2020 California Institute of Technology/Jet Propulsion Laboratory
6-
*
6+
* Copyright (c) 2026 Obeo
7+
*
78
* This program is free software: you can redistribute it and/or modify
89
* it under the terms of the Eclipse Public License as published by
910
* the Eclipse Foundation, version 2 of the License.
@@ -24,6 +25,7 @@
2425
* Balazs Grill, IncQuery
2526
* Ed Seidewitz, MDS
2627
* Miyako Wilson, JPL
28+
* Axel Richard, Obeo
2729
*
2830
*****************************************************************************/
2931
package org.omg.kerml.xtext.validation
@@ -33,7 +35,6 @@ import java.util.List
3335
import org.eclipse.emf.ecore.EObject
3436
import org.eclipse.emf.ecore.EClass
3537
import org.eclipse.emf.ecore.EStructuralFeature
36-
import org.eclipse.emf.ecore.resource.Resource
3738
import org.eclipse.xtext.validation.Check
3839

3940
import org.omg.sysml.lang.sysml.Type
@@ -1421,22 +1422,13 @@ class KerMLValidator extends AbstractKerMLValidator {
14211422

14221423
@Check
14231424
def checkLibraryPackage(LibraryPackage pkg) {
1424-
if (pkg.isStandard && !pkg.eResource.isModelLibrary) {
1425+
if (pkg.isStandard && !SysMLLibraryUtil.isLibraryResource(pkg.eResource)) {
14251426
warning(INVALID_LIBRARY_PACKAGE_NOT_STANDARD_MSG, pkg, SysMLPackage.eINSTANCE.libraryPackage_IsStandard, INVALID_LIBRARY_PACKAGE_NOT_STANDARD)
14261427
}
14271428
}
1428-
1429+
14291430
/* Utility Methods */
1430-
1431-
private def static boolean isModelLibrary(Resource resource) {
1432-
if (resource === null) {
1433-
return false;
1434-
} else {
1435-
val path = resource.URI.devicePath ?: resource.URI.path;
1436-
return path !== null && path.contains(SysMLLibraryUtil.modelLibraryPath);
1437-
}
1438-
}
1439-
1431+
14401432
def static boolean isBoolean(Expression condition) {
14411433
specializesFromLibrary(condition, condition.result, "ScalarValues::Boolean") ||
14421434
// LiteralBooleans currently don't have an inferred Boolean result type.

org.omg.sysml.logic/src/main/java/org/omg/sysml/logic/ResourceSetModelLibraryProvider.java

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*******************************************************************************
22
* SysML 2 Pilot Implementation
3-
* Copyright (c) Obeo
4-
*
3+
* Copyright (c) 2026 Obeo
4+
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the Eclipse Public License as published by
77
* the Eclipse Foundation, version 2 of the License.
@@ -16,7 +16,7 @@
1616
* along with this program. If not, see <https://www.eclipse.org/legal/epl-2.0/>.
1717
*
1818
* @license EPL-2.0 <http://spdx.org/licenses/EPL-2.0>
19-
*
19+
*
2020
*******************************************************************************/
2121

2222
package org.omg.sysml.logic;
@@ -44,8 +44,6 @@
4444
*/
4545
public class ResourceSetModelLibraryProvider implements IModelLibraryProvider {
4646

47-
private static final String MODEL_LIBRARY_FOLDER = "sysml.library";
48-
4947
/**
5048
* Resolves a qualified library name against the resources already loaded in
5149
* the context element's resource set.
@@ -87,7 +85,7 @@ private List<Resource> getCandidateResources(ResourceSet resourceSet) {
8785
List<Resource> otherResources = new ArrayList<>();
8886

8987
for (Resource resource : resourceSet.getResources()) {
90-
if (isLibraryResource(resource)) {
88+
if (SysMLLibraryUtil.isLibraryResource(resource)) {
9189
libraryResources.add(resource);
9290
} else {
9391
otherResources.add(resource);
@@ -97,31 +95,6 @@ private List<Resource> getCandidateResources(ResourceSet resourceSet) {
9795
return libraryResources.isEmpty() ? otherResources : libraryResources;
9896
}
9997

100-
/**
101-
* Identifies resources that should be treated as library candidates based on
102-
* the configured model library path or the conventional {@code sysml.library}
103-
* folder name in the URI.
104-
*/
105-
private boolean isLibraryResource(Resource resource) {
106-
if (resource == null || resource.getURI() == null) {
107-
return false;
108-
}
109-
110-
String modelLibraryPath = SysMLLibraryUtil.getModelLibraryPath();
111-
String uriString = resource.getURI().toString();
112-
String fileString = resource.getURI().toFileString();
113-
if (modelLibraryPath != null && !modelLibraryPath.isBlank()) {
114-
if (uriString.contains(modelLibraryPath)) {
115-
return true;
116-
}
117-
if (fileString != null && fileString.contains(modelLibraryPath)) {
118-
return true;
119-
}
120-
}
121-
122-
return resource.getURI().segmentsList().contains(MODEL_LIBRARY_FOLDER);
123-
}
124-
12598
/**
12699
* Attempts to resolve the qualified-name segments against each root element
127100
* contained by a resource.

org.omg.sysml.logic/src/main/java/org/omg/sysml/util/SysMLLibraryUtil.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*******************************************************************************
22
* SysML 2 Pilot Implementation
3-
* Copyright (c) Obeo
4-
*
3+
* Copyright (c) 2026 Obeo
4+
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the Eclipse Public License as published by
77
* the Eclipse Foundation, version 2 of the License.
@@ -16,7 +16,7 @@
1616
* along with this program. If not, see <https://www.eclipse.org/legal/epl-2.0/>.
1717
*
1818
* @license EPL-2.0 <http://spdx.org/licenses/EPL-2.0>
19-
*
19+
*
2020
*******************************************************************************/
2121

2222
package org.omg.sysml.util;
@@ -106,6 +106,43 @@ public static String getModelLibraryPath() {
106106
return modelLibraryPath;
107107
}
108108

109+
/**
110+
* Identifies whether an EMF resource should be treated as belonging to the
111+
* SysML/KerML model libraries.
112+
*
113+
* <p>A resource is considered a library resource when its URI matches the
114+
* currently configured model-library path, or when its URI contains the
115+
* final segment of {@link #DEFAULT_MODEL_LIBRARY_PATH}. Both the URI string
116+
* and file string forms are checked so this method works across platform,
117+
* file, and standalone EMF resource sets.
118+
*
119+
* @param resource the resource to classify
120+
* @return {@code true} if the resource appears to be a model-library resource;
121+
* {@code false} otherwise
122+
*/
123+
public static boolean isLibraryResource(Resource resource) {
124+
if (resource == null || resource.getURI() == null) {
125+
return false;
126+
}
127+
128+
URI uri = resource.getURI();
129+
String currentModelLibraryPath = getModelLibraryPath();
130+
if (currentModelLibraryPath != null && !currentModelLibraryPath.isBlank()) {
131+
String uriString = uri.toString();
132+
if (uriString.contains(currentModelLibraryPath)) {
133+
return true;
134+
}
135+
136+
String fileString = uri.toFileString();
137+
if (fileString != null && fileString.contains(currentModelLibraryPath)) {
138+
return true;
139+
}
140+
}
141+
142+
String defaultModelLibrarySegment = URI.createURI(DEFAULT_MODEL_LIBRARY_PATH).lastSegment();
143+
return defaultModelLibrarySegment != null && uri.segmentsList().contains(defaultModelLibrarySegment);
144+
}
145+
109146
/**
110147
* Installs the runtime-specific provider lookup used for subsequent library
111148
* resolution requests.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*******************************************************************************
2+
* SysML 2 Pilot Implementation
3+
* Copyright (c) 2026 Obeo
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the Eclipse Public License as published by
7+
* the Eclipse Foundation, version 2 of the License.
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* Eclipse Public License for more details.
14+
*
15+
* You should have received a copy of theEclipse Public License
16+
* along with this program. If not, see <https://www.eclipse.org/legal/epl-2.0/>.
17+
*
18+
* @license EPL-2.0 <http://spdx.org/licenses/EPL-2.0>
19+
*
20+
*******************************************************************************/
21+
22+
package org.omg.sysml.logic;
23+
24+
import static org.junit.Assert.assertFalse;
25+
import static org.junit.Assert.assertTrue;
26+
27+
import org.eclipse.emf.common.util.URI;
28+
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
29+
import org.junit.Test;
30+
import org.omg.sysml.util.SysMLLibraryUtil;
31+
32+
/**
33+
* Verifies classification of EMF resources as SysML/KerML model-library
34+
* resources.
35+
*/
36+
public class SysMLLibraryUtilTest {
37+
38+
@Test
39+
public void nullResourceIsNotLibraryResource() {
40+
assertFalse(SysMLLibraryUtil.isLibraryResource(null));
41+
}
42+
43+
@Test
44+
public void resourceWithoutUriIsNotLibraryResource() {
45+
assertFalse(SysMLLibraryUtil.isLibraryResource(new ResourceImpl()));
46+
}
47+
48+
@Test
49+
public void configuredModelLibraryDirectoryIsLibraryResource() {
50+
SysMLLibraryUtil.setModelLibraryDirectory("/tmp/sysml.library");
51+
52+
ResourceImpl resource = new ResourceImpl(URI.createFileURI("/tmp/sysml.library/Base.sysml"));
53+
54+
assertTrue(SysMLLibraryUtil.isLibraryResource(resource));
55+
}
56+
57+
@Test
58+
public void conventionalModelLibraryFolderIsLibraryResource() {
59+
SysMLLibraryUtil.setModelLibraryDirectory("/tmp/other-library");
60+
61+
ResourceImpl resource = new ResourceImpl(URI.createURI("platform:/resource/sysml.library/Base.sysml"));
62+
63+
assertTrue(SysMLLibraryUtil.isLibraryResource(resource));
64+
}
65+
66+
@Test
67+
public void ordinaryModelResourceIsNotLibraryResource() {
68+
SysMLLibraryUtil.setModelLibraryDirectory("/tmp/sysml.library");
69+
70+
ResourceImpl resource = new ResourceImpl(URI.createFileURI("/tmp/model/Test.sysml"));
71+
72+
assertFalse(SysMLLibraryUtil.isLibraryResource(resource));
73+
}
74+
}

0 commit comments

Comments
 (0)