ERPWIN API 使用指南
一、SCAPI
1. 定义
SCAPI 全称是Script Client API,是ALLFUSION DM 的一部分,可用于客制化插件开发和第三方工具。
2. 特点
a. 读取和操作模型数据对象(ADMO)
b. 集合和枚举
c. Connection Point
d. 丰富的错误处理信息,包含在errorSCAPI和errorERwin枚举。
e. 活动模型目录
f. 活动脚本 --- 包括一个脚本宿主环境,并提供调用脚本和附加组件的机制;提供注册附加组件和脚本到活动脚本环境的机制。
二、API 组件
1. SCAPI 的逻辑分层
l Application Tier
l Model Directory Tier
l Session Layer
l Model Data Tier
2. Application Tier
建立与持久装置中模型的连接,这里的持久装置包括文件和Model Manager Repository。
并且控制内存中的模型与持久装置中模型的数据交换。
应用层包含以下接口:
Interface |
Role |
Application |
是ERWIN DM API接口的入口。包含PersistenceUnits 和Sessions两个重要属性。 |
ApplicationEnvironment |
提供运行期环境信息 |
ApplicationServiceCollection |
提供一系统的应用层服务,如正向工程,反向工程,完全比较等 |
PersistenceUnitCollection |
包含已知的全部PersistenceUnit |
PersistenceUnit |
代表应用程序中的持久单元,它是驻留在内存的,并且以模型集的形式组织数据。客户端能连接到持久单元操作它和它所包含的数据。 |
ModelSetCollection |
持久单元所包含的模型集 |
ModelSet |
持久单元中的一个模型集,如EMX或EM2类 |
Properties |
一组属性 |
3. Model Directory Tier
存取和操作持久化存储装置中的目录,包括文件目录和模型管理器目录。
包括以下接口:
Interface |
Role |
ModelDirectoryCollection |
枚举所有顶层模型目录 |
ModelDirectory |
模型目录 |
ModelDirectoryUnit |
模型目录单元 |
4. Session Layer
建立存取内存中模型数据的连接
包括以下接口:
Interface |
Role |
SessionCollection |
活动会话 |
Session |
代表一个客户端与模型间的连接。客户端建立会话,并用它打开持久单元中的模型集。一个会话只能打开模型集中的一层 |
5. Model Data Tier
包括以下接口:
Interface |
Role |
ModelObjectCollection |
表示可操作对象集合。可以设置过滤规则限制某些成员不展示。 |
ModelObject |
模型对象 |
ModelPropertyCollection |
模型对象所包含的模型属性,也可以设置过滤规则。 |
ModelProperty |
存取和操作一个模型属性。属性可以是多值的,通过键来存取。当前多值属性是用数据实现的,键用array index表示。 |
PropertyValueCollection |
表示属性值列表 |
PropertyValue |
单一值,由数据和键组成 |
6. 存取模型数据
可以通过Model Directory Collection, Model Directory, Model Directory Unit在存储装置中定位模型。可以通过Persistence Unit Collection新建和注册一个新的模型,也可以从池中增加和删除模型。
Persistence Unit 维护一个属性集,可设置应用程序用户接口的可见性,存取属性等。Persistence Unit以链式模型集的方式组织数据,模型集又以树状结构组织,其中包含大量模型数据,且模型集对象位于最顶层。Erwin中将EMX定义为最顶层的模型集。EMX又包括二级模型级,简称EM2,它包含用户选项和用户接口数据。
通过Session存取模型集,模型集中包含很多数据,如实体,属性,关系。
模型集同样也包含元数据,即可能出现的应用程序对象和属性的描述,具体包括对象(实体、属性/Attribute、关系)和属性(Property),对象聚合(表述对象之间的关系,如模型拥有对象,对象拥有属性),属性关联。
新的模型建立时,它会默认包含一些对象,如Model Object, Main Subject Area, Stored Display。
SCAPI最初只包含以下级别的模型:
Name |
Description |
可执行动作 |
SCD_SL_M0 |
模型层 |
存取模型数据,建立删除对象,设置属性 |
SCD_SL_M1 |
元数据层 |
存取对象、属性的定义及其它元数据。建立删除用户定义属性和用户定义对象的定义。 |
7. 对象标识符
ERWIN中每一个对象都有一个对象标识符,它可以唯一地标识一个对象。它包含20个字节,即16个字节的GUID和32位的无符号数。
8. 对象标识符与TypeCode
TypeCode也是对象标识符,它们的格式必须相同。
9. Collections 与 Automation
10. 范例程序 ERwinSpy_Sample.vbp
三 、API使用详解
1.建立Application对象
SCAPI. Application SCApp=new SCAPI. Application ()
2.Application Properties
通过下表的方法可以获得Erwin的信息
2.1 Application接口
2.2 ApplicationEnvironment 接口
Category参数中的特征类别使用 (.)定义它的特征子集。
如果Category为空值,PropertyBag返回可用的特征全集。
Example:
public void GetApplicationFeatures(SCAPI.Application scApp){
Console.WriteLine("show all property:");
// 取得全部应用环境属性
SCAPI.PropertyBag scBag = scApp.ApplicationEnvironment.get_PropertyBag(null, null, true);
PrintPropertyBag(scBag);
Console.WriteLine("show all property:");
// 1.获取全部Categories
scBag = scApp.ApplicationEnvironment.get_PropertyBag(null, "Categories", true);
if (scBag.get_Value("Categories").GetType().IsArray)
{
string[] aCategories = (string[])scBag.get_Value("Categories");
if (aCategories.Length > 0)
{
// 2.将Categories中每个类别的属性打印出来
foreach (string categoryName in aCategories)
{
scBag = scApp.ApplicationEnvironment.get_PropertyBag(categoryName, null, true);
Console.WriteLine("Values for the " + categoryName + " category:");
PrintPropertyBag(scBag);
}
}
}
// 3. 得到Api属性值
Console.WriteLine("show api version");
scBag = scApp.ApplicationEnvironment.get_PropertyBag("Application.API", "API Version", true);
PrintPropertyBag(scBag);
}
private void PrintPropertyBag(PropertyBag scBag)
{
if (scBag != null)
{
for (int i = 0; i < scBag.Count; i++)
{
if (scBag.get_Value(i).GetType().IsArray)
{
string[] values = (string[])scBag.get_Value(i);
if (values.Length > 0)
{
Console.WriteLine(i + ")" + scBag.get_Name(i) + " is an array:");
foreach (string value in values)
{
System.Console.WriteLine("/t" + value);
}
}
}
else
Console.WriteLine(i + ")" + scBag.get_Name(i) + " = " + scBag.get_Value(i));
}
}
}
输出结果如下:
show all property:
0)Categories is an array:
Application
Application.API
Application.API.Features
Application.Persistence
Application.Persistence.FileSystem
Application.Persistence.ModelMart
1)Title = Computer Associates ERwin
2)Version = ERwin 4.1.4 Beta Build 3643
3)Hosting Application = Stand-alone
4)Metadata Version = V2.52 ERwin
5)API Version = SCAPI Version 4.2 (ERwin 4.1.4 Beta)
6)API Major Version Number = 4
7)API Minor Version Number = 2
8)Undo = Mot supported
9)Redo = Not supported
10)Change Logging = Not supported
11)Ownership Support = Ownership with cycles
12)Transactions = Begin, End and Rollback
13)Current Directory = C:/Documents and Settings/user/My Documents/Visual Studio 2005/Projects/ErwinSpy/ErwinSpy/bin/Debug
14)Model Mart Connection Types is an array:
SQL Server Vers. 7.x (using db-lib)
SQL Server 2000 (using db-lib)
Sybase System 11.9/12.0 (using ct-lib)
Oracle Vers. 8.xx/9i
Informix Vers. 9.xx
show all property:
Values for the category:
Categories is an array:
Application
Application.API
Application.API.Features
Application.Persistence
Application.Persistence.FileSystem
Application.Persistence.ModelMart
Values for the Application category:
0)Title = Computer Associates ERwin
1)Version = ERwin 4.1.4 Beta Build 3643
2)Hosting Application = Stand-alone
3)Metadata Version = V2.52 ERwin
Values for the Application.API category:
0)API Version = SCAPI Version 4.2 (ERwin 4.1.4 Beta)
1)API Major Version Number = 4
2)API Minor Version Number = 2
Values for the Application.API.Features category:
0)Undo = Mot supported
1)Redo = Not supported
2)Change Logging = Not supported
3)Ownership Support = Ownership with cycles
4)Transactions = Begin, End and Rollback
Values for the Application.Persistence category:
Values for the Application.Persistence.FileSystem category:
0)Current Directory = C:/Documents and Settings/user/My Documents/Visual Studio 2005/Projects/ErwinSpy/ErwinSpy/bin/Debug
Values for the Application.Persistence.ModelMart category:
0)Model Mart Connection Types is an array:
SQL Server Vers. 7.x (using db-lib)
SQL Server 2000 (using db-lib)
Sybase System 11.9/12.0 (using ct-lib)
Oracle Vers. 8.xx/9i
Informix Vers. 9.xx
show api version
0)API Version = SCAPI Version 4.2 (ERwin 4.1.4 Beta)
3.读取模型信息
SCAPI 客户端通过持久化单元池存取模型中的信息。
3.1 附加工具中使用ISAPI
API 客户端以DLL的形式附加在ERWIN的Add In菜单中。
3.1.1 Application Interface
3.1.2 PersistenceUnitCollection Interface
3.1.3 PersistenceUnit Interface
3.1.4 PersistenceUnit中的PropertyBag成员
3.1.5 PropertyBag Interface
同ApplicationEnviroment.PropertyBag的使用方法。
3.1.6 Example Program
public void ShowPUPropertyBag()
{
PersistenceUnit scPUnit = OpenModel("E://DW-Home//0400逻辑数据模型//详细设计//SPDB-EDW-LDM.ER1");
if (scPUnit != null)
{
PropertyBag pb = scPUnit.get_PropertyBag("Locator;Model Type", true);
Console.WriteLine("Locator="+pb.get_Value("Locator"));
Console.WriteLine("Model Type=" + pb.get_Value("Model Type"));
}
}
输出:
Locator=erwin://E:/DW-Home/0400逻辑数据模型/详细设计/SPDB-EDW-LDM.ER1
Model Type=ERwin Logical Physical Model
3.2 独立应用程序中使用API
即在ERWIN以外建立一个进程,与ERWIN不能共享数据,Application初始建立时PersistenceUnits是空集,要得到有效的PersistenceUnit,要么建立一个新的模型,要么打开已经存在的模型。
3.2.1 建立PropertyBag实例
接口方法:
3.2.2 新建PersistenceUnit实例
3.2.3 Example (新建一个PersistenceUnit 实例)
public SCAPI.PersistenceUnit CreateNewModel()
{
SCAPI.PersistenceUnits scPersistenceUnitCol;
scPersistenceUnitCol = DmApp.PersistenceUnits;
SCAPI.PropertyBag bag = new PropertyBag();
//bag.Add("Name","TestModel");
bag.Add("Model Type", 1);
return scPersistenceUnitCol.Create(bag, null);
}
3.3 打开已经存在的模型文件
3.3.1 PersistenceUnitCollection 接口
l Example (打开模型文件)
public SCAPI.PersistenceUnit OpenModel(string fileName)
{
SCAPI.PersistenceUnits scPersistenceUnitCol;
scPersistenceUnitCol = DmApp.PersistenceUnits;
return scPersistenceUnitCol.Add(fileName, "RDO=Yes");
}
3.3.2 打开一个Session
l SessionCollection 接口
l Session 接口
l Sample
scPUnit = OpenModel("E://DW-Home//0400逻辑数据模型//详细设计//SPDB-EDW-LDM.ER1");
scSession.Open(scPUnit, SC_SessionLevel.SCD_SL_M0, SC_SessionFlags.SCD_SF_NONE);
if (scSession.IsOpen())
{
…
}
3. 4 存取模型对象
l Session接口
l ModelObjectCollection接口
l 模型对象接口
ClassName包含Entity,Property,RelationShip
l Example(返回模型中全部对象的ObjectId,Name,ClassName)
public List<string> GetAllModelObjectName()
{
List<string> result = new List<string>();
SCAPI.PersistenceUnit scPUnit;
SCAPI.ModelObjects scMObjects;
// new session
SCAPI.Sessions scSessionCol = DmApp.Sessions;
SCAPI.Session scSession = scSessionCol.Add();
{
scPUnit = OpenModel("E://DW-Home//0400逻辑数据模型//详细设计//SPDB-EDW-LDM.ER1");
scSession.Open(scPUnit, SC_SessionLevel.SCD_SL_M0, SC_SessionFlags.SCD_SF_NONE);
if (scSession.IsOpen())
{
scMObjects = scSession.ModelObjects;
foreach (ModelObject scObj in scMObjects)
{
result.Add(scObj.ObjectId + "," + scObj.Name + "," + scObj.ClassName);
}
}
scSession = null;
return result;
}
}
3. 5 存取指定的模型对象
l ModelObjectCollection接口
l Sample (根据ObjectId查找模型对象)
ObjectId = "{A2713E92-40F7-48CC-8BC7-6EF3EB74975B}+00000000";
scMObjects = scSession.ModelObjects.Collect(ObjectId,
null, null, null, null);
3. 6 筛选对象集合
可以通过ModelObjectCollection的Collect方法建立模型对象的子集。该方法包含的五个参数都是可选项的。
l Sample 1(按ClassName过滤)
scMObjects = scSession.ModelObjects.Collect(scSession.ModelObjects.Root,
“Entity”, 1 , null, null);
l Sample 2(按ClassId过滤)
scMObjects = scSession.ModelObjects.Collect(scSession.ModelObjects.Root,
“{A2713E92-40F7-48CC-8BC7-6EF3EB74975B}+00000000”, null, null, null);
3.7 存取对象的属性
3.7.1 属性遍历
l ModelObject 接口
l ModelPropertyCollection 接口
l ModelProperty
l Sample
public List<string> IterateObjectProperties(string objName)
{
ModelProperties scObjProperties;
List<string> result = new List<string>();
SCAPI.PersistenceUnit scPUnit;
// new session
SCAPI.Sessions scSessionCol = DmApp.Sessions;
SCAPI.Session scSession = scSessionCol.Add();
scPUnit = OpenModel("E://DW-Home//0400逻辑数据模型//详细设计//SPDB-EDW-LDM.ER1");
scSession.Open(scPUnit, SC_SessionLevel.SCD_SL_M0, SC_SessionFlags.SCD_SF_NONE);
ModelObject scObj;
if (scSession.IsOpen())
{
if (null==objName )
scObj = scSession.ModelObjects.Root;
else
scObj = GetModelObject(scSession, objName);
if (scObj != null){
scObjProperties = scObj.Properties;
result.Add(scObj.Name + ":" + scObj.ClassName);
foreach (ModelProperty scObjProperty in scObjProperties)
{
if (scObjProperty.Count <= 1)
{
result.Add("-->" + scObjProperty.ClassName + "(" + scObjProperty.FormatAsString() + ")");
}
else
{
result.Add("-->" + scObjProperty.ClassName + "[" + scObjProperty.Count + "]");
for (int i = 0; i < scObjProperty.Count; i++)
{
result.Add("---->" + scObjProperty.get_Value(i, SCAPI.SC_ValueTypes.SCVT_BSTR));
}
}
}
}
}
return result;
}
3.8 存取向量与非向量属性值
l ModelProperty 接口
3.9 存取指定的属性
l PropertyValueCollection 接口
l Example
String propName= “Note”;
ModelProperty prop=scObj. Properties[propName];
Console.WiretLine(“Note=”+ prop. FormatAsString());
3.10 过滤属性
l ModelObject 接口
l Sample
//返回全部向量属性
ModelPropertyCollection scMpc = scObj.CollectProperties("", SC_ModelPropertyFlags.SCD_MPF_SCALAR, null);
//返回全部非向量属性
ModelPropertyCollection scMpc = scObj.CollectProperties("", ,null,SC_ModelPropertyFlags.SCD_MPF_SCALAR);
3.11使用Session修改模型
l Session 接口
l Sample
scTranId = scSession.BeginTransaction();
…
scSession.CommitTransaction(scTranId);
3.12 创建模型对象
l ModelObjectCollection
l Sample
// 在根节点上创建一个实体
scTranId = scSession.BeginTransaction();
scSession.ModelObjects.Add("Entity", null);
scSession.CommitTransaction(scTranId);
3.13 设置属性值
l ModelProperty
scObjProperty.set_Value(0,SC_ValueTypes.SCVT_BLOB,true)
3.14 删除对象
l ModelObjectCollection
bSucess= scObjCol.Remove(objId);
3.15 除属性和属性值
l ModelPropertyCollection接口
l ModelProperty 接口
3.16 删除模型
l PersistenceUnit
3.17 存取元模型信息
l Session 接口
l Example
scBag = scApp.ApplicationEnvironment.PropertyBag(“Application”,” Metadata Version”);
Session scSession= scApp.Sessions.Add();
scSession.Add(scBag,SC_SessionLevel.SCD_SL_M1);
3.18 关闭API
l Session 接口
l SessionCollection
l Example
Foreach(scSession in scSessions){
scSession.Close();
}
While(scSessions.Count>){
scSessions.Remove(0);
}
3.19 清除PersistenceUnits
l PersistenceUnitCollection 接口
如何读取实体的主键?
Entity->Primary Key->全部[Key Group Member Column]= 主建列的ObjectID