Skip to main content

Defining a Domain Programmatically

Defining a Domain Programmatically

To define a new domain using class methods, invoke the %iKnow.Domain.%New() persistent method, supplying the domain name as the method parameter. A domain name can be any valid string; domain names are not case-sensitive. The name you assign to this domain must be unique for the current namespace. This method returns a domain object reference (oref) which is unique for all namespaces of the InterSystems IRIS instance. You must then save this instance using the %Save() method to make it persistent. The domain Id property (an integer value) is not defined until you save the instance as a persistent object, as shown in the following example:

CreateDomain
   SET domOref=##class(%iKnow.Domain).%New("FirstExampleDomain")
   WRITE "Id before save: ",domOref.Id,!
   DO domOref.%Save()
   WRITE "Id after save: ",domOref.Id,!
CleanUp
   DO ##class(%iKnow.Domain).%DeleteId(domOref.Id)
   WRITE "All done"

Use NameIndexExists()Opens in a new tab to determine if the domain already exists. If the domain exists, use NameIndexOpen()Opens in a new tab to open it. If the domain doesn’t exist, use %New() to create it and then use %Save().

The following example checks whether a domain exists. If the domain doesn’t exist, the program creates it. If the domain does exist, the program opens it. For the purpose of demonstration, this program then randomly either deletes or doesn’t delete the domain.

DomainCreateOrOpen
  SET domn="mydomain"
  IF (##class(%iKnow.Domain).NameIndexExists(domn))
     { WRITE "The ",domn," domain already exists",!
       SET domo=##class(%iKnow.Domain).NameIndexOpen(domn)
       SET domId=domo.Id
     }
  ELSE {
     SET domo=##class(%iKnow.Domain).%New(domn)
     DO domo.%Save()
     SET domId=domo.Id
     WRITE "Created the ",domn," domain",!
     WRITE "with domain ID ",domId,! }
ContainsData
     SET x=domo.IsEmpty()
       IF x=1 {WRITE "Domain ",domn," contains no data",!}
       ELSE {WRITE "Domain ",domn," contains data",!}
CleanupForNextTime
    SET rnd=$RANDOM(2)
    IF rnd {
       SET stat=##class(%iKnow.Domain).%DeleteId(domId)
       IF stat {WRITE "Deleted the ",domn," domain" }
       ELSE { WRITE "Domain delete error:",stat }
       }
    ELSE {WRITE "No delete this time" }

The %iKnow.DomainOpens in a new tab class methods that create or open a domain are provided with an output %Status parameter. This parameter is set when the current system does not have license access to InterSystems NLP, and thus cannot create or open an InterSystems NLP domain.

FeedbackOpens in a new tab