-
Notifications
You must be signed in to change notification settings - Fork 0
Derived Measure
A derived measure is used to define a combined measure which calculates new measurements using one or more measurements stored on the Measure platform.
To defined a derived measure, implement the IDerivedMeasure interface. This interface will be called by the Measure Platform to calculate the measurement.
public interface IDerivedMeasure {
public List<IMeasurement> calculateMeasurement() throws Exception;
public void addMeasureInput(String reference,String role, IMeasurement value);
public Map<String,String> getProperties();
}
- calculateMeasurement(): Calculate and return a list of measurements based on provided measurement imputs.
- addMeasureInput(): Provide a way for the Measure Platform to communicate input measurements to the DerivedMeasure implementation.
- getProperties(): Provide a way for the Measure Platform to communicate properties to the Derived Measure implementation.
To implement a derived measure, please extend the DerivedMeasure class.
For more details about properties:
https://github.com/aabherve/measure-smm-measure-api/wiki/Measure-Properties
Required measurements are defined on the MetaData.xml. These references are identified by a measureRef and a role.
- The measureRef is the id of the measure which can provide a measurement as input.
- The role is the role of the imput in current measurement. This role allows to identify several instances of the same measure of a Derived Measure.
- The expirationDelay property allows to filter as input the measures which has been calculated recently
- The number property allows to select the number of inputs of this type which will be communicated to the derived measure implementation by the platform.
<references expirationDelay="60000" measureRef="RandomGenerator" number="1">
<role>RandomNumber A</role>
</references>
<references expirationDelay="60000" measureRef="RandomGenerator" number="1">
<role>RandomNumber B</role>
</references>
Inputs are defined when an instance on the measure is created on the Measure Platform.
RandomBinaryMeasure: A toy measure which returns the result of a binary operation between two RandomGenerator result
public class RandomBinaryMeasure extends DerivedMeasure {
@Override
public List<IMeasurement> calculateMeasurement() {
Integer result = 0;
// Retrive input Measurements by her Role
List<IMeasurement> op1 = getMeasureInputByRole("RandomNumber A");
List<IMeasurement> op2 = getMeasureInputByRole("RandomNumber B");
// Calculate result
if(op1.size() == 1 && op2.size() == 1){
String oper = "+";
// Retrive the operator as Property
oper = getProperty("Operation");
Integer val1 = (Integer) op1.get(0).getValues().get("value");
Integer val2 = (Integer) op2.get(0).getValues().get("value");
if(oper.equals("+")){
result = val1 + val2;
}else if(oper.equals("-")){
result = val1 - val2;
}else if(oper.equals("*")){
result = val1 * val2;
}else if(oper.equals("/")){
result = val1 / val2;
}
}
// Return result as new IntegerMeasurement
IntegerMeasurement measurement = new IntegerMeasurement();
measurement.setValue(result);
List<IMeasurement> measurements = new ArrayList<>();
measurements.add(measurement);
return measurements;
}
}
RandomSumMeasure: A toy measure which returns the sum of measurements provided by the RandomGenerator measure.
public class RandomSumImpl extends DerivedMeasure {
@Override
public List<IMeasurement> calculateMeasurement() throws Exception {
Integer result = 0;
for (IMeasurement operande : getMeasureInputByRole("RandomNumber")) {
try {
result = result + (Integer) operande.getValues().get("value");
} catch (NumberFormatException e) {
System.out.println("Non Numeric Operande");
}
}
IntegerMeasurement measurement = new IntegerMeasurement();
measurement.setValue(result);
List<IMeasurement> measurements = new ArrayList<>();
measurements.add(measurement);
return measurements;
}
}