MathBus

[ Term Structure | Algebra | Geometry | Logic | Drawing ]
[ Language Bindings | C++ | Java | Lisp | ML ]


Java Language Bindings

All classes in the Java implementation is contained in the mathbus package. All of the methods described in this section are public methods.

Registry

As with all implementations of the MathBus, the MathBus registry file is the canonical specification for the bindings and meanings of String identifiers and node labels. This file is converted into a set of Java declarations and hash tables to obviate the need to load the registry file and to speed access. In the Java implementation this information is managed in the class static class named mathbus.Registry. Before being used this class needs to be initialized by calling the the InitializeRegistry member function. Thus, most Java MathBus programs begin with the phrase:

Registry.InitializeRegistry();

When new releases of the the MathBus regsitry file are released, they may be converted into the appropriate Java definition files by first initializing the registry using Registry.LoadRegistryFile() and then writing out the files using Registry.WriteJavaRegistry().


void InitializeRegistry()

Initialize the registry from the declarations that are contained in the Java sources. This should be relatively fast and is the usual way the registry is initialized. Thus,

When a new registry distribution file is received, or when the Java implementation is being bootstrapped, the registry in the Java implementation be initialized using the LoadRegistryFile method. The Java source code files that are used by the Java implementation can be generated by the WriteJavaRegistry method.


void LoadRegistryFile(String
filename)

Reads the indicated file and initializes the registry with the file's contents. This file is expected to be a registry file formatted according to the specification in Section File Format of Regsitry Information. If the registry file is improperly formed or an I/O error occurs while reading the file an IOException will be thrown.


void WriteJavaRegistry(String
directory)

Using the current contents of the global registry, this routine writes the Java source files required to initialize the registry using InitializeRegistry() into directory. Two files are actually written MBS.java and RegInitializer.java. MBS.java defines a single class, mathbus.MBS that contains final static values for each of the node labels. RegInitializer.java. defines a class with a single member function. This function initializes all of the hash tables and data structures in the Registry class.

Access Methods

The registry itself is divided into two parts. One contains the global registry information that cannot be changed except by updating the registy file. The other part contains the local registry information. The routines provided below handle the most common accesses. Thus reads check both the local and global registry, while the routines provided for writing only write to the local registry.

It is rarely necessary to directly access the Registry, but when necessary the following methods can be used. The following member functions can be used to extract information about


boolean HasNumericLabel(String
ident)

Returns true if the indicated string already occurs in either the global or local registries. Looks up the value corresponding to ident with registry type regType.


boolean HasNumericLabel(String
ident)

Returns true if the indicated string already occurs in either the global or local registries. Looks up the value corresponding to ident with registry type regType.


String LookupIdentifier(String
regType, int value)

Determines the identifier whose value associated with the registry type regType is value.


void StoreLocal(String
ident, String regType, int value)

Associates value with the identifier ident and registry type regType.


int NumericLabel(String
ident)

Returns the integer corresponding to the symbolic node label ident.


String SymbolicLabel(int
ident)
Returns the Java string corresponding to the number node label ident.

To speed access of numeric string identifiers the mathbus.MBS object is defined. This object has only static members and the name of each is a global string and whose value is the corresponding numeric value.

Nodes

All MathBus nodes are represented as instances of the mathbus.node class. All of the methods in this subsection are methods of the mathbus.node class.

The following five methods are used to create MathBus nodes in Java. The first two are conventional constructor methods, while the last three also include a conversion between standard Java primitive types and MathBus terms (strings, integers and floating point numbers).


Node(int
lab, int nSubterms
)
Creates a node with lab as its label and with the ability to store as many as cnt sub-terms.


Node(String
lab, int nSubterms)

Creates a node with lab as it label and with the ability to store as many as cnt sub-terms. The symbolic label lab is converted to its numeric value before being inserted in the node.


Node(String
data)

Creates a node of with label MBO.String, where the sub-terms make up the given string.


Node(int
data)

Creates a node of with label MBO.LongInteger, where the sub-terms make up the given integer.


Node(double
data)

Creates a node of with label MBO.IEEEFloat, where the sub-terms make up the given real number.

The following routines are used to access the parts of a node. Notice that the label and the number of sub-terms of a node can only be examined. They cannot be modified. Note that the modifying operation, setSubterm, returns the modified node. These routines pay attention to nodes with attributes and only access the base node. Low level routines that can access the Attributes node themselves include a suffix of "q".


int label()
Returns the numeric label of the node.


int nSubterms()

Returns the number of sub-terms of the node.


Object subterm(int
index)
Returns the sub-term of the node associated with index index. The first sub-term has index 1, the second has index 2, etc.


Object setSubterm(int
index, int value)
Object setSubterm(int index, Node value)
Modifies the node so that the sub-term with index index to is value. Returns the value of the new sub-term.

The following three routines are used to manipulate the attributes of a node.


Node getAttribute(Node
node, StringId attrId)

Returns the value associated with the indicated attribute. If the indicated attribute is not associated with the node, then a Null node is returned.


Node setAttribute(Node
node, StringId attrId, Node attrValue)

Modifies node so that it has the indicated attributes. The value returned is the node with the new attribute.


Node deletetAttribute(Node
node, StringId attrId)
Removes the indicated attribute from the node if it is present. Returns the modified node without the attribute.

The following routines are slightly faster than the previous routines, but the user needs to be aware of attributes and how they are used.


int labelq()

Returns the numeric label of the node.


int nSubtermsq()

Returns the number of sub-terms of the node.

Object subtermq(int index)
Returns the sub-term of the node associated with index index. The first sub-term has index 1, the second has index 2, etc.


Object setSubtermq(int
index, int value)
Object setSubtermq(int index, Node value)
Modifies the node so that the sub-term with index index to is value. The value returned is the new value of the indicated sub-term.

Input/Output Routines


void writeNode(OutputStream
str)

Tools

The following routine computes the hash code of nodes.


int hashCode()

Computes the hash code of the node according to the algorithm given in the MathBus Term structure reference.

The following routine returns an enumeration of the sub-terms of a node. The enumeration returned keeps track of the types of the sub-terms as well as the sub-term index itself. This is for what it is most frequently used.


Subterm Enumeration subterms(Node
node)
Returns an instance of the SubtermEnumeration class for the node provided as an argument. An instance of the SubtermEnumeration class handles the methods: nextElement, thisIndex, thisSubtype and hasMoreElements.


Object nextElement()

Advances the (internal) index that points at sub-terms and returns the sub-term which is currently being pointed to.


int thisIndex()

Returns the index of the current sub-term.


int thisSubtype()

Returns the subtype the current sub-term. The value returned is 0 if the sub-term is a node reference, 1 if the sub-term is a 32-bit integer and 2 if the sub-term is a string identifier.


boolean hasMoreElements()

Returns true if there are any more sub-terms.

int subtype;

for (SubtermEnumeration enum = new SubtermEnumeration(this);
     enum.hasMoreElements(); ) {
   Object elt = enum.nextElement();

   buf.append(" ");

   if((subtype = enum.thisSubtype()) == enum.NodeSubtype) {
       buf.append(elt.toString());
     }
   else if (subtype == enum.IntSubtype) {
       buf.append(((Integer)elt).intValue());
     }
   else if (subtype == enum.StringIdSubtype) {
       buf.append("'");
       buf.append(Registry.SymbolicLabel(((Integer)elt).intValue()));
       buf.append("'");
     }
  }

Copyright Cornell University
For problems or questions regarding this web contact
[ SimLab project].
Last updated: November 03, 1997 by .