<!--[if !supportLists]-->1 <!--[endif]-->包定义
<!--[if !supportLists]-->1.1 <!--[endif]-->UML方式:
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public interface StPackage extends EPackage {
String eNAME = "st";
String eNS_URI = "http://st";
String eNS_PREFIX = "st";
…
}
<!--[if !supportLists]-->1.2 <!--[endif]-->Java方式:
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
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。
<!--[if !supportLists]-->1.3 <!--[endif]-->XML方式
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
<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的最后一段。
<!--[if !supportLists]-->2 <!--[endif]-->类定义
<!--[if !supportLists]-->2.1 <!--[endif]-->UML方式
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TeacherImpl extends EObjectImpl implements Teacher {
…
}
<!--[if !supportLists]-->b) <!--[endif]-->接口:
<!--[if !supportLists]--> i. <!--[endif]-->说明:如果把类的stereotype设置为<<interface>>,则EMF将使生成的EClass的interface字段设为true,但在EMF 2.1.0 的测试版中,EMF并不能识别出这个<<interface>>类型。
<!--[if !supportLists]-->c) <!--[endif]-->抽象类:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public abstract class TeacherImpl extends EObjectImpl implements Teacher {
…
}
<!--[if !supportLists]-->d) <!--[endif]-->单继承:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class JavaTeacherImpl extends TeacherImpl implements JavaTeacher {
…
}
<!--[if !supportLists]-->e) <!--[endif]-->多继承
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public interface JavaTeacher extends Teacher, Nothing, Java {
}
public class JavaTeacherImpl extends TeacherImpl implements JavaTeacher {
…
}
<!--[if !supportLists]-->2.2 <!--[endif]-->Java方式:
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
*/
public interface Student{}
说明:需要使用“@model”标记。
<!--[if !supportLists]-->b) <!--[endif]-->接口:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model interface="true"
* */
public interface Teacher{}
说明:通过声明interface属性,在生成代码当中将不会有TeacherImpl类生成。
<!--[if !supportLists]-->c) <!--[endif]-->抽象类:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model abstract="true"
* */
public interface Teacher{}
说明:通过声明abstract属性,在生成代码中TeacherImpl将成为一抽象类。
<!--[if !supportLists]-->d) <!--[endif]-->单继承:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
* */
public interface JavaTeacher extends Teacher{}
<!--[if !supportLists]-->e) <!--[endif]-->多继承:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
* */
public interface JavaTeacher extends Teacher,Java{}
说明:在多继承时,接口的实现类将会扩展排在extends中第一个位置的接口的实现类。上例中,生成代码中的JavaTeacherImpl将会extends TeacherImpl。
<!--[if !supportLists]-->2.3 <!--[endif]-->XML方式:
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
<xsd:complexType name="Teacher"/>
说明:类名由complexType中的name属性指定。
<!--[if !supportLists]-->b) <!--[endif]-->接口:
<!--[if !supportLists]--> i. <!--[endif]-->说明:XML没有提供用于定义接口的机制。
<!--[if !supportLists]-->c) <!--[endif]-->抽象类:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
<xsd:complexType name="Teacher" abstract="true"/>
<!--[if !supportLists]-->d) <!--[endif]-->单继承:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
<xsd:complexType name="Teacher"/>
<xsd:complexType name="JavaTacher">
<xsd:complexContent>
<xsd:extension base="st:Teacher"/>
</xsd:complexContent>
</xsd:complexType>
说明:通过ComplexType的扩展机制来实现单继承。
<!--[if !supportLists]-->e) <!--[endif]-->多继承:
<!--[if !supportLists]--> i. <!--[endif]-->说明:还没找到合适的多继承机制。
<!--[if !supportLists]-->3 <!--[endif]-->属性定义
<!--[if !supportLists]-->3.1 <!--[endif]-->UML方式:
<!--[if !supportLists]-->a) <!--[endif]-->单值属性:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TeacherImpl extends EObjectImpl implements Teacher {
protected static final String NAME_EDEFAULT = null;
protected String name = NAME_EDEFAULT;
public String getName() {…}
public void setName(String newName) {…}
…
}
<!--[if !supportLists]-->b) <!--[endif]-->多值属性:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TeacherImpl extends EObjectImpl implements Teacher {
protected EList students = null;
public EList getStudents() {…}
…
}
<!--[if !supportLists]-->c) <!--[endif]-->修改属性的Ecore属性值:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TeacherImpl extends EObjectImpl implements Teacher {
protected static final String UNCHANGEABLE_EDEFAULT = null;
protected String unchangeable = UNCHANGEABLE_EDEFAULT;
protected static final String VOLATILE_EDEFAULT = null;
protected static final String UNSETTABLE_EDEFAULT = null;
protected String unsettable = UNSETTABLE_EDEFAULT;
protected boolean unsettableESet = false;
public String getUnchangeable() {…}
public String getVolatile() {
throw new UnsupportedOperationException();
}
public void setVolatile(String newVolatile) {
throw new UnsupportedOperationException();
}
public String getUnsettable() {…}
public void setUnsettable(String newUnsettable) {… }
public void unsetUnsettable() {…}
public boolean isSetUnsettable() {…}
…
}
说明:对于unchangeable属性,我把他的changeable属性修改为false,于是在生成的代码当中,他将不包含有set方法。对于volatile属性,我把他的isVolatile属性设为true,于是在生成的代码当中对英语volatile的get()/set()方法都实现为空方法体,且抛出一个异常。对于unsettable属性,我把他的isUnsettable属性设为ture,于是在生成的代码当中除了有unsettable属性以外,还有一个用于标记该属性是否已设置的boolean量unsettableESet。
<!--[if !supportLists]-->d) <!--[endif]-->枚举类型:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public final class Score extends AbstractEnumerator {
public static final int GOOD = 0;
public static final int BAD = 1;
…
}
<!--[if !supportLists]-->3.2 <!--[endif]-->Java方式:
<!--[if !supportLists]-->a) <!--[endif]-->单值属性:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
*/
public interface Teacher{
/**
* @model
* */
public String getName();
}
说明:定义属性时,必须带有“@model”标记,且方法名必须符合getXyz(),或者
isXyz()的形式。
<!--[if !supportLists]-->b) <!--[endif]-->多值属性:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
*/
public interface Teacher{
/**
* @model type="String"
* */
public List getStudents();
}
<!--[if !supportLists]-->c) <!--[endif]-->修改属性的Ecore值:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
public interface Teacher extends EObject{
/**
* @model changeable="false"
*/
public String getUnchangeable();
/**
* @model volatile="true"
*/
public String getVolatile();
/**
* @model unsettable="true"
*/
public String getUnsettable();
}
<!--[if !supportLists]-->d) <!--[endif]-->枚举类型:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
*/
public final class Score {
/**
* @model
*/
public static final int GOOD = 0;
/**
* @model
*/
public static final int BAD = 1;
}
说明:枚举类型需要定义为final class类型,枚举值由类内部的static final int指定。
<!--[if !supportLists]-->3.3 <!--[endif]-->XML方式:
<!--[if !supportLists]-->a) <!--[endif]-->单值属性:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
<xsd:complexType name="Teacher">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<!--[if !supportLists]-->b) <!--[endif]-->多值属性:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
<xsd:complexType name="Teacher">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!--[if !supportLists]-->c) <!--[endif]-->修改属性的Ecore值:
<!--[if !supportLists]--> i. <!--[endif]-->说明:Schema没有提供修改Ecore值的方法。
<!--[if !supportLists]-->d) <!--[endif]-->枚举类型:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
<xsd:simpleType name="Score">
<xsd:restriction base="xsd:NCName">
<xsd:enumeration value="GOOD"/>
<xsd:enumeration value="BAD"/>
</xsd:restriction>
</xsd:simpleType>
<!--[if !supportLists]-->4 <!--[endif]-->引用定义
<!--[if !supportLists]-->4.1 <!--[endif]-->UML方式:
<!--[if !supportLists]-->a) <!--[endif]-->单向引用:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TeacherImpl extends EObjectImpl implements Teacher {
protected Student student = null;
public Student getStudent() {…}
public void setStudent(Student newStudent) {…}
…
}
<!--[if !supportLists]-->b) <!--[endif]-->双向引用:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TeacherImpl extends EObjectImpl implements Teacher {
protected Student student = null;
public Student getStudent() {…}
public void setStudent(Student newStudent) {…}
…
}
public class StudentImpl extends EObjectImpl implements Student {
protected Teacher teacher = null;
public Teacher getTeacher() {…}
public void setTeacher(Teacher newTeacher) {…}
…
}
<!--[if !supportLists]-->c) <!--[endif]-->多值引用:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TeacherImpl extends EObjectImpl implements Teacher {
protected EList student = null;
public EList getStudent() {…}
…
}
<!--[if !supportLists]-->d) <!--[endif]-->包含引用:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TeacherImpl extends EObjectImpl implements Teacher {
protected EList student = null;
public EList getStudent() {…}
}
说明:当使用包含引用时,由于被引用的对象和引用的对象会被保存在同一个资源内部,因此可以不使用Proxy的方式,因此Teacher内部会使用EObjectContainmentEList来保存对Student的引用。如果是普通引用,则考虑会使用EObjectResolvingEList。
<!--[if !supportLists]-->e) <!--[endif]-->修改引用的Ecore值:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TeacherImpl extends EObjectImpl implements Teacher {
protected NonProxy nonProxy = null;
protected NonChange nonChange = null;
protected Unset unset = null;
protected boolean unsetESet = false;
public NonProxy getNonProxy() {
return nonProxy;
}
public void setNonProxy(NonProxy newNonProxy) {…}
public Volatile getVolatile() {…}
public void setVolatile(Volatile newVolatile) {
throw new UnsupportedOperationException();
}
public NonChange getNonChange() {…}
public Unset getUnset() {…}
public void setUnset(Unset newUnset) {…}
public void unsetUnset() {…}
public boolean isSetUnset() {…}
…
}
说明:当引用的resolveProxy值设定为false时,生成的get方法将进行代理的解释步骤,而只简单的返回引用值。当引用的unchangeable值设定为true时,生成代码中将不包含set方法。当引用的volatile值设定为true时,生成的代码中将只包含空方法体。当引用的unsettable设定为true时,除了生成引用值以外,还生成用于判断引用是否已经设置的boolean量。
<!--[if !supportLists]-->f) <!--[endif]-->Map引用:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
说明:StudentTable的stereotype必须为MapEntry,她必须包含一个key的属性,且必须有一个名称为value的引用。
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TableImpl extends EObjectImpl implements BasicEMap.Entry {
protected static final int KEY_EDEFAULT = 0;
protected int key = KEY_EDEFAULT;
protected Student value = null;
public Object getKey() {…}
public void setKey(Object key) {…}
public Object getValue() {…}
public Object setValue(Object value) {…}
…
}
public class TeacherImpl extends EObjectImpl implements Teacher {
protected EMap table = null;
public EMap getTable() {…}
…
}
说明: Ecore中的EMap并不是从java.util.Map中继承而来,她继承的是EList,所以,她只有一列而不是两列值。对于EMap中的每个值,都是继承自java.util.Map$Entry的一个EClass,且该EClass包含了key和value属性。由于EMap的这个结构,因此我们不能用单值引用Table,而必须用多值,这等价于:
/**
* @model type="Table"
*/
ELis t getTable();
key:value |
key:value |
key:value |
key:value |
… |
key |
value |
key |
key |
… |
key |
value |
value |
… |
value |
java.util.Map |
EMap |
<!--[endif]-->
<!--[if !supportLists]-->4.2 <!--[endif]-->Java方式:
<!--[if !supportLists]-->a) <!--[endif]-->单向引用:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
*/
public interface Teacher {
/**
* @model
*/
public Student getStudent();
}
/**
* @model
*/
public interface Student {}
<!--[if !supportLists]-->b) <!--[endif]-->双向引用:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
*/
public interface Teacher {
/**
* @model
*/
public Student getStudent();
}
/**
* @model
*/
public interface Student {
/**
* @model
* */
public Teacher getTeacher();
}
<!--[if !supportLists]-->c) <!--[endif]-->多值引用:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
*/
public interface Teacher {
/**
* @model type="Student"
*/
public List getStudent();
}
<!--[if !supportLists]-->d) <!--[endif]-->包含引用:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
*/
public interface Teacher extends EObject {
/**
* @model type="Student" containment="true"
*/
public EList getStudent();
}
<!--[if !supportLists]-->e) <!--[endif]-->修改引用的Ecore值:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
*/
public interface Teacher extends EObject{
/**
* @model resolveProxies="false"
*/
public NonProxy getNonProxy();
/**
* @model volatile="true"
*/
public Volatile getVolitile();
/**
* @model changeable="false"
*/
public NonChange getNonChange();
/**
* @model unsettable="true"
*/
public Unset getUnset();
}
<!--[if !supportLists]-->f) <!--[endif]-->Map引用:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
*/
public interface Teacher extends EObject {
/**
* @model keyType="int" valueType="Student"
* */
public EMap getTable();
}
<!--[if !supportLists]-->4.3 <!--[endif]-->XML方式:
<!--[if !supportLists]-->a) <!--[endif]-->单向引用:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
<xsd:complexType name="Teacher">
<xsd:sequence>
<xsd:element name="student" type="st:Student"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Student">
</xsd:complexType>
<!--[if !supportLists]-->b) <!--[endif]-->双向引用:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
<xsd:complexType name="Teacher">
<xsd:sequence>
<xsd:element name="student" type="st:Student"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Student">
<xsd:sequence>
<xsd:element name="teacher" type="st:Teacher"/>
</xsd:sequence>
</xsd:complexType>
<!--[if !supportLists]-->c) <!--[endif]-->多值引用:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
<xsd:complexType name="Teacher">
<xsd:sequence>
<xsd:element name="student" type="st:Student"
minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!--[if !supportLists]-->d) <!--[endif]-->包含引用:
<!--[if !supportLists]--> i. <!--[endif]-->说明:通过complexType定义的,除了元素的类型为anyURI,QName,IDREF,IDREFS以外,containment的值都为true。
<!--[if !supportLists]-->e) <!--[endif]-->修改引用的Ecore值:
<!--[if !supportLists]--> i. <!--[endif]-->说明:Schema没有提供修改引用的Ecore值的机制。
<!--[if !supportLists]-->f) <!--[endif]-->Map引用:
<!--[if !supportLists]--> i. <!--[endif]-->说明:Schema没有提供用于定义Map引用的机制。
<!--[if !supportLists]-->5 <!--[endif]-->数据类型定义
<!--[if !supportLists]-->5.1 <!--[endif]-->UML方式:
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TeacherImpl extends EObjectImpl implements Teacher {
protected static final Date BORN_EDEFAULT = null;
protected Date born = BORN_EDEFAULT;
public Date getBorn() {…}
public void setBorn(Date newBorn) {… }
…
}
<!--[if !supportLists]-->5.2 <!--[endif]-->Java方式:
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
public interface StPackage {
/**
* @model instanceClass="java.util.Date"
* */
EDataType getNewDate();
}
/**
* @model
*/
public interface Teacher extends EObject {
/**
* @model
*/
public NewDate getBorn();
}
说明:使用Java方式声明新的类型时,需要在Package的声明中增加类型定义。
<!--[if !supportLists]-->5.3 <!--[endif]-->XML方式:
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
<xsd:simpleType name="NewDate">
<xsd:restriction base="xsd:date"/>
</xsd:simpleType>
<xsd:complexType name="Teacher">
<xsd:sequence>
<xsd:element name="born" type="st:NewDate"/>
</xsd:sequence>
</xsd:complexType>
<!--[if !supportLists]-->6 <!--[endif]-->方法定义
<!--[if !supportLists]-->6.1 <!--[endif]-->UML方式:
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->图:
<!--[if !vml]-->
<!--[endif]-->
<!--[if !supportLists]--> ii. <!--[endif]-->生成代码:
public class TeacherImpl extends EObjectImpl implements Teacher {
public void doSomething(String par) {
throw new UnsupportedOperationException();
}
}
<!--[if !supportLists]-->6.2 <!--[endif]-->Java方式:
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->代码:
/**
* @model
*/
public interface Teacher {
/**
* @model
*/
public void doSomething(String par);
}
<!--[if !supportLists]-->6.3 <!--[endif]-->XML方式:
<!--[if !supportLists]-->a) <!--[endif]-->定义:
<!--[if !supportLists]--> i. <!--[endif]-->说明:Schema不能定义操作。