From c0f6c6e3730c508048112e1f1a28bfd5037d71e8 Mon Sep 17 00:00:00 2001
From: Mauco03 <marco.gaibotti@skywarder.eu>
Date: Fri, 10 May 2024 19:07:16 +0200
Subject: [PATCH] [refactoring-ode][classes] Fixed a bug where subclasses could
 not be istanciated

---
 classes/Component.m       | 55 ++++++++++++++++++++++++++++++++-------
 classes/Rocket.m          |  5 ++--
 classes/components/Para.m |  2 +-
 3 files changed, 49 insertions(+), 13 deletions(-)

diff --git a/classes/Component.m b/classes/Component.m
index 617c407..cf1a5e4 100644
--- a/classes/Component.m
+++ b/classes/Component.m
@@ -1,25 +1,51 @@
 classdef Component < Config
 % Component: Represents an abstraction layer for all components
 %   Components standardize object construction, config and data loading
+%
+%   Constructor:
+%       - Component: Creates an instance of the Component class.
+%           Arguments:
+%               - mission: Mission, mission object, used to find config
+%               files
+%               - varsIn: If present, data is read from this variable
+%               instead of config file
+%
+%           Options:
+%
+%               - elementWise: 
+%                   - if true, data is copied element-by-element
+%                       into target class
+%                   - if false, the loaded data substitutes target class
 
     properties(Abstract, Access = protected) 
-        % Insert dependencies here
         mission Mission
     end
 
     methods
-        function obj = Component(mission, varsIn)
+        function obj = Component(mission, varsIn, options)
+            % Component: Contains and manages all bays
+            %
+            %   Constructor:
+            %       - Rocket: Creates an instance of the Rocket class.
+            %           Loaded config: rocketConfig.m > rocket (for overrides)
+            %           Loaded data: -
+            %           Arguments:
+            %               - mission: Mission, mission object
+            %               - varIn: (optional) config source. Alternative to config.m
+            %               file
+
             arguments (Input)
                 mission Mission = Mission()
                 varsIn = []
+                options.elementWise logical = true
             end
-            obj.mission = mission;
-            %if isempty(mission.name) && nargin > 0, warning('Mission arguments are empty. Returning an uninitialized component...'); end
+
             if isempty(mission.name)
                 return;
             end
+            
             if isempty(varsIn)
-                varsIn = obj.getConfig();
+                varsIn = obj.getConfig(mission);
             end
 
             fileName = obj.configName;
@@ -35,15 +61,26 @@ classdef Component < Config
                     'and the config file is correct'], fileName, varName);
             end
 
-            [varsIn.(varName).mission] = deal(mission);
-            obj = varsIn.(varName);
+            configObj = varsIn.(varName);
+
+            if options.elementWise
+                fields = obj.getProperties('writable');
+                for j = 1:size(fields, 2)
+                    if isempty([configObj.(fields{j})]), continue; end
+                    obj.(fields{j}) = configObj.(fields{j});
+                end
+            else
+                obj = configObj;
+            end
+
+            [obj.mission] = deal(mission);
         end
     end
     
     methods(Access = protected)
-        function outputVariables = getConfig(obj)
+        function outputVariables = getConfig(obj, mission)
             fileName = obj.configName;
-            filePath = obj.mission.configPath;
+            filePath = mission.configPath;
             file = fullfile(filePath, fileName);
 
             if ~isfile(file)
diff --git a/classes/Rocket.m b/classes/Rocket.m
index 7b6799a..7494483 100644
--- a/classes/Rocket.m
+++ b/classes/Rocket.m
@@ -231,12 +231,11 @@ classdef Rocket < Component
             obj@Component(mission, varIn);
 
             %% Loading data
-            if nargin == 0
+            if isempty(mission.name)
                 return;
             end
-            obj.mission = mission;
 
-            vars             = obj.getConfig(); % Load config once and apply to other bays
+            vars             = obj.getConfig(mission); % Load config once and apply to other bays
             obj.payload      = Payload(mission, vars);
             obj.recovery     = Recovery(mission, vars);
             obj.electronics  = Electronics(mission, vars);
diff --git a/classes/components/Para.m b/classes/components/Para.m
index d62907e..ef74338 100644
--- a/classes/components/Para.m
+++ b/classes/components/Para.m
@@ -27,7 +27,7 @@ classdef Para < matlab.mixin.Heterogeneous & Component
                 mission Mission = Mission()
                 varIn = []
             end
-            obj@Component(mission, varIn);
+            obj@Component(mission, varIn, "elementWise", false);
             
             %% Forcing creation of heterogeneous array
             if any(size(obj) > 1)
-- 
GitLab