Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter_Engine: Avoid signature duplication for AdapterId() methods #287

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Adapter_Engine/Adapter_Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Query\AdapterIds.cs" />
<Compile Include="Query\HasAdapterIdFragment.cs" />
<Compile Include="Query\AdapterId.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
46 changes: 17 additions & 29 deletions Adapter_Engine/Query/AdapterId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
using System.ComponentModel;
using System.Linq;
using System.Reflection;

using System.Collections;

namespace BH.Engine.Adapter
{
Expand All @@ -39,43 +39,33 @@ public static partial class Query
/**** Public Methods ****/
/***************************************************/

public static object AdapterId(this IBHoMObject bHoMObject, Type adapterIdFragmentType)
[PreviousVersion("4.0", "BH.Engine.Adapter.Query.AdapterId<T>(BH.oM.Base.IBHoMObject, Type adapterIdFragmentType)")]
[Description("Returns the BHoMObject's Id of the provided FragmentType, casted to its type.\n" +
"If more than one matching IdFragment is found, an error is returned.")]
[Input("notFoundWarning", "If true, a Warning is issued when no matching ID is found.")]
public static T AdapterId<T>(this IBHoMObject bHoMObject, Type adapterIdFragmentType, bool notFoundWarning = true)
{
if (!typeof(IAdapterId).IsAssignableFrom(adapterIdFragmentType))
{
BH.Engine.Reflection.Compute.RecordError($"The `{adapterIdFragmentType.Name}` is not a valid `{typeof(IAdapterId).Name}`.");
return null;
}

List<IAdapterId> fragmentList = bHoMObject.GetAllFragments(adapterIdFragmentType).OfType<IAdapterId>().ToList();

if (fragmentList.Count != 0)
{
IEnumerable<object> ids = fragmentList.Select(f => f.Id);

if (ids.Count() == 1)
return ids.First();
else
return ids;
}
else
return null;
}

public static T AdapterId<T>(this IBHoMObject bHoMObject, Type adapterIdFragmentType)
{
object id = AdapterId(bHoMObject, adapterIdFragmentType);
object id = AdapterIds(bHoMObject, adapterIdFragmentType);

if (id == null)
{
BH.Engine.Reflection.Compute.RecordWarning($"AdapterId is null or missing for an object of type {bHoMObject.GetType().Name}.");
if (notFoundWarning)
BH.Engine.Reflection.Compute.RecordWarning($"AdapterId is null or missing for an object of type {bHoMObject.GetType().Name}.");

return default(T);
}

if (id is T)
{
return (T)id;
}

if (id is IEnumerable && !(id is string))
{
BH.Engine.Reflection.Compute.RecordWarning($"More than one matching ID was found for type {adapterIdFragmentType.Name}.");
return default(T);
}

try
{
return (T)Convert.ChangeType(id, typeof(T));
Expand All @@ -86,7 +76,5 @@ public static T AdapterId<T>(this IBHoMObject bHoMObject, Type adapterIdFragment
return default(T);
}
}

}
}

70 changes: 70 additions & 0 deletions Adapter_Engine/Query/AdapterIds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2021, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Adapter;
using BH.oM.Base;
using BH.Engine.Base;
using BH.oM.Reflection.Attributes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;


namespace BH.Engine.Adapter
{
public static partial class Query
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

[PreviousVersion("4.0", "BH.Engine.Adapter.Query.AdapterId(BH.oM.Base.IBHoMObject, Type adapterIdFragmentType)")]
[Description("Returns the BHoMObject's Id of the provided FragmentType. " +
"If more than one matching IdFragment is found, the method returns a List of all Ids of that type." +
"If none is found, `null` is returned.")]
public static object AdapterIds(this IBHoMObject bHoMObject, Type adapterIdFragmentType = null)
alelom marked this conversation as resolved.
Show resolved Hide resolved
{
if (!typeof(IAdapterId).IsAssignableFrom(adapterIdFragmentType))
{
BH.Engine.Reflection.Compute.RecordError($"The `{adapterIdFragmentType.Name}` is not a valid `{typeof(IAdapterId).Name}`.");
return null;
}

List<IAdapterId> fragmentList = bHoMObject.GetAllFragments(adapterIdFragmentType).OfType<IAdapterId>().ToList();

if (fragmentList.Count != 0)
{
IEnumerable<object> ids = fragmentList.Select(f => f.Id);

if (ids.Count() == 1)
return ids.First();
else
return ids;
}
else
return null;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public abstract partial class BHoMAdapter
// All objects read from the model are to be deleted.
// Note that this means that only objects of the same type of the objects being pushed will be deleted.
if (readObjects.Any())
IDelete(typeof(T), readObjects.Select(obj => obj.AdapterId(AdapterIdFragmentType)), actionConfig);
IDelete(typeof(T), readObjects.Select(obj => obj.AdapterIds(AdapterIdFragmentType)), actionConfig);

objectsToCreate = newObjects;
}
Expand Down Expand Up @@ -178,11 +178,11 @@ public abstract partial class BHoMAdapter

// Extract the adapterIds from the toBeDeleted and call Delete() for all of them.
if (pushType != PushType.UpdateOrCreateOnly && toBeDeleted != null && toBeDeleted.Any())
IDelete(typeof(T), toBeDeleted.Select(obj => obj.AdapterId(AdapterIdFragmentType)), actionConfig);
IDelete(typeof(T), toBeDeleted.Select(obj => obj.AdapterIds(AdapterIdFragmentType)), actionConfig);

// Update the tags for the rest of the existing objects in the model
IUpdateTags(typeof(T),
readObjs_exclusive.Where(x => x.Tags.Count > 0).Select(x => x.AdapterId(AdapterIdFragmentType)),
readObjs_exclusive.Where(x => x.Tags.Count > 0).Select(x => x.AdapterIds(AdapterIdFragmentType)),
readObjs_exclusive.Where(x => x.Tags.Count > 0).Select(x => x.Tags),
actionConfig);

Expand Down Expand Up @@ -211,7 +211,7 @@ public abstract partial class BHoMAdapter
//For CreateNonExisting, the overlap objects are just kept, and not updated. To make sure tag functionality works though,
//The obejcts need to get their tags (if any) updated.
IUpdateTags(typeof(T),
diagram.Intersection.Where(x => x.Item1.Tags.Count > 0).Select(x => x.Item1.AdapterId(AdapterIdFragmentType)),
diagram.Intersection.Where(x => x.Item1.Tags.Count > 0).Select(x => x.Item1.AdapterIds(AdapterIdFragmentType)),
diagram.Intersection.Where(x => x.Item1.Tags.Count > 0).Select(x => x.Item1.Tags),
actionConfig);
}
Expand Down
4 changes: 2 additions & 2 deletions BHoM_Adapter/CRUD/IDelete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ protected virtual int Delete(Type type, string tag = "", ActionConfig actionConf
IEnumerable<IBHoMObject> withTag = Read(type, tag, actionConfig);

// Get indices of all with that tag only
IEnumerable<object> ids = withTag.Where(x => x.Tags.Count == 1).Select(x => x.AdapterId(AdapterIdFragmentType)).OrderBy(x => x);
IEnumerable<object> ids = withTag.Where(x => x.Tags.Count == 1).Select(x => x.AdapterIds(AdapterIdFragmentType)).OrderBy(x => x);
IDelete(type, ids);

// Remove tag if other tags as well
IEnumerable<IBHoMObject> multiTags = withTag.Where(x => x.Tags.Count > 1);
IUpdateTags(type, multiTags.Select(x => x.AdapterId(AdapterIdFragmentType)), multiTags.Select(x => x.Tags), actionConfig);
IUpdateTags(type, multiTags.Select(x => x.AdapterIds(AdapterIdFragmentType)), multiTags.Select(x => x.Tags), actionConfig);

return ids.Count();
}
Expand Down
2 changes: 1 addition & 1 deletion BHoM_Adapter/CRUD/IUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public abstract partial class BHoMAdapter
Type objectType = typeof(T);
if (m_AdapterSettings.UseAdapterId && typeof(IBHoMObject).IsAssignableFrom(objectType))
{
IDelete(typeof(T), objects.Select(x => ((IBHoMObject)x).AdapterId(AdapterIdFragmentType)), actionConfig);
IDelete(typeof(T), objects.Select(x => ((IBHoMObject)x).AdapterIds(AdapterIdFragmentType)), actionConfig);
}
return ICreate(objects, actionConfig);
}
Expand Down
2 changes: 1 addition & 1 deletion BHoM_Adapter/HelperMethods/AdapterId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void SetAdapterId(IBHoMObject bHoMObject, object id)

public object GetAdapterId(IBHoMObject bHoMObject)
{
return bHoMObject.AdapterId(AdapterIdFragmentType);
return bHoMObject.AdapterIds(AdapterIdFragmentType);
}

public T GetAdapterId<T>(IBHoMObject bHoMObject)
Expand Down
2 changes: 1 addition & 1 deletion BHoM_Adapter/HelperMethods/CopyBHoMObjectProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public abstract partial class BHoMAdapter

// Get id of the source and port it to the target
if (source.HasAdapterIdFragment(AdapterIdFragmentType))
target.SetAdapterId(AdapterIdFragmentType, source.AdapterId(AdapterIdFragmentType));
target.SetAdapterId(AdapterIdFragmentType, source.AdapterIds(AdapterIdFragmentType));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BHoM/BHoM_UI