App Reference Tracking Part 1 - Simple Object Arrays

I’ll first describe our actual use case but I created a “demo” application that’s more universal.

We have a need to track gauges used to perform inspection on our parts. The idea being that the operator will scan the gauges into the application for DHR purposes. We decided to store this data as part of the app completion data so the only way to accomplish that is to use object arrays.

Unfortunately, there’s VERY few methods to deal with or modify object arrays within an application which ended up making for some very interesting work arounds, but I digress. This post will only cover simple reference tracking and I’ll be working on a Part 2 another day.

What are Object Arrays?

The most common use case for object arrays are the results of SQL connector functions where multiple records are returned, but that’s really two questions in one, so first let’s start with What are objects in Tulip?

They’re basically a way to store and use a complex JSON object within an application. Once created you can alter the contents of the object using normal Data Manipulation methods.

Example 1:

{
"object": {
    "item 1": "text value 1",
    "item 2": integer,
    ...
    }
}

NOTE: In Tulip, once you create an object and use it, its structure and datatypes are fixed and cannot change.

Object Arrays are simply an array of objects with a zero based index.

Example 2:

{
"objects": {
    0: {
        "Item 1": "text value 1",
        "item 2": integer1
    }
    1: {
        "item 1: "text value 2",
        "item 2: integer1
    }
    ...
}

How Do You Use Object Arrays for Reference Counting?

This is actually somewhat complicated, but not nearly as complicated as Part 2 will be…

Example 3:

First let me show you the demo, and then we’ll break down how it works:

Simple Reference Counting

As you can see, instead of simply appending the duplicate animals, it actually updates the count to the sum of the existing object and the new one.

The Nitty-Gritty Details

First you need to create an object with the format you need with all the elements you want to store and their data type. For my demo I’m counting animal types and their count:

Then you need to create an object array with the exact same formal. You can do this manually but it’s easier to create the object array on the fly. Tulip will take care of making sure the names and data types are the same.

NOTE: Ignore the “Requirements Met?” part, that’s for Part 2 :slight_smile:

Now comes the fun part. All the logic to make this work.

Here’s a snapshot of the triggers and their orders:

  1. Gather existing items: This checks to see if this is the first time an object is being added or if we need to modify an existing object.

    We can only check if the array is empty, so if it it’s, the trigger’s a no-op, otherwise MAP_TO_TEXT_ARRAY is used to create a simple array we can actually use to find the index of an existing object.

  2. Load if it exists: Here we “load” (and I use the term loosely) the existing object if it’s already been added before:

    We make use of the simple array created by the previous trigger to find the index of the object we want to “load”. Then because objects in arrays cannot be modified in place, we move it into a new object with the same structure called Working Object and store the sum of the previous count and new count into a variable called Item Total Count.

    If this is the first time we’re seeing this item, just store the count into the Item Total Count variable as that makes the logic easier in the next trigger where we store the count.

    NOTE: Even if this is the first time the item type has been encountered we copy the Item from the Input Object to the Working Object just to make downstream logic simpler.

  3. Add or modify object array: Now we append the Working Object back onto the Object Array

    Because we merged the data to the Working Object already, we just need to push it on to the Object Array. This could be added to the previous trigger but we would have had to put in the push logic twice which I try to avoid when possible.

6 Likes

This is awesome Richard! Thank you for sharing :grinning:
It is really clear with the simplified demo you made how this all works. I know Objects and Object Arrays can be confusing for some folks in Tulip, especially if you are getting started, so I think this will be super helpful for others to learn from and reference in the future :star2:

Thanks!

I think we could definitely use more methods around handling arrays and object arrays, but in particular I was surprise to find there was no way to get the length of a simple array or the count of objects of an object array.

That would definitely simplify some of the logic especially for Part 2 :slight_smile:

In Release 277 - April 2024 we actually added in to expression editor the “LEN” function that allows you to get the length of an object array! You are on a LTS release, if I am remembering correctly, right? This should be part of the upcoming LTS 13 (since LTS 12 cuts off at r275).

Yes, I created this on our training instance which is currently LTS 12. I’ll refactor once we upgrade to LTS 13. Thanks!

1 Like

Wow, talk about perfect timing. I was just having an issue with this! Thanks for the detailed walkthrough.

1 Like

I just posted part two!

1 Like