Result

Components use Results to return results of a command in the form of a ParameterSet. Result is the value that is returned as an argument to the Completed SubmitResponse.

Creating a Result Requires:

Scala
source// keys
val k1: Key[Int]    = KeyType.IntKey.make("encoder")
val k2: Key[Int]    = KeyType.IntKey.make("windspeed")
val k3: Key[String] = KeyType.StringKey.make("filter")
val k4: Key[Int]    = KeyType.IntKey.make("notUsed")

// parameters
val p1: Parameter[Int]    = k1.set(22)
val p2: Parameter[Int]    = k2.set(44)
val p3: Parameter[String] = k3.set("A", "B", "C", "D")

// Create Result using madd
val r1: Result = Result().madd(p1, p2)
// Create Result using apply
val r2: Result = Result(p1, p2)
// Create Result and use add
val r3: Result = Result().add(p1).add(p2).add(p3)

// access keys
val k1Exists: Boolean = r1.exists(k1) // true

// access Parameters
val p4: Option[Parameter[Int]] = r1.get(k1)

// access values
val v1: Array[Int] = r1(k1).values
val v2: Array[Int] = r2.parameter(k2).values
// k4 is missing
val missingKeys: Set[String] = r3.missingKeys(k1, k2, k3, k4)

// remove keys
val r4: Result = r3.remove(k3)
Java
source//keys
Key<Integer> k1 = JKeyType.IntKey().make("encoder", JUnits.encoder);
Key<Integer> k2 = JKeyType.IntKey().make("windspeed", JUnits.NoUnits);
Key<String> k3 = JKeyType.StringKey().make("filter", JUnits.NoUnits);
Key<Integer> k4 = JKeyType.IntKey().make("notUsed", JUnits.NoUnits);

//parameters
Parameter<Integer> p1 = k1.set(22);
Parameter<Integer> p2 = k2.set(44);
Parameter<String> p3 = k3.set("A", "B", "C", "D");

//Create Result using madd
Result r1 = new Result().madd(p1, p2);
//Create Result in line
Result r2 = new Result().madd(p1, p2);
//Create Result and use madd, add
Result r3 = new Result().madd(p1, p2).add(p3);

//access keys
boolean k1Exists = r1.exists(k1); //true

//access Parameters
Optional<Parameter<Integer>> p4 = r1.jGet(k1);

//access values
List<Integer> v1 = r1.jGet(k1).orElseThrow().jValues();
List<Integer> v2 = r2.parameter(k2).jValues();

//k4 is missing
Set<String> missingKeys = r3.jMissingKeys(k1, k2, k3, k4);

//remove keys
Result r4 = r3.remove(k3);

JSON serialization

State variables can be serialized to JSON. The library has provided JsonSupport helper class and methods to serialize DemandState and CurrentState.

Scala
sourceimport play.api.libs.json.{JsValue, Json}

// key
val k1: Key[MatrixData[Double]] = DoubleMatrixKey.make("myMatrix")
// values
val m1: MatrixData[Double] = MatrixData.fromArrays(
  Array(1.0, 2.0, 3.0),
  Array(4.1, 5.1, 6.1),
  Array(7.2, 8.2, 9.2)
)

// parameter
val i1: Parameter[MatrixData[Double]] = k1.set(m1)

// result
val result: Result = Result().add(i1)

// json support - write
val resultJson: JsValue = JsonSupport.writeResult(result)

// optionally prettify
val str: String = Json.prettyPrint(resultJson)

// construct result from string
val scFromPrettyStr: Result = JsonSupport.readResult(Json.parse(str))

// json support - read
val result1: Result = JsonSupport.readResult(resultJson)
Java
source//key
Key<MatrixData<Double>> k1 = JKeyType.DoubleMatrixKey().make("myMatrix", JUnits.NoUnits);

//values
Double[][] doubles = {{1.0, 2.0, 3.0}, {4.1, 5.1, 6.1}, {7.2, 8.2, 9.2}};
MatrixData<Double> m1 = MatrixData.fromArrays(doubles);

//parameter
Parameter<MatrixData<Double>> i1 = k1.set(m1);

//ObsId
ObsId obsId = ObsId.apply("2020A-001-123");

//result
Result result = new Result().add(i1);

//json support - write
JsValue resultJson = JavaJsonSupport.writeResult(result);

//optionally prettify
String str = Json.prettyPrint(resultJson);

//construct result from string
Result result1 = JavaJsonSupport.readResult(Json.parse(str));

Unique Key Constraint

By choice, a ParameterSet in Result will be optimized to store only unique keys. In other words, trying to store multiple keys with same name, will be automatically optimized by removing duplicates.

Note

Parameters are stored in a Set, which is an unordered collection of items. Hence, it’s not predictable whether first or last duplicate copy will be retained. Hence, cautiously avoid adding duplicate keys.

Here are some examples that illustrate this point:

Scala
source// keys
val encoderKey: Key[Int] = KeyType.IntKey.make("encoder")
val filterKey: Key[Int]  = KeyType.IntKey.make("filter")
val miscKey: Key[Int]    = KeyType.IntKey.make("misc.")

// params
val encParam1 = encoderKey.set(1)
val encParam2 = encoderKey.set(2)
val encParam3 = encoderKey.set(3)

val filterParam1 = filterKey.set(1)
val filterParam2 = filterKey.set(2)
val filterParam3 = filterKey.set(3)

val miscParam1 = miscKey.set(100)

// Setup command with duplicate key via constructor
val result = Result(encParam1, encParam2, encParam3, filterParam1, filterParam2, filterParam3)
// four duplicate keys are removed; now contains one Encoder and one Filter key
val uniqueKeys1 = result.paramSet.toList.map(_.keyName)

// try adding duplicate keys via add + madd
val changedResult = result
  .add(encParam3)
  .madd(
    filterParam1,
    filterParam2,
    filterParam3
  )
// duplicate keys will not be added. Should contain one Encoder and one Filter key
val uniqueKeys2 = changedResult.paramSet.toList.map(_.keyName)

// miscKey(unique) will be added; encoderKey(duplicate) will not be added
val finalResult = result.madd(Set(miscParam1, encParam1))
// now contains encoderKey, filterKey, miscKey
val uniqueKeys3 = finalResult.paramSet.toList.map(_.keyName)
Java
source//keys
Key<Integer> encoderKey = JKeyType.IntKey().make("encoder", JUnits.encoder);
Key<Integer> filterKey = JKeyType.IntKey().make("filter", JUnits.NoUnits);
Key<Integer> miscKey = JKeyType.IntKey().make("misc.", JUnits.NoUnits);

//ObsId
ObsId obsId = ObsId.apply("2020A-001-123");

//params
Parameter<Integer> encParam1 = encoderKey.set(1);
Parameter<Integer> encParam2 = encoderKey.set(2);
Parameter<Integer> encParam3 = encoderKey.set(3);

Parameter<Integer> filterParam1 = filterKey.set(1);
Parameter<Integer> filterParam2 = filterKey.set(2);
Parameter<Integer> filterParam3 = filterKey.set(3);

Parameter<Integer> miscParam1 = miscKey.set(100);

//Setup command with duplicate key via madd
Result result = new Result().madd(encParam1, encParam2, encParam3, filterParam1, filterParam2, filterParam3);
//four duplicate keys are removed; now contains one Encoder and one Filter key
Set<String> uniqueKeys1 = result.jParamSet().stream().map(Parameter::keyName).collect(Collectors.toUnmodifiableSet());

//try adding duplicate keys via add + madd
Result changedResult = result.add(encParam3).madd(filterParam1, filterParam2, filterParam3);
//duplicate keys will not be added. Should contain one Encoder and one Filter key
Set<String> uniqueKeys2 = changedResult.jParamSet().stream().map(Parameter::keyName).collect(Collectors.toUnmodifiableSet());

//miscKey(unique) will be added; encoderKey(duplicate) will not be added
Result finalResult = result.madd(miscParam1, encParam1);
//now contains encoderKey, filterKey, miscKey
Set<String> uniqueKeys3 = finalResult.jParamSet().stream().map(Parameter::keyName).collect(Collectors.toUnmodifiableSet());

Source Code for Examples