Package edu.ie3.datamodel.io.source.csv
Class CsvDataSource
java.lang.Object
edu.ie3.datamodel.io.source.csv.CsvDataSource
- Direct Known Subclasses:
CsvGraphicSource
,CsvIdCoordinateSource
,CsvRawGridSource
,CsvResultEntitySource
,CsvSystemParticipantSource
,CsvThermalSource
,CsvTimeSeriesMappingSource
,CsvTimeSeriesMetaInformationSource
,CsvTimeSeriesSource
,CsvTypeSource
,CsvWeatherSource
Parent class of all .csv file related sources containing methods and fields consumed by allmost
all implementations of .csv file related sources.
- Since:
- 05.04.20
-
Field Summary
Modifier and TypeFieldDescriptionprotected final CsvFileConnector
protected final String
protected static final String
protected static final org.slf4j.Logger
protected static final String
protected static final String
protected static final String
protected static final String
protected static final String
-
Constructor Summary
ModifierConstructorDescriptionprotected
CsvDataSource
(String csvSep, String folderPath, FileNamingStrategy fileNamingStrategy) -
Method Summary
Modifier and TypeMethodDescriptionprotected <T extends AssetInput>
Stream<AssetInputEntityData>assetInputEntityDataStream
(Class<T> entityClass, Collection<OperatorInput> operators) Returns a stream of optionalAssetInputEntityData
that can be used to build instances of several subtypes ofUniqueEntity
by a correspondingEntityFactory
that consumes this data.protected <T extends AssetInput>
AssetInputEntityDataassetInputEntityDataStream
(Class<T> entityClass, Map<String, String> fieldsToAttributes, Collection<OperatorInput> operators) buildStreamWithFieldsToAttributesMap
(Class<? extends UniqueEntity> entityClass, CsvFileConnector connector) Tries to open a file reader from the connector based on the provided entity class and hands it over for further processing.buildStreamWithFieldsToAttributesMap
(Class<? extends UniqueEntity> entityClass, BufferedReader bufferedReader) Reads the first line (considered to be the headline with headline fields) and returns a stream of (fieldName to fieldValue) mapping where each map represents one row of the .csv file.csvRowFieldValueMapping
(BufferedReader reader, String[] headline) distinctRowsWithLog
(Collection<Map<String, String>> allRows, Function<Map<String, String>, String> keyExtractor, String entityDescriptor, String keyDescriptor) Returns a collection of maps each representing a row in csv file that can be used to built one entity.protected <T extends UniqueEntity>
Optional<T>findFirstEntityByUuid
(String entityUuid, Collection<T> entities) Returns anOptional
of the firstUniqueEntity
element of this collection matching the provided UUID or an emptyOptional
if no matching entity can be found.protected <T extends AssetTypeInput>
Optional<T>getAssetType
(Collection<T> types, Map<String, String> fieldsToAttributes, String skippedClassString) Checks if the requested type of an asset can be found in the provided collection of types based on the provided fields to values mapping.protected <T extends UniqueEntity>
Predicate<Optional<T>>isPresentCollectIfNot
(Class<? extends UniqueEntity> entityClass, ConcurrentHashMap<Class<? extends UniqueEntity>, LongAdder> invalidElementsCounterMap) Returns a predicate that can be used to filter optionals ofUniqueEntity
s and keep track on the number of elements that have been empty optionals.protected void
logSkippingWarning
(String entityDesc, String entityUuid, String entityId, String missingElementsString) protected <T extends AssetInput>
Stream<Optional<T>>nodeAssetEntityStream
(Class<T> entityClass, EntityFactory<T, NodeAssetInputEntityData> factory, Collection<NodeInput> nodes, Collection<OperatorInput> operators) Returns a stream of optional entities that can be build by usingNodeAssetInputEntityData
and their corresponding factory.protected Stream<Optional<NodeAssetInputEntityData>>
nodeAssetInputEntityDataStream
(Stream<AssetInputEntityData> assetInputEntityDataStream, Collection<NodeInput> nodes) Returns a stream of optionalNodeAssetInputEntityData
that can be used to build instances of several subtypes ofUniqueEntity
by a correspondingEntityFactory
that consumes this data.protected String[]
parseCsvRow
(String csvRow, String csvSep) Parse a given row of a valid RFC 4180 formatted csv rowprotected void
printInvalidElementInformation
(Class<? extends UniqueEntity> entityClass, LongAdder noOfInvalidElements) protected String
protected <T extends ResultEntity>
Stream<SimpleEntityData>simpleEntityDataStream
(Class<T> entityClass) Returns a stream ofSimpleEntityData
for result entity classes, using a fields-to-attributes map.
-
Field Details
-
log
protected static final org.slf4j.Logger log -
csvSep
-
connector
-
OPERATOR
- See Also:
-
NODE_A
- See Also:
-
NODE_B
- See Also:
-
NODE
- See Also:
-
TYPE
- See Also:
-
FIELDS_TO_VALUES_MAP
- See Also:
-
-
Constructor Details
-
CsvDataSource
-
-
Method Details
-
parseCsvRow
Parse a given row of a valid RFC 4180 formatted csv row- Parameters:
csvRow
- the valid rowcsvSep
- separator of the csv file- Returns:
- an array with the csv field values as strings
-
isPresentCollectIfNot
protected <T extends UniqueEntity> Predicate<Optional<T>> isPresentCollectIfNot(Class<? extends UniqueEntity> entityClass, ConcurrentHashMap<Class<? extends UniqueEntity>, LongAdder> invalidElementsCounterMap) Returns a predicate that can be used to filter optionals ofUniqueEntity
s and keep track on the number of elements that have been empty optionals. This filter let only pass optionals that are non-empty. Example usage:Collection.stream().filter(isPresentCollectIfNot(NodeInput.class, new ConcurrentHashMap<>()))
- Type Parameters:
T
- the type of the entity- Parameters:
entityClass
- entity class that should be used as they key in the provided counter mapinvalidElementsCounterMap
- a map that counts the number of empty optionals and maps it to the provided entity clas- Returns:
- a predicate that can be used to filter and count empty optionals
-
printInvalidElementInformation
protected void printInvalidElementInformation(Class<? extends UniqueEntity> entityClass, LongAdder noOfInvalidElements) -
saveMapGet
-
logSkippingWarning
-
findFirstEntityByUuid
protected <T extends UniqueEntity> Optional<T> findFirstEntityByUuid(String entityUuid, Collection<T> entities) Returns anOptional
of the firstUniqueEntity
element of this collection matching the provided UUID or an emptyOptional
if no matching entity can be found.- Type Parameters:
T
- type of the entity that will be returned, derived from the provided collection- Parameters:
entityUuid
- uuid of the entity that should be looked forentities
- collection of entities that should be- Returns:
- either an optional containing the first entity that has the provided uuid or an empty optional if no matching entity with the provided uuid can be found
-
buildStreamWithFieldsToAttributesMap
protected Stream<Map<String,String>> buildStreamWithFieldsToAttributesMap(Class<? extends UniqueEntity> entityClass, CsvFileConnector connector) Tries to open a file reader from the connector based on the provided entity class and hands it over for further processing.- Parameters:
entityClass
- the entity class that should be build and that is used to get the corresponding readerconnector
- the connector that should be used to get the reader from- Returns:
- a parallel stream of maps, where each map represents one row of the csv file with the mapping (fieldName to fieldValue)
-
buildStreamWithFieldsToAttributesMap
protected Stream<Map<String,String>> buildStreamWithFieldsToAttributesMap(Class<? extends UniqueEntity> entityClass, BufferedReader bufferedReader) Reads the first line (considered to be the headline with headline fields) and returns a stream of (fieldName to fieldValue) mapping where each map represents one row of the .csv file. Since the returning stream is a parallel stream, the order of the elements cannot be guaranteed.- Parameters:
entityClass
- the entity class that should be buildbufferedReader
- the reader to use- Returns:
- a parallel stream of maps, where each map represents one row of the csv file with the mapping (fieldName to fieldValue)
-
csvRowFieldValueMapping
-
distinctRowsWithLog
protected Set<Map<String,String>> distinctRowsWithLog(Collection<Map<String, String>> allRows, Function<Map<String, String>, String> keyExtractor, String entityDescriptor, String keyDescriptor) Returns a collection of maps each representing a row in csv file that can be used to built one entity. The uniqueness of each row is doubled checked by a) that no duplicated rows are returned that are full (1:1) matches and b) that no rows are returned that have the same composite key, which gets extracted by the provided extractor. As both cases destroy uniqueness constraints, an empty set is returned to indicate that these data cannot be processed safely and the error is logged. For case a), only the duplicates are filtered out and a set with unique rows is returned.- Parameters:
allRows
- collection of rows of a csv file an entity should be built fromkeyExtractor
- Function, that extracts the key from field to value mapping, that is meant to be uniqueentityDescriptor
- Colloquial descriptor of the entity, the data is foreseen for (for debug String)keyDescriptor
- Colloquial descriptor of the key, that is meant to be unique (for debug String)- Returns:
- either a set containing only unique rows or an empty set if at least two rows with the same UUID but different field values exist
-
getAssetType
protected <T extends AssetTypeInput> Optional<T> getAssetType(Collection<T> types, Map<String, String> fieldsToAttributes, String skippedClassString) Checks if the requested type of an asset can be found in the provided collection of types based on the provided fields to values mapping. The provided fields to values mapping needs to have one and only one field with keyTYPE
and a corresponding UUID value. If the type can be found in the provided collection based on the UUID it is returned wrapped in an optional. Otherwise an empty optional is returned and a warning is logged.- Type Parameters:
T
- the type of the resulting type instance- Parameters:
types
- a collection of types that should be used for searchingfieldsToAttributes
- the field name to value mapping incl. the keyTYPE
skippedClassString
- debug string of the class that will be skipping- Returns:
- either an optional containing the type or an empty optional if the type cannot be found
-
assetInputEntityDataStream
protected <T extends AssetInput> Stream<AssetInputEntityData> assetInputEntityDataStream(Class<T> entityClass, Collection<OperatorInput> operators) Returns a stream of optionalAssetInputEntityData
that can be used to build instances of several subtypes ofUniqueEntity
by a correspondingEntityFactory
that consumes this data.- Type Parameters:
T
- type of the entity that should be build- Parameters:
entityClass
- the entity class that should be buildoperators
- a collection ofOperatorInput
entities that should be used to build the data- Returns:
- stream of optionals of the entity data or empty optionals of the operator required for the data cannot be found
-
assetInputEntityDataStream
protected <T extends AssetInput> AssetInputEntityData assetInputEntityDataStream(Class<T> entityClass, Map<String, String> fieldsToAttributes, Collection<OperatorInput> operators) -
nodeAssetInputEntityDataStream
protected Stream<Optional<NodeAssetInputEntityData>> nodeAssetInputEntityDataStream(Stream<AssetInputEntityData> assetInputEntityDataStream, Collection<NodeInput> nodes) Returns a stream of optionalNodeAssetInputEntityData
that can be used to build instances of several subtypes ofUniqueEntity
by a correspondingEntityFactory
that consumes this data. param assetInputEntityDataStream- Parameters:
assetInputEntityDataStream
- a stream consisting ofAssetInputEntityData
that is enriched withNodeInput
datanodes
- a collection ofNodeInput
entities that should be used to build the data- Returns:
- stream of optionals of the entity data or empty optionals of the node required for the data cannot be found
-
nodeAssetEntityStream
protected <T extends AssetInput> Stream<Optional<T>> nodeAssetEntityStream(Class<T> entityClass, EntityFactory<T, NodeAssetInputEntityData> factory, Collection<NodeInput> nodes, Collection<OperatorInput> operators) Returns a stream of optional entities that can be build by usingNodeAssetInputEntityData
and their corresponding factory.- Type Parameters:
T
- Type of theAssetInput
to expect- Parameters:
entityClass
- the entity class that should be buildfactory
- the factory that should be used for the building processnodes
- a collection ofNodeInput
entities that should be used to build the entitiesoperators
- a collection ofOperatorInput
entities should be used to build the entities- Returns:
- stream of optionals of the entities that has been built by the factor or empty optionals if the entity could not have been build
-
simpleEntityDataStream
protected <T extends ResultEntity> Stream<SimpleEntityData> simpleEntityDataStream(Class<T> entityClass) Returns a stream ofSimpleEntityData
for result entity classes, using a fields-to-attributes map.- Type Parameters:
T
- Type of theResultEntity
to expect- Parameters:
entityClass
- the entity class that should be build- Returns:
- stream of
SimpleEntityData
-