Udostępnij za pośrednictwem


Porady: nawigowanie po relacjach z API UML

W Visual Studio Ultimate, wzór składa się z elementów połączonych ze sobą przez różne rodzaje relacji.W tym temacie opisano, jak przejść modelu w kodzie programu.

Przechodzenie przez relacje

Wszelkie relacje

Użyj GetRelatedElements<T>() , aby znaleźć wszystkie elementy podłączone do określonego elementu.Albo zestaw T do IRelationship do przechodzenia relacje wszelkiego rodzaju lub użyć bardziej określonego typu, takich jak IAssociation na przechodzenie przez właśnie tego typu.

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

Użyj GetRelatedLinks<T>() Aby znaleźć wszystkie relacje połączony element.

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

Skojarzenia

Skojarzenie jest relacją między dwie właściwości, z których każdy należy do klasyfikatora.

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));
}

 Generalizacja i realizacji

Generalizacja kończy się przeciwny dostępu:

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);
 

Zależność

/// 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);
 

Działalność krawędzi

/// 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);
 

Łącznik (montaż i delegacji)

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

Wiadomości i linie życia

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
 

Importowanie pakietu

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

Przypadek użycia rozszerzanie i obejmują

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

 Wyliczanie relacje

Wszystkie właściwości modelu UML, które zwracają wiele wartości, które są zgodne z < > IEnumerable interfejs.Oznacza to, że można używać Linq wyrażenia w kwerendzie i metody rozszerzenie określone w System.Linq obszaru nazw.

Na przykład:

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

Zobacz też

Koncepcje

Rozszerzanie diagramów i modeli UML

Porady: nawigowanie po modelu UML