From d6817f59ce015d8717f8e79dcb190843e4856d74 Mon Sep 17 00:00:00 2001 From: samatstarion Date: Sat, 28 Dec 2024 22:37:56 +0100 Subject: [PATCH] [Implement] DirectedRelationshipExtensions - Source and Target for Generalization [Implement] RelationshipExtensions - QueryRelatedElement for Generalization --- .../Extend/ClassExtensionsTestFixture.cs | 2 + ...rectedRelationshipExtensionsTestFixture.cs | 53 +++++++++++++++++++ .../RelationshipExtensionsTestFixture.cs | 53 +++++++++++++++++++ ...p.cs => DirectedRelationshipExtensions.cs} | 16 +++++- uml4net/Extend/RelationshipExtensions.cs | 9 +++- 5 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 uml4net.Tests/Extend/DirectedRelationshipExtensionsTestFixture.cs create mode 100644 uml4net.Tests/Extend/RelationshipExtensionsTestFixture.cs rename uml4net/Extend/{DirectedRelationship.cs => DirectedRelationshipExtensions.cs} (80%) diff --git a/uml4net.Tests/Extend/ClassExtensionsTestFixture.cs b/uml4net.Tests/Extend/ClassExtensionsTestFixture.cs index 2a446b6a..ec2dd6ab 100644 --- a/uml4net.Tests/Extend/ClassExtensionsTestFixture.cs +++ b/uml4net.Tests/Extend/ClassExtensionsTestFixture.cs @@ -39,9 +39,11 @@ public void Verify_that_the_SuperClass_of_a_class_returns_the_expected_result() var animal_is_generalization_of_mammal = new Generalization(); animal_is_generalization_of_mammal.General = animal; + animal_is_generalization_of_mammal.Specific = mammal; var mammal_is_generalization_of_cat = new Generalization(); mammal_is_generalization_of_cat.General = mammal; + mammal_is_generalization_of_cat.Specific = cat; cat.Generalization.Add(mammal_is_generalization_of_cat); animal.Generalization.Add(animal_is_generalization_of_mammal); diff --git a/uml4net.Tests/Extend/DirectedRelationshipExtensionsTestFixture.cs b/uml4net.Tests/Extend/DirectedRelationshipExtensionsTestFixture.cs new file mode 100644 index 00000000..513c3154 --- /dev/null +++ b/uml4net.Tests/Extend/DirectedRelationshipExtensionsTestFixture.cs @@ -0,0 +1,53 @@ +// ------------------------------------------------------------------------------------------------- +// +// +// 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. +// +// +// ------------------------------------------------------------------------------------------------ + +namespace uml4net.Tests.Extend +{ + using NUnit.Framework; + + using uml4net.Classification; + using uml4net.StructuredClassifiers; + + [TestFixture] + public class DirectedRelationshipExtensionsTestFixture + { + [Test] + public void Verify_that_when_Generalization_Source_and_Target_return_expected_result() + { + var animal = new Class { Name = "Animal" }; + var mammal = new Class { Name = "Mammal" }; + var cat = new Class { Name = "Cat" }; + + var animal_is_generalization_of_mammal = new Generalization(); + animal_is_generalization_of_mammal.General = animal; + animal_is_generalization_of_mammal.Specific = mammal; + + var mammal_is_generalization_of_cat = new Generalization(); + mammal_is_generalization_of_cat.General = mammal; + mammal_is_generalization_of_cat.Specific = cat; + + cat.Generalization.Add(mammal_is_generalization_of_cat); + animal.Generalization.Add(animal_is_generalization_of_mammal); + + Assert.That(animal_is_generalization_of_mammal.Source, Is.EquivalentTo([mammal] )); + Assert.That(animal_is_generalization_of_mammal.Target, Is.EquivalentTo([animal])); + } + } +} diff --git a/uml4net.Tests/Extend/RelationshipExtensionsTestFixture.cs b/uml4net.Tests/Extend/RelationshipExtensionsTestFixture.cs new file mode 100644 index 00000000..fedd946b --- /dev/null +++ b/uml4net.Tests/Extend/RelationshipExtensionsTestFixture.cs @@ -0,0 +1,53 @@ +// ------------------------------------------------------------------------------------------------- +// +// +// 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. +// +// +// ------------------------------------------------------------------------------------------------ + +namespace uml4net.Tests.Extend +{ + using NUnit.Framework; + + using uml4net.Classification; + using uml4net.StructuredClassifiers; + + [TestFixture] + public class RelationshipExtensionsTestFixture + { + [Test] + public void Verify_that_when_Generalization_QueryRelatedElement_return_expected_result() + { + var animal = new Class { Name = "Animal" }; + var mammal = new Class { Name = "Mammal" }; + var cat = new Class { Name = "Cat" }; + + var animal_is_generalization_of_mammal = new Generalization(); + animal_is_generalization_of_mammal.General = animal; + animal_is_generalization_of_mammal.Specific = mammal; + + var mammal_is_generalization_of_cat = new Generalization(); + mammal_is_generalization_of_cat.General = mammal; + mammal_is_generalization_of_cat.Specific = cat; + + cat.Generalization.Add(mammal_is_generalization_of_cat); + animal.Generalization.Add(animal_is_generalization_of_mammal); + + Assert.That(animal_is_generalization_of_mammal.RelatedElement, Is.EquivalentTo([animal, mammal])); + Assert.That(mammal_is_generalization_of_cat.RelatedElement, Is.EquivalentTo([mammal, cat])); + } + } +} diff --git a/uml4net/Extend/DirectedRelationship.cs b/uml4net/Extend/DirectedRelationshipExtensions.cs similarity index 80% rename from uml4net/Extend/DirectedRelationship.cs rename to uml4net/Extend/DirectedRelationshipExtensions.cs index d9143530..dc3fde44 100644 --- a/uml4net/Extend/DirectedRelationship.cs +++ b/uml4net/Extend/DirectedRelationshipExtensions.cs @@ -23,6 +23,8 @@ namespace uml4net.CommonStructure using System; using System.Collections.Generic; + using Classification; + /// /// The class provides extensions methods for /// @@ -39,7 +41,12 @@ public static class DirectedRelationshipExtensions /// public static List QueryTarget(this IDirectedRelationship directedRelationship) { - throw new NotSupportedException(); + if (directedRelationship is IGeneralization generalization) + { + return [generalization.General]; + } + + throw new NotSupportedException($"{directedRelationship.GetType()} not yet supported"); } /// @@ -53,7 +60,12 @@ public static List QueryTarget(this IDirectedRelationship directedRela /// public static List QuerySource(this IDirectedRelationship directedRelationship) { - throw new NotSupportedException(); + if (directedRelationship is IGeneralization generalization) + { + return [generalization.Specific]; + } + + throw new NotSupportedException($"{directedRelationship.GetType()} not yet supported"); } } } diff --git a/uml4net/Extend/RelationshipExtensions.cs b/uml4net/Extend/RelationshipExtensions.cs index 43f56934..4f7056d7 100644 --- a/uml4net/Extend/RelationshipExtensions.cs +++ b/uml4net/Extend/RelationshipExtensions.cs @@ -23,6 +23,8 @@ namespace uml4net.CommonStructure using System; using System.Collections.Generic; + using uml4net.Classification; + /// /// The class provides extensions methods for /// @@ -39,7 +41,12 @@ public static class RelationshipExtensions /// public static List QueryRelatedElement(this IRelationship relationship) { - throw new NotSupportedException(); + if (relationship is IGeneralization generalization) + { + return [generalization.Specific, generalization.General]; + } + + throw new NotSupportedException($"{relationship.GetType()} not yet supported"); } } }