LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Merge clusters with overrides

Solved!
Go to solution

Dear experts, 

 

I have two clusters: a Default values cluster and a Strict values cluster.
I want to merge them where:

  • If a value exists in the Strict cluster, it should override the value in the Default cluster.

  • If a value does not exist in the Strict cluster, then the value from the Default cluster should stay.

I could implement this manually, but my question is: does a built‑in block / function / component already exist for this kind of overriding‑merge behavior?
(Any kind of cluster element can occur, e.g., enum, array, etc)

Combine_clusters.png

 

0 Kudos
Message 1 of 19
(534 Views)

Sorry, no.  But if you're crazy enough, you could make an XNode that does that.

0 Kudos
Message 2 of 19
(506 Views)

I recall doing something similar to this while loading defaults from a config file in JSON format.

 

You might be able to convert your example strict values to JSON, then import the resulting JSON using a JSON to LV function with your typedef to update the subset while leaving the default values in place.

 

I don't think the LabVIEW native JSON functions do this; you would need to use the JKI or JDP Science JSON packages.

Message 3 of 19
(481 Views)

Per Phillip's recommendation:
Override Specific Cluster Elements.png

which uses this package:

dsbNI_0-1763149107543.png

 

Good luck!

 

Doug
Enthusiast for LabVIEW, DAQmx, and Sound and Vibration
Message 4 of 19
(471 Views)

@PhillipDBrooks wrote:

You might be able to convert your example strict values to JSON, then import the resulting JSON using a JSON to LV function with your typedef to update the subset while leaving the default values in place.


I would recommend against this as converting to a string will cause your floating point values to actually change. Plus the conversion to string and back is a lot more expensive than just doing an unbundle and bundle.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 19
(436 Views)

I think we need to take a step back and ask why you want to do this. Here are some relevant questions:

  1. Do you actually have two different clusters? What is the relation between them? What exactly is your setup here?
  2. Are you familiar with typedefs? Your clusters are not typedefs and it's possible that just having a typedef will be enough, as that can define your default values.

    In general, you want clusters to be typedefs, certainly if they're used in more than a single spot in the code. There may be some exceptions to this, but you should generally default to typedefs.

___________________
Try to take over the world!
0 Kudos
Message 6 of 19
(322 Views)

@tst wrote:

I think we need to take a step back and ask why you want to do this. Here are some relevant questions:

  1. Do you actually have two different clusters? What is the relation between them? What exactly is your setup here?
  2. Are you familiar with typedefs? Your clusters are not typedefs and it's possible that just having a typedef will be enough, as that can define your default values.

    In general, you want clusters to be typedefs, certainly if they're used in more than a single spot in the code. There may be some exceptions to this, but you should generally default to typedefs.

Agree. Also consider what you mean by "exists" in "If a value exists in the Strict cluster". If it doesn't exist, then its not in the cluster, and then its not a cluster of that type. Should any type of cluster be allowed and you just go by correct type and name or how should that work? Or does "not exist" mean the value is a default you interpret as "not existing"?

 

If you skip the strict cluster and change the bundle by name to a bundle, then you can connect the data you want to override the "default" cluster.

thols_0-1763367933922.png

 

 

Certified LabVIEW Architect
0 Kudos
Message 7 of 19
(280 Views)

@denesdomjan wrote:

Dear experts, 

 

I have two clusters: a Default values cluster and a Strict values cluster.
I want to merge them where:

  • If a value exists in the Strict cluster, it should override the value in the Default cluster.

  • If a value does not exist in the Strict cluster, then the value from the Default cluster should stay.

I could implement this manually...


A common use case is when the “Default Values” cluster keeps continuously growing during development. You keep adding new elements but still want to maintain "backward compatibility" with previous types, without using JSON or XML where this functionality is already built in.

One approach is to use the “Cluster to Array of VData” and “Array of VData to VCluster” pair functions from the OpenG Toolkit. These let you convert a cluster into an array of variants, override the desired values, and then convert it back. This works under the simple assumption that your values are sequential, so the "Default" cluster always contains the “Strict” portion at the beginning:

Snippet1.png

You can create a SubVI with two variant inputs and one output and reuse it. This makes it universal, so you don’t need to rewire individual elements anymore.

If you need more robust logic, for example when the order of “Strict” and “Default” elements differs but their names are unique, you can use Get Type Information to retrieve the names and override values accordingly:

Snippet2.png

If you have nested clusters, things get more complicated because you need to walk through all elements in sub clusters and define matching rules for when values should come from the “Strict” or the “Default” cluster. It’s also a good idea to define your "Default" cluster as a Typedef as shown above; otherwise, you’ll have to replace it in multiple places whenever new elements are added.

Message 8 of 19
(257 Views)

Here's an XNode that does it.

 

If you're not afraid of XNodes.

 

And you should be afraid of XNodes.

 

Especially XNodes from 3rd parties.

 

Especially if the 3rd party is a crazy old man.

Message 9 of 19
(233 Views)

@denesdomjan

Now you have options. Please post back to let us know which path you chose and why.

P.S. I might be inclined to go with Paul's XNode just because his post made me laugh.

Doug
Enthusiast for LabVIEW, DAQmx, and Sound and Vibration
0 Kudos
Message 10 of 19
(223 Views)