Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » How to increment an id feature automatically
How to increment an id feature automatically [message #1786549] Mon, 07 May 2018 07:29 Go to next message
Adalberto Jr is currently offline Adalberto JrFriend
Messages: 36
Registered: December 2017
Member
Hi folks,

I would like to know if it is possible to set an id feature of an object with 1 when the user creates it the first time. Then, other objects of the same type would have that id automatically incremented.

How to do this?

Regards

Adalberto Jr
Re: How to increment an id feature automatically [message #1786551 is a reply to message #1786549] Mon, 07 May 2018 08:00 Go to previous messageGo to next message
Pierre Guilet is currently offline Pierre GuiletFriend
Messages: 250
Registered: June 2017
Senior Member
Hello,

In your set operation you can use an AQL expression that will count the number of element of the same kind that the one created. It will be something like in the basic family example aql:container.members->filter(basicfamily::Man)->size()

Regards


Pierre Guilet - Obeo
Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: How to increment an id feature automatically [message #1786556 is a reply to message #1786551] Mon, 07 May 2018 08:26 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You need to answer questions such as:

does a derived class share/override the counter of the base class?

are stale ids re-used after deletion? i.e. if {1,2,3} are created, then 2 is deleted leaving {1.3} , what is the next id?
- 2 (fill the gap) => {1,2,3}
- 3 (count the elements, as in the AQL snippet) - oops => {1,3,3}
- 4 (next all-time) => {1,3,4}

are missing ids re-used after reloading? if I save a model one day and continue the next, loading {56,57,58}, is the next id 1 or 59?

does reloading reuse old IDs or start from 1?

are IDs reassigned by copy and paste?

You can elaborate the AQL to avoid re-use of ids, but what you require is a class level operation for which some variant of allInstances() and/or search is necessary; clumsy and inefficient; an interesting optimization challenge for tool vendors. If you really want to do this, I would recommend a Java/EMF solution in which getID() lazily allocates the ID from a 'static' EClass Map/Set appropriately scoped by derived EClasses. setID() will be invoked automatically by the loader if you choose to persist the ID. The transient and volatile attributes should support your design choice.

Regards

Ed Willink
Re: How to increment an id feature automatically [message #1786558 is a reply to message #1786556] Mon, 07 May 2018 08:54 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

"an interesting optimization challenge for tool vendors" ... https://bugs.eclipse.org/bugs/show_bug.cgi?id=534421 raised

The solution would be fairly easy if Ecore had 'static' modeling support like UML and OCL: https://bugs.eclipse.org/bugs/show_bug.cgi?id=285771

OCL may be able to workaround it, not a high priority, but thanks for an interesting motivating use case.

Regards

Ed Willink
Re: How to increment an id feature automatically [message #1786559 is a reply to message #1786551] Mon, 07 May 2018 08:55 Go to previous messageGo to next message
Adalberto Jr is currently offline Adalberto JrFriend
Messages: 36
Registered: December 2017
Member
Great.

Works fine.

Tks
Re: How to increment an id feature automatically [message #1786560 is a reply to message #1786559] Mon, 07 May 2018 08:59 Go to previous messageGo to next message
Adalberto Jr is currently offline Adalberto JrFriend
Messages: 36
Registered: December 2017
Member
Ed, I'll analyze your suggestions.

In fact, you are right.

Tks
Re: How to increment an id feature automatically [message #1786614 is a reply to message #1786560] Tue, 08 May 2018 13:07 Go to previous messageGo to next message
Adalberto Jr is currently offline Adalberto JrFriend
Messages: 36
Registered: December 2017
Member
Ed,

I was thinking about you said and I guess that it'd be enough to get the last ID used + 1.

It's not necessary to me fill gaps or reuse IDs.

So, as I'm new in Sirius, how can I do this? Calling Java services, functions?

Regards.
Re: How to increment an id feature automatically [message #1786618 is a reply to message #1786614] Tue, 08 May 2018 13:49 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

In AQL/OCL it's rather hard. In Java/EMF it's pretty easy; just define a static counter variable in the EClass Impl and bump it whenever getID() needs to create/allocate an ID. But make sure the attribute is derived, transient, !volatile, to ensure that loading/copying creates new IDs. Define distinct static counters in the EClass for each distinct ID-space, or just a single one in your 'ElementWithID' class from which every ID class derives.

private static int idCounter = 0;

public String My::getID() {
if (id == null) {
id = "My" + ++idCounter;
}
return id;
}

Regards

Ed Willink
Re: How to increment an id feature automatically [message #1786765 is a reply to message #1786618] Thu, 10 May 2018 20:13 Go to previous messageGo to next message
Adalberto Jr is currently offline Adalberto JrFriend
Messages: 36
Registered: December 2017
Member
Ed,

I created a method getID in the class Services.java but it's not available when I press ctrl + space (set operation - value expression).

Here's my class:


public class Services {

private static int idMU = 0;

/**
* See http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.sirius.doc%2Fdoc%2Findex.html&cp=24 for documentation on how to write service methods.
*/
public EObject myService(EObject self, String arg) {
// TODO Auto-generated code
return self;
}

public Integer getID() {
//if (idMU == null) {
//idMU = ++idMU;
//}
return ++idMU;
}

}

[Updated on: Thu, 10 May 2018 20:14]

Report message to a moderator

Re: How to increment an id feature automatically [message #1786766 is a reply to message #1786765] Thu, 10 May 2018 20:36 Go to previous messageGo to next message
Adalberto Jr is currently offline Adalberto JrFriend
Messages: 36
Registered: December 2017
Member
Sorry,

I fixed the problem and the method ID appeared.

My last question is: if I save the model and quit the diagram, the var ID will return to 0. And this is an error.

So, it's necessary to make a for loop in each EObject and get the max ID saved.

Could you, please, give an idea how to do this?

Is there any AQL function the returns a max value of a feature?

Regards.
Re: How to increment an id feature automatically [message #1786768 is a reply to message #1786766] Thu, 10 May 2018 21:17 Go to previous messageGo to next message
Adalberto Jr is currently offline Adalberto JrFriend
Messages: 36
Registered: December 2017
Member
I made a service but when I reopen the Diagram, the value of ID returns to 0.

Any idea?


private static int idMU = 0;

/**
* See
* http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.sirius.doc%2Fdoc%2Findex.html&cp=24
* for documentation on how to write service methods.
*/
public EObject myService(EObject self, String arg) {
// TODO Auto-generated code
return self;
}

public Integer getID(MU mu) {
ArrayList<MU> list = new ArrayList<MU>();
for (MU mu2 : list) {
if (idMU < mu2.getId()) idMU = mu2.getId();
}
return ++idMU;
}
Re: How to increment an id feature automatically [message #1786776 is a reply to message #1786768] Fri, 11 May 2018 07:58 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
hi

I wrote

"But make sure the attribute is derived, transient, !volatile, to ensure that loading/copying creates new IDs."

Regards

Ed Willink
Re: How to increment an id feature automatically [message #1786797 is a reply to message #1786776] Fri, 11 May 2018 21:07 Go to previous messageGo to next message
Adalberto Jr is currently offline Adalberto JrFriend
Messages: 36
Registered: December 2017
Member
Ed,

when I set up the ID feature as you suggested, I noticed the ID's returned to 0, for all objects.

I noticed too that IDs weren't available anymore to user edition in the properties window.

Look how I did.
  • Attachment: 1.png
    (Size: 40.29KB, Downloaded 111 times)
Re: How to increment an id feature automatically [message #1786800 is a reply to message #1786797] Sat, 12 May 2018 05:12 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Your original question was:

"I would like to know if it is possible to set an id feature of an object with 1 when the user creates it the first time. Then, other objects of the same type would have that id automatically incremented."

Of course it should not be edited since edits will not be saved; IDs are created lazily after loading. If you now want to allow editing you need to have a very clear plan for resolving clashes between new/copied/loaded/manually-re-used IDs.

I suggest you go back to the problem and work out what problem you are trying to solve. Then you may be able to formulate the requirements for your solution.

Regards

Ed Willink
Re: How to increment an id feature automatically [message #1786811 is a reply to message #1786800] Sat, 12 May 2018 19:09 Go to previous messageGo to next message
Adalberto Jr is currently offline Adalberto JrFriend
Messages: 36
Registered: December 2017
Member
Ok, Ed.

Please forgive me If my questions became a little bit confused. My goal has changed a bit because I'm evolving in using the tool.

Let me try to explain:

I did a Java service that returns the max ID used (below).

private static int idMU = 0;

/**
* See
* http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.sirius.doc%2Fdoc%2Findex.html&cp=24
* for documentation on how to write service methods.
*/
public EObject myService(EObject self, String arg) {
// TODO Auto-generated code
return self;
}

public Integer getID(MU mu) {
ArrayList<MU> list = new ArrayList<MU>();
for (MU mu2 : list) {
if (idMU < mu2.getId()) idMU = mu2.getId();
}
return ++idMU;
}


It's good for my goals, but there's a problem (for this moment): If I save and quit the diagram with 3 nodes (max ID = 3) and open it again later, the service doesn't return the max ID = 3 anymore. If I create a node, the ID is set to 1, instead of 4.

In other words, if the user quit the diagram to continues to create nodes later, the IDs will repeat.
Re: How to increment an id feature automatically [message #1786812 is a reply to message #1786811] Sat, 12 May 2018 19:41 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Yes. Very confused. and you don't make it any clearer. Obviously the solution for the old goal won't suit a changed goal. I have no idea what your goal is now. I suggest you review my earlier answer.

Do you want an auto-allocated ID that is guaranteed to exist and be unique, or do you want a persisted name with an initial hint that the user should improve, or .... ?

Until you know what properties you want ID to exhibit, it is very difficult to implement it.

(The IDs in your current solution do not repeat; they are ALL REALLOCATED starting at 0).

Regards

Ed Willink
Previous Topic:Relation Based Edge with Sirius
Next Topic:Subnode of Container
Goto Forum:
  


Current Time: Sat Apr 20 03:03:06 GMT 2024

Powered by FUDForum. Page generated in 0.04564 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top