Skip to content

Commit 6c4aab0

Browse files
Merge pull request #977 from ie3-institute/sp/#969-EntitySource-refactor
Refactoring `EntitySource` and neighboring classes
2 parents 59517a1 + 50ff2af commit 6c4aab0

File tree

78 files changed

+2153
-2503
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+2153
-2503
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Enhancing the error message for coordinate sources with invalid column names [#670](https://github.com/ie3-institute/PowerSystemDataModel/issues/670)
2323
- Allowing for additional unused columns in sources [#839](https://github.com/ie3-institute/PowerSystemDataModel/issues/839)
2424
- Improving column name validation to only run once per source [#849](https://github.com/ie3-institute/PowerSystemDataModel/issues/849)
25+
- Refactored and abstracted `EntitySource`s and `EntityData` creation [#969](https://github.com/ie3-institute/PowerSystemDataModel/issues/969)
2526

2627
## [4.1.0] - 2023-11-02
2728

docs/uml/main/EntitySourceClassDiagram.puml

Lines changed: 100 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
note "Assuming all classes to implement \nthe abstract methods of their interfaces\n\n" as generalNotes
44

55
abstract class EntitySource {
6-
- DataSource dataSource
7-
- <T extends AssetInput> Set<T> buildNodeAssetEntities(Class<T>, EntityFactory<T, NodeAssetInputEntityData>, Collection<NodeInput>, Collection<OperatorInput>, ConcurrentMap<Class<? extends UniqueEntity>, LongAdder>)
8-
- <T extends AssetInput> Set<T> buildNodeAssetEntities(Class<T>, EntityFactory<T, NodeAssetInputEntityData>, Collection<NodeInput>, Collection<OperatorInput>)
9-
- <T extends AssetInput> Set<T> buildAssetInputEntities(Class<T>, EntityFactory<T, AssetInputEntityData>, Collection<OperatorInput>)
10-
- <T extends AssetInput> Set<T> buildAssetInputEntities(Class<T>, EntityFactory<? extends InputEntity, SimpleEntityData>)
6+
# DataSource dataSource
7+
# {static} <E extends EntityData, T extends UniqueEntity, R extends E> Try<R, SourceException> enrichEntityData(E, String, Map<UUID, T>, BiFunction<E, T, R>)
8+
# {static} <E extends EntityData, T1 extends UniqueEntity, T2 extends UniqueEntity, R extends E> Try<R, SourceException> enrichEntityData(E, String, Map<UUID, T1>, String, Map<UUID, T2>, TriFunction<E, T1, T2, R>)
9+
# {static} <T extends UniqueEntity> Try<T, SourceException> getLinkedEntity(EntityData, String, Map<UUID, T>)
10+
# {static} <E extends EntityData, T extends UniqueEntity, R extends E> Try<R, SourceException> optionallyEnrichEntityData(E, String, Map<UUID, T>, T, BiFunction<E, T, R>)
11+
# Stream<Try<NodeAssetInputEntityData, SourceException>> buildNodeAssetEntities(Class<T>, Map<UUID, OperatorInput>, Map<UUID, NodeInput>)
12+
# {static} Stream<Try<NodeAssetInputEntityData, SourceException>> nodeAssetInputEntityDataStream(Stream<Try<AssetInputEntityData, SourceException>>, Map<UUID, NodeInput>)
13+
# Stream<Try<AssetInputEntityData, SourceException>> buildAssetInputEntities(Class<? extends AssetInput>, Map<UUID, OperatorInput>)
14+
# {static} Stream<Try<AssetInputEntityData, SourceException>> assetInputEntityDataStream(Stream<Try<EntityData, SourceException>>, Map<UUID, OperatorInput>)
15+
# Stream<Try<EntityData, SourceException>> buildEntityData(Class<? extends UniqueEntity>)
16+
# {static} <S extends UniqueEntity> Map<UUID, S> unpackMap(Stream<Try<S, FactoryException>>, Class<S>) throws SourceException
17+
# {static} <S extends UniqueEntity> Set<S> unpackSet(Stream<Try<S, FactoryException>>, Class<S>) throws SourceException
18+
# {static} <S, E extends Exception> Stream<S> unpackSet(Stream<Try<S, E>>, Class<S>) throws SourceException
1119
}
20+
1221
EntitySource <|-- GraphicSource
1322
EntitySource <|-- RawGridSource
1423
EntitySource <|-- ResultEntitySource
@@ -21,12 +30,13 @@ class GraphicSource {
2130
- RawGridSource rawGridSource
2231
- LineGraphicInputFactory lineGraphicInputFactory
2332
- NodeGraphicInputFactory nodeGraphicInputFactory
24-
- GraphicSource(TypeSource, RawGridSource, DataSource)
25-
- Optional<GraphicElements> getGraphicElements()
26-
- Set<NodeGraphicInput> getNodeGraphicInput()
27-
- Set<NodeGraphicInput> getNodeGraphicInput(Set<NodeInput>)
28-
- Set<LineGraphicInput> getLineGraphicInput()
29-
- Set<LineGraphicInput> getLineGraphicInput(Set<LineInput>)
33+
+ GraphicSource(TypeSource, RawGridSource, DataSource)
34+
+ GraphicElements getGraphicElements() throws SourceException
35+
+ GraphicElements getGraphicElements(Map<UUID, NodeInput>, Map<UUID, LineInput>) throws SourceException
36+
+ Set<NodeGraphicInput> getNodeGraphicInput() throws SourceException
37+
+ Set<NodeGraphicInput> getNodeGraphicInput(Map<UUID, NodeInput>) throws SourceException
38+
+ Set<LineGraphicInput> getLineGraphicInput() throws SourceException
39+
+ Set<LineGraphicInput> getLineGraphicInput(Map<UUID, LineInput>) throws SourceException
3040
}
3141

3242
class RawGridSource {
@@ -37,19 +47,21 @@ class RawGridSource {
3747
- Transformer3WInputFactory transformer3WInputFactory
3848
- SwitchInputFactory switchInputFactory
3949
- MeasurementUnitInputFactory measurementUnitInputFactory
40-
- Optional<RawGridElements> getGridData()
41-
- Set<NodeInput> getNodes()
42-
- Set<NodeInput> getNodes(Set<OperatorInput>)
43-
- Set<LineInput> getLines()
44-
- Set<LineInput> getLines(Set<NodeInput>, Set<LineTypeInput>, Set<OperatorInput>)
45-
- Set<Transformer2WInput> get2WTransformers()
46-
- Set<Transformer2WInput> get2WTransformers(Set<NodeInput>, Set<Transformer2WTypeInput>, Set<OperatorInput>)
47-
- Set<Transformer3WInput> get3WTransformers()
48-
- Set<Transformer3WInput> get3WTransformers(Set<NodeInput>, Set<Transformer3WTypeInput>, Set<OperatorInput>)
49-
- Set<SwitchInput> getSwitches()
50-
- Set<SwitchInput> getSwitches(Set<NodeInput>, Set<OperatorInput>)
51-
- Set<MeasurementUnitInput> getMeasurementUnits()
52-
- Set<MeasurementUnitInput> getMeasurementUnits(Set<NodeInput>, Set<OperatorInput>)
50+
+ RawGridSource(TypeSource typeSource, DataSource dataSource)
51+
+ RawGridElements getGridData() throws SourceException
52+
+ RawGridElements getGridData(Map<UUID, OperatorInput>, Map<UUID, NodeInput>, Map<UUID, LineInput>) throws SourceException
53+
+ Map<UUID, NodeInput> getNodes() throws SourceException
54+
+ Map<UUID, NodeInput> getNodes(Map<UUID, OperatorInput>) throws SourceException
55+
+ Map<UUID, LineInput> getLines() throws SourceException
56+
+ Map<UUID, LineInput> getLines(Map<UUID, OperatorInput>, Map<UUID, NodeInput>, Set<LineTypeInput>) throws SourceException
57+
+ Set<Transformer2WInput> get2WTransformers() throws SourceException
58+
+ Set<Transformer2WInput> get2WTransformers(Map<UUID, OperatorInput>, Map<UUID, NodeInput>, Set<Transformer2WTypeInput>) throws SourceException
59+
+ Set<Transformer3WInput> get3WTransformers() throws SourceException
60+
+ Set<Transformer3WInput> get3WTransformers(Map<UUID, OperatorInput>, Map<UUID, NodeInput>, Set<Transformer3WTypeInput>) throws SourceException
61+
+ Set<SwitchInput> getSwitches() throws SourceException
62+
+ Set<SwitchInput> getSwitches(Map<UUID, OperatorInput>, Map<UUID, NodeInput>) throws SourceException
63+
+ Set<MeasurementUnitInput> getMeasurementUnits() throws SourceException
64+
+ Set<MeasurementUnitInput> getMeasurementUnits(Map<UUID, OperatorInput>, Map<UUID, NodeInput>) throws SourceException
5365
}
5466

5567
class ResultEntitySource {
@@ -59,27 +71,27 @@ class ResultEntitySource {
5971
- NodeResultFactory nodeResultFactory
6072
- ConnectorResultFactory connectorResultFactory
6173
- FlexOptionsResultFactory flexOptionsResultFactory
62-
- ResultEntitySource(DataSource)
63-
- ResultEntitySource(DataSource, String)
64-
- Set<NodeResult> getNodeResults()
65-
- Set<SwitchResult> getSwitchResults()
66-
- Set<LineResult> getLineResults()
67-
- Set<Transformer2WResult> getTransformer2WResultResults()
68-
- Set<Transformer3WResult> getTransformer3WResultResults()
69-
- Set<FlexOptionsResult> getFlexOptionsResults()
70-
- Set<LoadResult> getLoadResults()
71-
- Set<PvResult> getPvResults()
72-
- Set<FixedFeedInResult> getFixedFeedInResults()
73-
- Set<BmResult> getBmResults()
74-
- Set<ChpResult> getChpResults()
75-
- Set<WecResult> getWecResults()
76-
- Set<StorageResult> getStorageResults()
77-
- Set<EvcsResult> getEvcsResults()
78-
- Set<EvResult> getEvResults()
79-
- Set<HpResult> getHpResults()
80-
- Set<CylindricalStorageResult> getCylindricalStorageResult()
81-
- Set<ThermalHouseResult> getThermalHouseResults()
82-
- Set<EmResult> getEmResults()
74+
+ ResultEntitySource(DataSource)
75+
+ ResultEntitySource(DataSource, String)
76+
+ Set<NodeResult> getNodeResults() throws SourceException
77+
+ Set<SwitchResult> getSwitchResults() throws SourceException
78+
+ Set<LineResult> getLineResults() throws SourceException
79+
+ Set<Transformer2WResult> getTransformer2WResultResults() throws SourceException
80+
+ Set<Transformer3WResult> getTransformer3WResultResults() throws SourceException
81+
+ Set<FlexOptionsResult> getFlexOptionsResults() throws SourceException
82+
+ Set<LoadResult> getLoadResults() throws SourceException
83+
+ Set<PvResult> getPvResults() throws SourceException
84+
+ Set<FixedFeedInResult> getFixedFeedInResults() throws SourceException
85+
+ Set<BmResult> getBmResults() throws SourceException
86+
+ Set<ChpResult> getChpResults() throws SourceException
87+
+ Set<WecResult> getWecResults() throws SourceException
88+
+ Set<StorageResult> getStorageResults() throws SourceException
89+
+ Set<EvcsResult> getEvcsResults() throws SourceException
90+
+ Set<EvResult> getEvResults() throws SourceException
91+
+ Set<HpResult> getHpResults() throws SourceException
92+
+ Set<CylindricalStorageResult> getCylindricalStorageResult() throws SourceException
93+
+ Set<ThermalHouseResult> getThermalHouseResults() throws SourceException
94+
+ Set<EmResult> getEmResults() throws SourceException
8395
}
8496

8597
class SystemParticipantSource{
@@ -96,44 +108,45 @@ class SystemParticipantSource{
96108
- StorageInputFactory storageInputFactory
97109
- WecInputFactory wecInputFactory
98110
- EvcsInputFactory evcsInputFactory
99-
- SystemParticipantSource(TypeSource, ThermalSource, RawGridSource, DataSource)
100-
- Optional<SystemParticipants> getSystemParticipants()
101-
- Set<BmInput> getBmPlants()
102-
- Set<BmInput> getBmPlants(Set<NodeInput>, Set<OperatorInput>, Set<BmTypeInput>)
103-
- Set<ChpInput> getChpPlants()
104-
- Set<ChpInput> getChpPlants(Set<NodeInput>, Set<OperatorInput>, Set<ChpTypeInput>, Set<ThermalBusInput>, Set<ThermalStorageInput>)
105-
- Set<EvInput> getEvs()
106-
- Set<EvInput> getEvs(Set<NodeInput>, Set<OperatorInput>, Set<EvTypeInput>)
107-
- Set<EvcsInput> getEvCS()
108-
- Set<EvcsInput> getEvCS(Set<NodeInput>, Set<OperatorInput>)
109-
- Set<FixedFeedInInput> getFixedFeedIns()
110-
- Set<FixedFeedInInput> getFixedFeedIns(Set<NodeInput>, Set<OperatorInput>)
111-
- Set<HpInput> getHeatPumps()
112-
- Set<HpInput> getHeatPumps(Set<NodeInput>, Set<OperatorInput>, Set<HpTypeInput>, Set<ThermalBusInput>)
113-
- Set<LoadInput> getLoads()
114-
- Set<LoadInput> getLoads(Set<NodeInput>, Set<OperatorInput>)
115-
- Set<PvInput> getPvPlants()
116-
- Set<PvInput> getPvPlants(Set<NodeInput>, Set<OperatorInput>)
117-
- Set<StorageInput> getStorages()
118-
- Set<StorageInput> getStorages(Set<NodeInput>, Set<OperatorInput>, Set<StorageTypeInput>)
119-
- Set<WecInput> getWecPlants()
120-
- Set<WecInput> getWecPlants(Set<NodeInput>, Set<OperatorInput>, Set<WecTypeInput>)
111+
+ SystemParticipantSource(TypeSource, ThermalSource, RawGridSource, DataSource)
112+
+ SystemParticipants getSystemParticipants() throws SourceException
113+
+ SystemParticipants getSystemParticipants(Map<UUID, OperatorInput>, Map<UUID, NodeInput>) throws SourceException
114+
+ Set<BmInput> getBmPlants() throws SourceException
115+
+ Set<BmInput> getBmPlants(Map<UUID, OperatorInput>, Map<UUID, NodeInput>, Set<BmTypeInput>) throws SourceException
116+
+ Set<ChpInput> getChpPlants() throws SourceException
117+
+ Set<ChpInput> getChpPlants(Map<UUID, OperatorInput>, Map<UUID, NodeInput>, Set<ChpTypeInput>, Map<UUID, ThermalBusInput>, Set<ThermalStorageInput>) throws SourceException
118+
+ Set<EvInput> getEvs() throws SourceException
119+
+ Set<EvInput> getEvs(Map<UUID, OperatorInput>, Map<UUID, NodeInput>, Set<EvTypeInput>) throws SourceException
120+
+ Set<EvcsInput> getEvcs() throws SourceException
121+
+ Set<EvcsInput> getEvcs(Map<UUID, OperatorInput>, Map<UUID, NodeInput>) throws SourceException
122+
+ Set<FixedFeedInInput> getFixedFeedIns() throws SourceException
123+
+ Set<FixedFeedInInput> getFixedFeedIns(Map<UUID, OperatorInput>, Map<UUID, NodeInput>) throws SourceException
124+
+ Set<HpInput> getHeatPumps() throws SourceException
125+
+ Set<HpInput> getHeatPumps(Map<UUID, OperatorInput>, Map<UUID, NodeInput>, Set<HpTypeInput>, Map<UUID, ThermalBusInput>) throws SourceException
126+
+ Set<LoadInput> getLoads() throws SourceException
127+
+ Set<LoadInput> getLoads(Map<UUID, OperatorInput>, Map<UUID, NodeInput>) throws SourceException
128+
+ Set<PvInput> getPvPlants() throws SourceException
129+
+ Set<PvInput> getPvPlants(Map<UUID, OperatorInput>, Map<UUID, NodeInput>) throws SourceException
130+
+ Set<StorageInput> getStorages() throws SourceException
131+
+ Set<StorageInput> getStorages(Map<UUID, OperatorInput>, Map<UUID, NodeInput>, Set<StorageTypeInput>) throws SourceException
132+
+ Set<WecInput> getWecPlants() throws SourceException
133+
+ Set<WecInput> getWecPlants(Map<UUID, OperatorInput>, Map<UUID, NodeInput>, Set<WecTypeInput>) throws SourceException
121134
}
122135

123136
class ThermalSource {
124137
- TypeSource typeSource
125138
- ThermalBusInputFactory thermalBusInputFactory
126139
- CylindricalStorageInputFactory cylindricalStorageInputFactory
127140
- ThermalHouseInputFactory thermalHouseInputFactory
128-
- ThermalSource(TypeSource, DataSource)
129-
- Set<ThermalBusInput> getThermalBuses()
130-
- Set<ThermalBusInput> getThermalBuses(Set<OperatorInput>)
131-
- Set<ThermalStorageInput> getThermalStorages()
132-
- Set<ThermalStorageInput> getThermalStorages(Set<OperatorInput>, Set<ThermalBusInput>)
133-
- Set<ThermalHouseInput> getThermalHouses()
134-
- Set<ThermalHouseInput> getThermalHouses(Set<OperatorInput>, Set<ThermalBusInput>)
135-
- Set<CylindricalStorageInput> getCylindricStorages()
136-
- Set<CylindricalStorageInput> getCylindricStorages(Set<OperatorInput>, Set<ThermalBusInput>)
141+
+ ThermalSource(TypeSource, DataSource)
142+
+ Map<UUID, ThermalBusInput> getThermalBuses() throws SourceException
143+
+ Map<UUID, ThermalBusInput> getThermalBuses(Map<UUID, OperatorInput>) throws SourceException
144+
+ Set<ThermalStorageInput> getThermalStorages() throws SourceException
145+
+ Set<ThermalStorageInput> getThermalStorages(Map<UUID, OperatorInput>, Map<UUID, ThermalBusInput>) throws SourceException
146+
+ Set<ThermalHouseInput> getThermalHouses() throws SourceException
147+
+ Set<ThermalHouseInput> getThermalHouses(Map<UUID, OperatorInput>, Map<UUID, ThermalBusInput>) throws SourceException
148+
+ Set<CylindricalStorageInput> getCylindricStorages() throws SourceException
149+
+ Set<CylindricalStorageInput> getCylindricStorages(Map<UUID, OperatorInput>, Map<UUID, ThermalBusInput>) throws SourceException
137150
}
138151

139152
class TypeSource {
@@ -142,16 +155,16 @@ class TypeSource {
142155
- LineTypeInputFactory lineTypeInputFactory
143156
- Transformer3WTypeInputFactory transformer3WTypeInputFactory
144157
- SystemParticipantTypeInputFactory systemParticipantTypeInputFactory
145-
- TypeSource(DataSource)
146-
- Set<Transformer2WTypeInput> getTransformer2WTypes()
147-
- Set<Transformer3WTypeInput> getTransformer3WTypes()
148-
- Set<OperatorInput> getOperators()
149-
- Set<LineTypeInput> getLineTypes()
150-
- Set<BmTypeInput> getBmTypes()
151-
- Set<ChpTypeInput> getChpTypes()
152-
- Set<HpTypeInput> getHpTypes()
153-
- Set<StorageTypeInput> getStorageTypes()
154-
- Set<WecTypeInput> getWecTypes()
155-
- Set<EvTypeInput> getEvTypes()
158+
+ TypeSource(DataSource)
159+
+ Map<UUID, Transformer2WTypeInput> getTransformer2WTypes() throws SourceException
160+
+ Map<UUID, Transformer3WTypeInput> getTransformer3WTypes() throws SourceException
161+
+ Map<UUID, OperatorInput> getOperators() throws SourceException
162+
+ Map<UUID, LineTypeInput> getLineTypes() throws SourceException
163+
+ Map<UUID, BmTypeInput> getBmTypes() throws SourceException
164+
+ Map<UUID, ChpTypeInput> getChpTypes() throws SourceException
165+
+ Map<UUID, HpTypeInput> getHpTypes() throws SourceException
166+
+ Map<UUID, StorageTypeInput> getStorageTypes() throws SourceException
167+
+ Map<UUID, WecTypeInput> getWecTypes() throws SourceException
168+
+ Map<UUID, EvTypeInput> getEvTypes() throws SourceException
156169
}
157170
@enduml

src/main/java/edu/ie3/datamodel/io/factory/EntityData.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import tech.units.indriya.ComparableQuantity;
2525

2626
/**
27-
* Internal API Contains data that is needed by an {@link EntityFactory} to generate an entity
27+
* Data used by {@link EntityFactory} to create an instance of an entity than can be created based
28+
* only on a mapping of fieldName to value. This class can be used whenever no additional data is
29+
* needed, but also functions as a parent class for extensions.
2830
*
2931
* @version 0.1
3032
* @since 28.01.20
3133
*/
32-
public abstract class EntityData extends FactoryData {
34+
public class EntityData extends FactoryData {
3335
private static final Logger logger = LoggerFactory.getLogger(EntityData.class);
3436
private static final GeoJsonReader geoJsonReader = new GeoJsonReader();
3537

@@ -39,11 +41,20 @@ public abstract class EntityData extends FactoryData {
3941
* @param fieldsToAttributes attribute map: field name to value
4042
* @param entityClass class of the entity to be created with this data
4143
*/
42-
protected EntityData(
44+
public EntityData(
4345
Map<String, String> fieldsToAttributes, Class<? extends UniqueEntity> entityClass) {
4446
super(fieldsToAttributes, entityClass);
4547
}
4648

49+
/**
50+
* Creates a new EntityData object based on a given {@link FactoryData} object
51+
*
52+
* @param factoryData The factory data object to use attributes of
53+
*/
54+
protected EntityData(FactoryData factoryData) {
55+
super(factoryData.getFieldsToValues(), factoryData.getTargetClass());
56+
}
57+
4758
@Override
4859
@SuppressWarnings("unchecked cast")
4960
public Class<? extends UniqueEntity> getTargetClass() {

src/main/java/edu/ie3/datamodel/io/factory/FactoryData.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ public abstract class FactoryData {
1818

1919
protected FactoryData(Map<String, String> fieldsToAttributes, Class<?> targetClass) {
2020
// this does the magic: case-insensitive get/set calls on keys
21-
TreeMap<String, String> insensitiveFieldsToAttributes =
22-
new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
23-
insensitiveFieldsToAttributes.putAll(fieldsToAttributes);
24-
this.fieldsToAttributes = insensitiveFieldsToAttributes;
21+
this.fieldsToAttributes = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
22+
this.fieldsToAttributes.putAll(fieldsToAttributes);
2523
this.targetClass = targetClass;
2624
}
2725

src/main/java/edu/ie3/datamodel/io/factory/SimpleEntityData.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/main/java/edu/ie3/datamodel/io/factory/SimpleEntityFactory.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)