Property Lists of Objects
We begin with a discussion of property
lists. Every Mathscript object may have a property
list. In fact, each object has a name, and if we think of the object itself as
the value of that name, then we see the origins of property lists. Each name is
an atomic symbol and it has associated with it a list of properties. The
most obvious property is its value (the object associated to the name).
But we may create other properties of the name, and those other properties are
located in the property list of the name.
The relevant functions and commands are:
Getproperty
Addproperty
Putproperty
Removeproperty
Whenever a property is assigned to a name, the property also has a name. We
call that the Propname. The property itself is some object, such as a variable, or a list, and is
designated the Prop.
Suppose we want to define a 'family tree' as a miniature database.
We have certain family members. Say William and Sarah are the parents of
John. John is married to Mary and they have children Liza and Tom.
Suppose William is 56 and Sarah is 50 years of age. John is 33 and
Mary is 34. Liza and Tom are twins and they are both 8 years old.
In MathScript, each of these names must be declared first as variables:
Make variable william
Make variable sarah
Make variable john
Make variable mary
Make variable liza
Make variable tom
And we have propnames: Father, Mother, Husband, Wife, Age, Children
Make variable father
Make variable mother
Make variable husband
Make variable wife
Make variable children
Make variable age
Now, we begin to create this database by entering:
putproperty william in father of john;
This creates for John a 'father' property. And it inserts William as the
value of the father property of John.
To check this, we use the dual function getproperty.
Getproperty(john,father) returns as its value: William.
Now enter
putproperty sarah in mother of john;
putproperty mary in wife of john;
putproperty 33 in age of john;
putproperty list(liza,tom) in children of john;
This gives the relevant information about John.
Notice that the 'children' property of John is a list, and not just a
variable. Suppose later that another boy, Ricky, is born to John and Mary. We might add Ricky to the list with the Command Addproperty in the following way.
Make variable Ricky
Addproperty Ricky to children of John.
Then the value of
Getproperty(John, Children)
would be: list(Ricky, Liza, Tom).
In general, the putproperty command overwrites any previous Prop, but the Addproperty command adds the item to the Prop if the Prop is a list, or has not yet been defined. For example, If you want
to add John's hobbies: Tennis and chess to the database,
Make variable tennis
Make variable chess
Make variable hobbies
Addproperty tennis to hobbies of John
Addproperty chess to hobbies of John
would work. This is because the first Addproperty call creates a List as the
Prop of Hobbies. And the second call adds Chess to that list.
Putproperty tennis in hobbies of John
Addproperty chess to hobbies of John
Would not work, because Putproperty makes the hobbies property a variable, not
a list, and one cannot add to a variable.
Finally, suppose you wish to delete an item entered on the property list of an
atom. Of course, a new Putproperty Command could replace it, but these lists consume memory, and it might be desirable
at some time to release that memory to the system. Then
Removeproperty propname of John
has the effect of removing from the property list of John the property with
the name propname.
Property List operators
1) Addproperty
Number of Arguments: 3
Argument types: First is arbitrary, second is a variable, third is
an object name.
Command description:
Addproperty Prop to Proptype of target
or
Addproperty Prop Proptype target
has the effect of adjoining to the 'proptype' property of target the item Prop. The 'proptype' property
must either be a list, or be undefined. If a proptype is undefined, its value
is returned as FALSE (or the empty list). If the property Prop was already in
the list, it is not added again. Target is the name of an object, and is not
evaluated.
2) Getproperty
Number of Arguments: 2
Argument types: first is an object name, second is a variable
Function description:
Getproperty takes 2 arguments:
Getproperty(target, propname) returns the value of the property named propname on the property list of
target. If there is no propname property, it returns FALSE, or list(). Target is
the name of an object, and is not evaluated.
3) Putproperty
Number of Arguments: 3
Argument types: First is arbitrary, second is a variable, and
third is an object name.
Command description:
Putproperty Prop in Proptype of target
or
Putproperty Prop Proptype target
has the effect of placing in the 'proptype' property of target the item Prop. The 'proptype' property is
overwritten. Target is the name of an object, and is not evaluated.
- Removeproperty
Number of Arguments: 2
Argument types: first is an object name, second is a variable.
Command description:
Removeproperty propname of target
or
Removeproperty propname target
removes the propname property from the property list of target. Target is
the name of an object, and is not evaluated. The memory is freed.
If later you call
Getproperty(target, propname) the return value will be FALSE.