Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Papyrus for Real Time » higher dimensional arrays?
higher dimensional arrays? [message #1783835] Mon, 19 March 2018 01:46 Go to next message
Juergen Dingel is currently offline Juergen DingelFriend
Messages: 42
Registered: January 2015
Member
Hi,

what is the best way to define attributes with 2-dimensional, or 3-dimenensional array types?

Thanks

Juergen
Re: higher dimensional arrays? [message #1783836 is a reply to message #1783835] Mon, 19 March 2018 02:30 Go to previous messageGo to next message
Mojtaba Bagherzadeh is currently offline Mojtaba BagherzadehFriend
Messages: 36
Registered: April 2016
Member
Hi,

I think the best way is to apply the AttributeProperties stereotype and then set the type. Please see the below screenshot.

index.php/fa/32367/0/
Thanks
Mojtaba
Re: higher dimensional arrays? [message #1783848 is a reply to message #1783836] Mon, 19 March 2018 11:30 Go to previous messageGo to next message
Juergen Dingel is currently offline Juergen DingelFriend
Messages: 42
Registered: January 2015
Member
Thanks.

Doesn't quite work for me though. I get the following compile error:

error: expected unqualified-id before '[' token
int[2][10]
^

Juergen
Re: higher dimensional arrays? [message #1783856 is a reply to message #1783835] Mon, 19 March 2018 15:05 Go to previous messageGo to next message
Ernesto Posse is currently offline Ernesto PosseFriend
Messages: 438
Registered: March 2011
Senior Member
I'm afraid that in C++ the syntax is

type name[size1][size2]...[sizeN];


so

int[3][2] attr;


is a C++ syntax error.

Unfortunately using the AttributeProperties.type property won't be much help here, as the code generator simply puts whatever is in this 'type' property before the attribute name in the generated code.

As a workaround, you could use the 'privateDeclarations', 'protectedDeclarations' or 'publicDeclarations' properties of the CapsuleProperties stereotype, or the PassiveClassProperties stereotype, to add any C++ declaration.

However, it would probably be better to create a suitable abstraction for the multi-dimentional array: define a class called e.g. My2DArray, which provides suitable setters and getters (in C++ you can define them as operators so that they look primitive), and internally it can use the '*Declarations' properties mentioned above, or some alternative representation, like an array of lists for a sparse array.


Re: higher dimensional arrays? [message #1783931 is a reply to message #1783856] Tue, 20 March 2018 13:59 Go to previous message
Juergen Dingel is currently offline Juergen DingelFriend
Messages: 42
Registered: January 2015
Member
Ok, I think I get it: Suppose you want capsule 'C' to contain an attribute 'int myArray[5][5]':
1) select the capsule 'C' and the 'Properties' tab
2) ensure that stereotype 'CapsuleProperties' has been applied (if not, click the green '+', select 'CapsuleProperties' on left, click '->', and then 'Ok')
3) expand 'CapsuleProperties', select property 'privateDeclarations' (or, 'protectedDeclarations', or 'publicDeclarations'), and input 'int myArray[5][5];' in text field on right (see attached screen shot).

Now, 'myArray' should be visible and usable in the action code of capsule 'C', e.g., you should be able to write:
myArray[0][0] = 0;
loggerP.log("[Switcher] myArray[0][0] = %d", myArray[0][0]);

in the action code of any transition of the state machine of 'C'.

While this should work, the not-so-nice thing is that the 'myArray' attribute
is 'hidden', i.e., it is not directly listed as an attribute of capsule 'C' in the
Model Explorer, meaning that (1) one may forget or not realize it is there, and
that (2) it is cumbersome to look up, e.g., the precise name or dimensions, etc

To avoid this, wrap the array into a passive class, e.g., 'MyArray' that holds the
array and offers the required access methods.

To create this passive class, right-click 'C', then 'New UML-RT Child', then 'Class'.
Call that class 'MyArray', and apply the 'PassiveClassProperties' stereotype. Use
the 'privateDeclarations' property as before to create the array. Then, define
the desired operations for the class (right-click 'MyArray', select 'New UML-RT Child',
then 'Operation', and then use the 'Properties | UML' and the 'Code Snippet' tabs).

Thanks everybody!

Juergen


Previous Topic:defer/recall
Next Topic:Layout issue
Goto Forum:
  


Current Time: Thu Apr 25 15:32:37 GMT 2024

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

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

Back to the top