Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » integer arrays
integer arrays [message #219829] Tue, 24 February 2009 09:59 Go to next message
Martin Baker is currently offline Martin BakerFriend
Messages: 31
Registered: July 2009
Member
Hi,

I need to use one and two dimensional integer arrays in my application and
I would appreciate it if anyone could advise me of the best way to do this.

What I am trying to to is define arrays to minimise any custom programming
that I have to do in order to edit these parameters and to serialise them
to XML.

I have done some experiments, explained below, but none of them give the
ideal result so I would appreciate ideas about how I could improve this.

My first attempt is to define an array like this:

/**
* @model containment="true"
*/
int[] getOneDArray();

This generates an entry in the model.ecore file:
IntArray [int[]]
I assume this entry means that I have to write the code for this type
myself.

When I generate the code it has the following error:
cannot cast from int[] to InternalEObject
this seems to be because I set containment="true" so I tried it without:

/**
* @model
*/
int[] getOneDArray();

This generates an entry in the model.ecore file:
<.> IntArray [int[]]

The code generated OK, but when I used the application it just treated
this like a single string rather than an integer array.
Since editing the parameter and serialising it to XML needs the values in
string form I thought it would be easier to just treat it as an array of
strings and then convert to array when I need to use it in my application.
So I tried this:

/**
* @model
*/
EList<String> getStringArray();

This works as I want, of course I will have to write code to validate user
input and also do the Integer.parseInt when I need to use. The thing that
concerns me more is that the UML diagram will not correctly show that this
is is an integer array. Also this seems to give a mismatch between the
model required for the application and the diagram.

An even more difficult question is how to work with two dimensional
arrays. This won't work:

/**
* @model
*/
int[][] getTwoDArray();

and this won't work either:

/**
* @model
*/
EList<EList<String>> getTwoDArray();

The only way that I can to do this is to intersperse a complete new class
called, say, 'row' then I can have a single dimensional array of rows, and
each row contains a a single dimensional array of elements. Bus again this
seems to give a mismatch between the model required for the application
and the diagram.

The other thing that I would like to do is show these arrays directly in
the diagram. Is it possible to put a jface TableViewer directly in the
rectangular box in the diagram?

Martin
Re: integer arrays [message #219840 is a reply to message #219829] Tue, 24 February 2009 10:12 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31781
Registered: July 2009
Senior Member
Martin,

Comments below.

Martin wrote:
> Hi,
>
> I need to use one and two dimensional integer arrays in my application
> and I would appreciate it if anyone could advise me of the best way to
> do this.
Sounds more like an EMF question than a GMF one.
>
> What I am trying to to is define arrays to minimise any custom
> programming that I have to do in order to edit these parameters and to
> serialise them to XML.
You've looked at how EByteArray is supported?
>
> I have done some experiments, explained below, but none of them give
> the ideal result so I would appreciate ideas about how I could improve
> this.
>
> My first attempt is to define an array like this:
>
> /**
> * @model containment="true"
> */
> int[] getOneDArray();
>
> This generates an entry in the model.ecore file:
> IntArray [int[]]
> I assume this entry means that I have to write the code for this type
> myself.
Yes, you'd have to implement the two methods in the XyzFactoryImpl for
doing createIntArrayFromString and convertIntArrayToString. A space
separated list ought to work nicely.
>
> When I generate the code it has the following error:
> cannot cast from int[] to InternalEObject
> this seems to be because I set containment="true" so I tried it without:
Yes, containment is only for EReferences and you really need an
EDataType since, as suggested, an int array cannot be an EObject.
>
> /**
> * @model
> */
> int[] getOneDArray();
>
> This generates an entry in the model.ecore file:
> <.> IntArray [int[]]
Yes an EDataType is what you need.
>
> The code generated OK, but when I used the application it just treated
> this like a single string rather than an integer array.
Yes, that's how it will be serialized.
> Since editing the parameter and serialising it to XML needs the values
> in string form I thought it would be easier to just treat it as an
> array of strings and then convert to array when I need to use it in my
> application. So I tried this:
>
> /**
> * @model
> */
> EList<String> getStringArray();
Why not just have a multi-valued Integer feature then?
>
> This works as I want, of course I will have to write code to validate
> user input and also do the Integer.parseInt when I need to use. The
> thing that concerns me more is that the UML diagram will not correctly
> show that this is is an integer array.
Because it's not. :-P
> Also this seems to give a mismatch between the model required for the
> application and the diagram.
>
> An even more difficult question is how to work with two dimensional
> arrays. This won't work:
>
> /**
> * @model
> */
> int[][] getTwoDArray();
Why won't it work? Of course no matter what you do, it will be
serialized and treated as a single value.
>
> and this won't work either:
>
> /**
> * @model
> */
> EList<EList<String>> getTwoDArray();
Here you'd need at least an EDataType to represent the inner
EList<String> and those would be treated as single values.
>
> The only way that I can to do this is to intersperse a complete new
> class called, say, 'row' then I can have a single dimensional array of
> rows, and each row contains a a single dimensional array of elements.
> Bus again this seems to give a mismatch between the model required for
> the application and the diagram.
> The other thing that I would like to do is show these arrays directly
> in the diagram. Is it possible to put a jface TableViewer directly in
> the rectangular box in the diagram?
It seems to me you should try to get EDataType for int[] and int[][]
working since that matches what you actually need in the Java API.
>
> Martin
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: integer arrays [message #219862 is a reply to message #219840] Tue, 24 February 2009 11:41 Go to previous message
Martin Baker is currently offline Martin BakerFriend
Messages: 31
Registered: July 2009
Member
> It seems to me you should try to get EDataType for int[] and int[][] working
since that matches what you actually need in the Java API.


Ed,

Thank you very much for your speedy and very helpful reply.

I apologise for asking questions at such a basic level but I am having
trouble finding documentation about how to use EDataType, I've tried
seaching the web but I cant find any sort of tutorial that covers this.
I'm planning to get the book "Eclipse Modeling Project: A Domain-Specific
Language Toolkit" when it comes out, but in the meantime I'm a bit stuck.

I can use this to generate the code.

/**
* @model
*/
EList<EDataType> getIntArray();

But then I'm not sure where to modify the code to do the String->int and
int->String conversions.
Can you recommend any online tutorials that would explain the structure of
the generated code at the right level to help me.

Thanks again,

Martin
Previous Topic:SlidableAnchor and custom polygon as NodeFigure
Next Topic:list of popupPredefinedItem id's - where?
Goto Forum:
  


Current Time: Mon Mar 08 07:43:58 GMT 2021

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

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

Back to the top