Defining a Hierarchy

Top  Previous  Next

Hierarchies are views onto topic maps. They define a root node and also define which association types should be followed to instantiate the hierarchy. The following code example shows how to define a hierarchy and populate it with data.

Important Note: It is not possible to create a hierarchy and hierarchy nodes in the same transaction. The hierarchy must be created in one transaction and then any number of hierarchy nodes in another. The code below follows this pattern.

 

      /// <summary>

      /// Creates and populates a new hierarchy in the topic map

      /// </summary>

      /// <param name="tmUri">The web address of the topic map to be populated</param>

      /// <returns>The web address of the hierarchy created</returns>

      private static Uri CreateHierarchy(Uri tmUri)

       {

          // create a topic map service client and retrieve the specified topic map

          var client = new TopicMapServiceClient();

          var tm = client.GetTopicMap(tmUri);

 

          // Look up previously created topics for creating the hierarchy definition

          var manages = tm.GetTopicBySubjectIdentifier(new Uri(SchemaConstants.Manages));

          var manager = tm.GetTopicBySubjectIdentifier(new Uri(SchemaConstants.Manager));

          var minion = tm.GetTopicBySubjectIdentifier(new Uri(SchemaConstants.Minion));

          var gra = tm.GetTopicBySubjectIdentifier(new Uri("http://www.networkedplanet.com/people/gra"));

          // Create a new topic to represent the hierarchy

          var managementHierarchyTopic = tm.CreateTopic("Management Hierarchy", "en", new Uri("http://www.networkedplanet.com/hierarchies/management"));

          // Use the hierarchy topic to create the hierarchy definition

           tm.CreateHierarchyDefinition(managementHierarchyTopic.WebAddress, manages.WebAddress, manager.WebAddress, minion.WebAddress, gra.WebAddress);

           tm.SaveChanges();

 

           tm = client.GetTopicMap(tmUri);

          // get hierarchies

          var hierarchies = tm.GetHierarchies();

 

          // add two children to root

          var p1 = tm.CreateTopic("p1", "en", new Uri("http://www.networkedplanet.com/people/p1"));

           p1.AddType(new Uri(SchemaConstants.Person));

          var p2 = tm.CreateTopic("p2", "en", new Uri("http://www.networkedplanet.com/people/p2"));

           p2.AddType(new Uri(SchemaConstants.Person));

 

          var managementHierarchy = hierarchies.First();

           managementHierarchy.CreateHierarchyNode(gra.TopicReference, p1.TopicReference);

           managementHierarchy.CreateHierarchyNode(gra.TopicReference, p2.TopicReference);

           tm.SaveChanges();

          return managementHierarchy.WebAddress;

       }