-
Notifications
You must be signed in to change notification settings - Fork 5
/
EntityProcessor.java
99 lines (90 loc) · 3.48 KB
/
EntityProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io.processor;
import edu.ie3.datamodel.exceptions.EntityProcessorException;
import edu.ie3.datamodel.models.StandardUnits;
import edu.ie3.datamodel.models.UniqueEntity;
import java.lang.reflect.Method;
import java.util.*;
import javax.measure.Quantity;
import javax.measure.quantity.Energy;
import javax.measure.quantity.Power;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Internal API Interface for EntityProcessors. Main purpose is to 'de-serialize' models into a
* fieldName to value representation to allow for an easy processing into a database or file sink
* e.g. .csv
*
* @version 0.1
* @since 31.01.20
*/
public abstract class EntityProcessor<T extends UniqueEntity> extends Processor<T> {
public static final Logger log = LoggerFactory.getLogger(EntityProcessor.class);
protected final String[] headerElements;
private final SortedMap<String, Method> fieldNameToMethod;
private static final String NODE_INTERNAL = "nodeInternal";
/**
* Create a new EntityProcessor
*
* @param registeredClass the class the entity processor should be able to handle
*/
protected EntityProcessor(Class<? extends T> registeredClass) {
super(registeredClass);
this.fieldNameToMethod =
mapFieldNameToGetter(registeredClass, Collections.singleton(NODE_INTERNAL));
this.headerElements = fieldNameToMethod.keySet().toArray(new String[0]);
}
/**
* Standard call to handle an entity
*
* @param entity the entity that should be 'de-serialized' into a map of fieldName to fieldValue
* @return an optional Map with fieldName to fieldValue or an empty optional if an error occurred
* during processing
*/
public Optional<LinkedHashMap<String, String>> handleEntity(T entity) {
if (!registeredClass.equals(entity.getClass()))
throw new EntityProcessorException(
"Cannot process "
+ entity.getClass().getSimpleName()
+ ".class with this EntityProcessor. Please either provide an element of "
+ registeredClass.getSimpleName()
+ ".class or create a new processor for "
+ entity.getClass().getSimpleName()
+ ".class!");
try {
return Optional.of(processObject(entity, fieldNameToMethod));
} catch (EntityProcessorException e) {
logger.error("Cannot process the entity{}.", entity, e);
return Optional.empty();
}
}
@Override
protected Optional<String> handleProcessorSpecificQuantity(
Quantity<?> quantity, String fieldName) {
return switch (fieldName) {
case "energy", "eConsAnnual", "eStorage":
yield quantityValToOptionalString(
quantity.asType(Energy.class).to(StandardUnits.ENERGY_IN));
case "q":
yield quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.REACTIVE_POWER_IN));
case "p", "pMax", "pOwn", "pThermal":
yield quantityValToOptionalString(
quantity.asType(Power.class).to(StandardUnits.ACTIVE_POWER_IN));
default:
log.error(
"Cannot process quantity with value '{}' for field with name {} in input entity processing!",
quantity,
fieldName);
yield Optional.empty();
};
}
@Override
public String[] getHeaderElements() {
return headerElements;
}
}