classdef PrepareStrategy < fxptds.WorkflowTopology.Strategy
    %% PREPARESTRATEGY class
    % Handles the Prepare using Analyzer action
    
    % Copyright 2018-2019 The MathWorks, Inc.
    
    % Access is protected and extended to Facade and to the test class
    methods (Access = {?fxptds.WorkflowTopology.Strategy, ?fxptds.WorkflowTopology.Facade, ?WorkflowTopologyTestCase})
        function execute(this)
            
            % declare the rootName
            rootName = this.TopModel;
            
            % invalidate all but root and setup nodes
            nodesToInvalidate = this.getNodesToInvalidate(rootName);
            this.invalidateNodes(nodesToInvalidate);
            
            % create a new PrepareDesign Node
            type = fxptds.WorkflowTopology.element.NodeType.PrepareAction;
            actionNode = this.createAndAddNodeByType(type);
            
            % connect the PrepareDesign to the source Design
            sourceName = rootName;
            targetName = actionNode.Name;
            this.verifyAndConnect(sourceName, targetName);
            
            % create a new PrepareDesign Node
            type = fxptds.WorkflowTopology.element.NodeType.PrepareDesign;
            designNode = this.createAndAddNodeByType(type);
            
            % connect the PrepareDesign to the action node
            sourceName = actionNode.Name;
            targetName = designNode.Name;
            this.verifyAndConnect(sourceName, targetName);
            
        end
        
    end
    
    methods (Access = {?fxptds.WorkflowTopology.Strategy, ?WorkflowTopologyTestCase})
        function nodesToInvalidate = getNodesToInvalidate(this, nodeName)
            % invalidate all action nodes of the current container execpt
            % for the setup node
            
            % get the current container
            query = this.createDefaultQuery();
            query.Type = "Setup";
            query.Limit = 1;
            query.SortDirection = 'descend';
            query.Property = "ContainerID";
            containerID = this.executeQuery(nodeName, query);
            containerID = this.processQueryData(containerID);
            
            % get the setup name of the current container
            query = this.createDefaultQuery();
            query.Type = "Setup";
            query.ContainerID = containerID;
            setupName = this.executeQuery(nodeName, query);
            setupName = this.processQueryData(setupName);
            
            % get all the actions in the current container
            query = this.createDefaultQuery();
            query.Type = "Action";
            query.ContainerID = containerID;
            allActionsInContainer = this.executeQuery(nodeName, query);
            
            % remove the setup name from the list of all actions
            nodesToInvalidate = setdiff(allActionsInContainer, setupName);
            nodesToInvalidate = string(nodesToInvalidate);
            
        end
        
    end
    
end

