Sometimes a database doesn’t include the necessary representation required for a specific task. For example, one of the classification models used in Chapter 7, Learning Models uses a boolean attribute to identify members of a class. The ProxWebKB database doesn’t include such an attribute, so we must create one based on other existing attribute values. Although you can create attributes directly in the Proximity Database Browser, it is sometimes more convenient to do so as part of a script. This section describes how to create a new attribute and use a function to assign values to that new attribute in a script. The syntax for the assignment function is identical, regardless of whether the attribute is created in a script or directly in the Proximity Database Browser.
In this example, we create an attribute needed for later Tutorial exercises: a binary isStudent object attribute that indicates whether or not a web page belongs to a student. We assign the value 1 to the isStudent attribute if the page’s pagetype attribute is Student, and 0 otherwise.
Creating a useful attribute requires two steps: creating the
appropriate database tables and populating its values.
The Proximity class’s addAttribute()
method lets you accomplish both of these steps in one
method call and provides great flexibility in how you assign values to
the new attribute.
To create and populate a new attribute, you specify a function
that describes how to assign values to the new attribute for each
entity. New attribute values can be constants or can be based on
existing attribute values. In this example, we use the value of the
existing pagetype attribute to determine the
value of the new isStudent
attribute. This function is specified as a string passed into the
addAttribute() method. You can create
attributes for any class of database entity—objects, links,
containers, or subgraphs.
See the AddAttribute
class for a full description of the syntax for
specifying the creation function.
This section describes the script found in
$PROX_HOME/doc/user/tutorial/examples/create-isStudent-attr.py.
Get the current set of object attributes and define the new attribute name. We use the set of current attributes to determine whether the new attribute already exists in the database.
currentAttrs = prox.objectAttrs newAttrName = "isStudent"
Define the function that determines the new attribute value.
This example uses a switch function. It sets the value of the new
attribute to 1 if the object’s
pagetype attribute is equal to
Student
and 0 otherwise. See the AddAttribute
class for details on
specifying function strings for defining new attributes.
newAttrFunction = "pagetype = \"Student\" ==> 1, default ==> 0"
See if this object attribute already exists in the database.
If it does, ask the user if we should delete it so we can redefine
it. Proximity provides two methods,
getYesNoFromUser()
and getStringFromUser(),
both members of the
Proximity class, that you can use to interact with your Python
programs. Both methods display a dialog with the specified prompt. The
getYesNoFromUser() method returns
true if the user selects Yes and
false if the user selects No. The
getStringFromUser() method returns the string
entered by the user.
prompt = "Attribute already exists. Delete and recreate?" if currentAttrs.isAttributeDefined(newAttrName): deleteExisting = prox.getYesNoFromUser(prompt)
If yes, delete the existing attribute and create it again.
if deleteExisting:
currentAttrs.deleteAttribute(newAttrName)
print "Creating new ", newAttrName, " attribute"
prox.addAttribute(currentAttrs,newAttrName,newAttrFunction)
If the attribute does not already exist, go ahead and create it.
else: print "Creating new ", newAttrName, " attribute" prox.addAttribute(currentAttrs,newAttrName,newAttrFunction)
Exercise 6.5. Adding a new attribute:
Before beginning, make sure that you are serving the ProxWebKB database using Mserver. Start the Proximity Database Browser if it is not already running.
From the Script menu, choose Run Script. Proximity displays the Open dialog.
Navigate to the $PROX_HOME/doc/user/tutorial/examples directory and
choose create-isStudent-attr.py.
Click Open.
Proximity opens a window to show you any output from the script along with a trace of the script execution. Proximity reports:
Status: starting running script: /proximity/doc/user/tutorial/examples/create-isStudent-attr.py Creating new isStudent attribute Status: finished running script
You can close this window after the script finishes.
Click Object attributes to see the new isStudent attribute.