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不能定义操作。
