Code generation from UML Classdiagram

hi,

what influence have associations in Classdiagrams to the later generated Code?

On the “Generalization” I see the differens, the class is in the Code respectively derived.

But…
if I for example connect two classes per assoziation or aggregation, the code looks the same, as if I generated Code from classes without any connection.

I would expect a differens.

May there be a problem with my program or is that correct?

I use “Rational Rose”.

thanks for any answers

I assume you’re asking for the difference between association and aggregation.

Association can be seen as “object A has object B.” Typically, this would be in the form of a member in code. This specification is quite simple and can sometimes be too vague, which is why we also have aggregation and composition.

Aggregation can be seen as “a collection of class B forms object A.” For example, a group of employees make up certain department. Keep in mind this is different from composition where the association here is stronger. For example, I see composition as “a collection of class B forms object A and needs A to stay alive.” For example, going back to the department/employee example, we can have departments composing a store. For me, the easy way to see this is if the store was destroyed, then its departments are destroyed along with it whereas if the department is destroyed, the employees remain (assuming they don’t get immediately fired but even then, we would have a record of the employee so in terms of software data, the employee remains).

I hope this helps. I also found this web page that may explain it better than I have: http://ootips.org/uml-hasa.html

I guess I didn’t explain my question very well^^, a next try…

There is a function in the program called: “generate code”. What does this function do??

In my example I have two classes A and B and they are connected via Association.

The generated Code:

//header file
class ClassA
{
public:
operationA();
private:
attributeA;
};

//the .cpp-file
#include "ClassA.h"
ClassA::operationA()
{
}

//the header file
class ClassB
{
public:
operationB();
private:
attributeB;
};

// the .cpp-file
#include "ClassB.h"
ClassB::operationB()
{
}

I see the connection in the UML-diagram, but I don’t see it in this Code.

I don’t quite follow. Is this a product-specific question rather than a UML question?

it is a UML question, this was just a random example.

What is the use of UML, only the documentation?

There is the possibility to automatically generate Code from the UML class diagram.

In the example above the Code is not written myself, I generated it automatically.

I want to know how this code generation works.

How are the connections in the code visible? Or does it only write down the class name, operations and attributes and that’s it?

UML is a modelling language. Code generation is possible given certain contexts.

From what I gathered, I believe your question really is product-specific (and not UML-specific), in which case it is beyond the scope of my knowledge.

To hopefully clarify for anyone wishing to jump in, I believe the poster is asking why the associations shown in a diagram is not appearing in generated code.

[quote=Anonymous]I guess I didn’t explain my question very well^^, a next try…

There is a function in the program called: “generate code”. What does this function do??

In my example I have two classes A and B and they are connected via Association.

The generated Code:

//header file
class ClassA
{
public:
operationA();
private:
attributeA;
};

//the .cpp-file
#include "ClassA.h"
ClassA::operationA()
{
}

//the header file
class ClassB
{
public:
operationB();
private:
attributeB;
};

// the .cpp-file
#include "ClassB.h"
ClassB::operationB()
{
}

I see the connection in the UML-diagram, but I don’t see it in this Code.
[/quote]
Could you please send me your project file for investigation? Thanks in advance!

Best regards,
Lilian Wong

I don’t have any project, I am just interested in UML. I may get a UML-project in two weeks.

What do I draw the line (association, aggreagation or composition) between two classes for? Only for the documentation of the project?

Like I said, UML is a modelling language. You draw lines to illustrate relationships between entities in your domain. The model is indeed documenting a software project.

You can use models to create interfaces for your software project but in the end, it is usually up to the developer to implement the code for the project.

Moderator - please delete my previous reply, in favour of this one.

Regards,

I have just found an excellent .pdf entitled Mapping the UML2 Semantics of Associations
to a Java Code Generation Model.

It is a great read, and it contains the following quote:

Association classes, multiplicities, aggregation and composition are not
correctly or not at all processed by most code generators. One reason
may be that the UML semantics is not formally defined in the UML
specification.

The same document attempts to define working transformations of UML objects, that may form best practice.

I’ve only just seen this thread but, I understand the problem.

I have an excellent book on UML, and it provides codes samples of how UML is transformed into code, in several languages.

I find that in Java and C#, the generate code for aggregation and composition associations to be the same.

I am still learning UML and Visual Paradigm (product-specific, but the same in Visual Studio), and I find that my composition associations don’t generate code. It may be that I need to apply a design pattern to the association, like a factory pattern, to make the association generate classes.

Certainly, there are design patterns that replicate the function of a composition relationship, when it comes to create class instances.

It may also be the case that a composition association has other uses, that prohibit it being coded to just one function.

So, the question remains unanswered.

How does a VP UML analyst ensure that information described in a composition association is transformed, when forward-engineering a VP Class diagram?

Is it something that is exposed in the output, but not implemented as code? Do I have to edit some file, to tell VP what to do with the association?

I understand that, in Microsoft Visual Studio .Net, one has to edit a UML Profile.XML file, to define what to do with each piece of info.

I can’t find the equivalent for VP.

Please advise.