Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -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 `<type>`, and defaults such as `Generalization::isSubstitutable`
elided. UML4NET also promotes EA `<documentation>` into UML `ownedComment`.
elided. UML4NET also promotes EA `<documentation>` 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 `<profiles>` 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

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 58 additions & 13 deletions XSplinter.Tests/Services/XmiSplitterServiceTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace XSplinter.Tests.Services

using uml4net;
using uml4net.Packages;
using uml4net.Profiling;
using uml4net.xmi.Readers;
using uml4net.xmi.Xmi;

Expand All @@ -34,7 +35,7 @@ public class XmiSplitterServiceTestFixture
private Mock<IConstraintRelocator> constraintRelocator;
private Mock<IExtensionBuilder> extensionBuilder;
private Mock<IXmiDocumentWriter> documentWriter;
private List<(IPackage Package, string Path, Documentation Documentation, IEnumerable<XmiExtension> Extensions, string UmlNamespaceUri)> written;
private List<(IPackage Package, string Path, XmiRoot XmiRoot, string UmlNamespaceUri)> written;
private SplitterConfig config;
private XmiSplitterService xmiSplitterService;

Expand Down Expand Up @@ -77,10 +78,10 @@ public void Setup()
this.written = [];

this.documentWriter
.Setup(x => x.Write(It.IsAny<IPackage>(), It.IsAny<string>(), It.IsAny<Documentation>(), It.IsAny<IEnumerable<XmiExtension>>(), It.IsAny<string>()))
.Callback<IPackage, string, Documentation, IEnumerable<XmiExtension>, string>(
(package, path, documentation, extensions, umlNamespaceUri) =>
this.written.Add((package, path, documentation, extensions, umlNamespaceUri)));
.Setup(x => x.Write(It.IsAny<IPackage>(), It.IsAny<string>(), It.IsAny<XmiRoot>(), It.IsAny<string>()))
.Callback<IPackage, string, XmiRoot, string>(
(package, path, xmiRoot, umlNamespaceUri) =>
this.written.Add((package, path, xmiRoot, umlNamespaceUri)));

this.config = new SplitterConfig
{
Expand Down Expand Up @@ -149,12 +150,12 @@ public void Verify_that_a_convertToLibrary_package_is_written_without_a_model_wr
{
Assert.That(library.Package, Is.Not.InstanceOf<IModel>());
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<IModel>());
Assert.That(full.Package.Name, Is.EqualTo("EA_Model"));
Assert.That(full.Package.PackagedElement.OfType<IPackage>().Single().Name, Is.EqualTo("Forge"));
Assert.That(full.Extensions, Is.Not.Null.And.Not.Empty);
Assert.That(full.XmiRoot.Extensions, Is.Not.Empty);
});
}

Expand Down Expand Up @@ -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<IXmiElement>(), It.IsAny<string>()))
.Callback<IXmiElement, string>((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<IXmiElement>(), It.IsAny<string>()))
.Returns(new PackageElementIds(["pkgForge"], ["root"]));

this.xmiSplitterService.Split("input.xmi", this.config, "output");

this.extensionBuilder.Verify(x => x.Build(
It.IsAny<XmiExtension>(),
"Forge",
It.Is<HashSet<string>>(ids => ids.SetEquals(new[] { "pkgForge" })),
It.IsAny<Dictionary<string, string>>()));
}

[Test]
public void Verify_that_Split_throws_when_the_root_package_is_missing()
{
Expand All @@ -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);
Expand All @@ -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 }
]
}
};

Expand Down
2 changes: 1 addition & 1 deletion XSplinter.Tests/XSplinter.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="uml4net.xmi" Version="8.5.0" />
<PackageReference Include="uml4net.xmi" Version="9.2.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
15 changes: 4 additions & 11 deletions XSplinter/Services/IXmiDocumentWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

namespace XSplinter.Services
{
using System.Collections.Generic;

using uml4net;
using uml4net.Packages;
using uml4net.xmi.Xmi;

Expand Down Expand Up @@ -38,17 +35,13 @@ public interface IXmiDocumentWriter
/// <param name="path">
/// The destination file path.
/// </param>
/// <param name="documentation">
/// The <see cref="Documentation"/> header to emit, or <c>null</c> when the document is
/// written without an <c>xmi:Documentation</c> element.
/// </param>
/// <param name="extensions">
/// The <see cref="XmiExtension"/> instances to emit, or <c>null</c> when the document
/// is written without an extension section.
/// <param name="xmiRoot">
/// The <c>xmi:XMI</c> level content to emit alongside the package: the documentation
/// header, the extensions and the stereotype applications.
/// </param>
/// <param name="umlNamespaceUri">
/// The UML namespace URI to declare, matching the version of UML used by the source document.
/// </param>
void Write(IPackage package, string path, Documentation documentation, IEnumerable<XmiExtension> extensions, string umlNamespaceUri);
void Write(IPackage package, string path, XmiRoot xmiRoot, string umlNamespaceUri);
}
}
19 changes: 2 additions & 17 deletions XSplinter/Services/XmiDocumentWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,7 +46,7 @@ public void EnsureDirectory(string path)
}

/// <inheritdoc />
public void Write(IPackage package, string path, Documentation documentation, IEnumerable<XmiExtension> extensions, string umlNamespaceUri)
public void Write(IPackage package, string path, XmiRoot xmiRoot, string umlNamespaceUri)
{
using var scope = XmiWriterBuilder.Create()
.WithLogger(this.loggerFactory)
Expand All @@ -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);
}
}
}
94 changes: 81 additions & 13 deletions XSplinter/Services/XmiSplitterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace XSplinter.Services

using uml4net;
using uml4net.Packages;
using uml4net.Profiling;
using uml4net.xmi.Xmi;

using XSplinter.Configuration;

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
{
Expand All @@ -217,18 +231,23 @@ public void Split(string inputPath, SplitterConfig config, string outputDirector

model.PackagedElement.Add(package);

var extensions = extension == null
? null
: new List<XmiExtension>
{
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))
Expand All @@ -240,6 +259,55 @@ public void Split(string inputPath, SplitterConfig config, string outputDirector
this.logger.LogInformation("Splitting complete");
}

/// <summary>
/// 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.
/// </summary>
/// <param name="packages">
/// The extracted packages.
/// </param>
/// <returns>
/// The identifiers of the enclosing packages.
/// </returns>
private static HashSet<string> QueryContainerIds(ICollection<IPackage> packages)
{
var containerIds = new HashSet<string>();

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;
}

/// <summary>
/// Queries the stereotype applications that belong to a document, which are those extending
/// an element written to that document.
/// </summary>
/// <param name="xmiRoot">
/// The <c>xmi:XMI</c> level content of the source document.
/// </param>
/// <param name="documentName">
/// The name of the document being written.
/// </param>
/// <returns>
/// The stereotype applications to write to the document.
/// </returns>
private static List<StereoTypeApplication> QueryStereotypeApplications(XmiRoot xmiRoot, string documentName)
{
return xmiRoot?.StereoTypeApplications?
.Where(application => application.ExtendedElement?.DocumentName == documentName)
.ToList() ?? [];
}

/// <summary>
/// Determines the name of the <c>uml:Model</c> wrapper: the name configured for the package,
/// otherwise the name configured for the whole run, otherwise the name of the model that
Expand Down
4 changes: 2 additions & 2 deletions XSplinter/XSplinter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="10.0.0" />
<PackageReference Include="uml4net.xmi" Version="8.5.0" />
<PackageReference Include="uml4net.xmi.Extensions.EnterpriseArchitect" Version="8.6.0" />
<PackageReference Include="uml4net.xmi" Version="9.2.0" />
<PackageReference Include="uml4net.xmi.Extensions.EnterpriseArchitect" Version="9.2.0" />
</ItemGroup>

</Project>
Loading