Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

XMI: Using C# to Capture UML Models : Page 2

UML models of the future will use XMI as a method of interchanging model data. And because XMI uses XML, suddenly the world is our oyster: In particular, you can extract model data and save it to a data store. Find out how to dissect data from your models using C#.


advertisement
Example 2: Adding Stereotypes
The second example refactors the first example to capture the server names and the "pc server" stereotype, which is the UML method for determining that each node is a server. Figure 3 gives the deployment diagram of the servers with the stereotypes added.

Figure 3. Stereotypes Added: Each stereotype represents the type of node whether it is a PC server, PC client or some other designation for describing the node.

My first step in modifying the prior code to recognize the new stereotypes is to manipulate the AddNode() method.



private void AddNode(XmlTextReader p_readXMI) { string nodeID; string nodeName; string stereotypeName; nodeID = p_readXMI.GetAttribute("name"); nodeName = p_readXMI.GetAttribute("xmi.id"); Console.Write(nodeID); Console.Write(" -> "); Console.WriteLine(nodeName); while (p_readXMI.Read() && (p_readXMI.NodeType == XmlNodeType.Element || p_readXMI.NodeType == XmlNodeType.Whitespace)) { switch (p_readXMI.LocalName) { case "ModelElement.stereotype": while (p_readXMI.Read() && (p_readXMI.NodeType == XmlNodeType.Element || p_readXMI.NodeType == XmlNodeType.Whitespace)) { if (p_readXMI.LocalName == "Stereotype") { stereotypeName = p_readXMI.GetAttribute("name"); Console.WriteLine("Stereotype = " + stereotypeName); } } break; } } }

Of particular note is an idiom that I used to search for XML elements that are inside other elements. I added code to check for either elements or white spaces (in XML documents these are spaces) to the p_readXMI.Read loop. This idiom only works so long as each element node is handled at the proper level of encapsulation in the XML document.

Encapsulation of elements is not handled very well in this way. For example, using the idiom code with the following UML will produce undesirable results.

<UML:Node name="Leasing"> <UML:TagName name="Test"> </UML:Node name="SmallLeasing"> </UML:TagName> </UML:Node>

Figure 4. Output for Example 2: Each stereotype is listed after the node name/id that was in Example 1.

The first element is processed as a node, the second element is ignored and breaks the original while loop, and the third element is processed as if it was a normal node at the same level as the first node. To fix this problem, check for the ending tags of elements using XMLNodetype.Endelement. For the purposes of this article, this isn't a problem and saves the extra coding steps required in checking for ending elements.

Stereotypes in XMI are encapsulated within the Node element. The code for this example checks for an element with the name "ModelElement.Stereotype" and, using the same idiom that I just discussed, processes an encapsulated element of "stereotype" and prints it out to the screen (see Figure 4). (Listing 2 has the complete source code for Example 2.)



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap