Skip to content

Commit

Permalink
[Refactor] XmiReader to return xmiReaderResult
Browse files Browse the repository at this point in the history
  • Loading branch information
samatstariongroup committed Nov 16, 2024
1 parent 70f5ca5 commit b1c813a
Show file tree
Hide file tree
Showing 18 changed files with 771 additions and 105 deletions.
11 changes: 4 additions & 7 deletions uml4net.Extensions.Tests/ClassExtensionsTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ namespace uml4net.Extensions.Tests
using uml4net.POCO.Packages;
using uml4net.POCO.StructuredClassifiers;
using uml4net.xmi;
using xmi.Readers;

[TestFixture]
public class ClassExtensionsTestFixture
{
private ILoggerFactory loggerFactory;

private IEnumerable<IPackage> packages;

private IPackage rootPackage;
private XmiReaderResult xmiReaderResult;

[OneTimeSetUp]
public void OneTimeSetUp()
Expand All @@ -67,15 +66,13 @@ public void SetUp()
.WithLogger(this.loggerFactory)
.Build();

this.packages = reader.Read(Path.Combine(rootPath, "UML.xmi"));

this.rootPackage = this.packages.Single();
this.xmiReaderResult = reader.Read(Path.Combine(rootPath, "UML.xmi"));
}

[Test]
public void Verify_that_QueryAllProperties_returns_expected_result()
{
var commonStructuresPackage = this.rootPackage.NestedPackage.Single(x => x.Name == "CommonStructure");
var commonStructuresPackage = this.xmiReaderResult.Root.NestedPackage.Single(x => x.Name == "CommonStructure");

var dependency = commonStructuresPackage.PackagedElement.OfType<IClass>().Single(x => x.Name == "Dependency");

Expand Down
11 changes: 4 additions & 7 deletions uml4net.Extensions.Tests/ClassifierExtensionsTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ namespace uml4net.Extensions.Tests
using uml4net.POCO.Packages;
using uml4net.POCO.StructuredClassifiers;
using uml4net.xmi;
using xmi.Readers;

[TestFixture]
public class ClassifierExtensionsTestFixture
{
private ILoggerFactory loggerFactory;

private IEnumerable<IPackage> packages;

private IPackage rootPackage;
private XmiReaderResult xmiReaderResult;

[OneTimeSetUp]
public void OneTimeSetUp()
Expand All @@ -67,15 +66,13 @@ public void SetUp()
.WithLogger(this.loggerFactory)
.Build();

this.packages = reader.Read(Path.Combine(rootPath, "UML.xmi"));

this.rootPackage = this.packages.Single();
this.xmiReaderResult = reader.Read(Path.Combine(rootPath, "UML.xmi"));
}

[Test]
public void Verify_that_QueryAllGeneralClassifiers_returns_expected_result()
{
var commonStructuresPackage = this.rootPackage.NestedPackage.Single(x => x.Name == "CommonStructure");
var commonStructuresPackage = this.xmiReaderResult.Root.NestedPackage.Single(x => x.Name == "CommonStructure");

var dependency = commonStructuresPackage.PackagedElement.OfType<IClass>().Single(x => x.Name == "Dependency");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="HtmlReportGeneratorTestFixture.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, softwareUseCases
// 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.Reporting.Tests.Generators
{
using System.IO;
using System.Linq;
using Microsoft.Extensions.Logging;

using NUnit.Framework;
using POCO.Packages;
using Reporting.Generators;
using Serilog;
using xmi;

[TestFixture]
public class HtmlReportGeneratorTestFixture
{
private ILoggerFactory loggerFactory;

private HtmlReportGenerator htmlReportGenerator;

private string modelPath;

private FileInfo modelFileInfo;

private FileInfo reportFileInfo;

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

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

[SetUp]
public void SetUp()
{
this.modelPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "UML.xmi");
this.modelFileInfo = new FileInfo(modelPath);

var reportPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "html-report.html");
this.reportFileInfo = new FileInfo(reportPath);
}

[Test]
public void Verify_that_the_report_generator_generates_a_report()
{
this.htmlReportGenerator = new HtmlReportGenerator(this.loggerFactory);

Assert.That(() => this.htmlReportGenerator.GenerateReport(modelFileInfo, reportFileInfo), Throws.Nothing);
}
}
}
12 changes: 4 additions & 8 deletions uml4net.Reporting.Tests/Generators/ModelInspectorTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,11 @@ public void Verify_that_Inspection_report_can_be_executed()
.WithLogger(this.loggerFactory)
.Build();

var packages = reader.Read(Path.Combine(rootPath, "UML.xmi"));

var rootPackage = packages.Single();
var xmiReaderResult = reader.Read(Path.Combine(rootPath, "UML.xmi"));

this.modelInspector = new ModelInspector(this.loggerFactory);

var report = this.modelInspector.Inspect(rootPackage, true);
var report = this.modelInspector.Inspect(xmiReaderResult.Root, true);

Log.Logger.Information(report);

Expand All @@ -109,11 +107,9 @@ public void Verify_that_inspect_class_returns_expected_result()
.WithLogger(this.loggerFactory)
.Build();

var packages = reader.Read(Path.Combine(rootPath, "UML.xmi"));

var rootPackage = packages.Single();
var xmiReaderResult = reader.Read(Path.Combine(rootPath, "UML.xmi"));

var classificationPackage = rootPackage.PackagedElement.OfType<IPackage>().Single(x => x.Name == "Classification") ;
var classificationPackage = xmiReaderResult.Root.PackagedElement.OfType<IPackage>().Single(x => x.Name == "Classification") ;

this.modelInspector = new ModelInspector(this.loggerFactory);

Expand Down
63 changes: 38 additions & 25 deletions uml4net.Reporting/Generators/HandleBarsReportGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ namespace uml4net.Reporting.Generators
using HandlebarsDotNet.Helpers;

using Microsoft.Extensions.Logging;
using Payload;
using POCO.Packages;
using POCO.StructuredClassifiers;
using Resources;

using uml4net.POCO.Packages;
using uml4net.POCO.StructuredClassifiers;
using uml4net.POCO.SimpleClassifiers;

using uml4net.Reporting.Payload;
using uml4net.Reporting.Resources;
using uml4net.xmi.Readers;

/// <summary>
/// Abstract super class from which all <see cref="HandlebarsDotNet"/> generators
/// need to derive
Expand Down Expand Up @@ -72,7 +75,7 @@ protected HandleBarsReportGenerator(ILoggerFactory loggerFactory = null) : base(
protected virtual void RegisterHelpers()
{
uml4net.HandleBars.StringHelper.RegisterStringHelper(this.Handlebars);
//uml4net.HandleBars.StructuralFeatureHelper.RegisterStructuralFeatureHelper(this.Handlebars);
uml4net.HandleBars.PropertyHelper.RegisterStructuralFeatureHelper(this.Handlebars);
uml4net.HandleBars.GeneralizationHelper.RegisterGeneralizationHelper(this.Handlebars);
uml4net.HandleBars.DocumentationHelper.RegisteredDocumentationHelper(this.Handlebars);
}
Expand Down Expand Up @@ -100,39 +103,49 @@ protected void RegisterEmbeddedTemplate(string name)
}

/// <summary>
/// Creates a <see cref="HandlebarsPayload"/> based on the provided root <see cref="IPackage"/>
/// Creates a <see cref="HandlebarsPayload"/> based on the provided root <see cref="XmiReaderResult"/>
/// </summary>
/// <param name="rootPackage">
/// the subject root <see cref="IPackage"/>
/// <param name="xmiReaderResult">
/// the subject <see cref="XmiReaderResult"/>
/// </param>
/// <returns>
/// an instance of <see cref="HandlebarsPayload"/>
/// </returns>
protected static HandlebarsPayload CreateHandlebarsPayload(IPackage rootPackage)
protected static HandlebarsPayload CreateHandlebarsPayload(XmiReaderResult xmiReaderResult)
{
var packages = rootPackage.QueryPackages();

var enums = new List<IEnumeration>();
var enumerations = new List<IEnumeration>();
var primitiveTypes = new List<IPrimitiveType>();
var dataTypes = new List<IDataType>();
var eClasses = new List<IClass>();
var classes = new List<IClass>();
var interfaces = new List<IInterface>();

foreach (var package in xmiReaderResult.Packages)
{
var containedPackages = package.QueryPackages();

foreach (var containedPackage in containedPackages)
{
enumerations.AddRange(containedPackage.PackagedElement.OfType<IEnumeration>());

primitiveTypes.AddRange(containedPackage.PackagedElement.OfType<IPrimitiveType>());

//foreach (var package in packages)
//{
// enums.AddRange(package.EClassifiers.OfType<EEnum>());
dataTypes.AddRange(containedPackage.PackagedElement
.OfType<IDataType>()
.Where(x => x is not IEnumeration && x is not IPrimitiveType));

// dataTypes.AddRange(package.EClassifiers
// .OfType<EDataType>()
// .Where(x => !(x is EEnum))
// .OrderBy(x => x.Name));
classes.AddRange(containedPackage.PackagedElement.OfType<IClass>());

// eClasses.AddRange(package.EClassifiers.OfType<EClass>());
//}
interfaces.AddRange(containedPackage.PackagedElement.OfType<IInterface>());
}
}

var orderedEnums = enums.OrderBy(x => x.Name);
var orderedEnumerations = enumerations.OrderBy(x => x.Name);
var orderedPrimitiveTypes = primitiveTypes.OrderBy(x => x.Name);
var orderedDataTypes = dataTypes.OrderBy(x => x.Name);
var orderedClasses = eClasses.OrderBy(x => x.Name);
var orderedClasses = classes.OrderBy(x => x.Name);
var orderedInterfaces = interfaces.OrderBy(x => x.Name);

var payload = new HandlebarsPayload(rootPackage, orderedEnums, orderedDataTypes, orderedClasses);
var payload = new HandlebarsPayload(xmiReaderResult.Root, xmiReaderResult.Packages, orderedEnumerations, orderedPrimitiveTypes, orderedDataTypes, orderedClasses, orderedInterfaces);

return payload;
}
Expand Down
41 changes: 39 additions & 2 deletions uml4net.Reporting/Generators/HtmlReportGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace uml4net.Reporting.Generators
{
using System;
using System.Diagnostics;
using System.IO;

using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -70,7 +71,26 @@ public string QueryReportType()
/// </returns>
public string GenerateReport(FileInfo modelPath)
{
throw new NotImplementedException();
if (modelPath == null)
{
throw new ArgumentNullException(nameof(modelPath));
}

var sw = Stopwatch.StartNew();

this.logger.LogInformation("Start Generating HTML report tables");

var template = this.Templates["uml-to-html-docs"];

var xmiReaderResult = this.LoadPackages(modelPath, modelPath.Directory);

var payload = CreateHandlebarsPayload(xmiReaderResult);

var generatedHtml = template(payload);

this.logger.LogInformation("Generated HTML report in {ElapsedTime} [ms]", sw.ElapsedMilliseconds);

return generatedHtml;
}

/// <summary>
Expand All @@ -84,7 +104,24 @@ public string GenerateReport(FileInfo modelPath)
/// </param>
public void GenerateReport(FileInfo modelPath, FileInfo outputPath)
{
throw new NotImplementedException();
if (outputPath == null)
{
throw new ArgumentNullException(nameof(outputPath));
}

var sw = Stopwatch.StartNew();

var generatedHtml = this.GenerateReport(modelPath);

if (outputPath.Exists)
{
outputPath.Delete();
}

using var writer = outputPath.CreateText();
writer.Write(generatedHtml);

this.logger.LogInformation("Generated and saved HTML report in {ElapsedTime} [ms]", sw.ElapsedMilliseconds);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions uml4net.Reporting/Generators/ModelInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,11 @@ public void GenerateReport(FileInfo modelPath, FileInfo outputPath)

this.logger.LogInformation("Start Generating Inspection Report");

var packages = this.LoadPackages(modelPath, modelPath.Directory);
var xmiReaderResult = this.LoadPackages(modelPath, modelPath.Directory);

var result = new StringBuilder();

foreach (var package in packages)
foreach (var package in xmiReaderResult.Packages)
{
result.Append(this.ReportHeader());
result.Append(this.Inspect(package, true));
Expand Down
7 changes: 3 additions & 4 deletions uml4net.Reporting/Generators/ReportGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace uml4net.Reporting.Generators

using uml4net.POCO.Packages;
using uml4net.xmi;
using xmi.Readers;

/// <summary>
/// abstract class from which all report generators need to derive
Expand Down Expand Up @@ -83,7 +84,7 @@ protected ReportGenerator(ILoggerFactory loggerFactory = null)
/// <returns>
/// a list of <see cref="IPackage"/>s
/// </returns>
protected IEnumerable<IPackage> LoadPackages(FileInfo modelPath, DirectoryInfo rootDirectory)
protected XmiReaderResult LoadPackages(FileInfo modelPath, DirectoryInfo rootDirectory)
{
this.logger.LogInformation("Loading UML model from {0}", modelPath.FullName);

Expand All @@ -92,9 +93,7 @@ protected IEnumerable<IPackage> LoadPackages(FileInfo modelPath, DirectoryInfo r
.WithLogger(this.loggerFactory)
.Build();

var packages = reader.Read(modelPath.FullName);

return packages;
return reader.Read(modelPath.FullName);
}
}
}
Loading

0 comments on commit b1c813a

Please sign in to comment.