运行模型到模型映射变换将生成一个临时 in-memory 模型。如果此模型的内容与文件系统中现有目标模型的内容不同,那么在执行变换后处理期间就会合并这两个模型。缺省合并过程是模型到模型变换框架中提供的一项变换规则,它会将现有目标模型装入内存,并使用结构化合并功能将此模型与生成的 in-memory 模型组合在一起。通过结构化合并获得的模型将替换现有目标模型。
模型中通常包含对其他模型中的对象的引用。例如,UML 模型中可能包含对 UML 概要文件中定义的构造型的引用,它也可能包含对 UML 库中定义的基本类型的引用。仅当这些跨模型引用将引用同一对象的同一 in-memory 实例时,结构化合并功能才会检测它们是否引用了该对象。
例如,假定已将 «Perspective» 构造型(它是在名为 Default 的 UML 概要文件中定义的)应用于已生成的 in-memory 模型中的 ExamplePackage 包,并且还应用于现有目标模型中的同一个包。将现有目标模型装入到用于结构化合并功能的内存之后,就解析了对 «Perspective» 构造型的引用。如果结构化合并功能未标识所生成的 in-memory 模型引用的内存中构造型对象的完全相同实例,那么结构化合并功能将无法检测是否对两个版本的 ExamplePackage 包都应用了同一构造型。缺省情况下,如果结构化合并功能检测到由要合并的模型装入并引用的同一模型的重复实例(例如,此示例中的 UML Default 概要文件),那么结构化合并功能就会停止。
为了避免存在同一模型的重复 in-memory 实例,应确保包含 in-memory 模型的资源集只具有每个模型的一个实例。
这些规则位于 com.ibm.xtools.transform.authoring 包中。有关这些规则的更多信息,请参阅 Rational Transformation Authoring Developer Guide API Reference。此样本中的变换同时使用了这些规则。探究已生成的变换源代码以了解它们在您可以定制的变换源代码的各个部分中的实例化。
下列代码段说明了如何使用 UMLDefaultLibrariesAddRule 规则。
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
private void addTransformElements(Registry registry) {
// You may add more transform element before the generated ones here
// Remember to remove the @generated tag or add NOT to it
addGeneratedTransformElements(registry);
// You may add more transform element after the generated ones here
// Remember to remove the @generated tag or add NOT to it
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated NOT
*/
private void addTransformElements(Registry registry) {
add(new UMLDefaultLibrariesAddRule());
// You may add more transform element before the generated ones here
// Remember to remove the @generated tag or add NOT to it
addGeneratedTransformElements(registry);
// You may add more transform element after the generated ones here
// Remember to remove the @generated tag or add NOT to it
}
/**
* Creates a root transformation. You may add more rules to the transformation here
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @param transform The root transformation
* @generated
*/
protected RootTransformation createRootTransformation(ITransformationDescriptor descriptor) {
// Create and return an instance of a class that extends RootTransformation
// and handles source objects of type = (resource) and target objects of type = (resource)
// and remove the @generated tag. The default implementation provided here is
// for source and target objects of type = (resource).
return new RootTransformation(descriptor, new MainTransform()){
protected void addPostProcessingRules() {
add(new UMLProfilesConsistencyCheckRule());
super.addPostProcessingRules();
}};
}
/**
* Creates a root transformation. You may add more rules to the transformation here
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @param transform The root transformation
* @generated NOT
*/
protected RootTransformation createRootTransformation(ITransformationDescriptor descriptor) {
// Create and return an instance of a class that extends RootTransformation
// and handles source objects of type = (resource) and target objects of type = (resource)
// and remove the @generated tag. The default implementation provided here is
// for source and target objects of type = (resource).
return new RootTransformation(descriptor, new MainTransform()){
protected void addPostProcessingRules() {
add(new CrossModelReferenceCheckRule());
add(new UMLProfilesConsistencyCheckRule());
super.addPostProcessingRules();
}};
}