# DataModel is not shared between nodes

**URL:** <https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773>\
**Category:** Java\
**Created:** [September 20, 2018, 9:19am UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773 "2018-09-20T09:19:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon53303193](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@anon53303193](https://forum.universal-robots.com/u/anon53303193)\
**Post date:** [September 20, 2018, 9:19am UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/1 "2018-09-20T09:19:48Z")

</div>

I’m having issues with the DataModel. Anything I set and get using model.get() and model.set() is only stored in the specific node.  
My URCap is structures as 1 parent node and 4 child nodes, and if I set() something in the parent node, the get() will return the default value in the child node. My question is then:  
Is my DataModel only available to the node it is get() and set() in? Meaning there’s 5 DataModels in my structure, or did I do something wrong?  
Afterwards I tried to add an installation node, but if I use the model.set() in the installation node, it is still not available for any of the other nodes. I believe it should be, so I’m guessing something’s wrong.

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [September 20, 2018, 11:16am UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/2 "2018-09-20T11:16:31Z")

</div>

Each URCap node has its own DataModel, which is not shared with any other node.

---

<div class="post-metadata">

**Author:** ![anon53303193](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@anon53303193](https://forum.universal-robots.com/u/anon53303193)\
**Post date:** [September 20, 2018, 12:13pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/3 "2018-09-20T12:13:27Z")

</div>

Thank you for the reply. So I can’t access the datamodel from a different node? Can I share variables between node in any way then?

---

<div class="post-metadata">

**Author:** ![anon16416181](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@anon16416181](https://forum.universal-robots.com/u/anon16416181)\
**Post date:** [September 20, 2018, 8:33pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/4 "2018-09-20T20:33:30Z")

</div>

Do you really need the DataModel?

You can create a singleton object that stores the information that you need, with a public static getter that you can use anywhere in your code.

You can check a sample implementation here: [Java Singleton Design Pattern Best Practices with Examples | DigitalOcean](https://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-examples). Then, you can add getters and setters for the information you need to share.

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [September 21, 2018, 7:05am UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/5 "2018-09-21T07:05:19Z")

</div>

Anything that should be accessible by the user should preferable be stored in the DataModel.  
The DataModel is part of the program and installation files, so if you do not store your data here, the data will only be stored locally, and the user will have to reconfigure this again on a new robot.

If you are a parent-node, you can read and modify your child-nodes.  
Either by them being built-in nodes such as Wait, or by using the URCap Custom API on your child nodes.

---

<div class="post-metadata">

**Author:** ![anon53303193](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@anon53303193](https://forum.universal-robots.com/u/anon53303193)\
**Post date:** [September 25, 2018, 10:01am UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/6 "2018-09-25T10:01:06Z")

</div>

How do I use the URCap Custom API?

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [September 25, 2018, 10:36am UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/7 "2018-09-25T10:36:00Z")

</div>

Check out the section “URCap Program Node proprietary API” in the “program node configuration” document in the SDK.

---

<div class="post-metadata">

**Author:** ![anon53303193](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@anon53303193](https://forum.universal-robots.com/u/anon53303193)\
**Post date:** [September 26, 2018, 7:20am UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/8 "2018-09-26T07:20:01Z")

</div>

Thank you, I’ve read the pdf, but does still have a few questions:  
So basically by implementing a Custom API on the child node, it is possible to access methods from the parent node?  
To implement this I have to define this interface with the lines:

interface CustomAPI {  
**Setting** getSetting ();  
void setSetting ( **Setting** setting );  
boolean isOptionEnabled ();  
void reset ();  
}

What are the functions of what’s inside the interface? Can I find that in any documentation? When I declare the interface with these it says that the type **Setting** is unresolved, so that is my main problem right now. I’ve declared the interface right before ProgramNodeContribution();.

I’m still learning Java, so I’ll apologise if it is obvious.

---

<div class="post-metadata">

**Author:** ![m.birkholz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/m.birkholz/32/7084_2.png) [@m.birkholz](https://forum.universal-robots.com/u/m.birkholz)\
**Post date:** [September 27, 2018, 9:07am UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/9 "2018-09-27T09:07:17Z")

</div>

i would also recommend using a singleton object to handle the datamodel object between your Contributions as @anon16416181 said.  
Just initialize the datamodel by the parent/master node and save the datamodel object reference to the singleton object.

I found the CustomAPI solution awkward and didn’t found a reasonable sample code for this.

---

<div class="post-metadata">

**Author:** ![anon53303193](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@anon53303193](https://forum.universal-robots.com/u/anon53303193)\
**Post date:** [October 23, 2018, 3:33pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/10 "2018-10-23T15:33:34Z")

</div>

I thought I would just share what I found out, after I got it to work:  
So you have to declare an interface the so called “CustomAPI” and in here you will have to declare what methods you want to use. What confused me was the things defined inside “interface CustomAPI { … }” in line 3-7 of the attached example. However in the case you only want to set the DataModel you’ll only need a method called something like “setChildDataModel( … );” or whatever.  
When you’ve done this, you will have to implement this interface to the child node like line 13 in the attached example, and then override the “setChildDataModel( … );” with the functionality you want like the other picture I’ve uploaded, which is my method.  
Now you can, with the help of the CustomAPI, access this method in the parent node with childAs.setChildDataModel(…); like it is done in the example from line 20. Here they are accessing the method childAs.setSetting( … );  
I hope this is clear if anyone needs it, and i apologize if my terminology is wrong as I’m not an experienced Java programmer.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/1X/a1db8779e6b16e457fb7baa14dac530b70ce8ea4.png)  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/1X/58e7800edcbf9f8e4231d61a5d1054d5b6f6f865.png)

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [October 23, 2018, 4:56pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/11 "2018-10-23T16:56:11Z")

</div>

@anon53303193,

Thanks a lot for sharing your gained knowledge, from working with this interface.

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [October 30, 2018, 3:11pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/12 "2018-10-30T15:11:51Z")

</div>

You may also want to check out this sample, demonstrating the custom API functionality:

> [@URCap Sample: URCap CustomAPI](https://forum.universal-robots.com/t/sample-urcap-customapi/3022):
>
> URCap CustomAPI Description of functionality: This example demonstrated the usage of the URCap CustomAPI interface. Allowing a parent node to configure the configuration of its child node. In this example, this is covered by allowing the parent node to define the color of the child node. What topics are covered: Proprietary API, Custom API, parent node, child node, interfaces [Find it on Github!](https://github.com/BomMadsen/URCap-CustomAPI) Requirements: URCaps API 1.3 (Swing)

---

<div class="post-metadata">

**Author:** ![anon43302037](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@anon43302037](https://forum.universal-robots.com/u/anon43302037)\
**Post date:** [February 14, 2019, 7:26pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/13 "2019-02-14T19:26:07Z")

</div>

And the DataModel is shared between 2 nodes which are of the same kind?? I’m having the trouble to save a value with model.set and model.get, 2 nodes of the same kind get the same value but I need every node save its own data, how can I achieve this?? thanks for the help

---

<div class="post-metadata">

**Author:** ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)\
**Post date:** [February 15, 2019, 8:46am UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/14 "2019-02-15T08:46:29Z")

</div>

Each individual node has its own DataModel.  
This is not shared with any other instance.

In the Program Node Service class, the DataModel provided in the `createNode()` method is a separate instance for each node inserted.  
The DataModel is empty, if this is a new instance of the node. If the node is copied or a program is loaded, this will give the DataModel containing previously configured values.

---

<div class="post-metadata">

**Author:** ![anon43302037](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@anon43302037](https://forum.universal-robots.com/u/anon43302037)\
**Post date:** [February 15, 2019, 3:00pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/15 "2019-02-15T15:00:40Z")

</div>

Yes, that what I been reading every where but I don’t know why mi node keep doing this…

![Capture](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/6/690edd231b1985f33f253420551116cba1713167.gif)

I hope this gif help me explain what it’s happening to me, I been checking the hello world example and the tutorial My First URCap, the hello world don’t make this but the URCap created in the tutorials do this too, I hope someone know the reason of this. Thanks for the reply

---

<div class="post-metadata">

**Author:** ![sam.verity-hilton](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/sam.verity-hilton/32/6046_2.png) [@sam.verity-hilton](https://forum.universal-robots.com/u/sam.verity-hilton)\
**Post date:** [February 15, 2019, 3:30pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/16 "2019-02-15T15:30:48Z")

</div>

Have you some code samples to help demonstrate this? what you should be doing to achieve the desired effect is:

1. make sure the code that defines the drop down box is in buildUI or a method called from buildUI.
2. Add an `ActionListener` to your drop down box.
3. in the `actionPerformed` method of that `ActionListener`, use something like `ContributionProvider.get().itemSelected(Object selectedItem)` to call the method `itemSelected` from your contribution class.
4. In your `itemSelected` method in your contribution class, use `model.set("selection", selectedItem)`
5. change your `getTitle` method to return `model.get("selection", defaultSelection)`.
6. Likewise, in `openView()` set the selected item of the drop down to be `model.get("selection", delfaultSelection)`, this will change the drop downs selected item to what it was when you were last in the node.

This way your the title for each node depends on the selected item that you chose when you each node was selected.  
Because there is only ever 1 instance of the view class, anything that depends solely on the state of different components in the view class can behave oddly (eg. the title of a node in the tree).

Hope this helps.

---

<div class="post-metadata">

**Author:** ![anon43302037](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@anon43302037](https://forum.universal-robots.com/u/anon43302037)\
**Post date:** [February 15, 2019, 3:46pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/17 "2019-02-15T15:46:10Z")

</div>

Yes I checked all the steps and everything is right, here a left the code I checked

> public void buildUI(JPanel panel, ContributionProvider contribution) {
> 
> ```
> panel.add(createTextBox("Select the stage of the process."));
> panel.add(createVerticalSpace(5));
> panel.add(createComboBox(contribution));
> 
> ```
> 
> }

My method createComboBox

> Preformatted textprivate Box createComboBox(final ContributionProvider contribution) {
> 
> ```
> Box box = Box.createHorizontalBox();
> box.setAlignmentX(Component.LEFT_ALIGNMENT);
> stages.setPreferredSize(new Dimension(150,25));
> stages.setPreferredSize(stages.getPreferredSize());
> stages.addItemListener(new ItemListener() {
> @Override
> public void itemStateChanged(ItemEvent e) {
> contribution.get().setStage((String)e.getItem());
> }
> });
> box.add(stages);
> return box;
> 
> ```
> 
> }

Method in contribution to save the selection

> public void setStage(final String stage) {
> 
> ```
> undoReduManager.recordChanges(new UndoableChanges() {
> @Override
> public void executeChanges() {
> model.set(nodeStage, stage);
> }
> });
> 
> ```
> 
> }

getTitle method

> public String getTitle() {
> 
> ```
> return model.get(nodeStage, "Not Stage Defined");
> 
> ```
> 
> }

openView method

> public void openView() {
> 
> ```
> view.setComboBoxData(installation.getStages());
> view.setComboBoxSelection(model.get(nodeStage, "notDefined"));
> 
> ```
> 
> }

Could it be a problem because of the installation node?? is the DataModel shared with the program nodes DataModel??

---

<div class="post-metadata">

**Author:** ![sam.verity-hilton](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/sam.verity-hilton/32/6046_2.png) [@sam.verity-hilton](https://forum.universal-robots.com/u/sam.verity-hilton)\
**Post date:** [February 15, 2019, 4:04pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/18 "2019-02-15T16:04:40Z")

</div>

Is that everythiing your `getTitle` method does, also, what does your `isDefined` method do, does that set anything? If so you should avoid setting thingsin this method, in fact doing so is considered good practice.

---

<div class="post-metadata">

**Author:** ![anon43302037](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@anon43302037](https://forum.universal-robots.com/u/anon43302037)\
**Post date:** [February 15, 2019, 4:16pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/19 "2019-02-15T16:16:37Z")

</div>

Yes thats all my getTitle method, and neither use set in isDefined method

> @Override  
> public boolean isDefined() {
> 
> ```
> if(model.isSet(nodeStage)) {
> return true;
> }
> return false;
> 
> ```
> 
> }

---

<div class="post-metadata">

**Author:** ![anon43302037](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@anon43302037](https://forum.universal-robots.com/u/anon43302037)\
**Post date:** [February 15, 2019, 10:26pm UTC](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773/20 "2019-02-15T22:26:55Z")

</div>

I found a workaround if someone have the same problem, I used addActionListener to the JComboBox instead of addItemListener. I don’t know why the addItemListener was causing this.

[Next page](https://forum.universal-robots.com/t/datamodel-is-not-shared-between-nodes/2773.md?page=2)
