Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Generating sub-models from main model
Generating sub-models from main model [message #1183354] Tue, 12 November 2013 20:21 Go to next message
Alex Kravets is currently offline Alex KravetsFriend
Messages: 543
Registered: November 2009
Senior Member
Let's say there is a an EMF model defined by XSD that describes some entity - a system. I'd like to have parts of this system in EMF model. Right now I just define separate XSD taken from the main XSD and generate EMF models off that. However, this means I am breaking away from the main XSD and if it changes I need to update my models as well, and these models have different namespace than original model which means I have adapt my model to main one

I tried creating new EMF model from main XSD and it works for defining different types, but since they are all coming from the same main model I cannot find how can I define different file extensions for each sub-model and create a totally different model that is still based on what main model defines.

Consider this example:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <!-- definition of simple elements -->
   <xs:element name="orderperson" type="xs:string" />
   <xs:element name="name" type="xs:string" />
   <xs:element name="address" type="xs:string" />
   <xs:element name="city" type="xs:string" />
   <xs:element name="country" type="xs:string" />
   <xs:element name="title" type="xs:string" />
   <xs:element name="note" type="xs:string" />
   <xs:element name="quantity" type="xs:positiveInteger" />
   <xs:element name="price" type="xs:decimal" />
   <!-- definition of attributes -->
   <xs:attribute name="orderid" type="xs:string" />
   <!-- definition of complex elements -->
   <xs:element name="shipto">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="name" />
            <xs:element ref="address" />
            <xs:element ref="city" />
            <xs:element ref="country" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="item">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="title" />
            <xs:element ref="note" minOccurs="0" />
            <xs:element ref="quantity" />
            <xs:element ref="price" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
   <xs:element name="shiporder">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="orderperson" />
            <xs:element ref="shipto" />
            <xs:element ref="item" maxOccurs="unbounded" />
         </xs:sequence>
         <xs:attribute ref="orderid" use="required" />
      </xs:complexType>
   </xs:element>
</xs:schema>


If I generate EMF model from this schema, I can create Item or Shitorder model, but they are going to be part of my order model. I can't for example create sampleItem.item from the same model.

Is there a technique I am not aware of that will allow accomplish what I am trying to do?

Thanks,
Alex
Re: Generating sub-models from main model [message #1183370 is a reply to message #1183354] Tue, 12 November 2013 20:36 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30350
Registered: July 2009
Senior Member
Alex,

Comments below.

On 12/11/2013 9:21 PM, Alex Kravets wrote:
> Let's say there is a an EMF model defined by XSD that describes some
> entity - a system. I'd like to have parts of this system in EMF model.
> Right now I just define separate XSD taken from the main XSD and
> generate EMF models off that. However, this means I am breaking away
> from the main XSD and if it changes I need to update my models as
> well, and these models have different namespace than original model
> which means I have adapt my model to main one
>
> I tried creating new EMF model from main XSD and it works for defining
> different types, but since they are all coming from the same main
> model I cannot find how can I define different file extensions for
> each sub-model and create a totally different model that is still
> based on what main model defines.
>
> Consider this example:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
> <!-- definition of simple elements -->
> <xs:element name="orderperson" type="xs:string" />
> <xs:element name="name" type="xs:string" />
> <xs:element name="address" type="xs:string" />
> <xs:element name="city" type="xs:string" />
> <xs:element name="country" type="xs:string" />
> <xs:element name="title" type="xs:string" />
> <xs:element name="note" type="xs:string" />
> <xs:element name="quantity" type="xs:positiveInteger" />
> <xs:element name="price" type="xs:decimal" />
> <!-- definition of attributes -->
> <xs:attribute name="orderid" type="xs:string" />
> <!-- definition of complex elements -->
> <xs:element name="shipto">
> <xs:complexType>
> <xs:sequence>
> <xs:element ref="name" />
> <xs:element ref="address" />
> <xs:element ref="city" />
> <xs:element ref="country" />
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> <xs:element name="item">
> <xs:complexType>
> <xs:sequence>
> <xs:element ref="title" />
> <xs:element ref="note" minOccurs="0" />
> <xs:element ref="quantity" />
> <xs:element ref="price" />
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> <xs:element name="shiporder">
> <xs:complexType>
> <xs:sequence>
> <xs:element ref="orderperson" />
> <xs:element ref="shipto" />
> <xs:element ref="item" maxOccurs="unbounded" />
> </xs:sequence>
> <xs:attribute ref="orderid" use="required" />
> </xs:complexType>
> </xs:element>
> </xs:schema>
>
>
> If I generate EMF model from this schema, I can create Item or Shitorder
I hope I never receive one of those! :-P
> model, but they are going to be part of my order model. I can't for
> example create sampleItem.item from the same model.
Why not? Every global element in the schema can be the root element of
some XML instance.
> Is there a technique I am not aware of that will allow accomplish what
> I am trying to do?
File extensions are independent from what model instances you want to
put in them, so you can define resource factories for as many extensions
as you want (manually). You might even specialize the resources the
factories create to treat a root object it doesn't like as an error
while loading so a shiporder in a *.item's resource would be rejected
during loading...
>
> Thanks,
> Alex
Re: Generating sub-models from main model [message #1183413 is a reply to message #1183370] Tue, 12 November 2013 21:20 Go to previous messageGo to next message
Alex Kravets is currently offline Alex KravetsFriend
Messages: 543
Registered: November 2009
Senior Member
Thanks Ed,

I thought I proofread everything. Sorry!

I'll propotype with resource factories. Are there any code generation approaches in place to achieving this, looks like something like this is done often? I got very used to generating clean code with a push of a button Smile

[Updated on: Tue, 12 November 2013 21:46]

Report message to a moderator

Re: Generating sub-models from main model [message #1183933 is a reply to message #1183413] Wed, 13 November 2013 05:39 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30350
Registered: July 2009
Senior Member
Alex,

Comments below.

On 12/11/2013 10:20 PM, Alex Kravets wrote:
> Thanks Ed,
>
> I thought I proofread everything. Sorry!
No problem, it was funny...
>
> I'll propotype with resource factories. Are there any code generation
> approaches in place to achieving this, looks like something like this
> is done often?
I suppose with generation adapters you could achieve something like
that, but I don't imagine it's done often. You can see an example if
you do File -> New -> Examples... -> Eclipse Modeling Framework ->
Generator Extension Example...
> I got very used to generating clean code with a push of a Burton :)
Who's Burton? Does he do everything for you when you push him? :-P I
need me one of those Burtons!
Re: Generating sub-models from main model [message #1184497 is a reply to message #1183933] Wed, 13 November 2013 13:50 Go to previous messageGo to next message
Alex Kravets is currently offline Alex KravetsFriend
Messages: 543
Registered: November 2009
Senior Member
Ed Merks wrote on Wed, 13 November 2013 00:39
Alex,

Comments below.

On 12/11/2013 10:20 PM, Alex Kravets wrote:
> Thanks Ed,
>
> I thought I proofread everything. Sorry!
No problem, it was funny...
>
> I'll propotype with resource factories. Are there any code generation
> approaches in place to achieving this, looks like something like this
> is done often?
I suppose with generation adapters you could achieve something like
that, but I don't imagine it's done often. You can see an example if
you do File -> New -> Examples... -> Eclipse Modeling Framework ->
Generator Extension Example...
> I got very used to generating clean code with a push of a Burton Smile
Who's Burton? Does he do everything for you when you push him? Razz I
need me one of those Burtons!


Thanks Ed.

You got me again...my ninja edit skills were slow this time Smile
Re: Generating sub-models from main model [message #1203909 is a reply to message #1184497] Fri, 22 November 2013 22:28 Go to previous messageGo to next message
Alex Kravets is currently offline Alex KravetsFriend
Messages: 543
Registered: November 2009
Senior Member
I ended up doing the following, created ResourceFactory that extends base generated ResourceFactory and associated that with my file extension. This worked and I am able to create and open global element from my main model. However, even though DocumentRoot is not serialized I still see it when I set Resource in the TreeViewer. It looks like I am doing something wrong because of this thread. So basically I have:

 <extension point="org.eclipse.emf.ecore.extension_parser">
      <parser
            type="myExtension"
            class="/path/to/SpecializedResourceFactoryImpl"/>
   </extension>


But how else should I associate file extension with ResourceFactory? If I can't avoid DocumentRoot in my Resource then, as it's being suggested here I need specialized ResourceItemProvider to skip over DocumentRoot, but I am not sure how to tie this provider during model loading.

I also tried adding XMLResource.OPTION_SUPPRESS_DOCUMENT_ROOT set to true in ResourceFactoryImpl, and this did what it promised, but after I created my model and saved it with some elements added I saw that '_._type' was was added to my global element, which seems normal in light of DocumentRoot being supressed, but I don't really want this serialized.

Thanks,
Alex
Re: Generating sub-models from main model [message #1204776 is a reply to message #1203909] Sat, 23 November 2013 08:32 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30350
Registered: July 2009
Senior Member
Alex,

Comments below.

On 22/11/2013 11:28 PM, Alex Kravets wrote:
> I ended up doing the following, created ResourceFactory that extends
> base generated ResourceFactory and associated that with my file
> extension. This worked and I am able to create and open global element
> from my main model. However, even though DocumentRoot is not
> serialized I still see it when I set Resource in the TreeViewer. It
> looks like I am doing something wrong because of this
> http://http://www.eclipse.org/forums/index.php/mv/msg/125914/386310/#msg_386310.
> So basically I have:
>
>
> <extension point="org.eclipse.emf.ecore.extension_parser">
> <parser
> type="myExtension"
> class="/path/to/SpecializedResourceFactoryImpl"/>
> </extension>
>
>
> But how else should I associate file extension with ResourceFactory?
That's the only way.
> If I can't avoid DocumentRoot in my Resource then, as it's being
> suggested
> http://www.eclipse.org/forums/index.php/mv/msg/125914/391583/#msg_391583
> I need specialized ResourceItemProvider to skip over DocumentRoot, but
> I am not sure how to tie this provider during model loading.
You can always specialize methods like doLoad and doSave, but that
shouldn't be necessary.
>
> I also tried adding XMLResource.OPTION_SUPPRESS_DOCUMENT_ROOT set to
> true in ResourceFactoryImpl, and this did what it promised, but after
> I created my model and saved it with some elements added I saw that
> '_._type' was was added to my global element, which seems normal in
> light of DocumentRoot being supressed, but I don't really want this
> serialized.
Did you try it in combination with OPTION_ELEMENT_HANDLER? During
saving, it's important to be able to deduce a global element declaration
(a feature of the document root) that's appropriate for the type of root
object you're serializing so the appropriate root element is serialized
to the resource.
> Thanks,
> Alex
Re: Generating sub-models from main model [message #1209715 is a reply to message #1204776] Mon, 25 November 2013 17:11 Go to previous message
Alex Kravets is currently offline Alex KravetsFriend
Messages: 543
Registered: November 2009
Senior Member
Thanks Ed, OPTION_ELEMENT_HANDLER was the missing link.
Previous Topic:[Texo] How to add the @DiscriminatorColumn(length = xxx) on a generated Entity with Texo
Next Topic:prune &amp; graft on model.
Goto Forum:
  


Current Time: Sat Aug 17 21:22:22 GMT 2019

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

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

Back to the top