如何:使用 UML API 导航关系

在 Visual Studio 旗舰版中,模型由多个按照不同类型的关系链接在一起的元素组成。 本主题描述如何在程序代码中导航模型。

遍历关系

任何关系

使用 GetRelatedElements<T>() 查找连接到指定元素的所有元素。 将 T 设置为 IRelationship 来遍历所有类型的关系,或者使用更明确的类型(如 IAssociation)仅遍历该指定类型。

IElement anElement;
// Select all elements related to anElement.
Context.CurrentDiagram.SelectShapes (
   anElement.GetRelatedElements<IRelationship>()
    .SelectMany(e=>e.Shapes()).ToArray());

使用 GetRelatedLinks<T>() 查找连接到某个元素的所有关系。

// Process all relationships connected to an element.
foreach (IRelationship relationship in 
   anElement.GetRelatedLinks<IRelationship>())
{
  Debug.Assert(relationship.SourceElement == anElement
      || relationship.TargetElement == anElement);
}

关联

关联是两个属性间的关系,其中每个属性属于一个分类器。

IClassifier classifier; // class, interface, component, actor, ...
// Get all the associations sourced from this classifier
foreach (IProperty p in classifier.GetOutgoingAssociationEnds())
{
  // p represents the end further end of an association.
  IType oppositeElement = p.Type; 
    // The type to which this association connects classifier
  
  IProperty oppositeProperty = p.Opposite;
    // The nearer end of the association.
  Debug.Assert(oppositeProperty.Type == classifier);
  IAssociation association = p.Association;
  Debug.Assert(association.MemberEnds.Contains(p)
     && association.MemberEnds.Contains(oppositeProperty));
}

 泛化和实现

访问泛化的相对端:

foreach (IClassifier supertype in classifier.Generals) {…}
foreach (IClassifier subtype in classifier.GetSpecifics()) {…}
Access the relationship itself:
foreach (IGeneralization gen in classifier.Generalizations) 
{ Debug.Assert(classifier == gen.Specific); }

/// InterfaceRealization:
IEnumerable<IInterface> GetRealizedInterfaces
    (this IBehavioredClassifier classifier);
IEnumerable<IBehavioredClassifier> GetRealizingClassifiers
    (this IInterface interface);
 

依赖项

/// Returns the elements depending on this element
IEnumerable<INamedElement> GetDependencyClients(this INamedElement element); 
/// Returns the elements this element depends on
IEnumerable<INamedElement> INamedElement GetDependencySuppliers(this INamedElement element);
 

活动边缘

/// Returns the nodes targeted by edges outgoing from this one
IEnumerable<IActivityNode> GetActivityEdgeTargets(this IActivityNode node);
/// Returns the nodes sourcing edges incoming to this one
IEnumerable<IActivityNode> GetActivityEdgeSources(this IActivityNode node);
 

连接线(程序集和委托)

/// Returns the elements connected via assembly 
/// or delegation to this one
IEnumerable<IConnectableElement> GetConnectedElements(this IConnectableElement element);
 

消息和生命线

IEnumerable<IMessage> GetAllOutgoingMessages(this ILifeline  lifeline); 
// both from lifeline and execution occurrences
IEnumerable<IMessage> GetAllIncomingMessages(this ILifeline  lifeline);
ILifeline GetSourceLifeline(this IMessage message); 
    // may return null for found messages
ILifeline GetTargetLifeline(this IMessage message);  
    // may return null for lost messages
 

包导入

IEnumerable<IPackage>GetImportedPackages(this INamespace namespace);
IEnumerable<INamespace> GetImportingNamespaces(this IPackage package);
 

用例扩展和包括

IEnumerable<IUseCase>GetExtendedCases(this IUseCase usecase);
IEnumerable<IUseCase>GetExtendingCases(this IUseCase usecase);
IEnumerable<IUseCase>GetIncludedCases(this IUseCase usecase);
IEnumerable<IUseCase>GetIncludingCases(this IUseCase usecase);

 枚举关系

UML 模型的所有返回多个值的属性都符合 IEnumerable<> 接口。 这意味着您可以使用 Linq Query Expressions(LINQ 查询表达式)和 System.Linq 命名空间中定义的扩展方法。

例如:

from shape in     Context.CurrentDiagram.GetSelectedShapes<IClassifier>()
where shape.Color == System.Drawing.Color.Red
select shape.Element

请参见

概念

扩展 UML 模型和关系图

如何:导航 UML 模型