Procedura: esplorare relazioni con l'API UML
In Visual Studio Ultimate un modello è dato da elementi collegati insieme da tipi diversi di relazioni.In questo argomento viene illustrato come spostarsi all'interno del modello nel codice del programma.
Attraversamento di relazioni
Qualsiasi relazione
Utilizzare GetRelatedElements<T>() per trovare tutti gli elementi connessi a un elemento specificato.Impostare T su IRelationship per attraversare relazioni di tutti i tipi oppure utilizzare un tipo più specifico come IAssociation per attraversare solo tale tipo.
IElement anElement;
// Select all elements related to anElement.
Context.CurrentDiagram.SelectShapes (
anElement.GetRelatedElements<IRelationship>()
.SelectMany(e=>e.Shapes()).ToArray());
Utilizzare GetRelatedLinks<T>() per trovare tutte le relazioni collegate a un elemento.
// Process all relationships connected to an element.
foreach (IRelationship relationship in
anElement.GetRelatedLinks<IRelationship>())
{
Debug.Assert(relationship.SourceElement == anElement
|| relationship.TargetElement == anElement);
}
Associazione
Un'associazione è una relazione tra due proprietà, ognuna delle quali appartiene a un classificatore.
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));
}
Generalizzazione e realizzazione
Accedere alle estremità opposte della generalizzazione:
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);
Dipendenza
/// 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);
Activity Edge
/// 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);
Connettore (assembly e delega)
/// Returns the elements connected via assembly
/// or delegation to this one
IEnumerable<IConnectableElement> GetConnectedElements(this IConnectableElement element);
Messaggi e linee di vita
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
Importazione pacchetto
IEnumerable<IPackage>GetImportedPackages(this INamespace namespace);
IEnumerable<INamespace> GetImportingNamespaces(this IPackage package);
Caso di utilizzo di inclusione ed estensione
IEnumerable<IUseCase>GetExtendedCases(this IUseCase usecase);
IEnumerable<IUseCase>GetExtendingCases(this IUseCase usecase);
IEnumerable<IUseCase>GetIncludedCases(this IUseCase usecase);
IEnumerable<IUseCase>GetIncludingCases(this IUseCase usecase);
Enumerazione delle relazioni
Tutte le proprietà del modello UML che restituiscono più valori sono conformi all'interfaccia IEnumerable <>.Ciò significa che è possibile utilizzare le espressioni di query LINQ e i metodi di estensione definiti nello spazio dei nomi System.Linq.
Ad esempio:
from shape in Context.CurrentDiagram.GetSelectedShapes<IClassifier>()
where shape.Color == System.Drawing.Color.Red
select shape.Element