Hi all
I need to define an embedding relationship between ClassA and ClassB that has a multiplicity 0..3, so that ClassA cannot contain more than 3 ClassB instances.
It seems like this is not possible (I am using the June CTP). I can only select the predefined multiplicities, but not define one myself.
Is this so, or is there a way to define other multiplicities
I could solve the problem by setting up a rule restraining the number of ClassB instances to 3, but this doesn’t seem like an optimal solution since the multiplicity would not be visible when studying the domain model.
Thanks,
Louise

Defining custom multiplicities
arogan
As I understand the Custom Accept feature it is used in the designer to prevent the user from dropping a connector (or port, shape, etc.) where it the relationship cannot be made. It affects the cursor and the click behaviour.
Is that also called if you create the relationship programatically
If, for example, I wrote code:
MyCar.Wheels.Add(FifthWheel);
would the accept method be called .. and when it returned false could I capture that as an exception around the line of code above
Perpetually curious...
Brian.
Yinon
The net result is that the user will be prevented from breaking the constraint.
Be aware of unintended consequences: There may be circumstances where the user needs to change the model, and the easiest way to make the change is to break the constraint temporarily. If you make that impossible you might annoy your users.
Brian.
zhengh
No it's only called by the toolbox. If you use the API as described, you're circumventing it.
However, you can drive the connection builder from the API and do all your connecting that way.
DevboyX
Hi Louise,
Best way is to set the multiplicity to 0..* in the DSL Definition file, then write a validation constraint. Details of how to do that are in the Samples in
C:\Program Files\Visual Studio 2005 SDK\2006.06\VisualStudioIntegration\Samples\DSLTools\
So if you want (pardon my drawing in typescript):
[Car].----wheels-1..4{CarHasWheels}1..1-car-----[Wheel]
then you'd add a validation constraint to Car that would say something like
if (this.wheels.Count>0)
{
context.LogError("Too many wheels on your car", ...
}
Have a look at the validation doc in the above directory in your VS SDK installation, or at the VS SDK help section.
The result is that your users can make as many links as they like while drawing, but get the error report when saving the file.
SogetiScott
The way I like to think of connection builders (which are a bit abstract) is that they're a single code place implementing the logic of some sort of linking UI gesture.
So in our generated designers, they're only used to provide the logic behind a connector tool.
However, you might imagine a popup dialog that ends up making a link and has two lists of items for source and target. You'd also use a connection builder to populate those lists and implement the eventual link.
rball
Yes, a "hard" constraint is an option, though I'd second Brian's caution - in general we prefer the soft option.
However, there's an even nicer way to do the hard constraint.
Find the connection builder that creates the connection: with the DSL Definition open, look in DSL Explorer under Connection Builders, and you'll see a connection builder for each connector. (A basic connection builder is automatically generated when you create a connector.) Open (i.e. click the [+]) the appropriate one; open Link Connect Directives; and select the link directive.
In the DSL Details window (if you can't find it, use the VS View/Other Windows menu), click Custom Accept for either the source or the target role - whichever you want to restrict. Click in the diagram and save the DSL Definition.
Regenerate the code with the Transform All Templates button in the header of the solution explorer. Build your solution. This will result in an error - you need to supply a routine that either accepts or rejects the connection the user is trying to make. Double-click on the error message, and you'll see a comment in the generated code telling you what you need to provide.
Put your code in a separate file in the DSL project. It will look something like this:
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;
namespace Company.Project.Language
{
public static partial class ConnectAssociationClassRelationship
{ private static bool CanAcceptSomeClassAsSource(SomeClass candidate)
{
return candidate.PropertyForThisRelationship.Count<=3;
}
}