sdr 0.5

net.cscott.sdr.calls
Class CallDB

java.lang.Object
  extended by net.cscott.sdr.calls.CallDB

public class CallDB
extends Object

CallDB holds all the calls and concepts we know about. It is a singleton class; its static constructor loads all the call definitions from files and other classes.

Author:
C. Scott Ananian

Field Summary
 Collection<Call> allCalls
           
static CallDB INSTANCE
           
 
Method Summary
 Call lookup(String name)
          Lookup a call in the database.
 Apply parse(Program program, String s)
          Parse a natural-language string of calls.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INSTANCE

public static final CallDB INSTANCE

allCalls

public final Collection<Call> allCalls
Method Detail

lookup

public Call lookup(String name)
Lookup a call in the database.

Throws:
IllegalArgumentException - if the call name is unknown.
Tests:
Basic lookup test:
js> db = CallDB.INSTANCE
net.cscott.sdr.calls.CallDB@1d66e22
js> db.lookup("square thru")
square thru[basic]
Check that exceptions are properly thrown for bogus calls:
js> try {
  >   CallDB.INSTANCE.lookup("foobar bat")
  > } catch (e) {
  >   print(e.javaException)
  > }
java.lang.IllegalArgumentException: Unknown call: foobar bat

parse

public Apply parse(Program program,
                   String s)
Parse a natural-language string of calls.

Tests:
Simple examples:
js> db = CallDB.INSTANCE
net.cscott.sdr.calls.CallDB@1d66e22
js> db.parse(Program.BASIC, "double pass thru")
(Apply double pass thru)
js> db.parse(Program.BASIC, "square thru three and a half")
(Apply square thru (Apply 3 1/2))
As a convenience, we also allow numbers specified with digits (even though this is never produced by the spoken language recognizer):
js> db = CallDB.INSTANCE ; undefined
js> db.parse(Program.BASIC, "square thru 3 1/2")
(Apply square thru (Apply 3 1/2))
We use a precedence grammar to resolve some ambiguities:
js> db = CallDB.INSTANCE ; undefined
js> db.parse(Program.BASIC, "do one half of a trade")
(Apply _fractional (Apply 1/2) (Apply trade))
js> db.parse(Program.PLUS, "do one half of a trade and roll")
(Apply _and_roll (Apply _fractional (Apply 1/2) (Apply trade)))
js> db.parse(Program.PLUS, "trade twice and roll")
(Apply _and_roll (Apply _fractional (Apply 2) (Apply trade)))
js> db.parse(Program.PLUS, "trade and roll twice")
(Apply _fractional (Apply 2) (Apply _and_roll (Apply trade)))
Semicolon-separated calls are also used in the typed (not the spoken) grammar:
js> db = CallDB.INSTANCE ; undefined
js> db.parse(Program.PLUS, "circulate; trade; u turn back")
(Apply and (Apply circulate) (Apply trade) (Apply u turn back))
Parentheses can be used in the typed (not spoken) grammar:
js> db = CallDB.INSTANCE ; undefined
js> db.parse(Program.PLUS, "do one half of a trade and roll")
(Apply _and_roll (Apply _fractional (Apply 1/2) (Apply trade)))
js> db.parse(Program.PLUS, "do one half of a ( trade and roll )")
(Apply _fractional (Apply 1/2) (Apply _and_roll (Apply trade)))
Ensure we throw an exception if the call is unrecognized or not on level:
js> db = CallDB.INSTANCE ; undefined
js> try {
  >   db.parse(Program.C4, "@trade")
  > } catch (e) {
  >   print(e.javaException)
  > }
net.cscott.sdr.calls.BadCallException: Not on list: @trade
js> try {
  >   db.parse(Program.C4, "foobar bat")
  > } catch (e) {
  >   print(e.javaException)
  > }
net.cscott.sdr.calls.BadCallException: Not on list: foobar bat
js> try {
  >   db.parse(Program.MAINSTREAM, "trade and roll")
  > } catch (e) {
  >   print(e.javaException)
  > }
net.cscott.sdr.calls.BadCallException: Not on list: trade and roll

sdr 0.5

Copyright © 2006-2009 C. Scott Ananian