白玉成的專欄

          Eclipse,讓我歡喜讓我憂!

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            17 隨筆 :: 0 文章 :: 6 評(píng)論 :: 0 Trackbacks
          在項(xiàng)目中用到EMF積累的一些基礎(chǔ)知識(shí),希望對(duì)大家有所幫助。

            1. EMF core 除了生成modelmodel 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)于URIUniform Resource Identifier

          EMF 中是通過(guò)URI來(lái)標(biāo)識(shí)package的,并且通過(guò)URI來(lái)唯一的確定resourcesURI包括三個(gè)部分:a scheme, a scheme-specific partan optional fragmentscheme主要決定了訪問(wèn)

          資源的協(xié)議;比如:Platform:/resource/……中的platformscheme-specific part包含一些authority, device和一些segments,如file:/c:/dir1/dir2/myfile.xmlfile是協(xié)議,沒(méi)有authorityc:device,剩下的3個(gè)是segmentsURI fragment 標(biāo)識(shí)了resource內(nèi)部的某個(gè)具體的內(nèi)容。如:file:/c:/dir1/dir2/myfile.xml#loc中的#loc EMF通過(guò)帶有fragmentURI來(lái)訪問(wèn)資源中的EObjects

           

          4. 關(guān)于URIConverter

          URIConverter的作用是normalize一個(gè)輸入URI,使之成為一個(gè)實(shí)際的針對(duì)某個(gè)resourceURI。它可以把namespace URIs(比如:http:///com/example/epo2.ecore)映射到物理文件的URIs, 或者重定向舊的(或別名)的URI參考到一個(gè)具體的實(shí)際的location。一個(gè)URIConverter維護(hù)一個(gè)URIURI的映射集合。比如,把一個(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ò)映射的mapkey替換成了相應(yīng)的value

          URIConverter的最原始是應(yīng)用在resource sets,用來(lái)定位resources.

           

          5. 關(guān)于ResourceResourceSet

          Resource 表示一個(gè)持久化的EOjbects的容器;ResourceSet表示一組Resource的集合,集合中的Resource同時(shí)創(chuàng)建或加載。Resource中比較重要的就是saveload方法,還有通過(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ò)帶有fregmentURI獲得一個(gè)EObject,與之相反的方法getURIFragment()通過(guò)EObject獲得相應(yīng)的fragment path

          ResourceSet中有些重要的方法:

          createResource()創(chuàng)建一個(gè)空的Resource

          getResource()通過(guò)resourceURI來(lái)創(chuàng)建Resource

          getEObject(),通過(guò)URI中的fregment來(lái)獲得具體的EObject對(duì)象。

           

          6. 關(guān)于Resource.Factory

          用來(lái)創(chuàng)建Resourceresource factory 要注冊(cè)到Registry實(shí)例中。一個(gè)factory 可以通過(guò)多種方式的URIs來(lái)注冊(cè),包括URI scheme或者URIextension。在插件方式的應(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.

          tipemf缺省的序列化方式是XMI。因此,如果沒(méi)有找到相應(yīng)注冊(cè)的factory缺省的就會(huì)返回以*注冊(cè)的缺省的factory,這個(gè)factory是針對(duì)XMIfactory,即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)XMLXMI

          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

          XMIXML基本相同,XMIResourceImpl就是

          XMLResourceImpl的簡(jiǎn)單繼承,只是在引用objects時(shí)使用XMI IDs

          posted on 2008-11-25 22:26 白玉成 閱讀(530) 評(píng)論(0)  編輯  收藏 所屬分類: EMF

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 彰化县| 房产| 乐昌市| 和田县| 绥宁县| 朝阳市| 吕梁市| 县级市| 肃南| 徐汇区| 屏东县| 新疆| 罗定市| 黔西县| 重庆市| 博爱县| 邹平县| 阳江市| 余干县| 四会市| 北宁市| 黑龙江省| 同江市| 泽州县| 拉萨市| 当涂县| 阿瓦提县| 共和县| 城口县| 望奎县| 萨嘎县| 嘉善县| 庆城县| 平潭县| 陕西省| 商南县| 万全县| 福鼎市| 松潘县| 乌什县| 甘肃省|