Skip to content

Commit

Permalink
[Implement] DirectedRelationshipExtensions - Source and Target for Ge…
Browse files Browse the repository at this point in the history
…neralization

[Implement] RelationshipExtensions - QueryRelatedElement for Generalization
  • Loading branch information
samatstariongroup committed Dec 28, 2024
1 parent 51e479e commit d6817f5
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 3 deletions.
2 changes: 2 additions & 0 deletions uml4net.Tests/Extend/ClassExtensionsTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
53 changes: 53 additions & 0 deletions uml4net.Tests/Extend/DirectedRelationshipExtensionsTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="DirectedRelationshipExtensionsTestFixture.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.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]));
}
}
}
53 changes: 53 additions & 0 deletions uml4net.Tests/Extend/RelationshipExtensionsTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="RelationshipExtensionsTestFixture.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.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]));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace uml4net.CommonStructure
using System;
using System.Collections.Generic;

using Classification;

/// <summary>
/// The <see cref="DirectedRelationshipExtensions"/> class provides extensions methods for <see cref="IDirectedRelationship"/>
/// </summary>
Expand All @@ -39,7 +41,12 @@ public static class DirectedRelationshipExtensions
/// </returns>
public static List<IElement> QueryTarget(this IDirectedRelationship directedRelationship)
{
throw new NotSupportedException();
if (directedRelationship is IGeneralization generalization)
{
return [generalization.General];
}

throw new NotSupportedException($"{directedRelationship.GetType()} not yet supported");
}

/// <summary>
Expand All @@ -53,7 +60,12 @@ public static List<IElement> QueryTarget(this IDirectedRelationship directedRela
/// </returns>
public static List<IElement> QuerySource(this IDirectedRelationship directedRelationship)
{
throw new NotSupportedException();
if (directedRelationship is IGeneralization generalization)
{
return [generalization.Specific];
}

throw new NotSupportedException($"{directedRelationship.GetType()} not yet supported");
}
}
}
9 changes: 8 additions & 1 deletion uml4net/Extend/RelationshipExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace uml4net.CommonStructure
using System;
using System.Collections.Generic;

using uml4net.Classification;

/// <summary>
/// The <see cref="RelationshipExtensions"/> class provides extensions methods for <see cref="IRelationship"/>
/// </summary>
Expand All @@ -39,7 +41,12 @@ public static class RelationshipExtensions
/// </returns>
public static List<IElement> 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");
}
}
}

0 comments on commit d6817f5

Please sign in to comment.