Querying Microsoft.Uml2 for Class Data – Part 1
I’ve decided to write a series of blog posts about working with the Microsoft.Uml2 domain, focusing specifically on the SQL Server Modeling Services Nov 2009 CTP Release 2 bits. It’s my goal to illustrate how to work with the UML domain for some of the most common scenarios we anticipate users to be running against Microsoft.Uml2 (feedback welcome on this, of course). As we’ll see through the series, the UML domain allows users to look at UML data in ways that is very difficult to do in practice (trust me, I know from first-hand experience), especially across multiple UML models.
In this series I’m going to focus on looking at UML data at a level of abstraction that is hopefully intuitive to the widest audience. Specifically, I’m going to be targeting a level of abstraction that mirrors the experience of using a graphical UML tool. The queries in the series will be inspired by what users see on a typical UML tool’s modeling canvas, and the query result sets will be shaped by the data a user sees in a UML diagram. As you might imagine, Microsoft.Uml2 supports much more fine-grained queries than what I’ll be demonstrating, but I think it’s kind of cool to start with examples that every UML user can immediately identify with.
The Basics
For this post we’ll use the following very simple UML Class as the running example:
Right, let’s start with a very simple query that allows us to retrieve three pieces of data about this class – the Id that uniquely identifies the Individual Class in the database, the Id of the folder that Individual lives in, and the ElementKind that specifies that Individual is a UML Class within the database. OK, the place to start is the Microsoft.Uml2.Classes view. Here’s some T-SQL to start with:
1: select C.[Id], C.[Folder], C.[ElementKind], C.[Name]
2: from [Microsoft.Uml2].[Classes] C
If I run the above snippet, I’ll see the data for every entry in the Classes view for which I have the required folder security. To constrain the result set I’m going to add two pieces of data that I know as the author of the UML model – the name of the Class (i.e., “Individual”) and the folder path in which I loaded the UML data (more info on loading UML data can be found here). Here’s the updated code:
1: select C.[Id], C.[Folder], C.[ElementKind], C.[Name]
2: from [Microsoft.Uml2].[Classes] C
3: where C.[Name] = 'Individual' and C.[Folder] = [Repository.Item].[PathsFolder] ('MyModels/FourPlusOne')
The snippet above illustrates the use of the PathsFolder function. If you’re anything like me, you’ll find the ability to specify folder path string in your queries extremely handy. Running the above query against the DB produces the following output:
Sweet. With that covered, time to move on to the attributes of the Individual Class.
Adding Attributes
Per the UML Superstructure specification, the attributes owned by a class are instances of UML Properties. Therefore, to get data related to the “firstName”, “middleName'”, and “lastName” attributes of the Individual Class we need to access the Microsoft.Uml2.Properties view. As you can imagine, there are a number of ways of accomplishing this, but I’ll stick to the “UML tool canvas” paradigm and try to get the data in one query – even if I have to bring back some redundant data. Here’s one way to add the Microsoft.Uml2.Properties view to my existing query:
1: select C.[Id] as ClassName, C.[Folder] as FolderId, C.[ElementKind] as ClassKind, C.[Name] as ClassName,
2: P.[Id] as PropertyId, P.[Name] as PropertyName, P.[Visibility]
3: from [Microsoft.Uml2].[Classes] C
4: inner join [Microsoft.Uml2].[Properties] P on (C.[Id] = P.[Class])
5: where C.[Name] = 'individual' and C.[Folder] = [Repository.Item].[PathsFolder] ('MyModels/FourPlusOne')
As you can see in the snippet above, I’ve added some aliases to enhance the readability of the result set. Running the query gives me the following results:
The result set above show the addition of the unique Id, name, and visibility for each Property of the Individual Class. The data is starting to take shape, but I need one last piece to top it off. To simulate the data presented on a UML tool’s canvas, I need to access the Type of each Property via the Microsoft.Uml2.Types view. A quick update to the T-SQL gives us the following:
1: select C.[Id] as ClassName, C.[Folder] as FolderId, C.[ElementKind] as ClassKind, C.[Name] as ClassName,
2: P.[Id] as PropertyId, P.[Name] as PropertyName, P.[Visibility],
3: T.[Id] as TypeId, T.[Name] as TypeName
4: from [Microsoft.Uml2].[Classes] C
5: inner join [Microsoft.Uml2].[Properties] P on (C.[Id] = P.[Class])
6: inner join [Microsoft.Uml2].[Types] T on (T.[Id] = P.[Type])
7: where C.[Name] = 'individual' and C.[Folder] = [Repository.Item].[PathsFolder] ('MyModels/FourPlusOne')
Which produces the following data when the query is executed:
Excellent! With the addition of the unique Id and name for each Property Type the query very much simulates the data for a UML Class as represented on the drawing canvas of a UML tool.
Next Time
For the next post I’ll finish up the query to get a UML canvas-like view of a UML Class out of the UML domain.
As always, feedback and questions are greatly appreciated.