- 浏览: 2474270 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (574)
- Book (62)
- Architecture (6)
- Java (39)
- Taobao (41)
- Distributed (4)
- Life (72)
- Database (7)
- Spring (16)
- Photography (15)
- Bicycle (41)
- Test (20)
- jBPM (8)
- Business (12)
- Movie (3)
- Ajax (15)
- Code (7)
- Eclipse (96)
- VIM (2)
- Music (6)
- Groovy (10)
- AutoHotKey (3)
- Dorado (10)
- Maven (7)
- Scrum (5)
- English (20)
- Financial (12)
- OSGi (3)
- Other (4)
- Tool (6)
- Browser (1)
- PPT (1)
- Project Management (4)
- Agile (6)
- Nosql (1)
- Search engine (6)
- Shell (2)
- Open Source (4)
- Storm (10)
- Guava (3)
- Baby (1)
- netty (1)
- Algorithm (1)
- Linux (1)
- Python (2)
最新评论
-
roy2011a:
https://github.com/ebottabi/sto ...
storm的序列化问题及与spring的结合方式 -
roy2011a:
能抗能打 写道哥们儿,你好!能共享下那个storm与sprin ...
storm的序列化问题及与spring的结合方式 -
Alick1:
兄弟,你之前是不是在深圳的正阳公司呆过啊?
storm的ack和fail -
liuleixwd:
先点个赞,写的非常好!有个问题请教下,如果我再bolt里不用e ...
storm的ack和fail -
yao-dd:
solr的facet查询
FROM:http://www.devx.com/Java/Article/29093/0/page/1
he Eclipse Modeling Framework (EMF) is a Java open source framework and code-generation facility for building tools and other applications based on a structured model. While the Eclipse Platform provides a powerful integration framework at the UI and file level, EMF enhances this capability to enable fine-grained data sharing among tools and applications.
Similar to other Java binding frameworks, e.g. JAXB or XMLBeans, given a model, EMF can generate Java source code that will allow you to create, query, update, deserialize, and serialize instances of your models. While the majority of Java binding frameworks support just one class of models, for example XML Schema, EMF supports generating code from XML Schema, UML class diagrams (Rational Rose or UML2), and annotated Java interfaces. In addition to the model code, EMF can also generate a complete application that includes a customizable editor.
The EMF-generated code has a built-in change notification mechanism and supports cross-document references. EMF provides a reflective API to access instances of your models and allows you to dynamically create models. EMF supports validation of model constraints. EMF provides powerful code generation tools that support regeneration of models and merging with user written code.
In this article, we'll explain just what the EMF is, take a look at the basic architecture.
EMF started out as an implementation of the Object Management Group's (OMG) Meta Object Facility (MOF) specification, which standardizes a metamodel for object oriented analysis and design. Over time, EMF was used to implement a large set of tools and thus evolved into an efficient Java implementation of a core subset of the MOF API.
The MOF-like core metamodel (model of a model) in EMF is called Ecore. In the current proposal for MOF 2.0, there is a similar subset of the MOF model, called Essential MOF (EMOF), which is separated out. There are small, mostly naming differences between Ecore and EMOF and therefore EMF can transparently read and write serializations of EMOF, allowing standard interchange of data between tools.
Today EMF's use is widespread. For example, EMF is used to implement the open source XML Schema Infoset Model (XSD), Service Data Objects (SDO), UML2, and Web Tools Platform (WTP) projects at Eclipse. In addition EMF is used in commercial products, such as Omondo EclipseUML and IBM Rational and WebSphere products.
Ecore and the Reflective API
One of the key interfaces in EMF is EObject, which is conceptually equivalent to java.lang.Object. All modeled objects, generated or not, implement this interface in order to provide several important features:
- Similarly to Java's Object.getClass(), using the eClass() method, you can retrieve the metadata of the instance, i.e., its EClass.
- On any EMF modeled object you can use the reflective API (eGet(), eSet()) to access its data. This is conceptually equivalent to Java's java.lang.reflect.Method.invoke() method, though much more efficient.
- From any instance object you can get its container (parent) using the eContainer() method.
- EObject also extends Notifier, which allows you to monitor all changes to the object's data.
As mentioned previously, EMF has its own simple metadata called Ecore. Figure 1 shows the complete class hierarchy of the Ecore metadata. In Figure 1, you can see that EPackage contains information about model classes (EClass) and data types (EDataType). EClass represents a modeled class, and specifies the attributes and references representing the data of instances. EAttribute represents simple data, specified by an EDataType. EReference represents an association between classes; its type is an EClass. EFactory contains methods to create model elements.
To find out more about EMF and Ecore please read the online overview or purchase the Eclipse Modeling Framework (EMF). The EMF Web site has several documents describing how to generate Java code from an XML Schema or UML diagram using EMF.
The next section describes an example which uses Ecore to create a simple model for companies, and then uses dynamic EMF to create, serialize and deserialize instances of this model. If you wish to follow along and you are already an Eclipse user, download and install the EMF 2.1 SDK or any newer version available on the EMF download site. Otherwise, you can download the standalone package, which includes EMF jars that do not have any dependencies on Eclipse and can be used in a standalone application.
Using Dynamic EMF Capabilities
In general, if you have models at development time, it is typically best to generate Java code because in this case your application will use less memory and provide faster access to data (using either the generated or the reflective API). While generating Java code serves a need for most applications, this might not always be the case. You might need to process data without requiring the availability of generated implementation classes. For example, you might not know at development time the model of the data you will be processing, making generated Java code a poor option
Dynamic, i.e., non-generated, classes can be created at runtime in several ways. Let's start by creating a company model programmatically using the Ecore API. The company model describes a company that has a name and departments. Each department is identified by a number and has employees. Each employee has a name. The code below shows an Ecore metamodel that corresponds to this model.
java 代码
- EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;
- EcorePackage ecorePackage = EcorePackage.eINSTANCE;
-
- // create an Company class
- EClass companyClass = ecoreFactory.createEClass();
- companyClass.setName("Company");
-
- // create company name
- EAttribute companyName = ecoreFactory.createEAttribute();
- companyName.setName("name");
- companyName.setEType(ecorePackage.getEString());
- companyClass.getEStructuralFeatures().add(companyName);
-
- //create an Employee class
- EClass employeeClass = ecoreFactory.createEClass();
- employeeClass.setName("Employee");
-
- //add a name attribute to an Employee class
- EAttribute employeeName = ecoreFactory.createEAttribute();
- employeeName.setName("name");
- employeeName.setEType(ecorePackage.getEString());
- employeeClass.getEStructuralFeatures().add(employeeName);
-
- //create a Department class
- EClass departmentClass = ecoreFactory.createEClass();
- departmentClass.setName("Department");
-
- //add department identification number
- EAttribute departmentNumber = ecoreFactory.createEAttribute();
- departmentNumber.setName("number");
- departmentNumber.setEType(ecorePackage.getEInt());
- departmentClass.getEStructuralFeatures().add(departmentNumber);
-
- //department class can contain reference to one or many employees
- EReference departmentEmployees = ecoreFactory.createEReference();
- departmentEmployees.setName("employees");
- departmentEmployees.setEType(employeeClass);
-
- // specify that it could be one or more employees
- departmentEmployees.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
- departmentEmployees.setContainment(true);
- departmentClass.getEStructuralFeatures().add(departmentEmployees);
-
- // company can contain reference to one or more departments
- EReference companyDepartments = ecoreFactory.createEReference();
- companyDepartments.setName("department");
- companyDepartments.setEType(departmentClass);
- companyDepartments.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
- companyDepartments.setContainment(true);
- companyClass.getEStructuralFeatures().add(companyDepartments);
-
- //create a package that represents company
- EPackage companyPackage = ecoreFactory.createEPackage();
- companyPackage.setName("company");
- companyPackage.setNsPrefix("company");
- companyPackage.setNsURI("http:///com.example.company.ecore");
- companyPackage.getEClassifiers().add(employeeClass);
- companyPackage.getEClassifiers().add(departmentClass);
- companyPackage.getEClassifiers().add(companyClass);
Using the reflective API you can create and initialize an instance of your model:
java 代码
- // get company factory
- EFactory companyFactory = companyPackage.getEFactoryInstance();
-
- // using the factory create instance of company class and
- // set company name
- EObject company = companyFactory.create(companyClass);
- company.eSet(companyName, "MyCompany");
-
- // create an instance of employee class
- EObject employee = companyFactory.create(employeeClass);
- //using reflective API initialize name of employee
- employee.eSet(employeeName, "John");
-
- // create an instance of department class
- EObject department = companyFactory.create(departmentClass);
- department.eSet(departmentNumber, new Integer(123));
- //add "John" to department
- ((List)department.eGet(departmentEmployees)).add(employee);
- // add the department to the company
- ((List)company.eGet(companyDepartments)).add(department);
Serializing and Deserializing Data
To serialize your model instances, you need to put a root object of your instance model into a resource. The EMF org.eclipse.emf.ecore.resource.Resource interface represents a physical storage location (such as file or URL) and provides methods to serialize and load data. Each resource is stored in a ResourceSet, which represents a collection of resources that have been created and loaded together, allowing for references among them. In particular, a ResourceSet keeps track of which resources have been loaded and makes sure that no resource in a ResourceSet is loaded twice.
Because EMF is capable of dealing with multiple model sources, e.g., XML Schema, it is also important to specify which resource implementation should be used for (de)serializing your data. Normally, when you invoke the ResourceSet.createResource(URI) method, it queries the Resource.Factory.Registry to look up a factory that is registered for that URI and uses it to create an appropriate resource implementation. Therefore, before you (de)serialize your data ensure that you register the appropriate resource factory implementation. EMF provides several Resource.Factory implementations:
- For XML data use org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl.
- For XMI data use org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl.
- For Ecore models use org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl.
With these EMF resources in your toolbox, you can use this code to serialize your data:
java 代码
- // create resource set and resource
- ResourceSet resourceSet = new ResourceSetImpl();
-
- // Register XML resource factory
- resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi",
- new XMIResourceFactoryImpl());
-
- Resource resource = resourceSet.createResource(URI.createFileURI("c:/temp/company.xmi"));
- // add the root object to the resource
- resource.getContents().add(company);
- // serialize resource – you can specify also serialization
- // options which defined on org.eclipse.emf.ecore.xmi.XMIResource
- resource.save(null);
The serialized form of the company.xmi is:
xml 代码
- <?xml version="1.0" encoding="ASCII"?>
- <company:Company xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
- xmlns:company="http:///com.example.company.ecore" name="MyCompany">
- <department number="123">
- <employees name="John"/>
- </department>
- </company:Company>
During deserialization, the namespace URIs of XML data are used to locate the required Ecore packages (which describe the model for your instance documents). Therefore, before you attempt to load any model, ensure that you register the namespace URI for each Ecore package your documents will be using:
java 代码
- // register package in local resource registry
- resourceSet.getPackageRegistry().put(companyPackage.getNsURI(), companyPackage);
- // load resource
- resource.load(null);
It is also important to notice the difference between local and global package (EPackage.Registry.INSTANCE) and resource factory (Resource.Factory.Registry.INSTANCE) registries. The global registry is static and therefore any application during lifetime of JVM can access the global registry and possibly overwrite it. To ensure that your registrations do not overwrite global registrations and vice versa, it is typically better to use local resource set registry.
Generating Dynamic Ecore from XML Schema
As mentioned previously, if your model is an XML Schema but you choose not to generate Java classes, you can dynamically create an Ecore model using the XSDEcoreBuilder. This example uses ipo.xsd:
java 代码
- XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
- ResourceSet resourceSet = new ResourceSetImpl();
- Collection eCorePackages =
- xsdEcoreBuilder.generate(URI.createFileURI("c:/temp/ipo.xsd"));
-
The generate method returns Ecore packages that are created for each URI in this schema. If the schema imports other namespaces, more than one Ecore package is returned. Each package is registered locally in the resource set used to convert schema. Therefore, if you use the same resource set to load your instance XML document, you don't need to register the packages yourself.
Because XML Schema includes more concepts than Ecore, for example wildcards, EMF uses Ecore EAnnotations to record the mapping to XML Schema. During (de) serialization of the data EMF needs to process these annotations. To ensure these annotations are respected during (de) serialization, you must use the XMLResource.ExtendedMetaData option:
java 代码
- HashMap options = new HashMap();
- options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
- // refer http://www.w3.org/TR/2004/PER-xmlschema-0-20040318/#ipo.xml
- Resource resource = resourceSet.createResource(URI.createFileURI("c:/temp/ipo.xml"));
- resource.load(options);
EMF 2.1 also adds a new capability that allows you to convert schemas to Ecore on the fly while loading an XML document that contains an xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute. It also allows you to load an XML document that has no schema associated with it. To use this functionality you need to register org.eclipse.emf.ecore.xmi.impl.GenericXMLResourceFactoryImpl:
java 代码
- resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml",
- new GenericXMLResourceFactoryImpl());
This article gave you a short introduction to EMF, explaining the core EMF concepts, and provided useful examples on how to exploit the dynamic capabilities of EMF.
Note: The opinions expressed in this paper are those of the authors, not of the IBM Corporation. IBM, alphaWorks, developerWorks, and WebSphere are trademarks of International Business Machines Corporation in the United States, other countries, or both. Rational is a registered trademark of International Business Machines Corporation and Rational Software Corporation, in the United States, other countries or both. Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Other company, product and service names may be trademarks or service marks of others.
发表评论
-
Java程序员25个必备的Eclipse插件
2012-01-12 22:36 21936原文:http://www.fromdev.com/2012/ ... -
关于插件更新安装的错误
2007-12-21 20:12 2217在更新插件的时候出现这样的错误: Unable to comp ... -
最近做eclipse branding, features的一些经验
2007-12-16 01:24 4552知道eclipse的splash怎么做 ... -
GEF学习笔记
2007-12-07 20:20 4164GEF以前学习过, 而且还 ... -
SWT布局深入学习
2007-11-30 23:00 7947以下内容是学习"The Definitive Gui ... -
Eclipse Action 深入学习笔记(3)
2007-11-25 17:59 4088filter元素是用来指定当 ... -
Eclipse Action 深入学习笔记(2)
2007-11-25 17:14 5218Object Action 这种Action是跟弹出的上下文菜 ... -
Eclipse Action 深入学习笔记(1)
2007-11-25 17:07 7672以前做插件用到的ActionSet都只是依葫芦画瓢,没有真正理 ... -
JFace Text Editor完全掌握之终极指南(4)
2007-11-24 17:08 5677错误标识(Error Marking) Error Marki ... -
JFace Text Editor完全掌握之终极指南(3)
2007-11-24 16:56 5506内容大纲 之所以要给编 ... -
JFace Text Editor完全掌握之终极指南(2)
2007-11-24 16:53 6734最后一步就是实现各种功能 语法高亮 首先我们要实现的是语法高亮 ... -
JFace Text Editor完全掌握之终极指南(1)
2007-11-24 16:17 9997JFace Text Editor是JFace里面一个功能强大 ... -
最近的Eclipse plugin开发总结
2007-11-24 11:30 4942List控件没有提供addDblClickListener方法 ... -
eclipse3.3关于JavaFileEditorInput兼容性问题的解决
2007-11-24 11:22 4676在eclipse3.3中,JavaFileEditor ... -
Eclipse WTP Projects Facets实战指南(2)
2007-11-21 21:13 5257修饰工作 给facet在选择列表中添加图标 格式如下: xml ... -
Eclipse WTP Projects Facets实战指南(1)
2007-11-21 20:21 9588这个文章基本是"Building Project F ... -
也说关于WizardPage的动态生成
2007-11-05 14:26 5159在Eclipse中一个Wizard=Wiza ... -
关于多页编辑器中不同Editor之间的Redo&Undo冲突的解决
2007-09-03 15:17 4061在我们的插件工具的开 ... -
TextEditor原理分析笔记
2007-08-23 15:48 3383Editor的语法着色原理:Eclipse使用damage , ... -
最近的Eclipse开发总结
2007-08-23 15:46 2122java.lang.IllegalAccessError: t ...
相关推荐
Eclipse建模框架(EMF)是一个强大的工具,用于通过建模的方法来简化和加速应用程序的开发。在深入探讨EMF的核心概念之前,需要明确什么是建模以及为什么建模技术在软件开发中如此重要。 建模是一种抽象表示现实...
Eclipse建模框架(Eclipse Modeling Framework,简称EMF)是Eclipse基金会提供的一款强大的建模工具,主要用于构建基于模型的应用程序。它基于统一建模语言(UML)和其他建模标准,为开发者提供了一种在Java环境中...
Eclipse建模框架(Eclipse Modeling Framework, EMF)是Eclipse项目的核心组件之一,它提供了一整套用于构建和运行模型的工具和API。EMF基于通用建模语言(Unified Modeling Language, UML),但同时也支持自定义...
基于Eclipse的模型框架 它是Eclipse MDA(Model Driven Architecture)的一个重要组成部分 是Eclipse中许多项目的基础 e g GEF EMF可以将模型转换成高效的 正确的 和易于定制的Java代码
通过阅读《Eclipse建模框架》这本书,你可以深入了解EMF的原理、使用方法以及最佳实践,从而更好地利用这一强大的建模工具进行软件开发和系统设计。书中可能会涵盖模型的创建、编辑器的定制、数据持久化、模型转换等...
Eclipse的EMF(Eclipse Modeling Framework)插件是一个强大的模型驱动开发工具,它基于Java构建,用于创建、编辑和管理复杂的数据模型。这个插件是Eclipse IDE的一个重要组成部分,特别适合那些需要进行领域特定...
【标题】"EMF Eclipse" 是一个与Eclipse集成的元建模框架,它允许开发者创建、编辑和操作复杂的模型。EMF(Eclipse Modeling Framework)是基于OMG的MOF(Meta-Object Facility)标准,为开发人员提供了一种在...
Eclipse Modeling Framework(EMF)是Eclipse平台的一部分,它提供了一套完整的工具和服务来支持建模工作。EMF的主要目标是为用户提供一种简便的方式来创建和管理复杂的软件模型。 **1.1.1 框架定位** EMF位于...
requires 'org.eclipse.emf.transaction 1.4.0' but it could not be found 解决办法:将下载解压后的这三个文件拷贝到eclipse的plugin文件夹下即可解决上述问题 org.eclipse.emf.transaction_1.4.0.v201003 31-1738...
**Eclipse建模框架实践代码**,简称EMF(Eclipse Modeling Framework),是Eclipse平台下的一个重要组成部分,主要用于构建和操作模型驱动的软件系统。EMF提供了强大的模型创建、存储、序列化和交换的能力,是Java...
org.eclipse.emf.transaction_1.4.0.v20100331-1738.jar org.eclipse.emf.validation_1.7.0.201306111341.jar org.eclipse.emf.workspace_1.5.1.v20120328-0001.jar
EMF(Eclipse Modeling Framework)是Eclipse中用于构建模型驱动工程的重要组件,它是基于Java的框架,提供了一套API来支持数据建模、持久化、序列化以及生成代码等任务。 EMF API文档是开发者理解和使用EMF的关键...
标签中的"EMF"和"eclipse modeling framework"进一步强调了这个主题与Eclipse的建模框架密切相关。EMF是Eclipse建模工具栈的重要组成部分,它不仅用于构建模型驱动的软件,还可以用于生成代码、执行验证和进行数据...
EMF是Eclipse项目下的一个重要组成部分,它是一个基于模型的软件开发框架,主要用于构建能够处理结构化数据的应用程序和工具。EMF提供了一套完整的工具链,包括模型到源代码的生成、数据持久化、以及基于模型的编辑...
### EMF建模框架知识点详解 #### 一、EMF简介 EMF(Eclipse Modeling Framework)是Eclipse平台下的一个重要子项目,主要用于提供一种基于模型的开发方式。EMF的核心理念是支持模型驱动架构(MDA, Model Driven ...
《Eclipse开发使用GEF和EMF》是IBM红皮书系列中的一部经典著作,主要探讨了如何在Eclipse环境中利用GEF(图形编辑框架)和EMF( Eclipse模型框架)进行图形化应用程序的开发。这两项技术是Eclipse平台上的核心组件,...
groovy-all-1.1-rc-2.jarjunit-4.3.1.jarorg.eclipse.emf.common_2.3.0.v200709252135.jarorg.eclipse.emf.ecore.xmi_2.3.1.v200709252135.jarorg.eclipse.emf.ecore_2.3.1.v200709252135.jarorg.eclipse.emf....