Component Atttribute FAQ
How do I access an Attribute value?
The getValue() Method provides, the content of an Attribute according to its type.
int a = attr.getValue(); // get the
How do I set an Attribute value ?
The setValue() Method puts in a new value for an attribute.
sc_attr.setValue(1.45); // scalar attribute
arr_attr.setValue(2, 12); // set Array element 2 to value 12
How do OMS Arrays relate to IEEE data types
Array type Mapping:
Java array base type | OMS type |
float (4byte) | Attribute.FloatArray |
double (8byte) | Attribute.DoubleArray |
boolean (1byte) | Attribute.BooleanArray |
int (4byte) | Attribute.IntegerArray |
How do I declare a dynamic 1D float array
This declares a one dimensional array of doubles, which lengths is not set. The dimension is named d
/** Array example
* @oms.dim d
*/
Attribute.FloatArray arr;
- Note: if the @oms.dim info is missing the array will be a 1D array per default.
How do I declare a dynamic 2D double array
This declares a two dimensional array of doubles, which lengths is not set. The dimension is named x and y
/** Array example
* @oms.dim x,y
*/
Attribute.DoubleArray arr;
- Note: Different dimensions are specified only in the meta data sections, All using the same array class.
How do I declare a static 1D float array?
This declares a two dimensional array of doubles, which length is set to 10. The dimension is named x.
/** Array example
* @oms.dim x:10
*/
Attribute.FloatArray arr;
- Note: Such an array usually maps to a FORTRAN array, which is statically allocated. Therefore any change in the length would cause an improper access to memory at the FORTRAN level.
How do I get the rank of an array?
This declares a two dimensional dynamic array of floats. The number of dimensions is two, the lengths of the dimensions
are both zero since it is not allocated yet. The rank of that array is two.
/** Array example
* @oms.dim x,y
*/
Attribute.FloatArray arr;
...
int rank = arr.getRank(); // 2
How do I get the actual dimension length?
This declares a two dimensional dynamic array of floats. The number of dimensions is two, the lengths of the dimensions
are both zero since it is not allocated yet.
/** Array example
* @oms.dim x,y
*/
Attribute.FloatArray arr;
...
int[] dims = arr.getDims();
System.out.println(dims.length); // ouput: 2
System.out.println(dims[0]); // ouput: 0
System.out.println(dims[1]); // output: 0
The second example shows the same code for a static array:
/** Array example
* @oms.dim x:2,y:3
*/
Attribute.FloatArray arr;
...
int[] dims = arr.getDims();
System.out.println(dims.length); // ouput: 2
System.out.println(dims[0]); // ouput: 2
System.out.println(dims[1]); // output: 3
How do I allocate a dynamic array?
/** Array example
* @oms.dim x,y
*/
Attribute.FloatArray arr;
...
int[] dims = arr.getDims();
System.out.println(dims.length); // ouput: 2
System.out.println(dims[0]); // ouput: 0
System.out.println(dims[1]); // output: 0
arr.allocate(new int[] {5,6});
System.out.println(dims.length); // ouput: 2
System.out.println(dims[0]); // ouput: 5
System.out.println(dims[1]); // output: 6
- Note: If the number of dimensions used in the allocate() call is different from the one specified in the @oms.dim section, an IllegalArgumentException is thrown. If the array is static, the allocate() call throws an IllegalAccessException
How do I access Array Elements?
/** Array example
* @oms.dim x:2, y:3
*/
Attribute.FloatArray arr;
...
int[] dims = arr.getDims();
assert dims.length==1 : "Invalid dimensions for arr";
for(int i = 0; i<dims[0]; i++) {
for(int j = 0; j<dims[1]; j++) {
// printing out the existing Value
System.out.println(Element "+i+","+j+ arr.getValue(i,j);
// setting the value
arr.setValue(i,j, 10 + i+ j);
}
}
How do I accessing the array store?
The following code accesses an array's store and computes the mean of all elements. Note that this code
works for all array access code which not depends on a certain array layout.
/** Array example
* @oms.dim x:2, y:3
*/
Attribute.FloatArray arr;
...
Array.Store store = arr.getStore();
System.out.println(store.size()) // 6
float sum = 0;
for(int i = 0; i<store.size(); i++) {
sum += store.getDouble(i);
}
System.out.println("Mean: " + sum/store.size());
Notes:
- There is clear distinction between the dimension rank (or shape)
and the dimension lengths. As someone might notice
there is no way in the OMS API that you can change the shape
of an array programmatically. What should be the use case?
There is none. Code is always written against a certain array shape.
- OMS Arrays are not dynamic regarding their layout,
they are mutable to the length of their dimension only.
In other words: You cannot change a one dimensional array into a
two dimensional array, but you can change the length of any array
dimension; that's what dynamic means.
- Static arrays as proposed are not allowing for any change at all,
even dimension lengths are fixed! Reason: They are mapped to static
allocated FORTRAN array.