Home » Modeling » EMF » csv to EObject data converter for import wizard
csv to EObject data converter for import wizard [message #754948] |
Sat, 05 November 2011 17:55 |
Tom H Messages: 139 Registered: July 2009 |
Senior Member |
|
|
Hi,
I would like to add a csv import wizard to my project, this is going to involve specifying an EClass for the target EObject and then iterating each of the fields attempting to coerce the value into either an EAttribute or EReference based on the name of the field.
1) First question... Has any other project already done a generic implementation of this that I can reference?
if not, (which would be surprising) is;
2) I guess that I would be looking to implement a createFromString EFactory method, which does the following;
iterate each of the fields, and look up an EStructuralFeature based on the field name and
if (the feature is an attribute)
create one, or n and eSet them to the new eObject
if (the reference is a containment)
try and create n EObjects using recursion into createFromString
if(the reference is a foreign key to an existing eObject)
lookup the reference and assign it
if(the field or the feature is ambiguous, offer to the user the options for resolving the cases)
else (Ignore any more complex cases)
does that make sense?, and am I missing any obvious problems, For simple cases only. (typically all the relevant packages are registered and available to EcoreUtil and its not going to be reasonable to createFromString anything that doesn't have a simple string interpretation)
3) How to add the method to all the packages?
If I want to be able to arbitrary objects from strings using my strategy above, which presumably is only going to work in simple cases, I would have to add the method to ALL the generated packages.
Where should I apply such a change?
Thanks
[Updated on: Sat, 05 November 2011 17:59] Report message to a moderator
|
|
|
Re: csv to EObject data converter for import wizard [message #754969 is a reply to message #754948] |
Sun, 06 November 2011 06:21 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
Comments below.
On 05/11/2011 6:56 PM, Tom H wrote:
> Hi,
>
> I would like to add a csv import wizard to my project, this is going
> to involve specifying an EClass for the target EObject and then
> iterating each of the fields attempting to coerce the value into
> either an EAttribute or EReference based on the name of the field.
>
>
> 1) First question... Has any other project already done a generic
> implementation of this that I can reference?
Not that I know of.
>
>
> if not, (which would be surprising) is;
>
> 2) I guess that I would be looking to implement a createFromString
> EFactory method, which does the following;
>
> iterate each of the fields, and look up an EStructuralFeature based on
> the field name and
>
> if (the feature is an attribute)
>
> create one, or n and eSet them to the new eObject
Yep.
>
> if (the reference is a containment)
>
> try and create n EObjects using recursion into createFromString
Can CSV nest things?
>
> if(the reference is a foreign key to an existing eObject)
>
> lookup the reference and assign it
You'll have to defer processing if there are forward references.
>
> if(the field or the feature is ambiguous, offer to the user the
> options for resolving the cases)
>
> else (Ignore any more complex cases)
>
>
>
>
> 3) How to add the method to all the packages?
What method. I don't get the sense you need anything generated for your
models. All the other serialization/deserialization methods use
reflection and are external to the model.
>
> If I want to be able to arbitrary objects from strings using my
> strategy above, which presumably is only going to work in simple
> cases, I would have to add the method to ALL the generated packages.
As I said, I don't see why you'd need to generate anything. XML didn't
require that so why would CSV?
>
> Where should I apply such a change?
>
> Thanks
>
>
>
>
>
>
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: csv to EObject data converter for import wizard [message #755015 is a reply to message #754969] |
Sun, 06 November 2011 20:59 |
Tom H Messages: 139 Registered: July 2009 |
Senior Member |
|
|
Ed Merks wrote on Sun, 06 November 2011 01:21Comments below.
> if (the reference is a containment)
>
> try and create n EObjects using recursion into createFromString
Can CSV nest things?
presumably with the right combination of quotes and escaping you could produce some monstrous Turducken of XML nested in json nested in csv.
But what I was thinking of is something like a list of keys in quotes to represent a set of foreign references. ie "1,2,3,67,34,"
But its not something I care about up front, but if it was a trivial case to handle then I might see if it comes up.
Ed Merks wrote on Sun, 06 November 2011 01:21
> 3) How to add the method to all the packages?
What method. I don't get the sense you need anything generated for your
models. All the other serialization/deserialization methods use
reflection and are external to the model.
>
> If I want to be able to arbitrary objects from strings using my
> strategy above, which presumably is only going to work in simple
> cases, I would have to add the method to ALL the generated packages.
As I said, I don't see why you'd need to generate anything. XML didn't
require that so why would CSV?
>
>
I guess that I am thinking that I would like to to attach custom logic to a modelled object in the definition stage, for example to convert "£100.43" to an general Currency type modelled object by providing logic to parse and split the string.
rather than xml to ecore
<money><code>GBP</code><amount>10043</amount><fraction>100</fraction></money> to some sort of csv2ecore where all the fields are mapped, I want to provide a function to split out the fields from some string.
Obviously I can create an interface "AsString" with some methods createFromString and convertToString, but that would add them to the body of the modelled object and not the EFactory which I where I want to be able to call the method from.
maybe I need to add an adapter to the object, which I can check is available to provide the parseFromString function for each modelled object?
Thanks
[Updated on: Sun, 06 November 2011 21:02] Report message to a moderator
|
|
|
Re: csv to EObject data converter for import wizard [message #755036 is a reply to message #755015] |
Mon, 07 November 2011 06:40 |
Ed Merks Messages: 33141 Registered: July 2009 |
Senior Member |
|
|
Tom,
Comments below.
On 06/11/2011 9:59 PM, Tom H wrote:
> Ed Merks wrote on Sun, 06 November 2011 01:21
>> Comments below.
>>
>> > if (the reference is a containment)
>> >
>> > try and create n EObjects using recursion into createFromString
>> Can CSV nest things?
>
>
> presumably with the right combination of quotes and escaping you could
> produce some monstrous http://en.wikipedia.org/wiki/Turducken of XML
> nested in json nested in csv.
>
> But what I was thinking of is something like a list of keys in quotes
> to represent a set of foreign references. ie "1,2,3,67,34,"
>
> But its not something I care about up front, but if it was a trivial
> case to handle then I might see if it comes up.
>
> Ed Merks wrote on Sun, 06 November 2011 01:21
>> > 3) How to add the method to all the packages?
>> What method. I don't get the sense you need anything generated for
>> your models. All the other serialization/deserialization methods use
>> reflection and are external to the model.
>> >
>> > If I want to be able to arbitrary objects from strings using my >
>> strategy above, which presumably is only going to work in simple >
>> cases, I would have to add the method to ALL the generated packages.
>> As I said, I don't see why you'd need to generate anything. XML
>> didn't require that so why would CSV?
>> >
>> >
>
>
>
> I guess that I am thinking that I would like to to attach custom logic
> to a modelled object in the definition stage, for example to convert
> "£100.43" to an general Currency type modelled object by providing
> logic to parse and split the string.
This looks more like something you'd use an EDataType for...
>
>
> rather than xml to ecore
> <money><code>GBP</code><amount>10043</amount><fraction>100</fraction></money>
> to some sort of csv2ecore where all the fields are mapped, I want to
> provide a function to split out the fields from some string.
>
> Obviously I can create an interface "AsString" with some methods
> createFromString and convertToString, but that would add them to the
> body of the modelled object and not the EFactory which I where I want
> to be able to call the method from.
>
>
>
>
>
>
>
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Goto Forum:
Current Time: Thu Apr 25 17:53:44 GMT 2024
Powered by FUDForum. Page generated in 0.04186 seconds
|