Skip to content

Compare UUIDs instead of Strings. #932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

### Changed
- Changing from comparing strings to comparing uuids in `EntitySource.findFirstEntityByUuid` [#829](https://github.com/ie3-institute/PowerSystemDataModel/issues/829)


## [4.1.0] - 2023-11-02

Expand Down
49 changes: 25 additions & 24 deletions src/main/java/edu/ie3/datamodel/io/source/EntitySource.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ protected String safeMapGet(Map<String, String> map, String key, String mapName)
* optional if no matching entity with the provided uuid can be found
*/
protected <T extends UniqueEntity> Optional<T> findFirstEntityByUuid(
String entityUuid, Collection<T> entities) {
UUID entityUuid, Collection<T> entities) {
return entities.stream()
.parallel()
.filter(uniqueEntity -> uniqueEntity.getUuid().toString().equalsIgnoreCase(entityUuid))
.filter(uniqueEntity -> uniqueEntity.getUuid().equals(entityUuid))
.findFirst();
}

/**
* 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 key {@link #TYPE} and a corresponding UUID value. If the type can
* be found in the provided collection based on the UUID it is returned wrapped in a {@link
* Success}. Otherwise a {@link Failure} is returned and a warning is logged.
* Checks if the requested type of 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 key {@link #TYPE} and a corresponding UUID value. If the type can be
* found in the provided collection based on the UUID it is returned wrapped in a {@link Success}.
* Otherwise a {@link Failure} is returned and a warning is logged.
*
* @param types a collection of types that should be used for searching
* @param fieldsToAttributes the field name to value mapping incl. the key {@link #TYPE}
Expand All @@ -96,7 +96,7 @@ protected <T extends AssetTypeInput> Try<T, SourceException> getAssetType(

Optional<T> assetType =
Optional.ofNullable(fieldsToAttributes.get(TYPE))
.flatMap(typeUuid -> findFirstEntityByUuid(typeUuid, types));
.flatMap(typeUuid -> findFirstEntityByUuid(UUID.fromString(typeUuid), types));

// if the type is not present we return an empty element and
// log a warning
Expand Down Expand Up @@ -167,29 +167,27 @@ protected <T extends AssetTypeInput> TypedConnectorInputEntityData<T> addTypeToE
*/
protected OperatorInput getFirstOrDefaultOperator(
Collection<OperatorInput> operators,
String operatorUuid,
Optional<UUID> operatorUuid,
String entityClassName,
String requestEntityUuid) {
if (operatorUuid == null) {
if (operatorUuid.isEmpty()) {
log.warn(
"Input source for class '{}' is missing the 'operator' field. "
+ "This is okay, but you should consider fixing the file by adding the field. "
+ "Defaulting to 'NO OPERATOR ASSIGNED'",
entityClassName);
return OperatorInput.NO_OPERATOR_ASSIGNED;
} else {
return operatorUuid.trim().isEmpty()
? OperatorInput.NO_OPERATOR_ASSIGNED
: findFirstEntityByUuid(operatorUuid, operators)
.orElseGet(
() -> {
log.debug(
"Cannot find operator with uuid '{}' for element '{}' and uuid '{}'. Defaulting to 'NO OPERATOR ASSIGNED'.",
operatorUuid,
entityClassName,
requestEntityUuid);
return OperatorInput.NO_OPERATOR_ASSIGNED;
});
return findFirstEntityByUuid(operatorUuid.get(), operators)
.orElseGet(
() -> {
log.debug(
"Cannot find operator with uuid '{}' for element '{}' and uuid '{}'. Defaulting to 'NO OPERATOR ASSIGNED'.",
operatorUuid,
entityClassName,
requestEntityUuid);
return OperatorInput.NO_OPERATOR_ASSIGNED;
});
}
}

Expand All @@ -212,7 +210,7 @@ protected Stream<Try<NodeAssetInputEntityData, SourceException>> nodeAssetInputE
// get the raw data
Map<String, String> fieldsToAttributes = assetInputEntityData.getFieldsToValues();
// get the node of the entity
String nodeUuid = fieldsToAttributes.get(NODE);
UUID nodeUuid = UUID.fromString(fieldsToAttributes.get(NODE));
Optional<NodeInput> node = findFirstEntityByUuid(nodeUuid, nodes);

// if the node is not present we return an empty element and
Expand Down Expand Up @@ -265,7 +263,10 @@ protected <T extends AssetInput> AssetInputEntityData assetInputEntityDataStream
Collection<OperatorInput> operators) {

// get the operator of the entity
String operatorUuid = fieldsToAttributes.get(OPERATOR);
Optional<UUID> operatorUuid =
Optional.ofNullable(fieldsToAttributes.get(OPERATOR))
.filter(s -> !s.isBlank())
.map(UUID::fromString);
OperatorInput operator =
getFirstOrDefaultOperator(
operators,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/ie3/datamodel/io/source/GraphicSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ protected Try<NodeGraphicInputEntityData, SourceException> buildNodeGraphicEntit
Map<String, String> fieldsToAttributes, Set<NodeInput> nodes) {

// get the node of the entity
String nodeUuid = fieldsToAttributes.get(NODE);
UUID nodeUuid = UUID.fromString(fieldsToAttributes.get(NODE));
Optional<NodeInput> node = findFirstEntityByUuid(nodeUuid, nodes);

// if the node is not present we return a failure
Expand Down Expand Up @@ -197,7 +197,7 @@ protected Try<LineGraphicInputEntityData, SourceException> buildLineGraphicEntit
Map<String, String> fieldsToAttributes, Set<LineInput> lines) {

// get the node of the entity
String lineUuid = fieldsToAttributes.get("line");
UUID lineUuid = UUID.fromString(fieldsToAttributes.get("line"));
Optional<LineInput> line = findFirstEntityByUuid(lineUuid, lines);

// if the node is not present we return an empty element and
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/edu/ie3/datamodel/io/source/RawGridSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,8 @@ protected Try<ConnectorInputEntityData, SourceException> buildUntypedConnectorIn
Map<String, String> fieldsToAttributes = assetInputEntityData.getFieldsToValues();

// get the two connector nodes
String nodeAUuid = fieldsToAttributes.get(NODE_A);
String nodeBUuid = fieldsToAttributes.get(NODE_B);
UUID nodeAUuid = UUID.fromString(fieldsToAttributes.get(NODE_A));
UUID nodeBUuid = UUID.fromString(fieldsToAttributes.get(NODE_B));
Optional<NodeInput> nodeA = findFirstEntityByUuid(nodeAUuid, nodes);
Optional<NodeInput> nodeB = findFirstEntityByUuid(nodeBUuid, nodes);

Expand Down Expand Up @@ -621,7 +621,7 @@ protected Try<Transformer3WInputEntityData, SourceException> addThirdNode(
Map<String, String> fieldsToAttributes = typeEntityData.getFieldsToValues();

// get nodeC of the transformer
String nodeCUuid = fieldsToAttributes.get("nodeC");
UUID nodeCUuid = UUID.fromString(fieldsToAttributes.get("nodeC"));
Optional<NodeInput> nodeC = findFirstEntityByUuid(nodeCUuid, nodes);

// if nodeC is not present we return a failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -928,12 +928,15 @@ protected Try<ChpInputEntityData, SourceException> buildChpEntityData(
Optional<ThermalStorageInput> thermalStorage =
Optional.ofNullable(fieldsToAttributes.get(THERMAL_STORAGE))
.flatMap(
thermalStorageUuid -> findFirstEntityByUuid(thermalStorageUuid, thermalStorages));
thermalStorageUuid ->
findFirstEntityByUuid(UUID.fromString(thermalStorageUuid), thermalStorages));

// get the thermal bus input for this chp unit
Optional<ThermalBusInput> thermalBus =
Optional.ofNullable(fieldsToAttributes.get("thermalBus"))
.flatMap(thermalBusUuid -> findFirstEntityByUuid(thermalBusUuid, thermalBuses));
.flatMap(
thermalBusUuid ->
findFirstEntityByUuid(UUID.fromString(thermalBusUuid), thermalBuses));

// if the thermal storage or the thermal bus are not present we return an
// empty element and log a warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class EntitySourceTest extends Specification {
)

when:
def actual = dummyEntitySource.findFirstEntityByUuid(uuid.toString(), entities)
def actual = dummyEntitySource.findFirstEntityByUuid(uuid, entities)

then:
actual.present
Expand All @@ -58,7 +58,7 @@ class EntitySourceTest extends Specification {
def "A CsvDataSource should always return an operator. Either the found one (if any) or OperatorInput.NO_OPERATOR_ASSIGNED"() {

expect:
dummyEntitySource.getFirstOrDefaultOperator(operators, operatorUuid, entityClassName, requestEntityUuid) == expectedOperator
dummyEntitySource.getFirstOrDefaultOperator(operators, Optional.of(UUID.fromString(operatorUuid)), entityClassName, requestEntityUuid) == expectedOperator

where:
operatorUuid | operators | entityClassName | requestEntityUuid || expectedOperator
Expand Down