Skip to content

Commit

Permalink
[Replace] hand-coded readers with generated readers
Browse files Browse the repository at this point in the history
[Add] BMN xmi file for extra testing
  • Loading branch information
samatstariongroup committed Dec 24, 2024
1 parent dd9ba12 commit 91d3933
Show file tree
Hide file tree
Showing 50 changed files with 4,802 additions and 2,616 deletions.
78 changes: 78 additions & 0 deletions uml4net.xmi.Tests/DecisionModelNotationReaderTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="DecisionModelNotationReaderTestFixture.cs" company="Starion Group S.A.">
//
// Copyright 2019-2024 Starion Group S.A.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
// ------------------------------------------------------------------------------------------------

namespace uml4net.xmi.Tests
{
using System.IO;
using System.Linq;

using Microsoft.Extensions.Logging;

using NUnit.Framework;

using Serilog;

using uml4net.Values;
using uml4net.Packages;
using uml4net.SimpleClassifiers;
using uml4net.StructuredClassifiers;
using uml4net.xmi;

[TestFixture]
public class DecisionModelNotationReaderTestFixture
{
private ILoggerFactory loggerFactory;

[OneTimeSetUp]
public void OneTimeSetUp()
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger();

this.loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddSerilog();
});
}

[Test]
public void Verify_that_DMN_XMI_can_be_read()
{
var rootPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData");

var reader = XmiReaderBuilder.Create()
.UsingSettings(x => x.LocalReferenceBasePath = rootPath)
.WithLogger(this.loggerFactory)
.Build();

var xmiReaderResult = reader.Read(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "dtc-23-02-13.xmi.xml"));

Assert.That(xmiReaderResult.Packages.Count, Is.EqualTo(2));

var model = xmiReaderResult.Root as IModel;

Assert.That(model.Name, Is.EqualTo("Data"));

Assert.That(model.NestedPackage.Count, Is.EqualTo(4));
}
}
}
3 changes: 3 additions & 0 deletions uml4net.xmi.Tests/uml4net.xmi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
</ItemGroup>

<ItemGroup>
<Content Include="..\resources\Decision Model and Notation\dtc-23-02-13.xmi.xml" Link="TestData\dtc-23-02-13.xmi.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="..\resources\UML\PrimitiveTypes.xmi.xml" Link="TestData\PrimitiveTypes.xmi">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// <copyright file="ClassReader.cs" company="Starion Group S.A.">
//
// Copyright (C) 2019-2024 Starion Group S.A.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,25 @@ namespace uml4net.xmi.Readers.StructuredClassifiers
using Microsoft.Extensions.Logging;

using uml4net;
using uml4net.Actions;
using uml4net.Activities;
using uml4net.Classification;
using uml4net.CommonBehavior;
using uml4net.CommonStructure;
using uml4net.Deployments;
using uml4net.Interactions;
using uml4net.Packages;
using uml4net.SimpleClassifiers;
using uml4net.StateMachines;
using uml4net.StructuredClassifiers;
using uml4net.UseCases;
using uml4net.Utils;
using uml4net.Values;
using uml4net.xmi.Cache;
using uml4net.xmi.Readers;
using uml4net.xmi.Readers.Classification;
using uml4net.xmi.Readers.CommonStructure;
using uml4net.xmi.Readers.Values;

/// <summary>
/// The purpose of the <see cref="CollaborationUseReader"/> is to read an instance of <see cref="ICollaboration"/>
/// The purpose of the <see cref="CollaborationUseReader"/> is to read an instance of <see cref="ICollaborationUse"/>
/// from the XMI document
/// </summary>
public class CollaborationUseReader : XmiElementReader<ICollaborationUse>, IXmiElementReader<ICollaborationUse>
Expand All @@ -72,13 +73,13 @@ public CollaborationUseReader(IXmiReaderCache cache, ILoggerFactory loggerFactor
}

/// <summary>
/// Reads the <see cref="ICollaboration"/> object from its XML representation
/// Reads the <see cref="ICollaborationUse"/> object from its XML representation
/// </summary>
/// <param name="xmlReader">
/// an instance of <see cref="XmlReader"/>
/// </param>
/// <returns>
/// an instance of <see cref="ICollaboration"/>
/// an instance of <see cref="ICollaborationUse"/>
/// </returns>
public override ICollaborationUse Read(XmlReader xmlReader)
{
Expand Down Expand Up @@ -107,9 +108,68 @@ public override ICollaborationUse Read(XmlReader xmlReader)
poco.XmiId = xmlReader.GetAttribute("xmi:id");

this.Cache.Add(poco.XmiId, poco);

poco.Name = xmlReader.GetAttribute("name");

var typeXmlAttribute = xmlReader.GetAttribute("type");
if (!string.IsNullOrEmpty(typeXmlAttribute))
{
poco.SingleValueReferencePropertyIdentifiers.Add("type", typeXmlAttribute);
}

var visibilityXmlAttribute = xmlReader.GetAttribute("visibility");
if (!string.IsNullOrEmpty(visibilityXmlAttribute))
{
poco.Visibility = (VisibilityKind)Enum.Parse(typeof(VisibilityKind), visibilityXmlAttribute, true);
}



while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element)
{
switch (xmlReader.LocalName)
{
case "name":
poco.Name = xmlReader.ReadElementContentAsString();
break;
case "nameExpression":
var nameExpressionValue = (IStringExpression)this.xmiElementReaderFacade.QueryXmiElement(xmlReader, this.Cache, this.LoggerFactory, "uml:StringExpression");
poco.NameExpression.Add(nameExpressionValue);
break;
case "ownedComment":
var ownedCommentValue = (IComment)this.xmiElementReaderFacade.QueryXmiElement(xmlReader, this.Cache, this.LoggerFactory, "uml:Comment");
poco.OwnedComment.Add(ownedCommentValue);
break;
case "roleBinding":
var roleBindingValue = (IDependency)this.xmiElementReaderFacade.QueryXmiElement(xmlReader, this.Cache, this.LoggerFactory, "uml:Dependency");
poco.RoleBinding.Add(roleBindingValue);
break;
case "type":
this.CollectSingleValueReferencePropertyIdentifier(xmlReader, poco, "type");
break;
case "visibility":
var visibilityValue = xmlReader.ReadElementContentAsString();
if (!string.IsNullOrEmpty(visibilityValue))
{
poco.Visibility = (VisibilityKind)Enum.Parse(typeof(VisibilityKind), visibilityValue, true); ;
}
break;
default:
var defaultLineInfo = xmlReader as IXmlLineInfo;
throw new NotSupportedException($"CollaborationUseReader: {xmlReader.LocalName} at line:position {defaultLineInfo.LineNumber}:{defaultLineInfo.LinePosition}");
}
}
}

}

return poco;
}
}
}

// ------------------------------------------------------------------------------------------------
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
// ------------------------------------------------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="UseCaseReader.cs" company="Starion Group S.A.">
// <copyright file="CommentReader.cs" company="Starion Group S.A.">
//
// Copyright (C) 2019-2024 Starion Group S.A.
//
Expand All @@ -22,7 +22,7 @@
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
// ------------------------------------------------------------------------------------------------

namespace uml4net.xmi.Readers.UseCases
namespace uml4net.xmi.Readers.CommonStructure
{
using System;
using System.Collections.Generic;
Expand All @@ -32,81 +32,123 @@ namespace uml4net.xmi.Readers.UseCases
using Microsoft.Extensions.Logging;

using uml4net;
using uml4net.Actions;
using uml4net.Activities;
using uml4net.Classification;
using uml4net.CommonBehavior;
using uml4net.CommonStructure;
using uml4net.Deployments;
using uml4net.Interactions;
using uml4net.Packages;
using uml4net.SimpleClassifiers;
using uml4net.StateMachines;
using uml4net.StructuredClassifiers;
using uml4net.UseCases;
using uml4net.Utils;
using uml4net.Values;
using uml4net.xmi.Cache;
using uml4net.xmi.Readers;
using uml4net.xmi.Readers.Classification;
using uml4net.xmi.Readers.CommonStructure;
using uml4net.xmi.Readers.Values;

/// <summary>
/// The purpose of the <see cref="UseCaseReader"/> is to read an instance of <see cref="IUseCase"/>
/// The purpose of the <see cref="CommentReader"/> is to read an instance of <see cref="IComment"/>
/// from the XMI document
/// </summary>
public class UseCaseReader : XmiElementReader<IUseCase>, IXmiElementReader<IUseCase>
public class CommentReader : XmiElementReader<IComment>, IXmiElementReader<IComment>
{
private readonly IXmiElementReaderFacade xmiElementReaderFacade;

/// <summary>
/// Initializes a new instance of the <see cref="UseCaseReader"/> class.
/// Initializes a new instance of the <see cref="CommentReader"/> class.
/// </summary>
/// <param name="cache">
/// The cache in which each <see cref="IXmiElement"/>> is stored
/// </param>
/// <param name="loggerFactory">
/// The (injected) <see cref="ILoggerFactory"/> used to set up logging
/// </param>
public UseCaseReader(IXmiReaderCache cache, ILoggerFactory loggerFactory)
public CommentReader(IXmiReaderCache cache, ILoggerFactory loggerFactory)
: base(cache, loggerFactory)
{
this.xmiElementReaderFacade = new XmiElementReaderFacade();
}

/// <summary>
/// Reads the <see cref="IUseCase"/> object from its XML representation
/// Reads the <see cref="IComment"/> object from its XML representation
/// </summary>
/// <param name="xmlReader">
/// an instance of <see cref="XmlReader"/>
/// </param>
/// <returns>
/// an instance of <see cref="IUseCase"/>
/// an instance of <see cref="IComment"/>
/// </returns>
public override IUseCase Read(XmlReader xmlReader)
public override IComment Read(XmlReader xmlReader)
{
if (xmlReader == null)
{
throw new ArgumentNullException(nameof(xmlReader));
}

IUseCase poco = new UseCase();
IComment poco = new Comment();

if (xmlReader.MoveToContent() == XmlNodeType.Element)
{
var xmiType = xmlReader.GetAttribute("xmi:type");

if (!string.IsNullOrEmpty(xmiType) && xmiType != "uml:UseCase")
if (!string.IsNullOrEmpty(xmiType) && xmiType != "uml:Comment")
{
throw new XmlException($"The XmiType should be 'uml:UseCase' while it is {xmiType}");
throw new XmlException($"The XmiType should be 'uml:Comment' while it is {xmiType}");
}
else
{
xmiType = "uml:UseCase";
xmiType = "uml:Comment";
}

poco.XmiType = xmiType;

poco.XmiId = xmlReader.GetAttribute("xmi:id");

this.Cache.Add(poco.XmiId, poco);

var annotatedElementXmlAttribute = xmlReader.GetAttribute("annotatedElement");
if (!string.IsNullOrEmpty(annotatedElementXmlAttribute))
{
var annotatedElementXmlAttributeValues = annotatedElementXmlAttribute.Split(SplitMultiReference, StringSplitOptions.RemoveEmptyEntries).ToList();
poco.MultiValueReferencePropertyIdentifiers.Add("annotatedElement", annotatedElementXmlAttributeValues);
}

poco.Body = xmlReader.GetAttribute("body");


var annotatedElementValues = new List<string>();

while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element)
{
switch (xmlReader.LocalName)
{
case "annotatedElement":
this.CollectMultiValueReferencePropertyIdentifiers(xmlReader, annotatedElementValues, "annotatedElement");
break;
case "body":
poco.Body = xmlReader.ReadElementContentAsString();
break;
case "ownedComment":
var ownedCommentValue = (IComment)this.xmiElementReaderFacade.QueryXmiElement(xmlReader, this.Cache, this.LoggerFactory, "uml:Comment");
poco.OwnedComment.Add(ownedCommentValue);
break;
default:
var defaultLineInfo = xmlReader as IXmlLineInfo;
throw new NotSupportedException($"CommentReader: {xmlReader.LocalName} at line:position {defaultLineInfo.LineNumber}:{defaultLineInfo.LinePosition}");
}
}
}

if (annotatedElementValues.Count > 0)
{
poco.MultiValueReferencePropertyIdentifiers.Add("annotatedElement", annotatedElementValues);
}

}

return poco;
Expand Down
Loading

0 comments on commit 91d3933

Please sign in to comment.