classdef SimpleSimulationStrategy < fxptds.WorkflowTopology.Strategy
    %% SIMPLESIMULATIONSTRATEGY class
    % Handles the Collect Ranges action in FPT when Simulation is selected
    % as the collection method
    
    % 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;
            targetName = this.FPTRunID;
            
            
            % starting at the root, find the most recent root or autoscaler
            % design that has been created
            query = this.createDefaultQuery();
            query.Type = "Design";
            query.Limit = 1;
            
            % execute the query and extract value from cell array
            extractedData = this.executeQuery(rootName, query);
            sourceName = this.processQueryData(extractedData);
            
            if this.Store.doesNodeExist(targetName)
                collectNode = this.Store.getNodeObject(targetName);
                collectNode.toggleIsSim();
                % if the node Name has already been declared in the Store,
                % invalidate all Actions that succeed the target node ex. A
                % user simulates to create run name Run1 and then proposes data
                % types for that run. The user then derives ranges using the
                % same run name Run1. The original SimulateAction and Proposal
                % from that derive should be invalidated
                nodesToInvalidate = this.getNodesToInvalidate(targetName);
                % nodesToInvalidate is an array of node names to invalidate
                this.invalidateNodes(nodesToInvalidate);
                
                % remove any existing SDI IDs
                collectNode.clearSDIRunIDs();
                % post-processing the node by adding the SDIRunID
                collectNode.addToSDIRunIDs(this.SDIRunID);
                
            else
                % create a new SimulateAction Node
                type = fxptds.WorkflowTopology.element.NodeType.SimulateAction;
                node = this.createAndAddNodeByType(type);
                
                % post-processing the node by adding the SDIRunID
                node.addToSDIRunIDs(this.SDIRunID);
                
                % connect the Simulate Action to the Root design
                this.verifyAndConnect(sourceName, targetName);
            end
            
        end
        
    end
    
    methods (Access = {?fxptds.WorkflowTopology.Strategy, ?WorkflowTopologyTestCase})
        function nodesToInvalidate = getNodesToInvalidate(this, nodeName)
            % call super to get the workflow branch
            nodesToInvalidate = getNodesToInvalidate@fxptds.WorkflowTopology.Strategy(this, nodeName);
            % remove the initial node
            nodesToInvalidate = setdiff(nodesToInvalidate, nodeName);
        end
        
    end
    
end

