diff --git a/CLAUDE.md b/CLAUDE.md index d07197a..d31b608 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -66,7 +66,7 @@ mockable. | --- | --- | | `Program.cs` | Entry point: parses args, deserializes the config, calls `Split`. | | `Configuration/SplitterConfig.cs`, `PackageConfig.cs` | Config models. | -| `Services/XmiSplitterService.cs` | Orchestrator: load → find packages (recursively, optionally scoped by `rootPackageName`) → stamp → map connectors → write. Names the `uml:Model` wrapper from `modelName`, else mirrors the source's. Has a convenience ctor and an injectable ctor. | +| `Services/XmiSplitterService.cs` | Orchestrator: load → find packages (recursively, optionally scoped by `rootPackageName`) → stamp → map connectors → write. Names the `uml:Model` wrapper from `modelName`, else mirrors the source's. Writes each stereotype application to the document its extended element was stamped with, and keeps the packages enclosing the extracted ones out of every extension. Has a convenience ctor and an injectable ctor. | | `Services/XmiModelLoader.cs` | UML4NET read, with the EA extender + extension content reader (inert for non-EA files). Also detects the source's UML namespace so output matches the input's UML version. | | `Services/PackageDocumentAssigner.cs` | Stamps `IXmiElement.DocumentName`. Traverses only `AggregationKind.Composite` properties (from UML4NET `[Property]` metadata) so *referenced* elements keep pointing at their own document. Returns contained + referenced ids. | | `Services/ExtensionBuilder.cs` | Filters the EA extension per package, deciding membership from the parsed EA `Element`/`Connector` and their resolved `ExtendedElement`. `CanFilter` is false for a foreign extension, which is then copied unchanged into every output. | @@ -85,7 +85,13 @@ mockable. `xmi:Documentation` header and `href` targets all match, but expect `utf-8` instead of `windows-1252` (UML4NET hardcodes it), different attribute order, `type` as an attribute rather than a nested ``, and defaults such as `Generalization::isSubstitutable` - elided. UML4NET also promotes EA `` into UML `ownedComment`. + elided. UML4NET also promotes EA `` into UML `ownedComment`, and its 9.x + writer emits the header and the extension as `xmi:documentation`/`xmi:extension` carrying an + `xmi:type`, where EA writes `xmi:Documentation`/`xmi:Extension`. +- **The profile itself is not carried over.** Stereotype applications are written with their + profile's namespace, but the EA `` section is emptied and the model's + `profileApplication` is not copied to the new `uml:Model` wrapper: EA references its profiles + by a URI no reader can resolve. ## Conventions diff --git a/README.md b/README.md index 9da0f50..b92d9d7 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ Any UML XMI is supported. Enterprise Architect exports additionally get their `x filtered per package; the UML namespace, the `uml:Model` wrapper and the presence of an extension are all taken from the source document. +Stereotype applications are written to the file that holds the element they extend. + ## Build Status Branch | Build Status diff --git a/XSplinter.Tests/Services/XmiSplitterServiceTestFixture.cs b/XSplinter.Tests/Services/XmiSplitterServiceTestFixture.cs index d886d44..1adf17d 100644 --- a/XSplinter.Tests/Services/XmiSplitterServiceTestFixture.cs +++ b/XSplinter.Tests/Services/XmiSplitterServiceTestFixture.cs @@ -16,6 +16,7 @@ namespace XSplinter.Tests.Services using uml4net; using uml4net.Packages; + using uml4net.Profiling; using uml4net.xmi.Readers; using uml4net.xmi.Xmi; @@ -34,7 +35,7 @@ public class XmiSplitterServiceTestFixture private Mock constraintRelocator; private Mock extensionBuilder; private Mock documentWriter; - private List<(IPackage Package, string Path, Documentation Documentation, IEnumerable Extensions, string UmlNamespaceUri)> written; + private List<(IPackage Package, string Path, XmiRoot XmiRoot, string UmlNamespaceUri)> written; private SplitterConfig config; private XmiSplitterService xmiSplitterService; @@ -77,10 +78,10 @@ public void Setup() this.written = []; this.documentWriter - .Setup(x => x.Write(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny>(), It.IsAny())) - .Callback, string>( - (package, path, documentation, extensions, umlNamespaceUri) => - this.written.Add((package, path, documentation, extensions, umlNamespaceUri))); + .Setup(x => x.Write(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Callback( + (package, path, xmiRoot, umlNamespaceUri) => + this.written.Add((package, path, xmiRoot, umlNamespaceUri))); this.config = new SplitterConfig { @@ -149,12 +150,12 @@ public void Verify_that_a_convertToLibrary_package_is_written_without_a_model_wr { Assert.That(library.Package, Is.Not.InstanceOf()); Assert.That(library.Package.Name, Is.EqualTo("Primitives")); - Assert.That(library.Extensions, Is.Null); + Assert.That(library.XmiRoot.Extensions, Is.Empty); Assert.That(full.Package, Is.InstanceOf()); Assert.That(full.Package.Name, Is.EqualTo("EA_Model")); Assert.That(full.Package.PackagedElement.OfType().Single().Name, Is.EqualTo("Forge")); - Assert.That(full.Extensions, Is.Not.Null.And.Not.Empty); + Assert.That(full.XmiRoot.Extensions, Is.Not.Empty); }); } @@ -198,12 +199,48 @@ public void Verify_that_the_xmi_documentation_header_is_only_written_for_full_EA Assert.Multiple(() => { - Assert.That(library.Documentation, Is.Null); - Assert.That(full.Documentation, Is.Not.Null); - Assert.That(full.Documentation.Exporter, Is.EqualTo("Enterprise Architect")); + Assert.That(library.XmiRoot.Documentation, Is.Null); + Assert.That(full.XmiRoot.Documentation, Is.Not.Null); + Assert.That(full.XmiRoot.Documentation.Exporter, Is.EqualTo("Enterprise Architect")); }); } + [Test] + public void Verify_that_a_stereotype_application_is_written_with_the_element_it_extends() + { + this.documentAssigner + .Setup(x => x.Assign(It.IsAny(), It.IsAny())) + .Callback((element, documentName) => element.DocumentName = documentName) + .Returns(new PackageElementIds([], [])); + + this.xmiSplitterService.Split("input.xmi", this.config, "output"); + + var library = this.written.Single(document => document.Path.EndsWith("CSharp_Primitives.xmi")); + var full = this.written.Single(document => document.Path.EndsWith("Forge.xmi")); + + Assert.Multiple(() => + { + Assert.That(library.XmiRoot.StereoTypeApplications.Select(application => application.XmiId), Is.EqualTo(new[] { "onPrimitives" })); + Assert.That(full.XmiRoot.StereoTypeApplications.Select(application => application.XmiId), Is.EqualTo(new[] { "onForge" })); + }); + } + + [Test] + public void Verify_that_the_extension_of_a_package_excludes_the_container_it_is_split_out_of() + { + this.documentAssigner + .Setup(x => x.Assign(It.IsAny(), It.IsAny())) + .Returns(new PackageElementIds(["pkgForge"], ["root"])); + + this.xmiSplitterService.Split("input.xmi", this.config, "output"); + + this.extensionBuilder.Verify(x => x.Build( + It.IsAny(), + "Forge", + It.Is>(ids => ids.SetEquals(new[] { "pkgForge" })), + It.IsAny>())); + } + [Test] public void Verify_that_Split_throws_when_the_root_package_is_missing() { @@ -227,8 +264,10 @@ public void Verify_that_Split_throws_when_a_configured_package_is_missing() private static LoadedModel CreateLoadedModel() { var root = new Package { XmiId = "root", Name = "5. Data Structure" }; - root.PackagedElement.Add(new Package { XmiId = "pkgPrim", Name = "Primitives" }); - root.PackagedElement.Add(new Package { XmiId = "pkgForge", Name = "Forge" }); + var primitives = new Package { XmiId = "pkgPrim", Name = "Primitives" }; + var forge = new Package { XmiId = "pkgForge", Name = "Forge" }; + root.PackagedElement.Add(primitives); + root.PackagedElement.Add(forge); var model = new Model { XmiId = "model", Name = "EA_Model" }; model.PackagedElement.Add(root); @@ -239,7 +278,13 @@ private static LoadedModel CreateLoadedModel() XmiRoot = new XmiRoot { Documentation = new Documentation { Exporter = "Enterprise Architect", ExporterVersion = "6.5" }, - Extensions = [new XmiExtension { Extender = "Enterprise Architect", ExtenderId = "6.5" }] + Extensions = [new XmiExtension { Extender = "Enterprise Architect", ExtenderId = "6.5" }], + StereoTypeApplications = + [ + new StereoTypeApplication { XmiId = "onRoot", ExtendedElement = root }, + new StereoTypeApplication { XmiId = "onPrimitives", ExtendedElement = primitives }, + new StereoTypeApplication { XmiId = "onForge", ExtendedElement = forge } + ] } }; diff --git a/XSplinter.Tests/XSplinter.Tests.csproj b/XSplinter.Tests/XSplinter.Tests.csproj index db3ba65..20606c6 100644 --- a/XSplinter.Tests/XSplinter.Tests.csproj +++ b/XSplinter.Tests/XSplinter.Tests.csproj @@ -16,7 +16,7 @@ all - + all diff --git a/XSplinter/Services/IXmiDocumentWriter.cs b/XSplinter/Services/IXmiDocumentWriter.cs index 3873258..200850d 100644 --- a/XSplinter/Services/IXmiDocumentWriter.cs +++ b/XSplinter/Services/IXmiDocumentWriter.cs @@ -8,9 +8,6 @@ namespace XSplinter.Services { - using System.Collections.Generic; - - using uml4net; using uml4net.Packages; using uml4net.xmi.Xmi; @@ -38,17 +35,13 @@ public interface IXmiDocumentWriter /// /// The destination file path. /// - /// - /// The header to emit, or null when the document is - /// written without an xmi:Documentation element. - /// - /// - /// The instances to emit, or null when the document - /// is written without an extension section. + /// + /// The xmi:XMI level content to emit alongside the package: the documentation + /// header, the extensions and the stereotype applications. /// /// /// The UML namespace URI to declare, matching the version of UML used by the source document. /// - void Write(IPackage package, string path, Documentation documentation, IEnumerable extensions, string umlNamespaceUri); + void Write(IPackage package, string path, XmiRoot xmiRoot, string umlNamespaceUri); } } diff --git a/XSplinter/Services/XmiDocumentWriter.cs b/XSplinter/Services/XmiDocumentWriter.cs index d6c7089..39e109a 100644 --- a/XSplinter/Services/XmiDocumentWriter.cs +++ b/XSplinter/Services/XmiDocumentWriter.cs @@ -8,12 +8,10 @@ namespace XSplinter.Services { - using System.Collections.Generic; using System.IO; using Microsoft.Extensions.Logging; - using uml4net; using uml4net.Packages; using uml4net.xmi; using uml4net.xmi.Settings; @@ -48,7 +46,7 @@ public void EnsureDirectory(string path) } /// - public void Write(IPackage package, string path, Documentation documentation, IEnumerable extensions, string umlNamespaceUri) + public void Write(IPackage package, string path, XmiRoot xmiRoot, string umlNamespaceUri) { using var scope = XmiWriterBuilder.Create() .WithLogger(this.loggerFactory) @@ -59,20 +57,7 @@ public void Write(IPackage package, string path, Documentation documentation, IE settings.Indent = true; }); - var writer = scope.Build(); - - if (extensions == null) - { - writer.Write(package, path); - } - else if (documentation == null) - { - writer.Write(package, path, extensions); - } - else - { - writer.Write(package, path, documentation, extensions); - } + scope.Build().Write([package], path, xmiRoot); } } } diff --git a/XSplinter/Services/XmiSplitterService.cs b/XSplinter/Services/XmiSplitterService.cs index ee12a4c..49be878 100644 --- a/XSplinter/Services/XmiSplitterService.cs +++ b/XSplinter/Services/XmiSplitterService.cs @@ -17,6 +17,8 @@ namespace XSplinter.Services using uml4net; using uml4net.Packages; + using uml4net.Profiling; + using uml4net.xmi.Xmi; using XSplinter.Configuration; @@ -163,6 +165,13 @@ public void Split(string inputPath, SplitterConfig config, string outputDirector packageName); } + var containerIds = QueryContainerIds(packageNodes.Values); + + foreach (var ids in packageElementIds.Values) + { + ids.ExceptWith(containerIds); + } + if (this.logger.IsEnabled(LogLevel.Information)) { this.logger.LogInformation( @@ -204,7 +213,12 @@ public void Split(string inputPath, SplitterConfig config, string outputDirector package.Name = packageConfig.ModelName; } - this.documentWriter.Write(package, outputPath, null, null, loaded.UmlNamespaceUri); + var libraryRoot = new XmiRoot + { + StereoTypeApplications = QueryStereotypeApplications(result.XmiRoot, packageConfig.OutputFile) + }; + + this.documentWriter.Write(package, outputPath, libraryRoot, loaded.UmlNamespaceUri); } else { @@ -217,18 +231,23 @@ public void Split(string inputPath, SplitterConfig config, string outputDirector model.PackagedElement.Add(package); - var extensions = extension == null - ? null - : new List - { - this.extensionBuilder.Build( - extension, - packageConfig.Name, - packageElementIds[packageConfig.Name], - connectorPackageMap) - }; - - this.documentWriter.Write(model, outputPath, result.XmiRoot?.Documentation, extensions, loaded.UmlNamespaceUri); + var xmiRoot = new XmiRoot + { + Documentation = result.XmiRoot?.Documentation, + Extensions = extension == null + ? [] + : + [ + this.extensionBuilder.Build( + extension, + packageConfig.Name, + packageElementIds[packageConfig.Name], + connectorPackageMap) + ], + StereoTypeApplications = QueryStereotypeApplications(result.XmiRoot, packageConfig.OutputFile) + }; + + this.documentWriter.Write(model, outputPath, xmiRoot, loaded.UmlNamespaceUri); } if (this.logger.IsEnabled(LogLevel.Information)) @@ -240,6 +259,55 @@ public void Split(string inputPath, SplitterConfig config, string outputDirector this.logger.LogInformation("Splitting complete"); } + /// + /// Queries the identifiers of the packages that enclose the extracted packages without being + /// extracted themselves. Such a container is split around and never written, so no output may + /// carry Enterprise Architect entries for it. + /// + /// + /// The extracted packages. + /// + /// + /// The identifiers of the enclosing packages. + /// + private static HashSet QueryContainerIds(ICollection packages) + { + var containerIds = new HashSet(); + + foreach (var package in packages) + { + for (var owner = package.Owner; owner != null; owner = owner.Owner) + { + if (owner is IPackage container && !packages.Contains(container) && !string.IsNullOrEmpty(owner.XmiId)) + { + containerIds.Add(owner.XmiId); + } + } + } + + return containerIds; + } + + /// + /// Queries the stereotype applications that belong to a document, which are those extending + /// an element written to that document. + /// + /// + /// The xmi:XMI level content of the source document. + /// + /// + /// The name of the document being written. + /// + /// + /// The stereotype applications to write to the document. + /// + private static List QueryStereotypeApplications(XmiRoot xmiRoot, string documentName) + { + return xmiRoot?.StereoTypeApplications? + .Where(application => application.ExtendedElement?.DocumentName == documentName) + .ToList() ?? []; + } + /// /// Determines the name of the uml:Model wrapper: the name configured for the package, /// otherwise the name configured for the whole run, otherwise the name of the model that diff --git a/XSplinter/XSplinter.csproj b/XSplinter/XSplinter.csproj index fc370d2..bebfb1a 100644 --- a/XSplinter/XSplinter.csproj +++ b/XSplinter/XSplinter.csproj @@ -26,8 +26,8 @@ - - + +