function setDownstreamIntegrationDriver(this, hDI)

%   Copyright 2023 The MathWorks, Inc.

% Set DownstreamIntegrationDriver for the model, which is saved on the
% hdlcc object. The DownstreamIntegrationDriver is created and set when
% first accessed (from hdlcc.getDownstreamIntegrationDriver). If it gets
% created outside of the hdlcc object (e.g. from the HDLWA or HWCLI), we
% also set it on the hdlcc object from the constructor of the
% DownstreamIntegrationDriver.

if ~isempty(hDI) && ~strcmp(hDI.getModelName, this.getModelName)
    error(message('HDLShared:hdlcoderui:IncorrectModelNameOnDriver', hDI.getModelName, this.getModelName));
end

this.DownstreamIntegrationDriver = hDI;

%% Keep configset source and cache in sync

% Update on the configset cache (if setting from the source)
csCache = this.getParent.getConfigSetCache;
if ~isempty(csCache)
    hdlcc = csCache.getComponent('HDL Coder');
    hdlcc.DownstreamIntegrationDriver = hDI;
end

% Update on the configset source (if setting from the cache)
csSource = this.getParent.getConfigSetSource;
if ~isempty(csSource)
    hdlcc = csSource.getComponent('HDL Coder');
    hdlcc.DownstreamIntegrationDriver = hDI;
end


end
