In this article, we will show how to calculate a component's position in the component hierarchy using Gremlin. Used in a calculated field this can be a convenient way to quickly get an understanding of the component's position in the hierarchy without having to utilize the hierarchy navigator. This can be particularly useful in presentations where the hierarchy navigator is not available.
Calculating a Specific Component's Position in the Hierarchy
In this example, we will calculate the position in the component hierarchy for the component called Sales Strategy and Plans. Assuming that the top level is number 1, we can see that the component is located at level 4 in our Business Capabilities workspace:
Assuming that we only have one component called Sales Strategy and Plans we can use the following query to calculate its position in the component hierarchy:
has('name', 'Sales Strategy and Plans').
In the first two lines of the query, we find the component called Sales Strategy and Plans. In the third line of the query we repeatedly keep the current component (using
emit()) and traverse up the hierarchy by following the outgoing ardoq_parent reference. The ardoq_parent reference is not an actual reference, but in Ardoq we expose child-parent relationships in Gremlin as a reference called ardoq_parent. In the fourth line of the query we count the number of components we are left with. This will give us the component's position in the component hierarchy since the position is equal to the number of ancestor components plus the component itself.
As expected we get 4 as a result.
Creating a Calculated Field to Show a Component's Position in the Hierarchy
In this example we will create a calculated field that shows the hierarchy position of any component the field is applied to. We will only cover the actual Gremlin query used to create the calculated field, so if this is your first time creating a calculated field make sure to also check out https://help.ardoq.com/en/articles/2987623-calculated-fields to learn how to set up a calculated field.
Below we have modified our previous query to fit how Ardoq expects calculated fields to be defined:
project('id', 'name', 'value').
You may notice that the last line of the query is very similar to our previous query. The rest of the query is common for all calculated fields in Ardoq and ensures that one value is calculated for each component that the calculated field is applied to. Running the query in the calculated field editor we can see that the calculated value for our Sales Strategy and Plans component is 4, as expected:
After saving and applying the field, we can navigate to our Sales Strategy and Plans component and verify that our Position In Hierarchy field has been set to 4, as expected: