Jerome Lefebvre

Rollups without rollups

Blog Post created by Jerome Lefebvre on Oct 15, 2020

It is a common task to what to know the number of child elements in a certain state and expose that as an AF attribute of the parent element. This is typically done by creating an attribute with a value of 0/1 per state that you want to measure and then sum those up using a rollup. This gets less pleasant to do the more states you want to capture. I've been trying to find alternative methods to do this, and this is the best I got so far, even though it leads to strange looking analyses.


This is a strange setup, and I'm not really suggesting this is a better way to do things, but maybe these alternative ways of using AF will spur on even further creative use of it.


# The input

I have 4 child elements:

each has an attribute called status which is an enumeration set


# The resulting calculation



# Explanation of each line

## Line 1 - Indicies

I need to be able to iterate over each child elements, the way that I do so is to grab an array which has the numbers 1 to N, where N is greater than the number of children. Here I do so with an AF array.





## Line 2 - Statuses

I need to access the status attribute of each child element. There are several limitations to this, I need something that I can iterate over, I need to be able to handle the cases where there are fewer child elements than my indices. If I try to access an attribute of a child element of a child that does not exist the analysis will simply no run. Thus, I need something that allows me to build a path to that attribute and return a bad state if that child doesn't exist. 

TagNum is just the thing. It is a method that retrieves information about an attribute, yet the input is a string. It also simply returns a calc fail if the string fails to resolve to anything.


There are many ways to create a path to a child element attribute, but in this case, I will use the index of the child element to create a relative path. Something like, ".\[@index=2]|Status".

As I want to iterate over each index, I can use the MapData to loop over my array of numbers 1 to N.


## Line 3 and 4 - Counting the statuses

FilterData is another method that can loop over an array. In this case, I only want to leave the values that match a certain value. Once we have those values, we can simply count them up using ArrayLength




## Alternatives to line 1

There aren't many ways to build an array with certain properties in an analysis.

But, here is one way to build an array with values from 1 to N. 

It works by grabbing the last 10 values of a tag (in latest to oldest order), then counting up the number of stored values.