Use EMF out of eclipse Environment

Source: Internet
Author: User

Once you are familiar with EMF, I am always used to using tools to construct data models for new projects, and then let EMF generate Java for me.Code. When the model needs to be modified, you also need to use tools to modify the model itself, and then let EMF update the changes to the Java code to ensure the synchronization of the model and code. In a recent Struts-based Web project, I tried to use EMF out of the eclipse runtime environment and found that it is easier than imagined. The following is a summary of some experiences.

1. EMF can be used out of the eclipse environment, so no matter whether your project is a pure SWT ApplicationProgram, Swing applications, web-based applications, or even applications without GUI, these non-Eclipse plug-in projects can also use the model code generated by EMF. However, the. Edit and. Editor Sections generated by EMF are difficult to use, because. Edit mainly provides support for various jface viewers, and. Editor is an eclipse-based editor.

2. to use the code generated by using EMF independently from eclipse, You need to include the EMF Runtime Library in the project. You can find the download item named standalone on the EMF download page. jar files are included in the Project classpath.

3. In addition to adding these library files, a small amount of initialization work needs to be executed when your application starts. The initialization method is specifically introduced on the EMF-FAQ page, basically to do is to execute the following two commands at the startup of the program:

 
Xxxpackage =Xxxpackage. einstance; resource. Factory. Registry. instance. getextensiontofactorymap (). Put ("XXX ",NewXmiresourcefactoryimpl ());

Update (): If the model file contains namespace declarations other than EMF (for example, to open. emx files), you need to register them. The following code registers the name space of gmf1.0:

 
(Delegator) epackage. Registry. instance). Put ("http://www.eclipse.org/gmf/runtime/1.0.0/notation", notationpackage. einstance );

4. The steps for generating model code with EMF are basically the same as those at the same time. It is still ". ecore->. genmodel-> JAVA code ". Generally, you can continue development based on the Java code generated by EMF in this EMF project. If you use other ides, you can copy and use the Java code, only pay attention to the process of generating and copying code after each ecore modification.

5. Processing of multiple models and cross-model references. One advantage of EMF is that it can save the model of the entire project into multiple files and allow mutual dependencies between files. For example, in an online store system, the model can be divided into three parts: user information, product information, and order information. Obviously, order information depends on user and product information, therefore, the management of users and commodities can be developed independently. On the one hand, it increases the degree of modularization and the possibility of reuse, for example, in another project, you can only make small changes or even reuse user management or product management without modification.

EMF uses resourceset to process references between multiple models. For example, when an online store loads an order instance from a file (usually in XMI format, EMF does not actually read files that store user or product information. Instead, it uses proxy to generate a substitute, when the program needs to obtain the detailed information of the user corresponding to the order (for example, the execution of theorder. getcustomer (). the getaddress () method will actually read the user information file and find the corresponding user information according to the order user ID.

Therefore, if your project has multiple models that are referenced by each other, we recommend that you maintain a global resourceset instance, which can be created at program startup:

Resourceset =NewResourcesetimpl ();

For common applications, you can use Singleton to maintain the resourceset instance. If you are in a web application, you can also use application to maintain it. In a program, each model loaded must be added to the resourceset instance. The specific method is as follows:

 //  Get global resourceset instance Resourceset = ;  //  File to load String filename =; //  E.g. "C: \ work \ test. XXX" Uri fileuri = Uri. createfileuri (filename );  //  Create Resource Xmiresource resource = (Xmiresource) resourceset. createresource (fileuri );  //  Load from File Resource. Load ( Null  );  //  Add Resource to global resourceset instance  Resourceset. getresources (). Add (Resource );  //  Get your model Xxxmodel = (xxxmodel) resource. getcontents (). Get (0 );

6. For other common methods, the following uses the model in the online store (see this post) as an example:

Create a new product by using the createproduct () method of shopfacoprocessor instead of using new productimpl ():

Product P =Shopfactory. einstance. createproduct ();//OrProduct P = (product) shopfactory. einstance. Create (shoppackage. literals. Product );

Add the newly created product to the shop ):

 
Shop model =...; Product P=...; Model. getproducts (). Add (P );

The EMF reflection mechanism is used to obtain all attributes of the product type (non-reference type). All attributes of an eobject can be obtained in this way:

Product P =...; Eclass=P. eclass ();For(Iterator =Eclass. geteallattributes (). iterator (); iterator. hasnext ();) {eattribute ATTR= (Eattribute) iterator. Next ();//Properties, such as product # NameObject value = P. eget (ATTR );//Attribute value, such as "administrative man"...//Do whatever with the value}

All references of the category type are obtained through the EMF reflection mechanism. Any eobject can obtain all references of the category type in this way:

 category Category =  ...; Eclass  =  category. eclass ();   for  (iterator =  eclass. geteallreferences (). iterator (); iterator. hasnext ();) {ereference ref  = (ereference) iterator. next (); ///   reference, for example, category # products  object value = cat Egory. eget (REF); ///   reference value, for example, elist 
   
     (one-to-multiple scenario) 
   ...  //   do whatever with the value }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.