Associating models
Here we explain how to manipulate the relationships between models.
Adding relationships
Relationships between models can be set using the Relate
method of the IModel
object.
This method can only add reference relationships. Ownership relationships cannot be added using this method, as they must be added at the same time as the child element model. For information on adding ownership relationships and child element models, see Adding a model.
public void RelateModels(ICommandContext c, ICommandParams p)
{
//Create a model in the project
var ucModel = c.App.Workspace.CurrentProject.AddNewRootModel("UseCaseModel");
//Create a model that will be the related end
IModel usecase = ucModel.AddNewModel("UseCases", "UseCase");
usecase.SetField("Name", "Following");
IModel actor = ucModel.AddNewModel("Actors", "Actor");
actor.SetField("Name", "Driver");
//Relate the related end model
usecase.Relate("MainActor", actor);
}
Getting relatedness
A collection of related objects (IRelationship
) can be obtained using the GetRelations
method of the IModel
object. This method can be used to obtain both owned and referenced relationships.
public void GetRelations(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//Get the associations
foreach ( IRelationship useCaseRel in model.GetRelations("UseCases") )
{
//Get the name of the target model of the association
var targetName = useCaseRel.Target?.Name;
//Write to output
c.App.Output.WriteLine("sample", $"Name: {targetName}");
}
}
Delete Association
To delete an association between models, use the UnRelate
method of the IModel
object.
This method can only delete reference associations. Ownership associations cannot be deleted with this method because they must be deleted at the same time as the child element models. For information on deleting owned relationships and child element models, see Deleting a model.
public void UnRelate(ICommandContext c, ICommandParams p)
{
//Get the relationship end of the relationship to be deleted
//When retrieving a model by specifying the model path
IModel useCase = c.App.Workspace.CurrentProject.GetModelByPath("Sample/UseCaseModel/Follow-up");
IModel actor = c.App.Workspace.CurrentProject.GetModelByPath("Sample/UseCaseModel/Driver");
//Delete the relationship in the "MainActor" field
useCase.UnRelate("MainActor", actor);
}
You can also delete all reference relationships between specified models using the UnRelateAll
method of the IModel
object.
public void UnRelateAll(ICommandContext c, ICommandParams p)
{
//Get the related end of the relationship to be deleted
//When retrieving a model by specifying the model path
IModel useCase = c.App.Workspace.CurrentProject.GetModelByPath("Sample/UseCaseModel/Follow-up");
IModel actor = c.App.Workspace.CurrentProject.GetModelByPath("Sample/UseCaseModel/Driver");
//Delete all reference relationships connecting the specified models
useCase.UnRelateAll(actor);
}
You can also delete relationships using the UnRelate
method of the IRelationship
object. Note that if you delete during loop processing such as foreach
, the contents of the collection may change during processing, causing an exception. We recommend that you finalize the collection with the ToList
method.
public void UnRelate(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//Get the relationships
IRelationshipCollection relationships = model.GetRelations("MainActors");
foreach ( IRelationship transition in relationships.ToList() )
{
//This is an example of deleting the relationship if the name of the related model is `SomeName`
if ( transition.Target.Name == "SomeName" )
{
transition.UnRelate();
}
}
}
Changing the relationship
The related model can be changed using the Relate
method of the IRelationship
object.
This method can only be used to change reference relationships. The related end of an owned relationship cannot be added using this method.
The following changes are possible.
- Change both the source and destination
- Leave the source as is and change only the destination
- Leave the destination as is and change only the source
Below is an example of changing the destination.
public void ChangeTarget(ICommandContext c, ICommandParams p)
{
//Get the source of the relationship to be changed
IModel sourceModel = c.App.Workspace.CurrentModel;
//Get the new destination model
IModel newTargetModel = c.App.Workspace.CurrentProject.GetModelByPath("Sample/UseCaseModel/Forward Monitoring Sensor");
//Get the relationship
IRelationshipCollection relationships = sourceModel.GetRelations("MainActors");
foreach ( IRelationship transition in relationships )
{
//If the destination model is named `Driver`, this is an example of changing the destination to `Forward Monitoring Sensor`
//Specify the same model as the current source for the source
if ( transition.Target.Name == "Driver" )
{
transition.Relate(sourceModel, newTargetModel, -1, -1);
}
}
}
The third and fourth arguments of the Relate
method of the IRelationship
object specify the new source position and new destination position, respectively.
New source position (sourceIndex):
This is the position where the destination model will hold the source model as a field value. Please specify an index with 0 as the first position. If you specify an index outside the range of the number of elements of the corresponding field value (-1, a value larger than the number of elements of the field value, etc.), it will be added to the end.
New destination position (targetIndex):
This is the position where the source model will hold the destination model as a field value. Please specify an index with 0 as the first position. If you specify an index outside the range of the number of elements of the corresponding field value (-1, a value larger than the number of elements of the field value, etc.), it will be added to the end.