Skip to content

Commit

Permalink
[Refactor] LiteralUnlimitedNatural to be of type string instead of in…
Browse files Browse the repository at this point in the history
…teger
  • Loading branch information
samatstariongroup committed Dec 27, 2024
1 parent b954b10 commit 71453f2
Show file tree
Hide file tree
Showing 11 changed files with 415 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ public IContainerList<IComment> OwnedComment
/// <summary>
/// The specified UnlimitedNatural value.
/// </summary>
[Property(xmiId: "LiteralUnlimitedNatural-value", aggregation: AggregationKind.None, lowerValue: 1, upperValue: 1, isOrdered: false, isReadOnly: false, isDerived: false, isDerivedUnion: false, isUnique: false, defaultValue: "0")]
[Property(xmiId: "LiteralUnlimitedNatural-value", aggregation: AggregationKind.None, lowerValue: 1, upperValue: 1, isOrdered: false, isReadOnly: false, isDerived: false, isDerivedUnion: false, isUnique: false, defaultValue: "")]
[Implements(implementation: "ILiteralUnlimitedNatural.Value")]
public int Value { get; set; }
public string Value { get; set; }

/// <summary>
/// Determines whether and how the NamedElement is visible outside its owning Namespace.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public partial interface ILiteralUnlimitedNatural : ILiteralSpecification
/// <summary>
/// The specified UnlimitedNatural value.
/// </summary>
[Property(xmiId: "LiteralUnlimitedNatural-value", aggregation: AggregationKind.None, lowerValue: 1, upperValue: 1, isOrdered: false, isReadOnly: false, isDerived: false, isDerivedUnion: false, isUnique: false, defaultValue: "0")]
public int Value { get; set; }
[Property(xmiId: "LiteralUnlimitedNatural-value", aggregation: AggregationKind.None, lowerValue: 1, upperValue: 1, isOrdered: false, isReadOnly: false, isDerived: false, isDerivedUnion: false, isUnique: false, defaultValue: "")]
public string Value { get; set; }

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="LiteralUnlimitedNaturalReader.cs" company="Starion Group S.A.">
//
// Copyright (C) 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>
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
// ------------------------------------------------------------------------------------------------

namespace uml4net.xmi.Readers
{
using System;
using System.Linq;
using System.Xml;

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.InformationFlows;
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;

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

/// <summary>
/// Initializes a new instance of the <see cref="LiteralUnlimitedNaturalReader"/> 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 LiteralUnlimitedNaturalReader(IXmiReaderCache cache, ILoggerFactory loggerFactory)
: base(cache, loggerFactory)
{
this.xmiElementReaderFacade = new XmiElementReaderFacade();
}

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

var defaultLineInfo = xmlReader as IXmlLineInfo;

ILiteralUnlimitedNatural poco = new LiteralUnlimitedNatural();

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

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

poco.XmiType = xmiType;

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

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

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

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

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

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

poco.Value = xmlReader.GetAttribute("value");

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 "owningTemplateParameter":
this.CollectSingleValueReferencePropertyIdentifier(xmlReader, poco, "owningTemplateParameter");
break;
case "templateParameter":
this.CollectSingleValueReferencePropertyIdentifier(xmlReader, poco, "templateParameter");
break;
case "type":
this.CollectSingleValueReferencePropertyIdentifier(xmlReader, poco, "type");
break;
case "value":
poco.Value = xmlReader.ReadElementContentAsString();
break;
case "visibility":
var visibilityValue = xmlReader.ReadElementContentAsString();
if (!string.IsNullOrEmpty(visibilityValue))
{
poco.Visibility = (VisibilityKind)Enum.Parse(typeof(VisibilityKind), visibilityValue, true); ;
}
break;
default:
throw new NotSupportedException($"LiteralUnlimitedNaturalReader: {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
Expand Up @@ -58,6 +58,7 @@
<Compile Remove="Expected\AutoGenXmiReaders\GeneralizationReader.cs" />
<Compile Remove="Expected\AutoGenXmiReaders\LiteralIntegerReader.cs" />
<Compile Remove="Expected\AutoGenXmiReaders\LiteralRealReader.cs" />
<Compile Remove="Expected\AutoGenXmiReaders\LiteralUnlimitedNaturalReader.cs" />
<Compile Remove="Expected\AutoGenXmiReaders\OpaqueExpressionReader.cs" />
<Compile Remove="Expected\AutoGenXmiReaders\PropertyReader.cs" />
<Compile Remove="Expected\AutoGenXmiReaders\StateMachineReader.cs" />
Expand Down Expand Up @@ -238,6 +239,9 @@
<Content Include="..\resources\UML\UML.xmi.xml" Link="TestData\UML.xmi">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Expected\AutoGenXmiReaders\LiteralUnlimitedNaturalReader.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="..\resources\SySML2\SysML.uml" Link="TestData\SysML.uml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
12 changes: 10 additions & 2 deletions uml4net.Extensions/PropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static bool QueryIsNumeric(this IProperty property)
{"Integer", "int"},
{"Real", "double"},
{"String", "string"},
{"UnlimitedNatural", "int"},
{"UnlimitedNatural", "string"},
};

/// <summary>
Expand Down Expand Up @@ -180,7 +180,15 @@ public static bool QueryIsEnumerable(this IProperty property)
switch (property.UpperValue.SingleOrDefault())
{
case ILiteralUnlimitedNatural literalUnlimitedNatural:
value = literalUnlimitedNatural.Value;

if (literalUnlimitedNatural.Value == "*")
{
value = int.MaxValue;
}
else
{
value = int.Parse(literalUnlimitedNatural.Value);
}
break;
case ILiteralInteger literalInteger:
value = literalInteger.Value;
Expand Down
4 changes: 2 additions & 2 deletions uml4net.Extensions/ValueSpecificationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public static string QueryDefaultValueAsString(this IValueSpecification valueSpe
return literalString.Value;
case ILiteralUnlimitedNatural literalUnlimitedNatural:

if (literalUnlimitedNatural.Value == int.MaxValue)
if (literalUnlimitedNatural.Value == "*")
{
return "int.MaxValue";
}

return literalUnlimitedNatural.Value.ToString();
return literalUnlimitedNatural.Value;
case IInstanceValue instanceValue:
return instanceValue.Instance.Name;
default:
Expand Down
Loading

0 comments on commit 71453f2

Please sign in to comment.