1. EMF core 除了生成model和model implementation外,至少還生成兩個(gè)接口:*Factory和*Package。其中, Factory:生成實(shí)例的工廠;Package:提供一些靜態(tài)的constants(例如:被用來(lái)生成方法的feature constants )和一些訪問(wèn)模型元數(shù)據(jù)的公用方法。
2. EMF生成的方法都帶有“@generated”標(biāo)簽。EMF重新生成代碼時(shí)不會(huì)覆蓋用戶添加的方法(不帶有@generated)。
3. 關(guān)于URI(Uniform Resource Identifier)
在EMF 中是通過(guò)URI來(lái)標(biāo)識(shí)package的,并且通過(guò)URI來(lái)唯一的確定resources。URI包括三個(gè)部分:a scheme, a scheme-specific part和an optional fragment。scheme主要決定了訪問(wèn)
資源的協(xié)議;比如:Platform:/resource/……中的platform。scheme-specific part包含一些authority, device和一些segments,如file:/c:/dir1/dir2/myfile.xml,file是協(xié)議,沒(méi)有authority,c:是device,剩下的3個(gè)是segments。URI fragment 標(biāo)識(shí)了resource內(nèi)部的某個(gè)具體的內(nèi)容。如:file:/c:/dir1/dir2/myfile.xml#loc中的#loc。 EMF通過(guò)帶有fragment的URI來(lái)訪問(wèn)資源中的EObjects。
4. 關(guān)于URIConverter
URIConverter的作用是normalize一個(gè)輸入URI,使之成為一個(gè)實(shí)際的針對(duì)某個(gè)resource的URI。它可以把namespace URIs(比如:http:///com/example/epo2.ecore)映射到物理文件的URIs, 或者重定向舊的(或別名)的URI參考到一個(gè)具體的實(shí)際的location。一個(gè)URIConverter維護(hù)一個(gè)URI到URI的映射集合。比如,把一個(gè)命名空間URI映射到物理文件:
URIConverter converter = new URIConverterImpl();
URI uri1 = URI.createURI("http:///somemodel.ecore");
URI uri2 =URI.createURI("platform:/resource/project/somemodel.ecore");
converter.getURIMap().put(uri1, uri2);
在如下面代碼:
URI normalized = converter.normalize(uri1);
System.out.println(normalized);
打印的結(jié)果是:platform:/resource/project/somemodel.ecore
URIConverter.normalize()方法只是簡(jiǎn)單的通過(guò)映射的map把key替換成了相應(yīng)的value。
URIConverter的最原始是應(yīng)用在resource sets,用來(lái)定位resources.
5. 關(guān)于Resource和ResourceSet
Resource 表示一個(gè)持久化的EOjbects的容器;ResourceSet表示一組Resource的集合,集合中的Resource同時(shí)創(chuàng)建或加載。Resource中比較重要的就是save和load方法,還有通過(guò)URI fragments訪問(wèn)資源中的Object的機(jī)制,如:
Resource resource =…
Item item = (Item)resource.getEObject("http://@orders.0/@items.2");
Item item =…
String fragment = resource.getURIFragment(item);
上面代碼中的兩個(gè)方法,getEObject通過(guò)帶有fregment的URI獲得一個(gè)EObject,與之相反的方法getURIFragment()通過(guò)EObject獲得相應(yīng)的fragment path。
ResourceSet中有些重要的方法:
createResource()創(chuàng)建一個(gè)空的Resource;
getResource()通過(guò)resource的URI來(lái)創(chuàng)建Resource;
getEObject(),通過(guò)URI中的fregment來(lái)獲得具體的EObject對(duì)象。
6. 關(guān)于Resource.Factory
用來(lái)創(chuàng)建Resource,resource factory 要注冊(cè)到Registry實(shí)例中。一個(gè)factory 可以通過(guò)多種方式的URIs來(lái)注冊(cè),包括URI scheme或者URI的extension。在插件方式的應(yīng)用中,通過(guò)擴(kuò)展點(diǎn)的方式在插件加載的時(shí)候注冊(cè)descriptor。下面是Resource的源代碼
public interface Resource extends Notifier
{
interface Factory
{
Resource createResource(URI uri);
interface Descriptor
{
Factory createFactory();
}
interface Registry
{
Factory getFactory(URI uri);
Map getProtocolToFactoryMap();
String DEFAULT_EXTENSION = "*";
Map getExtensionToFactoryMap();
Registry INSTANCE = new ResourceFactoryRegistryImpl
();
}
}
}
下面是Registry中的getFactory()方法的算法(引用原文):
1. Check for a factory in the protocolToFactoryMap, using
the scheme of the URI.
2. If nothing was found, check the extensionToFactoryMap
using the file extension of the URI.
3. If still nothing was found, check the
extensionToFactoryMap using the DEFAULT_EXTENSION
(that is, the wildcard character "*").
4. If no extension match was found, call the
delegatedGetFactory() method. This allows you to supply
your own factory registry, with its own lookup
criteria.
5. If a descriptor was found, instead of an actual
factory, call the createFactory() method on the
descriptor to create the factory.
6. Finally, return the factory if one was found, or null.
tip:emf缺省的序列化方式是XMI。因此,如果沒(méi)有找到相應(yīng)注冊(cè)的factory缺省的就會(huì)返回以*注冊(cè)的缺省的factory,這個(gè)factory是針對(duì)XMI的factory,即XMIResourceFactoryImpl。如,對(duì)于XMIResourceFactoryImpl的擴(kuò)展點(diǎn)聲明:
<extension point = "org.eclipse.emf.ecore.extension_parser">
<parser type="*" class="org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl"/>
</extension>
當(dāng)非插件應(yīng)用的時(shí)候,可以通過(guò)手工的方式來(lái)注冊(cè)factory,如:
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put
("*", new XMIResourceFactoryImpl());
7. Resource通過(guò)兩種方式實(shí)現(xiàn)XML和XMI。
1) XML
實(shí)現(xiàn)Resource存儲(chǔ)的重要方法是save, 只有在方法eIsSet()返回true時(shí),相關(guān)的屬性和引用才會(huì)被序列化到xml中。對(duì)于存儲(chǔ)的xml文件的格式可以通過(guò)設(shè)置OPTION_XML_MAP屬性來(lái)完成,如下面原文:
There are several reasons why you might want to use OPTION_XML_MAP when saving a resource:
1. You want to save your data so that it conforms to a particular XML Schema.
2. You have changed your model, and you want the resource to produce XML documents that will work with the old version of the model.
3. You want to design your own XML format without changing your model.
2) XMI
XMI和XML基本相同,XMIResourceImpl就是
XMLResourceImpl的簡(jiǎn)單繼承,只是在引用objects時(shí)使用XMI IDs