CSVFileReader Class
(Sequencer::CSVFileReader)Header: | #include <CSVFileReader> |
Inherits: | ServiceBlock |
Reimplemented Public Functions
virtual EventID | ProcessAsync(const EventID &eventInID, ArgumentData &data) const override |
- 2 public functions inherited from Sequencer::ServiceBlock
- 18 public functions inherited from CDPOperator
- 17 public functions inherited from CDPOperatorBase
- 46 public functions inherited from CDPBaseObject
- 26 public functions inherited from CDP::StudioAPI::CDPNode
- 22 public functions inherited from CDP::StudioAPI::ICDPNode
Reimplemented Protected Functions
virtual bool | ProcessEvent(const EventID &eventInID, ArgumentData &data) override |
- 11 protected functions inherited from Sequencer::ServiceBlock
- 1 protected function inherited from CDP::StudioAPI::CDPNode
Additional Inherited Members
- 2 public functions inherited from Sequencer::ServiceBlock
- 18 public functions inherited from CDPOperator
- 17 public functions inherited from CDPOperatorBase
- 46 public functions inherited from CDPBaseObject
- 26 public functions inherited from CDP::StudioAPI::CDPNode
- 22 public functions inherited from CDP::StudioAPI::ICDPNode
- 1 public variable inherited from CDPOperatorBase
- 1 static public member inherited from CDPBaseObject
- 11 protected functions inherited from Sequencer::ServiceBlock
- 1 protected function inherited from CDP::StudioAPI::CDPNode
- 8 protected variables inherited from CDPOperator
- 5 protected variables inherited from CDPOperatorBase
- 11 protected variables inherited from CDPBaseObject
Detailed Description
The CSVFileReader makes it possible to map contents from a .csv (or similar) file into Arguments.
The CSV File Format
CSV (Comma-Separated Values) files attempt to represent table-data. A CSV file is a delimiter based file, where each item is delimited (separated) by a character, typically one of ; (semicolon), , (comma), \t (tab), or some other character that is not used in the data that the file holds. The CSV file typically has a header containing the names (and order) of the data items. It can also have comments and empty lines, that the machine parser skips. Here is a short example .csv file content:
# this is a comment A1;A2;A3 1;10;100 2;11;101 3;12;102
The file above is typically interpreted like this:
- Line1 is a comment, and is skipped.
- Line2 is the header. It lists the names of the values in the file. Typically, A1 is the first column, A2 is the second column, and so on.
- Line3 contains the first set of values: A1=1,A2=10 and A3=100
- Line4 contains the second set of values: A1=2,A2=11 and A3=101
- Line5 contains the last set of values: A1=3,A2=12 and A3=102
Note: See the CSVFileReader_HeaderMapping Argument description for more information about mapping of the header to the .csv values.
CSVFileReader Usage
The CSVFileReader is typically configured with FileName, Delimiter and SkipPrefix. These are internal Arguments that can be set by editing them inside the CSVFileReader instance.
To tell the CSVFileReader which items from the header should be used, add arguments with names matching the CSV header names to the CSVFileReader. The Argument type must match the datatype of the content of the .csv file, or else the content will be truncated or misrepresented. Make sure to update the Line Data member with all the Arguments to emit. Connect the Read from a trigger source, and connect the Line to where you want to process the data.
For the CSV example above, add 3 Argument<unsigned int> named A1, A2 and A3 to the CSVFileReader. Select each argument and uncheck the Input property to make the arguments appear on the output side of the CSVFileReader ServiceBlock. Select Line, then select the Data property and make sure that A1, A2, A3 and Index are checked (The Index argument can be used to keep track of the sequence currently being emitted).
Note: In some cases it is useful to add data from other sources to the CSVFileReader as Arguments, and to also emit those along with the .csv data. This is allowed, and can be achieved by adding those arguments to the CSVFileReader, and then updating the Line.Data to emit them along with the .csv data.
How It Works
The CSVFileReader has two different modes, selectable by HeaderMapping. It is possible to choose between HeaderMapsToColumns (the default) and HeaderMapsToRows.
If HeaderMapping=HeaderMapsToColumns; when the CSVFileReader Read event is triggered, the following happens (pseudo-code):
- The file is opened and read line by line. If error, Error is emitted.
- The first non-empty, non-skipped line is identified as the header, and is mapped against the arguments in Line.Data. If error, Error is emitted.
- For each line in the file, map values from that line into the corresponding arguments. If error, Error is emitted, else a Line EventOut is emitted:
- In the first Line, A1=1, A2=10, A3=100 and Index=0.
- In the second Line, A1=2, A2=11, A3=101 and Index=1.
- In the third Line, A1=3, A2=12, A3=102 and Index=2.
- The file is closed and Done is emitted.
If HeaderMapping=HeaderMapsToRows; when the CSVFileReader Read event is triggered, the following happens (pseudo-code):
- The file is opened and read line by line. If error, Error is emitted.
- The first non-empty, non-skipped line is identified as the header, and is mapped against the arguments in Line.Data. If error, Error is emitted.
- Each line in the file is read, and values from that line is mapped into an array. If error, Error is emitted.
- When the amount of lines read matches the number of items in the header, values from the array are mapped in a left to right manner (column by column) into the correct arguments. Each data-set is emitted using the Line EventOut:
- In the first Line, A1=1, A2=2, A3=3 and Index=0.
- In the second Line, A1=10, A2=11, A3=12 and Index=1.
- In the third Line, A1=100, A2=101, A3=102 and Index=2.
- The file is closed and Done is emitted.
Events and Arguments
The CSVFileReader ServiceBlock has the following events:
Name | Type | Description |
---|---|---|
Init | Input | Used for initialization setup and chaining (CSVFileReader Init just emits Inited) |
Read | Input | Emits the CSV content as Line events according to how the CSVFileReader has been configured |
Inited | Output | The CSVFileReader has been initialized |
Line | Output | Sends one coherent set of data according to the header and configured output Arguments. Make sure to edit the Data member of Line to select which arguments should be passed in the Line message. |
Error | Output | An error has occurred, check ErrorText for more information. |
Done | Output | Signifies that the last data has been emitted. |
The following Arguments can be used to configure the CSVFileReader:
Argument Name | Type | Value | Description |
---|---|---|---|
FileName | Input | Relative or full native path name | The file name to read from, with the optional path prefix, relative to the application folder |
Delimiter | Input | Any single character, or '\t' | Character used as a separator for the data, typically ';'. If the tab is used as a separator, set the Delimiter to '\t' (without the quotes). |
SkipPrefix | Input | Any string | Any line that starts with this string is skipped. Note also that empty lines are skipped. |
HeaderMapping | Input | HeaderMapsToColumns, HeaderMapsToRows | Specify whether the header maps to Columns (ordinary .csv mapping), or whether it maps to rows. If it maps to columns, then the number of columns (horizontal items) must match the number of header items. If it maps to rows, then the number of rows (vertical items after the header) must match the number of header items. Any mismatch will cause an Error to be emitted. |
Index | Output | [0..n] | 0-based index that is incremented for each data-set emit. When HeaderMapping=HeaderMapsToColumns: Index equals the current row number. When HeaderMapping=HeaderMapsToRows: Index equals the current column number. |
ErrorText | Output | A descriptive text | A text containing the cause of the error being emitted in the Error EventOut. |
<Header-item> | Output | mapped from .csv | Add Arguments to the CSVFileReader and name the items exactly the same as the items in the .csv header. Any arguments matching names in the .csv header will get the corresponding values from the csv-file. |
Troubleshooting
During debugging and testing, we recommend connecting the Error EventOut to a Sequencer::DebugPrinter ServiceBlock to quickly identify issues caused by incorrect configuration or corrupt .csv files.
See also MessageArgument, ReceiveMessage, SendMessage, DebugPrinter, Basic Block, and Service Blocks.
Member Function Documentation
[override virtual]
EventID CSVFileReader::ProcessAsync(const EventID &eventInID, ArgumentData &data) const
Reimplemented from ServiceBlock::ProcessAsync().
Reads the file and emits the data according to how HeaderMapping is configured.
[override virtual protected]
bool CSVFileReader::ProcessEvent(const EventID &eventInID, ArgumentData &data)
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.