Creating a Schema

Top  Previous  Next

The first activity in any Web3 development is to create a schema. Calling CreateSchema on the SchemaServiceClient creates a new local schema object. Nothing is sent to the server until SchemaServiceClient.SaveAsNew() is called. When SaveAsNew() is called the Schema object’s WebAddress property is updated to be the web address (URI) of the schema resource.

The following code example shows the creation of schema which contains a number of topic types, occurrence types and association templates. The comments inline describe in detail what is happening.

 

      /// <summary>

      /// Creates a new schema and returns its web address

      /// </summary>

      /// <returns>The URI address of the new schema</returns>

      private Uri CreateSchema()

       {

          // create a new schema client.

          var schemaClient = new SchemaServiceClient(_baseUri);

 

          // create a new schema. Note this only creates a local object.

          // it's not until the schema is first saved that it is sent to the server

          // The method takes a label for the schema, the schema subject identifier

          // the schema version and the schema status.

          var schema = schemaClient.CreateSchema("Business Schema", "http://www.networkedplanet.com/ontology/business-schema", "1.0", "draft");

 

          // Now create all the types and schema constraints

 

          // First define the occurrence types

          var age = schema.AddOccurrenceType("age", "en", new Uri(XmlSchemaDatatypes.Integer), SchemaConstants.Age);

 

          var weight = schema.AddOccurrenceType("weight", "en", new Uri(XmlSchemaDatatypes.Decimal), SchemaConstants.Weight);

 

          var description = schema.AddOccurrenceType("description", "en", new Uri(XmlSchemaDatatypes.String),

                                                      SchemaConstants.Description);

 

          // define all the types and express the constraints between the types

          // and the occurrence types

 

          // define constraints that express that topics of type person should have exactly 1 age and may have at most 1 description

          var person = schema.AddTopicType("person", "en", SchemaConstants.Person);

           person.AddOccurrenceConstraint(description, 0, 1);

           person.AddOccurrenceConstraint(age, 1, 1);

 

          // define constraints that express that topics of type product should have exactly 1 weight and may have at most 1 description

          var product = schema.AddTopicType("product", "en", SchemaConstants.Product);

           product.AddOccurrenceConstraint(description, 0, 1);

           product.AddOccurrenceConstraint(weight, 0, 1);

 

          // define constraints that express that topics of type company may have at most 1 description

          var company = schema.AddTopicType("company", "en", SchemaConstants.Company);

           company.AddOccurrenceConstraint(description, 0, 1);

 

          // Association templates define the way in which topics of certain types can be connected together.

 

          // Define a binary association template called 'worksfor'. That relates topics of type person and

          // company in the roles of employee and employer.

          var worksfor = schema.AddAssociationType("worksfor", "en", SchemaConstants.WorksFor);

          var employee = schema.AddRoleType("employee", "en", SchemaConstants.Employee);

          var employer = schema.AddRoleType("employer", "en", SchemaConstants.Employer);

           schema.CreateAssociationTemplate(worksfor, employee, person, employer, company);

 

          // Define an association template where the same topic type plays roles on either side.

          // Instances of the Person topic type play the role of manager and minion in the association manages.

          var manages = schema.AddAssociationType("manages", "en", SchemaConstants.Manages);

          var manager = schema.AddRoleType("manager", "en", SchemaConstants.Manager);

          var minion = schema.AddRoleType("minion", "en", SchemaConstants.Minion);

           schema.CreateAssociationTemplate(manages, manager, person, minion, person);

 

          // Define a reflexive association template. This is one where their are always exactly two roles

          // and the role type is the same for both. The constraining topic type is of course the same both both

          // roles. This template defines friendOf, where instances of the topic type person play the role of

          // friend.

          var friendOf = schema.AddAssociationType("friendOf", "en", SchemaConstants.FriendOf);

          var friend = schema.AddRoleType("friend", "en", SchemaConstants.Friend);

           schema.CreateReflexiveAssociationTemplate(friendOf, friend, person);

 

          // save the schema and return the web address

           schemaClient.SaveAsNew(schema);

          return schema.WebAddress;

       }