Skip to content

Commit 1390bff

Browse files
flatombeAxelRICHARD
authored andcommitted
[1018] Rework library loading to be extensible and re-usable
* Move our library loading code into SysONLibraryLoader, which downstream applications may re-use to load their own libraries from the classpath. * Also make the library loading unit tests rely on them. * Make internal methods of SysMLStandardLibrariesConfiguration protected instead of private so it can be easily extended/redefined. * The list of EPackages to register, and of libraries to load, are now defined in their own protected method so as to be easily redefined. * Move KerML/SysML standard libraries loading out of the constructor of the @configuration. Now the standard libraries get loaded when they are needed for the first time. Downstream applications may choose this moment to be at application start, or, by default, at the latest time possible, that is, when first opening a SysML project. * Rename 'SysMLStandardLibrariesConfiguration' into 'SysONDefaultLibrariesConfiguration' to reflect the fact that default libraries may include non-standard libraries and that it is up to the application to determine which libraries are the default ones. * Update changelog and release notes accordingly. Bug: #1018 Signed-off-by: Florent Latombe <florent.latombe@obeo.fr>
1 parent 7fc4299 commit 1390bff

File tree

10 files changed

+394
-158
lines changed

10 files changed

+394
-158
lines changed

CHANGELOG.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* A new service has been added to centralize the behavior of moving semantic elements in _org.eclipse.syson.services.api.ISysMLMoveElementService_.
1111
It replaces the public methods _UtilService.moveMembership_ and _ToolService.moveSemanticElement_
1212
* A new service has been added to centralize the verification of read-only elements in _org.eclipse.syson.services.api.ISysMLReadOnlyService_.
13+
- https://github.com/eclipse-syson/syson/issues/1018[#1018] `SysMLStandardLibrariesConfiguration` has been refactored and renamed `SysONDefaultLibrariesConfiguration`.
1314

1415

1516
=== Dependency update
@@ -54,6 +55,7 @@ The changes are:
5455
- https://github.com/eclipse-syson/syson/issues/960[#960] [general-view] In the selection dialog of the creation tools for `Stakeholders` and `Actors`, display possible `Part Usage` candidates in a tree instead of a list.
5556
- https://github.com/eclipse-syson/syson/issues/1012[#1012] [general-view] Allow package nodes to be smaller than their default size
5657
- https://github.com/eclipse-syson/syson/issues/1006[#1006] [details] Display `FeatureValue.isDefault` and `FeatureValue.isInitial` in Core tab of `FeatureValue` concept.
58+
- https://github.com/eclipse-syson/syson/issues/1018[#1018] [libraries] Make customizing the default metamodels and libraries available in SysML projects easier by making default implementation `SysONDefaultLibrariesConfiguration` more extensible.
5759

5860

5961
=== New features

backend/application/syson-application-configuration/src/main/java/org/eclipse/syson/application/configuration/SysMLEditingContextProcessor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.springframework.stereotype.Service;
3737

3838
/**
39-
* {@link IEditingContextProcessor} for SysML libraries. All SysML standard libraries should be loaded when a project
39+
* {@link IEditingContextProcessor} for SysML libraries. All SysML default libraries should be loaded when a project
4040
* (i.e. an editing context) is loaded.
4141
*
4242
* @author arichard
@@ -46,12 +46,12 @@ public class SysMLEditingContextProcessor implements IEditingContextProcessor {
4646

4747
private final Logger logger = LoggerFactory.getLogger(SysMLEditingContextProcessor.class);
4848

49-
private final SysMLStandardLibrariesConfiguration standardLibraries;
49+
private final SysONDefaultLibrariesConfiguration defaultLibraries;
5050

5151
private final IStudioCapableEditingContextPredicate studioCapableEditingContextPredicate;
5252

53-
public SysMLEditingContextProcessor(SysMLStandardLibrariesConfiguration standardLibraries, IStudioCapableEditingContextPredicate studioCapableEditingContextPredicate) {
54-
this.standardLibraries = Objects.requireNonNull(standardLibraries);
53+
public SysMLEditingContextProcessor(SysONDefaultLibrariesConfiguration standardLibraries, IStudioCapableEditingContextPredicate studioCapableEditingContextPredicate) {
54+
this.defaultLibraries = Objects.requireNonNull(standardLibraries);
5555
this.studioCapableEditingContextPredicate = Objects.requireNonNull(studioCapableEditingContextPredicate);
5656
}
5757

@@ -63,7 +63,7 @@ public void preProcess(IEditingContext editingContext) {
6363
siriusWebEditingContext.getDomain().getResourceSet().eAdapters().add(new SysONEContentAdapter());
6464

6565
Instant start = Instant.now();
66-
ResourceSet sourceResourceSet = this.standardLibraries.getLibrariesResourceSet();
66+
ResourceSet sourceResourceSet = this.defaultLibraries.getLibrariesResourceSet();
6767
ResourceSet targetResourceSet = siriusWebEditingContext.getDomain().getResourceSet();
6868
/*
6969
* Use a common copier for all the resources to make sure cross-references are correctly copied.
@@ -93,7 +93,7 @@ public void preProcess(IEditingContext editingContext) {
9393
copier.copyReferences();
9494
Instant finish = Instant.now();
9595
long timeElapsed = Duration.between(start, finish).toMillis();
96-
this.logger.info("Copy all standard libraries in the editing context in {} ms", timeElapsed);
96+
this.logger.info("Copied all default libraries in the editing context in {} ms", timeElapsed);
9797
}
9898
}
9999

backend/application/syson-application-configuration/src/main/java/org/eclipse/syson/application/configuration/SysMLStandardLibrariesConfiguration.java

Lines changed: 0 additions & 107 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024, 2025 Obeo.
3+
* This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Obeo - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.syson.application.configuration;
14+
15+
import java.nio.file.Paths;
16+
import java.time.Duration;
17+
import java.time.Instant;
18+
import java.util.Collections;
19+
import java.util.List;
20+
import java.util.stream.Stream;
21+
22+
import org.eclipse.emf.ecore.EPackage;
23+
import org.eclipse.emf.ecore.impl.EPackageRegistryImpl;
24+
import org.eclipse.emf.ecore.resource.ResourceSet;
25+
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
26+
import org.eclipse.sirius.components.emf.services.JSONResourceFactory;
27+
import org.eclipse.sirius.emfjson.resource.JsonResourceFactoryImpl;
28+
import org.eclipse.syson.application.libraries.SysONLibraryLoader;
29+
import org.eclipse.syson.application.libraries.SysONLibraryLoadingDefinition;
30+
import org.eclipse.syson.sysml.SysmlPackage;
31+
import org.eclipse.syson.sysml.helper.EMFUtils;
32+
import org.eclipse.syson.sysml.util.ElementUtil;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
35+
import org.springframework.context.annotation.Configuration;
36+
37+
/**
38+
* Configuration to load the KerML and SysML standard libraries.
39+
*
40+
* @author arichard
41+
*/
42+
@Configuration
43+
public class SysONDefaultLibrariesConfiguration {
44+
45+
public static final SysONLibraryLoadingDefinition KERML = new SysONLibraryLoadingDefinition(
46+
"KerML Standard Library",
47+
ElementUtil.KERML_LIBRARY_SCHEME,
48+
Paths.get("kerml.libraries"),
49+
Collections.singletonList(JsonResourceFactoryImpl.EXTENSION),
50+
new JSONResourceFactory()::createResource);
51+
52+
public static final SysONLibraryLoadingDefinition SYSML = new SysONLibraryLoadingDefinition(
53+
"SysML Standard Library",
54+
ElementUtil.SYSML_LIBRARY_SCHEME,
55+
Paths.get("sysml.libraries"),
56+
Collections.singletonList(JsonResourceFactoryImpl.EXTENSION),
57+
new JSONResourceFactory()::createResource);
58+
59+
private static final Logger LOGGER = LoggerFactory.getLogger(SysONDefaultLibrariesConfiguration.class);
60+
61+
private ResourceSet librariesResourceSet;
62+
63+
public SysONDefaultLibrariesConfiguration() {
64+
}
65+
66+
public ResourceSet getLibrariesResourceSet() {
67+
if (this.librariesResourceSet == null) {
68+
this.librariesResourceSet = this.createAndInitializeResourceSetWithLibraries();
69+
}
70+
return this.librariesResourceSet;
71+
}
72+
73+
protected ResourceSet createAndInitializeResourceSetWithLibraries() {
74+
Instant start = Instant.now();
75+
76+
final EPackage.Registry ePackageRegistry = new EPackageRegistryImpl();
77+
final ResourceSet resourceSet = new ResourceSetImpl();
78+
resourceSet.setPackageRegistry(ePackageRegistry);
79+
80+
this.populateWithDefaultEPackages(ePackageRegistry);
81+
this.populateWithDefaultLibraries(resourceSet);
82+
83+
EMFUtils.resolveAllNonDerived(resourceSet);
84+
85+
Instant finish = Instant.now();
86+
long timeElapsed = Duration.between(start, finish).toMillis();
87+
LOGGER.info("Initialization of default libraries completed in %sms".formatted(timeElapsed));
88+
89+
return resourceSet;
90+
}
91+
92+
/**
93+
* Provides the default libraries. The default libraries are made available to all SysML projects of the
94+
* application.
95+
*
96+
* Override this to customize the standard libraries or add extra default libraries to all SysML projects.
97+
*
98+
* @return a (non-{@code null}) {@link List} of (non-{@code null}) {@link SysONLibraryLoadingDefinition}.
99+
*/
100+
protected List<SysONLibraryLoadingDefinition> getDefaultLibraries() {
101+
return Stream.of(KERML, SYSML).toList();
102+
}
103+
104+
/**
105+
* Provides the default metamodels to register.
106+
*
107+
* @return a (non-{@code null}) {@link List} of (non-{@code null}) {@link EPackage}.
108+
*/
109+
protected List<EPackage> getDefaultEPackages() {
110+
return Collections.singletonList(SysmlPackage.eINSTANCE);
111+
}
112+
113+
protected void populateWithDefaultLibraries(final ResourceSet resourceSet) {
114+
final SysONLibraryLoader sysonLibraryLoader = new SysONLibraryLoader();
115+
for (final SysONLibraryLoadingDefinition libraryLoadingDefinition : this.getDefaultLibraries()) {
116+
sysonLibraryLoader.loadLibraryResources(resourceSet, libraryLoadingDefinition);
117+
}
118+
}
119+
120+
protected void populateWithDefaultEPackages(final EPackage.Registry ePackageRegistry) {
121+
for (final EPackage ePackage : this.getDefaultEPackages()) {
122+
ePackageRegistry.put(ePackage.getNsURI(), ePackage);
123+
LOGGER.info("Registered EPackage '%s' (prefix '%s') against URI '%s'".formatted(ePackage.getName(), ePackage.getNsPrefix(), ePackage.getNsURI()));
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)