From 7b6e2465e0acf0894ecc97ae22eeca8ade627b81 Mon Sep 17 00:00:00 2001 From: Alessio Lombardi Date: Fri, 1 Nov 2019 11:43:29 +0000 Subject: [PATCH] Moved all Adapter Actions in their folder. --- BHoM_Adapter/AdapterActions/Execute.cs | 48 +++++++++++ BHoM_Adapter/AdapterActions/Move.cs | 56 +++++++++++++ BHoM_Adapter/AdapterActions/Pull.cs | 65 +++++++++++++++ BHoM_Adapter/AdapterActions/Push.cs | 85 +++++++++++++++++++ BHoM_Adapter/AdapterActions/Remove.cs | 51 ++++++++++++ BHoM_Adapter/BHoMAdapter.cs | 108 +------------------------ BHoM_Adapter/BHoM_Adapter.csproj | 5 ++ 7 files changed, 311 insertions(+), 107 deletions(-) create mode 100644 BHoM_Adapter/AdapterActions/Execute.cs create mode 100644 BHoM_Adapter/AdapterActions/Move.cs create mode 100644 BHoM_Adapter/AdapterActions/Pull.cs create mode 100644 BHoM_Adapter/AdapterActions/Push.cs create mode 100644 BHoM_Adapter/AdapterActions/Remove.cs diff --git a/BHoM_Adapter/AdapterActions/Execute.cs b/BHoM_Adapter/AdapterActions/Execute.cs new file mode 100644 index 00000000..c0830ebc --- /dev/null +++ b/BHoM_Adapter/AdapterActions/Execute.cs @@ -0,0 +1,48 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2018, 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 . + */ + +using BH.oM.Base; +using BH.Engine.Base; +using BH.oM.Data.Requests; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace BH.Adapter +{ + public abstract partial class BHoMAdapter + { + /******************************************************/ + /**** Public Adapter Methods "Adapter ACTIONS" *****/ + /******************************************************/ + /* These methods represent Actions that the Adapter can complete. + They are publicly available in the UI as individual components, e.g. in Grasshopper, under BHoM/Adapters tab. */ + + // Used to send specific commands to the external software, if it supports it. It should be implemented (overridden) at the Toolkit level. + public virtual bool Execute(string command, Dictionary parameters = null, Dictionary config = null) + { + return false; + } + } +} diff --git a/BHoM_Adapter/AdapterActions/Move.cs b/BHoM_Adapter/AdapterActions/Move.cs new file mode 100644 index 00000000..6cc968d5 --- /dev/null +++ b/BHoM_Adapter/AdapterActions/Move.cs @@ -0,0 +1,56 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2018, 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 . + */ + +using BH.oM.Base; +using BH.Engine.Base; +using BH.oM.Data.Requests; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace BH.Adapter +{ + public abstract partial class BHoMAdapter + { + /******************************************************/ + /**** Public Adapter Methods "Adapter ACTIONS" *****/ + /******************************************************/ + /* These methods represent Actions that the Adapter can complete. + They are publicly available in the UI as individual components, e.g. in Grasshopper, under BHoM/Adapters tab. */ + + // Performs a Pull and then a Push. Useful to move data between two different software without passing it through the UI. + public virtual bool Move(BHoMAdapter to, IRequest request, Dictionary pullConfig = null, Dictionary pushConfig = null) + { + string tag = ""; + if (request is FilterRequest) + tag = (request as FilterRequest).Tag; + + IEnumerable objects = Pull(request, pullConfig); + int count = objects.Count(); + return to.Push(objects.Cast(), tag, pushConfig).Count() == count; + } + + + } +} diff --git a/BHoM_Adapter/AdapterActions/Pull.cs b/BHoM_Adapter/AdapterActions/Pull.cs new file mode 100644 index 00000000..09d30a37 --- /dev/null +++ b/BHoM_Adapter/AdapterActions/Pull.cs @@ -0,0 +1,65 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2018, 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 . + */ + +using BH.oM.Base; +using BH.Engine.Base; +using BH.oM.Data.Requests; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace BH.Adapter +{ + public abstract partial class BHoMAdapter + { + /******************************************************/ + /**** Public Adapter Methods "Adapter ACTIONS" *****/ + /******************************************************/ + /* These methods represent Actions that the Adapter can complete. + They are publicly available in the UI as individual components, e.g. in Grasshopper, under BHoM/Adapters tab. */ + + // Calls the appropriate basic CRUD/Read method. + public virtual IEnumerable Pull(IRequest request, Dictionary config = null) + { + // --------------------------------------------------------------------------------- // + // *** Temporary retrocompatibility fix *** + // If it's a FilterRequest, check if it should read IResults or Objects with that. + // This should be replaced by an appropriate IResultRequest. + FilterRequest filterReq = request as FilterRequest; + if (filterReq != null) + if (typeof(BH.oM.Common.IResult).IsAssignableFrom(filterReq.Type)) + return ReadResults(filterReq); + // --------------------------------------------------------------------------------- // + + if (request is IResultRequest) + return ReadResults(request as dynamic); + + if (request is IRequest) + return Read(request as dynamic); + + return new List(); + } + + } +} diff --git a/BHoM_Adapter/AdapterActions/Push.cs b/BHoM_Adapter/AdapterActions/Push.cs new file mode 100644 index 00000000..ba5f06d3 --- /dev/null +++ b/BHoM_Adapter/AdapterActions/Push.cs @@ -0,0 +1,85 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2018, 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 . + */ + +using BH.oM.Base; +using BH.Engine.Base; +using BH.oM.Data.Requests; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace BH.Adapter +{ + public abstract partial class BHoMAdapter + { + /******************************************************/ + /**** Public Adapter Methods "Adapter ACTIONS" *****/ + /******************************************************/ + /* These methods represent Actions that the Adapter can complete. + They are publicly available in the UI as individual components, e.g. in Grasshopper, under BHoM/Adapters tab. */ + + // Performs the full CRUD if implemented, or calls the appropriate basic CRUD/Create method. + public virtual List Push(IEnumerable objects, string tag = "", Dictionary pushConfig = null) + { + bool success = true; + + // Get the Push Type from the pushConfig. + string pushType; + object ptObj; + if (pushConfig != null && pushConfig.TryGetValue("PushType", out ptObj)) + pushType = ptObj.ToString(); + else + pushType = "Replace"; + + // Wrap non-BHoM objects into a Custom BHoMObject to make them work as BHoMObjects. + List objectsToPush = Modify.WrapNonBHoMObjects(objects, Config, tag, pushConfig).ToList(); + + // Clone the objects for immutability in the UI. CloneBeforePush should always be true, except for very specific cases. + objectsToPush = Config.CloneBeforePush ? objectsToPush.Select(x => x.DeepClone()).ToList() : objects.ToList(); + + // Perform the actual Push. + Type iBHoMObjectType = typeof(IBHoMObject); + MethodInfo miToList = typeof(Enumerable).GetMethod("Cast"); + foreach (var typeGroup in objectsToPush.GroupBy(x => x.GetType())) + { + MethodInfo miListObject = miToList.MakeGenericMethod(new[] { typeGroup.Key }); + + var list = miListObject.Invoke(typeGroup, new object[] { typeGroup }); + + if (iBHoMObjectType.IsAssignableFrom(typeGroup.Key)) + { + if (pushType == "Replace") + success &= CRUD(list as dynamic, tag); + else if (pushType == "UpdateOnly") + { + success &= UpdateOnly(list as dynamic, tag); + } + } + } + + return success ? objectsToPush : new List(); + } + + } +} diff --git a/BHoM_Adapter/AdapterActions/Remove.cs b/BHoM_Adapter/AdapterActions/Remove.cs new file mode 100644 index 00000000..527aafe0 --- /dev/null +++ b/BHoM_Adapter/AdapterActions/Remove.cs @@ -0,0 +1,51 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2018, 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 . + */ + +using BH.oM.Base; +using BH.Engine.Base; +using BH.oM.Data.Requests; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace BH.Adapter +{ + public abstract partial class BHoMAdapter + { + /******************************************************/ + /**** Public Adapter Methods "Adapter ACTIONS" *****/ + /******************************************************/ + /* These methods represent Actions that the Adapter can complete. + They are publicly available in the UI as individual components, e.g. in Grasshopper, under BHoM/Adapters tab. */ + + // To be renamed to "Remove" or something different from the CRUD method "Delete". + // Calls the basic CRUD/Delete method. + public virtual int Delete(IRequest request, Dictionary config = null) + { + // If the provided request is a FilterRequest, the specific wrapper method for FilterRequest is called. + // For all other cases, Toolkits should implement specific IRequests and the related CRUD Wrapper method(s). + return Delete(request as dynamic); + } + } +} diff --git a/BHoM_Adapter/BHoMAdapter.cs b/BHoM_Adapter/BHoMAdapter.cs index 288fab18..a443cb69 100644 --- a/BHoM_Adapter/BHoMAdapter.cs +++ b/BHoM_Adapter/BHoMAdapter.cs @@ -44,117 +44,11 @@ public abstract partial class BHoMAdapter protected AdapterConfig Config { get; set; } = new AdapterConfig(); - /******************************************************/ - /**** Public Adapter Methods "Adapter ACTIONS" *****/ - /******************************************************/ - /* These methods represent Actions that the Adapter can complete. - They are publicly available in the UI as individual components, e.g. in Grasshopper, under BHoM/Adapters tab. */ - - // Performs the full CRUD if implemented, or calls the appropriate basic CRUD/Create method. - public virtual List Push(IEnumerable objects, string tag = "", Dictionary pushConfig = null) - { - bool success = true; - - // Get the Push Type from the pushConfig. - string pushType; - object ptObj; - if (pushConfig != null && pushConfig.TryGetValue("PushType", out ptObj)) - pushType = ptObj.ToString(); - else - pushType = "Replace"; - - // Wrap non-BHoM objects into a Custom BHoMObject to make them work as BHoMObjects. - List objectsToPush = Modify.WrapNonBHoMObjects(objects, Config, tag, pushConfig).ToList(); - - // Clone the objects for immutability in the UI. CloneBeforePush should always be true, except for very specific cases. - objectsToPush = Config.CloneBeforePush ? objectsToPush.Select(x => x.DeepClone()).ToList() : objects.ToList(); - - // Perform the actual Push. - Type iBHoMObjectType = typeof(IBHoMObject); - MethodInfo miToList = typeof(Enumerable).GetMethod("Cast"); - foreach (var typeGroup in objectsToPush.GroupBy(x => x.GetType())) - { - MethodInfo miListObject = miToList.MakeGenericMethod(new[] { typeGroup.Key }); - - var list = miListObject.Invoke(typeGroup, new object[] { typeGroup }); - - if (iBHoMObjectType.IsAssignableFrom(typeGroup.Key)) - { - if (pushType == "Replace") - success &= CRUD(list as dynamic, tag); - else if (pushType == "UpdateOnly") - { - success &= UpdateOnly(list as dynamic, tag); - } - } - } - - return success ? objectsToPush : new List(); - } - - /***************************************************/ - - // Calls the appropriate basic CRUD/Read method. - public virtual IEnumerable Pull(IRequest request, Dictionary config = null) - { - // --------------------------------------------------------------------------------- // - // *** Temporary retrocompatibility fix *** - // If it's a FilterRequest, check if it should read IResults or Objects with that. - // This should be replaced by an appropriate IResultRequest. - FilterRequest filterReq = request as FilterRequest; - if (filterReq != null) - if (typeof(BH.oM.Common.IResult).IsAssignableFrom(filterReq.Type)) - return ReadResults(filterReq); - // --------------------------------------------------------------------------------- // - - if (request is IResultRequest) - return ReadResults(request as dynamic); - - if (request is IRequest) - return Read(request as dynamic); - - return new List(); - } - - /***************************************************/ - - // Performs a Pull and then a Push. Useful to move data between two different software without passing it through the UI. - public virtual bool Move(BHoMAdapter to, IRequest request, Dictionary pullConfig = null, Dictionary pushConfig = null) - { - string tag = ""; - if (request is FilterRequest) - tag = (request as FilterRequest).Tag; - - IEnumerable objects = Pull(request, pullConfig); - int count = objects.Count(); - return to.Push(objects.Cast(), tag, pushConfig).Count() == count; - } - - /***************************************************/ - - // Calls the basic CRUD/Delete method. - public virtual int Delete(IRequest request, Dictionary config = null) - { - // If the provided request is a FilterRequest, the specific wrapper method for FilterRequest is called. - // For all other cases, Toolkits should implement specific IRequests and the related CRUD Wrapper method(s). - - return Delete(request as dynamic); - } - - /***************************************************/ - - // Used to send specific commands to the external software, if it supports it. It should be implemented (overridden) at the Toolkit level. - public virtual bool Execute(string command, Dictionary parameters = null, Dictionary config = null) - { - return false; - } - - /***************************************************/ /**** Protected Methods ****/ /***************************************************/ - // This field has to be implemented (overridden) at the Toolkit level for the CRUD method to work. + // This method has to be implemented (overridden) at the Toolkit level for the CRUD method to work. // It tells the CRUD what kind of relationship (dependency) exists between the Types that must be Pushed. // E.g. A Line has dependency type of Points. See the wiki or look at existing Toolkit implementations for more info. protected virtual List DependencyTypes() diff --git a/BHoM_Adapter/BHoM_Adapter.csproj b/BHoM_Adapter/BHoM_Adapter.csproj index 723c53bc..6b7a36f0 100644 --- a/BHoM_Adapter/BHoM_Adapter.csproj +++ b/BHoM_Adapter/BHoM_Adapter.csproj @@ -84,6 +84,11 @@ + + + + +