EMF-Ecore模型创建

80酷酷网    80kuku.com

  core|创建
1          包定义

1.1         UML方式:

a)       定义:

                                    i.             图:

                                          



                                 ii.             生成代码:

     publicinterface StPackage extends EPackage {

          String eNAME = "st";

          String eNS_URI = "http://st";

          String eNS_PREFIX = "st";



}

 

1.2         Java方式:

a)       定义:

                                    i.             代码:

public interface stPackage {

public String eName = "st";

public String eNS_URI = "http://st";

public String eNS_PREFIX = "st";

}

说明:interface声明中不能带“model”标记,接口名称要以“Package”结尾。接

口可以声明以下三个字段eName,eNS_URI,eNS_PREFIX。

 

 

1.3         XML方式

a)       定义:

                                    i.             代码:

<xsd:schema xmlns:xsd=http://www.w3.org/2001/XMLSchema

targetNamespace=http://st

xmlns:st="http://st">

</xsd:schema>

说明:nsURI的值由targeNamespace指定。nsPrefix的值由targetNamespace导出。

name是targetNamespace的最后一段。

 

2          类定义

2.1         UML方式

a)       定义:

                                    i.             图:



                                 ii.             生成代码:

public class TeacherImpl extendsEObjectImpl implements Teacher {

     …

}

 

b)       接口:

                                    i.             说明:如果把类的stereotype设置为<<interface>>,则EMF将使生成的EClass的interface字段设为true,但在EMF 2.1.0的测试版中,EMF并不能识别出这个<<interface>>类型。

 

c)       抽象类:

                                    i.             图:



                                 ii.             生成代码:

public abstract class TeacherImplextends EObjectImpl implements Teacher {



}

 

d)       单继承:

                                    i.             图:



                                 ii.             生成代码:

public class JavaTeacherImpl extendsTeacherImpl implements JavaTeacher {



}

 

 

e)       多继承

                                    i.             图:



                                 ii.             生成代码:

public interface JavaTeacher extendsTeacher, Nothing, Java {

}

 

public class JavaTeacherImpl extendsTeacherImpl implements JavaTeacher {

     …

}

 

2.2         Java方式:

a)       定义:

                                    i.             代码:

/**

*   model

*/

public interfaceStudent{}

说明:需要使用“model”标记。

 

b)       接口:

                                    i.             代码:

/**

 * model interface="true"

 * */

public interface Teacher{}

说明:通过声明interface属性,在生成代码当中将不会有TeacherImpl类生成。

 

c)       抽象类:

                                    i.             代码:

/**

 * model abstract="true"

 * */

public interface Teacher{}

说明:通过声明abstract属性,在生成代码中TeacherImpl将成为一抽象类。

 

d)       单继承:

                                    i.             代码:

/**

 * model

 * */

public interface JavaTeacher extendsTeacher{}

 

e)       多继承:

                                    i.             代码:

/**

 * model

 * */

public interface JavaTeacher extends Teacher,Java{}

说明:在多继承时,接口的实现类将会扩展排在extends中第一个位置的接口的实现类。上例中,生成代码中的JavaTeacherImpl将会extends TeacherImpl。

 

2.3         XML方式:

a)       定义:

                                    i.             代码:

<xsd:complexType name="Teacher"/>

说明:类名由complexType中的name属性指定。

 

b)       接口:

                                    i.             说明:XML没有提供用于定义接口的机制。

 

c)       抽象类:

                                    i.             代码:

<xsd:complexTypename="Teacher" abstract="true"/> 

 

d)       单继承:

                                    i.             代码:

<xsd:complexTypename="Teacher"/>   

<xsd:complexTypename="JavaTacher">

     <xsd:complexContent>

         <xsd:extension base="st:Teacher"/>

     </xsd:complexContent>

</xsd:complexType>

     说明:通过ComplexType的扩展机制来实现单继承。

 

e)       多继承:

                                    i.             说明:还没找到合适的多继承机制。

 

3          属性定义

3.1         UML方式:

a)       单值属性:

                                    i.             图:



                                 ii.             生成代码:

public class TeacherImpl extendsEObjectImpl implements Teacher {

     protectedstatic final String NAME_EDEFAULT = null;

     protectedString name = NAME_EDEFAULT;

     publicString getName() {…}

     publicvoid setName(String newName) {…}

     …

}

 

b)       多值属性:

                                    i.             图:



                                 ii.             生成代码:

public class TeacherImpl extendsEObjectImpl implements Teacher {

     protectedEList students = null;

     publicEList getStudents() {…}

     …

}

 

c)       修改属性的Ecore属性值:

                                    i.             图:



                                 ii.             生成代码:

public class TeacherImpl extendsEObjectImpl implements Teacher {

     protectedstatic final String UNCHANGEABLE_EDEFAULT = null; 

     protectedString unchangeable = UNCHANGEABLE_EDEFAULT; 

 

     protectedstatic final String VOLATILE_EDEFAULT = null;

 

     protectedstatic final String UNSETTABLE_EDEFAULT = null;

     protectedString unsettable = UNSETTABLE_EDEFAULT;

     protectedboolean unsettableESet = false;

    

     publicString getUnchangeable() {…}

 

     publicString getVolatile() {       

         thrownew UnsupportedOperationException();

     }

    

     publicvoid setVolatile(String newVolatile) {     

         thrownew UnsupportedOperationException();

     }

 

     publicString getUnsettable() {…}

     publicvoid setUnsettable(String newUnsettable) {… }   

     publicvoid unsetUnsettable() {…}

     publicboolean isSetUnsettable() {…}



}

说明:对于unchangeable属性,我把他的changeable属性修改为false,于是在生成的代码当中,他将不包含有set方法。对于volatile属性,我把他的isVolatile属性设为true,于是在生成的代码当中对英语volatile的get()/set()方法都实现为空方法体,且抛出一个异常。对于unsettable属性,我把他的isUnsettable属性设为ture,于是在生成的代码当中除了有unsettable属性以外,还有一个用于标记该属性是否已设置的boolean量unsettableESet。

 

d)       枚举类型:

                                    i.             图:



                                 ii.             生成代码:

public final class Score extendsAbstractEnumerator {  

     publicstatic final int GOOD = 0;

     publicstatic final int BAD = 1;



}

 

3.2         Java方式:

a)       单值属性:

                                    i.             代码:

/**

 *  model

 */

public interface Teacher{

/**

* model

* */

public String getName();

}

说明:定义属性时,必须带有“model”标记,且方法名必须符合getXyz(),或者

isXyz()的形式。

 

b)       多值属性:

                                    i.             代码:

/**

 *  model

 */

public interface Teacher{

/**

      * model type="String"

      * */   

     publicList getStudents();

}

 

c)       修改属性的Ecore值:

                                    i.             代码:

public interface Teacher extendsEObject{

     /**

      * modelchangeable="false"

      */

     publicString getUnchangeable();

 

     /**

      * modelvolatile="true"

      */

     publicString getVolatile();

 

     /**

      * modelunsettable="true"

      */

     publicString getUnsettable();

}

 

d)       枚举类型:

                                    i.             代码:

/**

 * model

 */

public finalclass Score {

     /**

      * model

      */

     publicstatic final int GOOD = 0;

 

     /**

      * model

      */

     publicstatic final int BAD = 1;

}

说明:枚举类型需要定义为final class类型,枚举值由类内部的static final int指定。

 

3.3         XML方式:

a)       单值属性:

                                    i.             代码:

<xsd:complexTypename="Teacher">

<xsd:sequence>

<xsd:element name="name" type="xsd:string"/>

</xsd:sequence>

</xsd:complexType>

 

b)       多值属性:

                                    i.             代码:

<xsd:complexTypename="Teacher">

<xsd:sequence>

<xsd:elementname="name" type="xsd:string" minOccurs="0"

maxOccurs="unbounded"/>                       

         </xsd:sequence>

     </xsd:complexType>

 

c)       修改属性的Ecore值:

                                    i.             说明:Schema没有提供修改Ecore值的方法。

 

d)       枚举类型:

                                    i.             代码:

<xsd:simpleType name="Score">

<xsd:restriction base="xsd:NCName">

<xsd:enumeration value="GOOD"/>

<xsd:enumeration value="BAD"/>

</xsd:restriction>

</xsd:simpleType>

 

4          引用定义

4.1         UML方式:

a)       单向引用:

                                    i.             图:



                                 ii.             生成代码:

public class TeacherImpl extendsEObjectImpl implements Teacher {

     protectedStudent student = null;

     publicStudent getStudent() {…}

     publicvoid setStudent(Student newStudent) {…}

     …

}

 

b)       双向引用:

                                    i.             图:



                                 ii.             生成代码:

public class TeacherImpl extendsEObjectImpl implements Teacher {

     protectedStudent student = null;

     publicStudent getStudent() {…}

     publicvoid setStudent(Student newStudent) {…}

     …

}

 

public class StudentImpl extendsEObjectImpl implements Student {

     protectedTeacher teacher = null;

     publicTeacher getTeacher() {…}

     publicvoid setTeacher(Teacher newTeacher) {…}

     …

}

 

c)       多值引用:

                                    i.             图:



                                 ii.             生成代码:

public class TeacherImpl extends EObjectImplimplements Teacher {

     protectedEList student = null;

     publicEList getStudent() {…}

     …

}

 

d)       包含引用:

                                    i.             图:



                                 ii.             生成代码:

public class TeacherImpl extendsEObjectImpl implements Teacher {

     protectedEList student = null;

     publicEList getStudent() {…}

}

说明:当使用包含引用时,由于被引用的对象和引用的对象会被保存在同一个资源内部,因此可以不使用Proxy的方式,因此Teacher内部会使用EObjectContainmentEList来保存对Student的引用。如果是普通引用,则考虑会使用EObjectResolvingEList。

 

e)       修改引用的Ecore值:

                                    i.             图:



                                 ii.             生成代码:

public class TeacherImpl extendsEObjectImpl implements Teacher {

     protectedNonProxy nonProxy = null;

 

     protectedNonChange nonChange = null;

 

     protectedUnset unset = null;

     protectedboolean unsetESet = false;

 

     publicNonProxy getNonProxy() {

         returnnonProxy;

     }

     publicvoid setNonProxy(NonProxy newNonProxy) {…}

 

     publicVolatile getVolatile() {…}

 

     publicvoid setVolatile(Volatile newVolatile) {

         thrownew UnsupportedOperationException();

     }

 

     publicNonChange getNonChange() {…}

 

     publicUnset getUnset() {…}

     publicvoid setUnset(Unset newUnset) {…}

     publicvoid unsetUnset() {…}

     publicboolean isSetUnset() {…}



}

说明:当引用的resolveProxy值设定为false时,生成的get方法将进行代理的解释步骤,而只简单的返回引用值。当引用的unchangeable值设定为true时,生成代码中将不包含set方法。当引用的volatile值设定为true时,生成的代码中将只包含空方法体。当引用的unsettable设定为true时,除了生成引用值以外,还生成用于判断引用是否已经设置的boolean量。

 

f)       Map引用:

                                    i.             图:



说明:StudentTable的stereotype必须为MapEntry,她必须包含一个key的属性,且必须有一个名称为value的引用。

 

                                 ii.             生成代码:

public class TableImpl extendsEObjectImpl implements BasicEMap.Entry {

     protectedstatic final int KEY_EDEFAULT = 0;

     protectedint key = KEY_EDEFAULT;

     protectedStudent value = null; 

     publicObject getKey() {…}

     publicvoid setKey(Object key) {…} 

     publicObject getValue() {…}

     publicObject setValue(Object value) {…}



}

 

public class TeacherImpl extendsEObjectImpl implements Teacher {

     protectedEMap table = null;

     publicEMap getTable() {…}

     …

}

 

说明: Ecore中的EMap并不是从java.util.Map中继承而来,她继承的是EList,所以,她只有一列而不是两列值。对于EMap中的每个值,都是继承自java.util.Map$Entry的一个EClass,且该EClass包含了key和value属性。由于EMap的这个结构,因此我们不能用单值引用Table,而必须用多值,这等价于:

     /**

      *   model type="Table"

*/

     EList getTable();

 

?SHAPE?\* MERGEFORMAT
key:value

key:value

key:value

key:value



key

value

key

key



key

value

value



value

java.util.Map

EMap


 

4.2         Java方式:

a)       单向引用:

                                    i.             代码:

/**

 * model

 */

public interface Teacher {

     /**

      * model

      */

     publicStudent getStudent();

}

 

/**

 * model

 */

public interface Student {}

 

b)       双向引用:

                                    i.             代码:

/**

 * model

 */

public interface Teacher {

     /**

      * model

      */

     publicStudent getStudent();

}

 

/**

 * model

 */

public interface Student {

     /**

      * model

      * */

     publicTeacher getTeacher();

}

 

c)       多值引用:

                                    i.             代码:

/**

 * model

 */

public interface Teacher {

     /**

      * modeltype="Student"

      */

     publicList getStudent();

}

 

d)       包含引用:

                                    i.             代码:

/**

 * model

 */

public interface Teacher extendsEObject {

     /**

      * model type="Student" containment="true"

      */

     publicEList getStudent();

}

 

e)       修改引用的Ecore值:

                                    i.             代码:

/**

 * model

 */

public interface Teacher extendsEObject{

     /**

      * model resolveProxies="false"

      */

     publicNonProxy getNonProxy();

 

     /**

      * model volatile="true"

      */

     publicVolatile getVolitile();

 

     /**

      * model changeable="false"

      */

     publicNonChange getNonChange();

 

     /**

      * model unsettable="true"

      */

     publicUnset getUnset();

}

 

f)       Map引用:

                                    i.             代码:

/**

 * model

 */

public interface Teacher extendsEObject {    

     /**

      * model keyType="int"valueType="Student"

      * */

     publicEMap getTable();

}

 

4.3         XML方式:

a)       单向引用:

                                    i.             代码:

<xsd:complexTypename="Teacher">

<xsd:sequence>

<xsd:elementname="student" type="st:Student"/>

</xsd:sequence>

</xsd:complexType>

 

<xsd:complexTypename="Student">

</xsd:complexType>

 

b)       双向引用:

                                    i.             代码:

<xsd:complexTypename="Teacher">

<xsd:sequence>

<xsd:elementname="student" type="st:Student"/>

</xsd:sequence>

</xsd:complexType>

 

<xsd:complexTypename="Student">

<xsd:sequence>

<xsd:elementname="teacher" type="st:Teacher"/>

</xsd:sequence>

</xsd:complexType>

 

c)       多值引用:

                                    i.             代码:

<xsd:complexTypename="Teacher">

<xsd:sequence>

<xsd:elementname="student" type="st:Student"

minOccurs="0"maxOccurs="unbounded"/>

</xsd:sequence>

</xsd:complexType>

    

d)       包含引用:

                                    i.             说明:通过complexType定义的,除了元素的类型为anyURI,QName,IDREF,IDREFS以外,containment的值都为true。

 

e)       修改引用的Ecore值:

                                    i.             说明:Schema没有提供修改引用的Ecore值的机制。

 

f)       Map引用:

                                    i.             说明:Schema没有提供用于定义Map引用的机制。

 

5          数据类型定义

5.1         UML方式:

a)       定义:

                                    i.             图:



                                 ii.             生成代码:

public class TeacherImpl extendsEObjectImpl implements Teacher {

     protectedstatic final Date BORN_EDEFAULT = null;

     protectedDate born = BORN_EDEFAULT;

     publicDate getBorn() {…}

     publicvoid setBorn(Date newBorn) {… }

     …

}

5.2         Java方式:

a)       定义:

                                    i.             代码:

public interface StPackage {

     /**

      * model instanceClass="java.util.Date"

      * */

     EDataType getNewDate();

}

 

/**

 * model

 */

public interface Teacher extendsEObject {

     /**

      * model

      */

     publicNewDate getBorn();

}

说明:使用Java方式声明新的类型时,需要在Package的声明中增加类型定义。

 

5.3         XML方式:

a)       定义:

                                    i.             代码:

<xsd:simpleTypename="NewDate">

<xsd:restriction base="xsd:date"/>

</xsd:simpleType>

<xsd:complexTypename="Teacher">

<xsd:sequence>

<xsd:elementname="born" type="st:NewDate"/>

</xsd:sequence>

</xsd:complexType>

 

6          方法定义

6.1         UML方式:

a)       定义:

                                    i.             图:



                                 ii.             生成代码:

public class TeacherImpl extendsEObjectImpl implements Teacher {

     publicvoid doSomething(String par) {

          throw new UnsupportedOperationException();

     }

}

 

6.2         Java方式:

a)       定义:

                                    i.             代码:

/**

 * model

 */

public interface Teacher {

     /**

      * model

      */

     publicvoid doSomething(String par);

}

 

6.3         XML方式:

a)       定义:

                                    i.             说明:Schema不能定义操作。



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: