Does OCL work with composite structure diagrams? - uml

Is OCL meant to be used in combination with a composite structure diagram?
Or does it not make sense? If it makes sense, could someone give a quick example for a possible OCL constraint, e.g. based on this example diagram source ?

UML specifically mentions such situation (section 7.6. of specs)
Constraints themselves can be attached to any kind of UML Element, or in fact a collection of Elements.
And finally:
7.2.1
The root concepts of Element and Relationship provide the basis for all other modeling concepts in UML.
Which means that you can add constraint to anything.

Related

In UML, does composition imply an association class?

In a UML diagram where one class is comprised by another class, is there an implicit association table, or does it need to be shown?
For example, if I have Chapters that are composition of Paragraphs, is this alone enough:
Or does the association class to be explicitly shown like so:
I've never seen it done like that latter example, so I'm assuming it's implied. Or maybe I haven't normalized the data properly (considering chapter both appears in the Chapter class and the Paragraph class).
Simply, there is no implication. If you need an association class, you need to define it. However, UML is not about diagraming, it's about modeling. You can omit your association class in a diagram if you want to. The association class will still exist in your model, though.
No, it is not implied.
The reason why you haven't seen this is that in most cases it simply doesn't make sense. In the association class the class part additionally describes the properties of the association. In your example you create an artificial object that actually brings little or nothing. This kind of approach can be useful for many-to-many relationships which are impossible for composition (each part can have at most one whole). Even if you wanted to show Foreign Key it would simply be on one of the classes. But that's implied by the association itself. According to UML specification an inline attribute is equivalent to an association.
Moreover on UML you can depict many to many relationship simply but applying appropriate multiplicities on association ends. You may want to show the class depicting that only when modelling on the code level.
No, it is not implied.
UML is not about tables, but about classes. The author of the class diagram should tell the audience how a class should be interpreted. Some UML diagrams model the real world, others model a piece of application functionality and yet others depict a physical implementation.
Apparently, reading your question, you are modeling a relational database, where a class is a physical table. In that case, I would expect that every table is explicitly modeled as a UML class.
The UML standard does not demand this.
By the way, the notation (PK) and (FK) is not in accordance with the UML standard.

Class Diagram and Object Diagram

I am making a structural diagram in UML using classes and objects and wondered if I was doing it correctly.
Would the following (on the right) be the correct way to represent an object diagram of the left-hand side class diagram?
No. You are using a Generalization which is not the case. Either leave that out or use a stereotyped dependency <<instanceOf>>. Note that this is no standard as per UML specs. But those specs leave you the freedom to express is this way.
The class from which the object is instantiated is already shown in the name objectName::className.
From Annex A p. 683 of the UML 2.5 specs, which describes the organization of diagram types:
NOTE. This taxonomy provides a logical organization for the various major kinds of diagrams. However, it does not preclude mixing different kinds of diagram types, as one might do when one combines structural and behavioral elements (e.g., showing a state machine nested inside an internal structure). Consequently, the boundaries between the various kinds of diagram types are not strictly enforced.

Semantic relationship in UML

I found a definition for association in UML as below.
An "association" in UML is defined as a kind of relationship between
classes,which represents the semantic relationship between two or more classes that
involves connections (links) among their instances .
I am not clear what is semantic relationship. Can anyone explain it with example with comparing it with non semantic relationship?
Associations in plain text
An association is a semantic relationship. The UML clause means that there is a structural relationship between instances of the associated classes independently of any specific implementation. "Semantic" underlines that the relationship is between the instances themselves, and not just "accidentally" for an operation:
Use associations primarily where there are structural relationships among objects. Do not use them to show transient relationships such as parameters or local variables of procedures.- Booch, Rumbaugh & Jacobson in Unified Modeling Language User Guide, 2nd edition
More arguments
What is "semantic"?
The term "semantic" is borrowed from linguistics and refers to the meanings behind the words. Linguists and map words (e.g. "Car", "Driver") to their meaning (e.g. a real car, a real person), and analyze the relation between words with a view of this mapping:
So, applying this to UML (modelling language) if you'd have the classes Car and Driver, you CAN model the semantic relationship as an association between the two classes.
What is not semantic?
Not all relationships are of semantic nature. You can have dependencies, which can express a technical relationship:
a transient relationship during an operation: with a factory pattern for example, a DriverFactory would «create» a Driver instance. Both instances the factory and the driver are related only at the exact moment of the creation operation. But the two instances are completely independent the nanosecond after.The same applies if the implementation of an operation needs to create a local instance of another class. Both classes are not associated, since we could imagine another implementation that works without such an instance.
a structural dependency: Maybe an operation require some other classes as parameter. Since the parameters themselves are transient, there is no association. But nevertheless, teh class needs to know about the other class.
For the records, I'm grateful to this public domain contributor for the nice car and driver and to 18f for advice on inclusive communication.
The subject of Semantics is sense. If one thing has something that is connected by sense to another thing, it is the semantic relationship. That definition is terribly wide. And, applied to UML relationship, incorrect. It is incorrect for two reasons.
First, UML covers not only Class-Instance languages, such as Java or C++, but pure object languages with heredity created by Prototypes as well. And this second variant is not covered by your definition at all.
Second, in UML you can have class A connected to class B through some complex AssociationClass, that is shown by a box with arrows, not arrow only. And it still will be named an association and it IS a semantic relationship, too. But a the semantic relationship goes from A to B through two classes, it is still a semantic relationship, but it is not an association in UML.
If you are trying to be deep in subject, better read the UML documentation: "An Association classifies a set of tuples representing links between typed instances." (UML 2.5, pharagraph 11.5.1). Notice: ANY link between two instances can be shown as an association. Maybe the book you are reading is wholely not so bad, but in the very place that you have cited the author merely tries to use pretty words not understanding their meaning and not even trying to be understandable to readers and to be CORRECT.
A model says something about the things being modeled. This is its semantics. Almost all elements of UML have semantics, defined in the sections titled "semantics". One exception is the comment. Adding a comment to a model doesn't influence its meaning.
Then why does the UML say this explicitely about associations? The reason is, that an association may or may not say something about the structure. If it doesn't, it is purely semantic.
For example it could tell us, that a Car can have at most one Driver. It doesn't necessarily mean that the car has an attribute of type Driver.
If we want to model that the two associated classes own attributes typed by the other end of the association, the notation will show this by small dots on its ends. Many people are not familiar with this notation and interpret associations without a dot in the same way. However, without the dots attributes are owned by the association itself and the structure of the classes is not influenced.
By the way, a class being the type of a parameter of an operation or having a dependency also means a semantic relationship.

Enterprise Architect - Difference between Stereotype and Type

I am using the Enterprise Architect and I saw in the properties of an element (> Related > Links) that there are displayed two meta informations for one element ("Element stereotype" and the other one for "Type"). Can someone explain me the difference between these two meta informations?
Thank you!
When EA refers to "Type" in this context, it approximates to a UML Metaclass, i.e. a kind of element defined by the UML specification (e.g. Class, Interface, Activity). A Stereotype extends a UML metaclass to define a kind of element NOT defined by the UML specification.
UML supports stereotypes, which are an inbuilt mechanism for logically extending or altering the meaning, display, characteristics or syntax of a basic UML model elements. You can apply stereotypes to a range of model element types. (Sparx)
Types are a common name for Classes and Interfaces and Enumerations and other such things if a language has them. Also is used without any exact definition.

UML notation: What is this relationship?

Looking at below UML diagram (decorator pattern), what is the name of relationship between Decorator and component? Is it Association?
The two relationships between Component and Decorator constitute another pattern - Composite.
Decorator is a subtype of Component (rel with triangle)
Decorator aggregates (includes) many Components (rel with diamond)
It allows recursive tree structures to be created. Operations called on the aggregate Decorator are delegated to its contained elements. Hence, in your diagram, the behaviour of Decorator.Operation() is to call Component.Operation() on each of its aggregated Components. Since one or more of thpse could themselves be a Decorator, the call to Operation() propagates through the tree structure.
One is an extension the other one aggregation (that's the one with the small nob at Decorator).
Meaning that a Decorator can contain 0 or more Components.
hth
Mario
It's aggregation. Decorator has many Components.
Aggregation. Easiest way to see the difference is with "to be" and "to have".
An object Component "has a decorator". An object Decorator "is a component".
You are describing an aggregation.
The problem is that this diagram should be reversed from your code or you should get a code from your diagram. Just graphical UML designs are not enough for me.
When I use tools with no code mapping to UML then my modeling is sooner or later horrible !!
I create nice diagrams but except for presentation nobody really use them. I have realized that it was a waste of time for everybody in the team.
I have switched to a professional tool then my project was really used by the team and not only a graphical view used once during a story board.
What surprised me is developers have also corrected my diagrams because at implementation stage what was good in UML was impossible in the code :-)
This is why UML as standalone has no value for me if you use the class diagram. I did this mistake using open source graphical tool but will not do it again !!
extends relationship

Resources