Creating A Schema

The API allows you to create a new schema from scratch. The schema model is built in memory and can then be written to an XML file or into a topic map stored on a TMCore server using the APIs described in the section called “Saving A Schema”.

To create a new schema, use the static method CreateSchema() in the SchemaUtils class. This method returns a new instance of the ISchema interface.

New types and scoping topics can be added to the schema using the Create... methods of the ISchema interface. Each Create... method requires two identifier strings. The type ID string is an internal identifier that will not be used unless the schema is written as an NPCL XML file, in which case the ID will be written as the id attribute of the NPCL XML element. The subject identifier should be a URI identifier for the type. Each unique type must have a different subject identifier. It is an error to create two types of the same meta-type with the same subject identifier. However, it is allowed to create two types of different meta-types with the same subject identifier.

Constraints can be added by using the Create...() method on the parent type for the constraint (as shown in the containment hierarchy diagram in the previous section). So, to create an Association Role Constraint, use the CreateAssociationRoleConstraint() method on the IAssociationType instance that the constraint applies to. The Create... methods for constraints require you to specify the other types that participate in the constraint and the value of the Minimum and Maximum Cardinality constraints.

The code below shows a simple example of creating a schema from scratch using the API.

Example 12.1. C# Code To Create An NPCL Schema

// Every type will use the same base URI for its subject identifier
string psiBase = "http://www.networkedplanet.com/npcl/tests/";

// Get a new blank schema
ISchema schema = SchemaUtils.CreateSchema();

// Person Topic Type
ITopicType person = schema.CreateTopicType("person", psiBase + "person");
person.DisplayName = "Person";

// Company Topic Type
ITopicType company = schema.CreateTopicType("company", psiBase + "company");
company.DisplayName = "Company";

// Employment Association Type
IAssociationType employment = schema.CreateAssociationType("employment", psiBase + "employment");
employment.DisplayName = "Employment";

// Employer Role Type
IRoleType employer = schema.CreateRoleType("employer", psiBase + "employer");
employer.DisplayName = "Employer";

// Employee Role Type
IRoleType employee = schema.CreateRoleType("employee", psiBase + "employee");
employee.DisplayName = "Employee";

// Employment association allows exactly 1 Employer role
employment.CreateAssociationRoleConstraint(employer, 1, 1, "Employs");
// Employment association allows exactly 1 Employee role
employment.CreateAssociationRoleConstraint(employee, 1, 1, "Works For");

// A Person can play the role of Employee 0 or more times
person.CreateRolePlayerConstraint(employee, employment, 0, Cardinality.Unbounded);

// A Company can play the role of Employer 0 or more times
company.CreateRolePlayerConstraint(employer, employment, 0, Cardinality.Unbounded);

// Age Occurrence Type allows integer values between 0 and 120
IOccurrenceType age = schema.CreateOccurrenceType("age", psiBase + "age");
age.DatatypeConstraint = "http://www.w3.org/2001/XMLSchema#int";
age.MinValueConstraint = "0";
age.MaxValueConstraint = "120";

// Person Topic Type can have 0 or 1 Age occurrences
person.CreateOccurrenceConstraint(age, 0, 1);

// Language Topic Type
ITopicType languageTt = schema.CreateTopicType("language", psiBase + "language");
languageTt.DisplayName = "Language";
// Language topics can be used to scope Names or Occurrences (but not Associations)
languageTt.ScopingFacet = Scoping.Name | Scoping.Occurrence;

// Secret Scoping Topic
IScopingTopic secret = schema.CreateScopingTopic("secret", psiBase + "secret");

// Entity Abstract Type
IAbstractType entity = schema.CreateAbstractType("entity", psiBase + "entity");
// Legal Entity Abstract TYpe
IAbstractType legalEntity = schema.CreateAbstractType("legal-entity", psiBase + "legal-entity");

// Create A Class Hierarchy with Entity as the root.
entity.AddDirectSubclass(legalEntity);
legalEntity.AddDirectSubclass(person);
legalEntity.AddDirectSubclass(company);