We have already seen the Caché population utility in action. We populated Contact twice: first without Address and then with Address. In both cases we relied completely on the default behavior of the utility to generate appropriate data for the different properties in our class. The default behavior worked well. It generated names for the Name property, city names for the City property, street addresses for the Street property, and so on. It also randomly selected either “Business” or “Personal” for Type, which we restricted to these two possible values. Now, however, the default behavior will not give us exactly what we want. PhoneNumber contains a property called Number and the default behavior of the populate utility for a property named Number is to generate a random number, but not a random phone number.
The populate utility does, however, include a class, %PopulateUtils, which contains methods for generating the right sorts of data for a number of different situations. In particular, it contains the USPhone method for generating US style phone numbers. We can link a %PopulateUtils method to a property through the property's POPSPEC parameter.
In PhoneNumber, update the property declaration of Number to include a POPSPEC parameter with the value “USPhone()”. It looks like this.
Property Number As %String (POPSPEC="USPhone()");
After updating the class definition, save and recompile the classes.
Now, use Terminal to remove all instances of Contact. Use the following command:
USER>Do ##class(ContactDB.Contact).Populate(10) USER>Do ##class(ContactDB.PhoneNumber).Populate(25)