gusucode.com > vnt工具箱matlab源码程序 > vnt/vnt/+vnt/+internal/+mixin/+message/Extractions.m

    classdef (Abstract) Extractions < handle
% Extractions Implements common message extraction operations.
%
%   This class is provided as a mixin that implements extraction operations
%   for message and message-like data items, such as J1939 parameter
%   groups. It provides:
%   
%       - Extract all by name
%       - Extract recent by name
%       - Extract by time (start/end time)
%
%   See also VNT.

% Authors: Jaremy Pyle
% Copyright 2015 The MathWorks, Inc.

properties (Abstract, Hidden, SetAccess = protected)
    % PrivateTimestamp - The timestamp of the item.
    PrivateTimestamp
    % PrivateName - The name of the item per the database definition.
    PrivateName
end

methods (Access = protected)
    
    function [matches, remainder] = doExtractAllByName(obj, desiredNames)
    % doExtractAllByName Runs extract all by name with remainder on data items.
    %
    %   This method implements a generic algorithm that takes the desired
    %   names as strings and finds matches and remainders in the data items
    %   with those names. It is used to perform extract all by name in the 
    %   toolbox for both CAN messages and J1939 parameter groups.
        
        % Get the names of all the items into a local cell array for 
        % faster comparing.
        allNames = {obj.PrivateName};

        % Loop through each name requested for extraction.
        logicalMatches = [];
        for ii = 1:numel(desiredNames)
            % Find the indexes of all objects matching the extraction
            % name. The output is transposed as we deal with row vectors
            % when handling items and strmatch returns a column vector.
            indexByName = strmatch(desiredNames{ii}, allNames, 'exact')'; %#ok<MATCH3>

            % Add the newly found indexes to the full list of matches. If
            % no matches where made, adding empty into the match list will
            % have no effect on it.
            logicalMatches = [logicalMatches indexByName]; %#ok<AGROW>
        end

        % Sort the match list.
        logicalMatches = sort(logicalMatches);

        % Create a logical index map that represents the remainder by
        % creating a full true map and setting all of the matches to false.
        logicalRemaining = true(1, numel(obj));
        logicalRemaining(logicalMatches) = false;

        % Set the output. If no matches were found, extracted will inherently
        % return as empty.
        matches = obj(logicalMatches);
        remainder = obj(logicalRemaining);
    end
    
    function matches = doExtractRecentByName(obj, desiredNames)
    % doExtractRecentByName Runs extract recent by name on data items.
    %
    %   This method implements a generic algorithm that takes the desired
    %   names as strings and finds the most recent matches in the data
    %   items with those names based on timestamps. It is used to perform
    %   extract recent by name in the toolbox for both CAN messages and
    %   J1939 parameter groups.
        
        % Initialize the output in case no matches are found. We want to
        % set an empty value of the appropriate type to the input.
        matches = eval([class(obj) '.empty()']);
        
        % Get the names of all the items into a local cell array for 
        % faster comparing.
        allNames = {obj.PrivateName};
        
        % Loop through each name requested for extraction.
        for ii = 1:numel(desiredNames)
            % Find all items matching the extraction name.
            itemsByName = obj(strmatch(desiredNames{ii}, allNames, 'exact')'); %#ok<MATCH3>
            
            % Check for matches.
            if ~isempty(itemsByName)
                % Get the timestamps of the matches.
                timestamps = [itemsByName.PrivateTimestamp];
                
                % Get the latest occurring item of the matches.
                latestItem = itemsByName(timestamps == max(timestamps));
                
                % Set the actual extracted item as the last one in
                % the array. This addresses an issue should multiple
                % messages have the same timestamp because they were
                % received faster than the resolution of the timestamp.
                % This is a possible scenario with virtual channels.
                matches(end + 1) = latestItem(end); %#ok<AGROW>
            end
        end
        
        % Run final processing if matches were made.
        if ~isempty(matches)
            % Sort the matches by their timestamps.
            [~, sortIndex] = sort([matches.Timestamp]);
            matches = matches(sortIndex);            
        end
    end
    
    function matches = doExtractByTime(obj, startTime, endTime)
    % doExtractByTime Runs extract by time on data items.
    %
    %   This method implements a generic algorithm to get logical indexes
    %   of timestamps falling between the provided start and end times. It
    %   is used to perofrm extract by time in the toolbox for both CAN
    %   messages and J1939 parameter groups.
        
        % Get the timestamp values.
        allTimestamps = [obj.PrivateTimestamp];
        
        % Get the logical indexes into the array of all timestamps having
        % a value falling between the provided start and end times.
        logicalMatches = (allTimestamps >= startTime) & (allTimestamps <= endTime);
        
        % Use the logicals to pull out the specific items. The array of
        % extracted items will be empty be default if all logical indexes
        % are false.
        matches = obj(logicalMatches);
    end
    
end

end