All Packages  This Package  Class Hierarchy  Class Search  Index

Class aima.util.Expression

java.lang.Object
   |
   +----aima.util.DottedPair
           |
           +----aima.util.Expression

The Expression class provides static methods to facilitate JAVA programming in terms of LISP-like atomic and dotted-pair expressions. While the Expression class (currently) extends the DottedPair class, this is done as a mere convenience so that one can do the nicer looking:

    Expression expr    = new Expression("a",new Integer(5));
    String     aString = (String) Expression.first(expr);
 
rather than:
    DottedPair dp      = new DottedPair("a",new Integer(5));
    String     aString = (String) Expression.first(dp);
 
The idea here is that the Expression class, with its associated static methods creates the illusion that it can manipulate atomic as well as dotted pair objects, without accentuating the fact that non-atomic objects are actually implemented in terms of the DottedPair class. Other than the constructor method, all the other methods are static, to conceptually keep separate the generic atom/dotted-pair manipulation methods from the actual dotted-pair implementation in the DottedPair class. An atom in this implementation is any JAVA Object that is neither an instance of the DottedPair class nor is the special, unique (nil-like) object used to terminate DottedPair's and to represent the empty list. The static methods of this class expect that run-time exceptions will be thrown if they are called inappropriately. For example:
    Integer    atom1  = new Integer(1);
    Integer    atom2  = new Integer(2);
    Expression expr12 = new Expression(atom1,atom2);
    Object     good   = Expression.first(expr12); // OK
    Object     bad    = Expression.first(atom1);  // Runtime error
 
The last statement will throw an error because you cannot take the first element of an atom. Currently this error is a JAVA mis-casting runtime error, but perhaps in the future we may define an Expression specific error to throw.

See Also: DottedPair


public class  Expression
     extends aima.util.DottedPair
{
          // Constructors 1
     public Expression(Object, Object);

          // Methods 25
     public static DottedPair append(Object, Object) throws ClassCastException;
     public static DottedPair assoc(Object, Object) throws ClassCastException;
     public static Object car(Object) throws ClassCastException;
     public static Object cdr(Object) throws ClassCastException;
     public static DottedPair copySeq(Object);
     public static Object elt(Object, int) throws ClassCastException, RuntimeException;
     public static boolean eql(Object, Object);
     public static boolean equals(Object, Object);
     public static Object first(Object) throws ClassCastException;
     public static boolean isAtomic(Object);
     public static boolean isCompound(Object);
     public static boolean isNull(Object);
     public static boolean isTrueCompound(Object);
     public static int length(Object) throws ClassCastException;
     public static void main(String[]);
     public static DottedPair member(Object, Object) throws ClassCastException;
     public static DottedPair nreverse(Object);
     public static DottedPair nthcdr(int, Object) throws ClassCastException;
     public static Object parse(Reader) throws Exception;
     public static Object parse(String) throws Exception;
     public static Object rest(Object) throws ClassCastException;
     public static DottedPair reuse(Object, Object, Object) throws ClassCastException;
     public static DottedPair reverse(Object);
     public static DottedPair subseq(Object, int) throws ClassCastException, RuntimeException;
     public static DottedPair subseq(Object, int, int) throws ClassCastException, RuntimeException;
}



Constructors


Expression

   public Expression(Object car, 
                     Object cdr) 

Creates a new compound (dotted) expresssion.

Parameter Description
car the Object to go to the left of the "dot"
cdr the Object to go to the right of the "dot"

See Also: DottedPair




Methods


reuse

   public static DottedPair reuse(Object car, 
                                  Object cdr, 
                                  Object compObj)  throws ClassCastException

Invokes the DottedPair reuse method, assuming that compObj is a compound DottedPair object.

Parameter Description
car see first parameter of DottedPair.reuse
cdr see second parameter of DottedPair.reuse
compObj the object on which to invoke DottedPair.reuse(car,cdr)

Returns:
the result of DottedPair.reuse
Throws: ClassCastException
Thrown at runtime if compObj is not a compound DottedPair object.

See Also: reuse



isNull

   public static boolean isNull(Object obj) 

Tells whether or not the given object is the unique "NULL" object that represents the empty compound expression (which is used to terminate compound expressions that are true lists).

Parameter Description
obj Object to test.

Returns:
true, iff obj is the NULL object.

See Also: getNull



isAtomic

   public static boolean isAtomic(Object obj) 

Tells whether or not the given object is considered an atomic expression. Analogous to the atom predicate in LISP.

Parameter Description
obj Object to test.

Returns:
true, iff obj is neither a dotted compound expression, nor the special, unique NULL (empty) compound object.

See Also: isAtomic, getNull



isTrueCompound

   public static boolean isTrueCompound(Object obj) 

Tells whether or not the given object is considered a true compound expression. Analogous to the consp predicate in LISP.

Parameter Description
obj Object to test.

Returns:
true, iff obj is a dotted compound but not the special empty NULL compound expression.

See Also: isTrueCompound, getNull, isCompound



isCompound

   public static boolean isCompound(Object obj) 

Tells whether or not the given object is considered a compound expression. Analogous to the listp predicate in LISP.

Parameter Description
obj Object to test.

Returns:
true, iff obj is a compound expression, including the special empty NULL compound expression.

See Also: DottedPair



equals

   public static boolean equals(Object obj1, 
                                Object obj2) 

Tells whether or not the given objects are equal, analogous to the equal predicate in LISP.

Parameter Description
obj1 first Object to compare.
obj2 second Object to compare.

Returns:
true, iff obj1 and obj2 are equal in the LISP equal sense.

See Also: equals



eql

   public static boolean eql(Object obj1, 
                             Object obj2) 

Tells whether or not the given objects are equal, analogous to the eql predicate in LISP.

Parameter Description
obj1 first Object to compare.
obj2 second Object to compare.

Returns:
true, iff obj1 and obj2 are equal in the LISP eql sense.

See Also: eql



assoc

   public static DottedPair assoc(Object target, 
                                  Object assocList)  throws ClassCastException

Invokes the DottedPair assoc method, assuming that assocList is a compound DottedPair object, in the form of an association list.

Note: Should one day implement a version of this code that takes an arbitrary predicate, rather than assuming eql. Similarly, should possibly take a function that directs the search to more than just the first element of each pair.

Parameter Description
target the Object for which to search.
assocList the association list to scan.

Returns:
The pair whose first element is eql to the given target, or the special Null object if no matching element is found.
Throws: ClassCastException
Thrown at runtime if assocList is not a compound DottedPair object.

See Also: assoc, eql



length

   public static int length(Object listObj)  throws ClassCastException

Invokes the DottedPair length method, assuming that argument list is a compound DottedPair object, and returns the number of top-level elements of this list.

Parameter Description
listObj a true (Null terminated) list

Returns:
The number of top-level elements of this list.
Throws: ClassCastException
Thrown at runtime if this is not a DottedPair that is also a true list (ends in a non-Null atomic Object).

See Also: getNull, length



member

   public static DottedPair member(Object target, 
                                   Object searchList)  throws ClassCastException

Invokes the DottedPair member method, assuming that searchList is a compound DottedPair object, in the form of a true list.

Note: Should one day implement a version of this code that takes an arbitrary predicate, rather than assuming eql.

Parameter Description
target the Object for which to search.
searchList the list to search.

Returns:
The sublist of the given searchList whose first element is eql to the given target.
Throws: ClassCastException
Thrown at runtime if searchList is not a compound DottedPair object.

See Also: member, eql



nthcdr

   public static DottedPair nthcdr(int n, 
                                   Object listObj)  throws ClassCastException

Invokes the DottedPair nthcdr method, assuming that listObj is a compound DottedPair object, in the form of a true list.

Parameter Description
n the number of times to apply cdr
listObj the initial list.

Returns:
The result of applying cdr the specified number of times.
Throws: ClassCastException
Thrown at runtime if searchList is not a compound DottedPair object.

See Also: nthcdr



elt

   public static Object elt(Object listObj, 
                            int index)  throws ClassCastException, RuntimeException

Invokes the DottedPair elt method, assuming that listObj is a compound DottedPair object, in the form of a true list.

Parameter Description
listObj the list.
index the (0-based) index.

Returns:
The element of the list found at the given (0-based) index.
Throws: ClassCastException
Thrown at runtime if listObj is not a compound DottedPair object.
Throws: RuntimeException
Thrown if index is out of bounds (negative or beyond the end of the list).

See Also: elt



subseq

   public static DottedPair subseq(Object listObj, 
                                   int start, 
                                   int end)  throws ClassCastException, RuntimeException

Invokes the DottedPair subseq method, assuming that listObj is a compound DottedPair object.

Parameter Description
listObj the list whose subset is to be copied.
start the (0-based) start index.
end the (0-based) end index.

Returns:
a top level copy of the indicated subset of the given list.
Throws: ClassCastException
Possibly thrown at runtime if this is not a true list (ends in a non-Null atomic Object).
Throws: RuntimeException
Thrown if given indices are beyond the bounds of the list.

See Also: subseq



subseq

   public static DottedPair subseq(Object listObj, 
                                   int start)  throws ClassCastException, RuntimeException

Invokes the DottedPair subseq method, assuming that listObj is a compound DottedPair object.

Parameter Description
listObj the list whose suffix is to be copied.
start the (0-based) start index.

Returns:
a top level copy of the indicated suffix of the given list.
Throws: ClassCastException
Possibly thrown at runtime if this is not a true list (ends in a non-Null atomic Object).
Throws: RuntimeException
Thrown if given start index is beyond the bounds of the list.

See Also: subseq



copySeq

   public static DottedPair copySeq(Object listObj) 

Invokes the DottedPair copySeq method, assuming that listObj is a compound DottedPair object.

Returns:
a top level copy of the indicated list.
Throws: ClassCastException
Possibly thrown at runtime if this is not a true list (ends in a non-Null atomic Object).

See Also: copySeq



nreverse

   public static DottedPair nreverse(Object listObj) 

Invokes the DottedPair nreverse method, assuming that listObj is a compound DottedPair object.

Parameter Description
listObj the list to destructively reverse.

Returns:
the first DottedPair element of the (destructively) reversed given list.
Throws: ClassCastException
Possibly thrown at runtime if this is not a true list (ends in a non-Null atomic Object).

See Also: nreverse



reverse

   public static DottedPair reverse(Object listObj) 

Invokes the DottedPair reverse method, assuming that listObj is a compound DottedPair object.

Parameter Description
listObj the list to reverse.

Returns:
a reversed (non-destructive) copy of the given list.
Throws: ClassCastException
Possibly thrown at runtime if this is not a true list (ends in a non-Null atomic Object).

See Also: reverse



append

   public static DottedPair append(Object list1, 
                                   Object list2)  throws ClassCastException

Returns a new list that is the result of appending a (surface level) copy of the first list argument to the second list argument.

Note: As in LISP, the result contains a reference to the second list argument passed (the passed argument is not itself copied).

Parameter Description
list1 the list to which to append.
list2 the list that is appended.

Returns:
The appended result.
Throws: ClassCastException
Thrown at runtime if list1 and list2 are not both compound DottedPair objects. Additionally, list1 had better be a true list or the same exception will be thrown when the internal DottedPair.append method reaches the atomic last element of list1.


car

   public static Object car(Object compObj)  throws ClassCastException

Invokes the DottedPair car method, assuming that compObj is a compound DottedPair object.

Parameter Description
compObj the object on which to invoke DottedPair.car()

Returns:
the result of DottedPair.car (the first element of the pair)
Throws: ClassCastException
Thrown at runtime if compObj is not a compound DottedPair object.

See Also: car



first

   public static Object first(Object compObj)  throws ClassCastException

An alias for the car method.

Parameter Description
compObj the object on which to invoke DottedPair.car()

Returns:
the result of DottedPair.car (the first element of the pair)
Throws: ClassCastException
Thrown at runtime if compObj is not a compound DottedPair object.

See Also: car



cdr

   public static Object cdr(Object compObj)  throws ClassCastException

Invokes the DottedPair cdr method, assuming that compObj is a compound DottedPair object.

Parameter Description
compObj the object on which to invoke DottedPair.cdr()

Returns:
the result of DottedPair.cdr (the right element of the pair)
Throws: ClassCastException
Thrown at runtime if compObj is not a compound DottedPair object.

See Also: cdr



rest

   public static Object rest(Object compObj)  throws ClassCastException

An alias for the cdr method.

Parameter Description
compObj the object on which to invoke DottedPair.cdr()

Returns:
the result of DottedPair.cdr (the right element of the pair)
Throws: ClassCastException
Thrown at runtime if compObj is not a compound DottedPair object.

See Also: cdr



parse

   public static Object parse(Reader reader)  throws Exception

Reads and parses the next atomic or compound expression from the given Reader stream.

Parameter Description
reader The stream from which to read.

Returns:
An object consistent with the description in the stream.
Throws: Exception
An Exception will be thrown if a parsing error occurs.

See Also: Reader



parse

   public static Object parse(String str)  throws Exception

Reads and parses the first atomic or compound expression from the given String.

Parameter Description
str The String to parse.

Returns:
An object consistent with the description in str.
Throws: Exception
An Exception will be thrown if a parsing error occurs.


main

   public static void main(String[] args) 

Tests out the functionality of this class.

Parameter Description
args Ignored.

Overrides:
main in class DottedPair


All Packages  This Package  Class Hierarchy  Class Search  Index
Freshly brewed Java API Documentation automatically generated with polardoc Version 1.0.4