Building Parsers with Java

sjm.examples.sling
Class Variable

java.lang.Object
  |
  +--sjm.examples.sling.SlingFunction
        |
        +--sjm.examples.sling.Variable

public class Variable
extends SlingFunction

A variable is a named place that can store another function. The Variable class is a subclass of SlingFunction, which allows other functions to compose themselves from other functions. For example, if r is a variable, then sling(r, 1) is a valid function. This function can be plotted after it is evaluated for some value of r. The following code creates a function that contains the variable r. The code sets a value for r, evaluates the function, and shows the new function's value for a few different times:

     // s = sling(r, 1);
     Variable r = new Variable("r"); 
     SlingFunction s = new Sling(r, new Point(0, 1));
     System.out.println("s: " + s);
 
     // set r to 10 and evaluate s into a new function s10
     r.setValue(new Point(0, 10));
     SlingFunction s10 = s.eval();
     System.out.println("s10: " + s10);
 
     // show s10 a few times
     for (double time = 0; time < 0.25; time += .05) { 
         System.out.println(s10.f(time));
     }
 
This code prints out:
     s: sling(r, (0.0, 1.0))
     s10: sling((0.0, 10.0), (0.0, 1.0))
     (10.0, 0.0)
     (9.510565162951535, 3.090169943749474)
     (8.090169943749475, 5.877852522924732)
     (5.87785252292473, 8.090169943749475)
     (3.0901699437494745, 9.510565162951535) 
 
The printout shows: a function that contains a variable; an evaluated function; the evaluated function's value for a few times in the first quarter of a plot of the function.


Field Summary
protected  java.lang.String name
           
 
Fields inherited from class sjm.examples.sling.SlingFunction
source
 
Constructor Summary
Variable(java.lang.String name)
          Create a new variable with the given name.
 
Method Summary
 SlingFunction eval()
          Return the value of the contents of this variable.
 Point f(double t)
          Throws an InternalError.
 void setValue(SlingFunction f)
          Sets the value of the variable to the given function.
 java.lang.String toString()
          Returns a string representation of this variable.
 
Methods inherited from class sjm.examples.sling.SlingFunction
extrema, fresh
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

protected java.lang.String name
Constructor Detail

Variable

public Variable(java.lang.String name)
Create a new variable with the given name.
Method Detail

eval

public SlingFunction eval()
Return the value of the contents of this variable.
Overrides:
eval in class SlingFunction
Throws:
UnassignedVariableException - if the variable has no value

f

public Point f(double t)
Throws an InternalError. Variables themselves cannot be plotted. A function that contains a variable must be evaluated before it is plotted.
Overrides:
f in class SlingFunction
Tags copied from class: SlingFunction
Parameters:
t - a number that represents how far along a plot is, and thus tells which point to return
Returns:
the value of the function at the given time

setValue

public void setValue(SlingFunction f)
Sets the value of the variable to the given function.

toString

public java.lang.String toString()
Returns a string representation of this variable.
Overrides:
toString in class java.lang.Object

by Steve Metsker