All Packages This Package Class Hierarchy Class Search Index
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 errorThe 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; }
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
public static DottedPair reuse(Object car, Object cdr, Object compObj) throws ClassCastException
Invokes the
DottedPair
reuse
method, assuming that compObj is a compoundDottedPair
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
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
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.
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
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
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
andobj2
are equal in the LISP equal sense.See Also: equals
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
andobj2
are equal in the LISP eql sense.See Also: eql
public static DottedPair assoc(Object target, Object assocList) throws ClassCastException
Invokes the
DottedPair
assoc
method, assuming that assocList is a compoundDottedPair
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 compoundDottedPair
object.
public static int length(Object listObj) throws ClassCastException
Invokes the
DottedPair
length
method, assuming that argumentlist
is a compoundDottedPair
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 atomicObject
).
public static DottedPair member(Object target, Object searchList) throws ClassCastException
Invokes the
DottedPair
member
method, assuming that searchList is a compoundDottedPair
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 iseql
to the giventarget
.- Throws: ClassCastException
- Thrown at runtime if
searchList
is not a compoundDottedPair
object.
public static DottedPair nthcdr(int n, Object listObj) throws ClassCastException
Invokes the
DottedPair
nthcdr
method, assuming that listObj is a compoundDottedPair
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 compoundDottedPair
object.See Also: nthcdr
public static Object elt(Object listObj, int index) throws ClassCastException, RuntimeException
Invokes the
DottedPair
elt
method, assuming that listObj is a compoundDottedPair
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 compoundDottedPair
object.- Throws: RuntimeException
- Thrown if
index
is out of bounds (negative or beyond the end of the list).See Also: elt
public static DottedPair subseq(Object listObj, int start, int end) throws ClassCastException, RuntimeException
Invokes the
DottedPair
subseq
method, assuming that listObj is a compoundDottedPair
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
public static DottedPair subseq(Object listObj, int start) throws ClassCastException, RuntimeException
Invokes the
DottedPair
subseq
method, assuming that listObj is a compoundDottedPair
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
public static DottedPair copySeq(Object listObj)
Invokes the
DottedPair
copySeq
method, assuming that listObj is a compoundDottedPair
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
public static DottedPair nreverse(Object listObj)
Invokes the
DottedPair
nreverse
method, assuming that listObj is a compoundDottedPair
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
public static DottedPair reverse(Object listObj)
Invokes the
DottedPair
reverse
method, assuming that listObj is a compoundDottedPair
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
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
andlist2
are not both compoundDottedPair
objects. Additionally,list1
had better be a true list or the same exception will be thrown when the internalDottedPair.append
method reaches the atomic last element oflist1
.
public static Object car(Object compObj) throws ClassCastException
Invokes the
DottedPair
car
method, assuming that compObj is a compoundDottedPair
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 compoundDottedPair
object.See Also: car
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 compoundDottedPair
object.See Also: car
public static Object cdr(Object compObj) throws ClassCastException
Invokes the
DottedPair
cdr
method, assuming that compObj is a compoundDottedPair
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 compoundDottedPair
object.See Also: cdr
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 compoundDottedPair
object.See Also: cdr
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
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.
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 IndexFreshly brewed Java API Documentation automatically generated with polardoc Version 1.0.4