Skip to content

Derived Measure

Antonin Abherve edited this page Feb 16, 2017 · 2 revisions

Derived Measure API

A Derived measure is used to defined a combined measure which will calculate new measurements using one or more measurements stored on measure platform.

To defined a Direct Measure, implement the IDerivedMeasure interface. This interface will be called by the MeasurePlatform 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 liste of Measurement based on provided measurement imputs.
  • addMeasureInput() : Provide a way for the MeasurePlatform to communicate input measurementto the DerivedMeasure implementation.
  • getProperties() : Provide a way for the MeasurePlatform to communicate properties to the DerivedMeasure implementation.

To implement a derived measure, please extend the **DerivedMeasure **class.

Properties

For more details about properties:
https://github.com/aabherve/measure-smm-measure-api/wiki/Measure-Properties

Inputs Measurements

Required Measurements are defined on the MetaData.xml. Thes references are identified by a measureRef and a role.

  • The **measureRef ** is the id the measure which can provide a measurement as imput.
  • The role is the Role of the imput in current Measurement. This role allow to identifyseveral instance of the same Measure in a DerivedMeasure.
  • The expirationDelay property allow to filter as input the measures which has been calculated recently
  • The number property allow to select the number of input of this type which will be communicated to 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 a Instance on the measure is created on MeasurePlatform

Derived Measure Examples

RandomBinaryMeasure : A toy measure whch return 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 whch return 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;
	}
}