|
JSAPI 2.0 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface GrammarManager
Provides management of Grammar
s.
A Grammar
defines a set of words (technically known as tokens)
and defines acceptable patterns in which the tokens may be arranged.
The methods in this interface operate on and reflect Grammar
changes as they are requested.
The
Using Grammars
section describes how a Recognizer
uses Grammar
s,
including how Grammar
changes are incorporated
(see Committing Changes).
GrammarManager
provides several approaches for
creating and loading Grammar
s:
createRuleGrammar
:
create a RuleGrammar
programmatically
loadGrammar
: create a RuleGrammar
from grammar text.
Other important RuleGrammar
functions include:
deleteGrammar
deletes a loaded Grammar
.
listGrammars
returns an array of
all Grammar
s loaded.
RuleGrammar
s that an application creates,
some Recognizer
s have "built-in" Grammar
s.
Built-in Grammar
s are automatically loaded into the
Recognizer
when the Recognizer
is created and are
accessible through the listGrammars
method.
Different Recognizer
s will have different built-in Grammar
s
so applications should not rely upon built-in Grammar
s
if they require portability.
Grammar
s may use standard URI conventions to refer to each other.
The standard
SRGS
conventions for resolving any relative references apply
(see "Resolving Relative URIs").
If any further resolution is required,
information from the following two methods is used in priority order:
A special "resource:"
scheme may be used to force resolution with
getResourceAsStream
.
This avoids unintended redirection of references that begin with "/"
by EngineProperties.setBase
.
See that method for details.
A special "grammar:"
scheme is recommended as an opaque reference
in the case of String
-based Grammar
s.
This allows reference resolution between these types of Grammar
s.
The
HelloWorld
example illustrates the use of this scheme.
GrammarManager
object has a separate name-space.
Applications should be aware that changes to a Grammar
object affect
all parts of the application that use that Grammar
.
However, if two separate applications reference different
GrammarManager
s and use a Grammar
with the same name,
they are effectively independent - changes
by one application do not affect the operation of the other application.
Engine
is in the ALLOCATED
state
and not DEALLOCATING_RESOURCES
.
The call blocks if the Engine
is in the
ALLOCATING_RESOURCES
state and completes when the Engine
reaches the ALLOCATED
state.
Otherwise, an exception is thrown for an Engine
in the DEALLOCATING_RESOURCES
or DEALLOCATED
states.
Grammar
,
RuleGrammar
,
Recognizer
Method Summary | |
---|---|
void |
addGrammarListener(GrammarListener listener)
Requests notifications of GrammarEvent s related to
Grammar s managed by this GrammarManager . |
RuleGrammar |
createRuleGrammar(String grammarReference,
String rootName)
Creates a new RuleGrammar for this GrammarManager with
the specified grammarReference and rootName . |
RuleGrammar |
createRuleGrammar(String grammarReference,
String rootName,
SpeechLocale locale)
Creates a new RuleGrammar for this GrammarManager
with the specified grammarReference , rootName ,
and locale . |
void |
deleteGrammar(Grammar grammar)
Deletes a Grammar from this GrammarManager . |
Grammar |
getGrammar(String grammarReference)
Gets the Grammar with the specified grammarReference . |
int |
getGrammarMask()
Gets the mask to filter GrammarEvent s for
added GrammarListener s. |
Grammar[] |
listGrammars()
Lists the Grammar s known to this GrammarManager . |
Grammar |
loadGrammar(String grammarReference,
String mediaType)
Loads a Grammar from a URI or named resource. |
Grammar |
loadGrammar(String grammarReference,
String mediaType,
boolean loadReferences,
boolean reloadReferences,
Vector loadedGrammars)
Loads a Grammar from a URI or named resource
with options for loading any referenced Grammar s. |
Grammar |
loadGrammar(String grammarReference,
String mediaType,
InputStream byteStream,
String encoding)
Loads a Grammar from grammar text provided by
an InputStream . |
Grammar |
loadGrammar(String grammarReference,
String mediaType,
Reader charStream)
Loads a Grammar from grammar text provided by
a Reader . |
Grammar |
loadGrammar(String grammarReference,
String mediaType,
String grammarText)
Loads a RuleGrammar from grammar text provided
as a String . |
void |
removeGrammarListener(GrammarListener listener)
Removes a GrammarListener from this GrammarManager . |
void |
setGrammarMask(int mask)
Sets the mask to filter GrammarEvent s for
added GrammarListener s. |
Method Detail |
---|
void addGrammarListener(GrammarListener listener)
GrammarEvent
s related to
Grammar
s managed by this GrammarManager
.
An application can attach multiple listeners to a GrammarManager
.
The setGrammarMask
method controls masking
of GrammarEvent
s.
listener
- a listener for GrammarEvent
sremoveGrammarListener
,
GrammarEvent
,
setGrammarMask
RuleGrammar createRuleGrammar(String grammarReference, String rootName) throws IllegalArgumentException, EngineStateException, EngineException
RuleGrammar
for this GrammarManager
with
the specified grammarReference
and rootName
.
This method is the same as calling
createRuleGrammar(grammarReference, rootName, null)
.
See that method for details.
grammarReference
- the reference to the RuleGrammar
to
createrootName
- the name of the root Rule
for this
RuleGrammar
RuleGrammar
object
EngineException
- if RuleGrammar
s are not supported
IllegalArgumentException
- if the RuleGrammar
reference
already exists
EngineStateException
- when not in the standard Conditions for OperationcreateRuleGrammar(String, String, SpeechLocale)
getSupportsMarkup
RuleGrammar createRuleGrammar(String grammarReference, String rootName, SpeechLocale locale) throws IllegalArgumentException, EngineStateException, EngineException
RuleGrammar
for this GrammarManager
with the specified grammarReference
, rootName
,
and locale
.
The grammarReference
is the name used for
this RuleGrammar
.
The grammarReference
must not already exist for
this GrammarManager
.
A non-null
rootName
defines the root Rule
and
specifies where processing begins for this RuleGrammar
.
In this case, rootName
is automatically made activatable
with
setActivatable(String, boolean)
.
If the rootName
is null
,
then the root Rule
remains undefined and may be defined
later with setRoot
.
Rule
s must be added with the addRule
method.
These Rule
s, including the root Rule
, may have
PUBLIC
scope to allow external reference by name.
The locale
determines the default SpeechLocale
used when
interpreting the vocabulary within the RuleGrammar
.
It must match a SpeechLocale
listed in the return from
RecognizerMode.getSpeechLocales
.
For example,
((RecognizerMode) recognizer.getEngineMode()).getSpeechLocales()
.
The first SpeechLocale
from that list is used as the default
if locale
is null
.
An EngineException
is thrown if RuleGrammar
s are
not supported.
The listGrammars
method provides a list of all Grammar
s
available.
This method operates as defined when the standard Conditions for Operation apply.
grammarReference
- the reference to the RuleGrammar
to createrootName
- the name of the root Rule
for this RuleGrammar
locale
- the SpeechLocale
of the resulting RuleGrammar
RuleGrammar
object
EngineException
- if RuleGrammar
s are not supported
IllegalArgumentException
- if the RuleGrammar
reference
already exists or an unexpected SpeechLocale
is given
EngineStateException
- when not in the standard Conditions for OperationcreateRuleGrammar
,
setActivatable
,
RuleGrammar.setActivatable(String, boolean)
,
listGrammars
,
addRule
,
getReference
,
getRoot
,
setRoot
,
PUBLIC
,
resume
,
getSupportsMarkup
,
getSpeechLocales
,
GrammarEvent
,
GrammarException
getSupportsMarkup
void deleteGrammar(Grammar grammar) throws IllegalArgumentException, EngineStateException
Grammar
from this GrammarManager
.
Deleted Grammar
s are no longer returned by listGrammars
.
GrammarManager
s may chose to mark built-in Grammar
s
as deleted rather than to actually delete them.
This method operates as defined when the standard Conditions for Operation apply.
grammar
- the Grammar
to delete
IllegalArgumentException
- if the Grammar
is unknown
EngineStateException
- when not in the standard Conditions for OperationlistGrammars
,
getGrammar
Grammar getGrammar(String grammarReference) throws EngineStateException
Grammar
with the specified grammarReference
.
The grammarReference
is a URI or named resource.
A Grammar
is created with one of the
createRuleGrammar
or loadGrammar
methods.
Referenced Grammar
s may include "built-in" Grammar
s
defined by the system, but portable applications should avoid these.
This method operates as defined when the standard Conditions for Operation apply.
grammarReference
- a reference to a Grammar
Grammar
or null
if the referenced
Grammar
does not exist.
EngineStateException
- when not in the standard Conditions for OperationcreateRuleGrammar
,
loadGrammar
,
listGrammars
,
deleteGrammar
,
RuleGrammar
int getGrammarMask()
GrammarEvent
s for
added GrammarListener
s.
See setGrammarMask
for details.
The event mask can be set and retrieved in any Recognizer
state.
GrammarEvent
sGrammarEvent
,
GrammarListener
,
setGrammarMask
Grammar[] listGrammars() throws EngineStateException
Grammar
s known to this GrammarManager
.
A zero-length list is returned if there are none.
This method lists Grammar
s added by the application and
"built-in" Grammar
s - Grammar
s defined by the system.
Use the instanceof
operator to determine the type of
Grammar
(for example, RuleGrammar
) and
which methods apply.
This method operates as defined when the standard Conditions for Operation apply.
Grammar
s associated with this GrammarManager
EngineStateException
- when not in the standard Conditions for OperationcreateRuleGrammar
,
loadGrammar
,
getGrammar
,
deleteGrammar
,
RuleGrammar
Grammar loadGrammar(String grammarReference, String mediaType) throws GrammarException, IllegalArgumentException, IOException, EngineStateException, EngineException
Grammar
from a URI or named resource.
The grammarReference
specifies a URI or named resource
and the mediaType
specifies the type of Grammar
loaded.
If mediaType
is null
and cannot be determined,
it defaults to "application/srgs+xml".
For mediaType
"application/srgs+xml"
,
a RuleGrammar
is returned.
A GrammarException
is thrown if the mediaType
is not supported.
If a RuleGrammar
is loaded and a root Rule
is defined,
then it is automatically made activatable
with
setActivatable(String, boolean)
.
If a Grammar
corresponding to grammarReference
already exists, it is over-written.
This method uses default parameters to control the loading of
any referenced Grammar
s and is equivalent to:
loadGrammar(grammarReference, mediaType, true, false, null)
;
This means that any referenced Grammar
s are loaded,
any existing referenced Grammar
s are not reloaded,
and no list of loaded Grammar
s is indicated.
See that method for detail.
This method attempts to load the referenced Grammar
, all
Grammar
s referenced from that Grammar
, and so on.
This recursive load stops when
Grammar
s are reached that have already been loaded or
when no more references are found.
The intent is to ensure that every Grammar
needed
to use the referenced Grammar
is loaded.
For example, if we load Grammar
X
,
which references Grammar
Y
, which
references Grammar
s A
and B
,
then all four Grammar
s are loaded.
If any of the Grammar
s referenced from X
are
already loaded, then they and their references are not reloaded.
The listGrammars
method provides a list of all Grammar
s
available.
An EngineException
is thrown if the mediaType
is
"application/srgs+xml"
and RuleGrammar
s are not supported.
This method operates as defined when the standard Conditions for Operation apply.
grammarReference
- the URI or named resource to loadmediaType
- the type of Grammar
to load
Grammar
object
GrammarException
- if the mediaType
is not supported or if
any loaded grammar text contains an error
IllegalArgumentException
- if a problem is encountered accessing
the grammarReference
IOException
- if an I/O error occurs
EngineException
- if RuleGrammar
s are not supported
EngineStateException
- when not in the standard Conditions for OperationsetActivatable
,
listGrammars
,
loadGrammar(String,String,boolean,boolean,Vector)
,
setBase
getSupportsMarkup
when mediaType is "application/srgs+xml"
, getSupportsLetterToSound
when the vocabulary specified is not knownGrammar loadGrammar(String grammarReference, String mediaType, boolean loadReferences, boolean reloadReferences, Vector loadedGrammars) throws GrammarException, IllegalArgumentException, IOException, EngineStateException, EngineException
Grammar
from a URI or named resource
with options for loading any referenced Grammar
s.
The grammarReference
specifies a URI or named resource
and the mediaType
specifies the type of Grammar
loaded.
If mediaType
is null
and cannot be determined,
it defaults to "application/srgs+xml".
For mediaType
"application/srgs+xml"
,
a RuleGrammar
is returned.
A GrammarException
is thrown if the mediaType
is not supported.
If a RuleGrammar
is loaded and a root Rule
is defined,
then it is automatically made activatable
with
setActivatable(String, boolean)
.
If a Grammar
corresponding to grammarReference
already exists, it is over-written.
This method provides control over whether Grammar
s
are recursively loaded,
are reloaded even if they have already been loaded,
and allows the caller
to receive a list of all Grammar
s loaded by this method.
The three additional parameters of this method provide the following
extensions over the
loadGrammar(String,String)
method:
loadReferences
:
If true
, this method recursively
loads referenced Grammar
s.
If false
,
this method only loads the Grammar
specified by the grammarReference
parameter.
reloadReferences
:
If true
,
this method always loads referenced Grammar
s,
even if already loaded into the GrammarManager
.
Any previous versions of the Grammar
s are overwritten.
If false
, this method does not load referenced
Grammar
s that are already loaded.
loadedGrammars
: if non-null
,
then the GrammarManager
appends a reference to
any loaded Grammar
s to this parameter.
RuleReference
s within Grammar
s may be
relative or fully-qualified.
This method attempts to load all Grammar
s by properly
resolving references.
A GrammarException
is thrown if any syntax problems are found.
Grammar
loading ceases when a syntax problem is found
and any such Grammar
is ignored.
However, any referenced Grammar
s loaded as
a result of this method remain.
The loadedGrammars
parameter provides access to
the list of Grammar
s actually loaded.
The listGrammars
method provides a list of all Grammar
s
available.
An EngineException
is thrown if the mediaType
is
"application/srgs+xml"
and RuleGrammar
s are not supported.
This method operates as defined when the standard Conditions for Operation apply.
grammarReference
- the URI or named resource to loadmediaType
- the type of Grammar
to loadloadReferences
- flag to indicate recursive loading of
referenced Grammar
sreloadReferences
- flag to indicate reloading of referenced
Grammar
s, even if they are already loadedloadedGrammars
- if non- null
, a reference to
each loaded RuleGrammar
is appended
Grammar
GrammarException
- if the mediaType
is not supported or if
any loaded grammar text contains an error
IllegalArgumentException
- if a problem is encountered accessing
the grammarReference
IOException
- if an I/O error occurs
EngineException
- if RuleGrammar
s are not supported
EngineStateException
- when not in the standard Conditions for OperationsetActivatable
,
listGrammars
,
loadGrammar(String, String)
,
setBase
getSupportsMarkup
when mediaType is "application/srgs+xml"
, getSupportsLetterToSound
when the vocabulary specified is not knownGrammar loadGrammar(String grammarReference, String mediaType, InputStream byteStream, String encoding) throws GrammarException, IllegalArgumentException, IOException, EngineStateException, EngineException
Grammar
from grammar text provided by
an InputStream
.
The application defines the grammarReference
for
this Grammar
and the mediaType
specifies the type of Grammar
loaded.
The encoding
specifies how the InputStream
is read.
If mediaType
is null
and cannot be determined,
it defaults to "application/srgs+xml".
For mediaType
"application/srgs+xml"
,
a RuleGrammar
is returned.
A GrammarException
is thrown if the mediaType
is not supported.
If encoding
is null
and cannot be determined,
the default encoding for the mediaType
is used.
An IllegalArgumentException
is thrown if the encoding
is not supported.
If a RuleGrammar
is loaded and a root Rule
is defined,
then it is automatically made activatable
with
setActivatable(String, boolean)
.
If a Grammar
corresponding to grammarReference
already exists, it is over-written.
This method does not load any referenced Grammar
s.
The listGrammars
method provides a list of all Grammar
s
available.
An EngineException
is thrown if the mediaType
is
"application/srgs+xml"
and RuleGrammar
s are not supported.
This method operates as defined when the standard Conditions for Operation apply.
grammarReference
- the name of the RuleGrammar
to loadmediaType
- the type of Grammar
to loadbyteStream
- the InputStream
from which the grammar text is loadedencoding
- the encoding for the InputStream
Grammar
object
GrammarException
- if the mediaType
is not supported or if
any loaded grammar text contains an error
IllegalArgumentException
- if grammarReference
is invalid
IOException
- if an I/O error occurs
EngineStateException
- when not in the standard Conditions for Operation
EngineException
- if RuleGrammar
s are not supportedlistGrammars
,
setActivatable
,
setBase
getSupportsMarkup
, getSupportsLetterToSound
when the vocabulary specified is not knownGrammar loadGrammar(String grammarReference, String mediaType, Reader charStream) throws GrammarException, IllegalArgumentException, IOException, EngineStateException, EngineException
Grammar
from grammar text provided by
a Reader
.
The application defines the grammarReference
for
this Grammar
and the mediaType
specifies the type of Grammar
loaded.
If mediaType
is null
and cannot be determined,
it defaults to "application/srgs+xml".
For mediaType
"application/srgs+xml"
,
a RuleGrammar
is returned.
A GrammarException
is thrown if the mediaType
is not supported.
If a RuleGrammar
is loaded and a root Rule
is defined,
then it is automatically made activatable
with
setActivatable(String, boolean)
.
If a Grammar
corresponding to grammarReference
already exists, it is over-written.
This method does not load any referenced Grammar
s.
The listGrammars
method provides a list of all Grammar
s
available.
An EngineException
is thrown if the mediaType
is
"application/srgs+xml"
and RuleGrammar
s are not supported.
This method operates as defined when the standard Conditions for Operation apply.
grammarReference
- the name of the RuleGrammar
to loadmediaType
- the type of Grammar
to loadcharStream
- the Reader
from which the grammar text is loaded
Grammar
object
GrammarException
- if the mediaType
is not supported or if
any loaded grammar text contains an error
IllegalArgumentException
- if grammarReference
is invalid
IOException
- if an I/O error occurs
EngineStateException
- when not in the standard Conditions for Operation
EngineException
- if RuleGrammar
s are not supportedlistGrammars
,
setActivatable
,
setBase
getSupportsMarkup
, getSupportsLetterToSound
when the vocabulary specified is not knownGrammar loadGrammar(String grammarReference, String mediaType, String grammarText) throws GrammarException, IllegalArgumentException, IOException, EngineStateException, EngineException
RuleGrammar
from grammar text provided
as a String
.
The application defines the grammarReference
for
this RuleGrammar
and the mediaType
specifies the type of Grammar
loaded.
If mediaType
is null
and cannot be determined,
it defaults to "application/srgs+xml".
For mediaType
"application/srgs+xml"
,
a RuleGrammar
is returned.
A GrammarException
is thrown if the mediaType
is not supported.
If a RuleGrammar
is loaded and a root Rule
is defined,
then it is automatically made activatable
with
setActivatable(String, boolean)
.
If a Grammar
corresponding to grammarReference
already exists, it is over-written.
This method does not load any referenced Grammar
s.
The listGrammars
method provides a list of all Grammar
s
available.
An EngineException
is thrown if the mediaType
is
"application/srgs+xml"
and RuleGrammar
s are not supported.
This method operates as defined when the standard Conditions for Operation apply.
grammarReference
- the name of the RuleGrammar
to loadmediaType
- the type of Grammar
to loadgrammarText
- the grammar text to load
Grammar
object
GrammarException
- if the mediaType
is not supported or if
any loaded grammar text contains an error
IllegalArgumentException
- if grammarReference
is invalid
IOException
- if an I/O error occurs
EngineStateException
- when not in the standard Conditions for Operation
EngineException
- if RuleGrammar
s are not supportedsetActivatable
,
listGrammars
,
setBase
getSupportsMarkup
when mediaType is "application/srgs+xml"
, getSupportsLetterToSound
when the vocabulary specified is not knownvoid removeGrammarListener(GrammarListener listener)
GrammarListener
from this GrammarManager
.
listener
- the GrammarListener
to removeaddGrammarListener
void setGrammarMask(int mask)
GrammarEvent
s for
added GrammarListener
s.
It can improve efficiency to mask events that are not used.
Events of interest can be seen by using the logical OR of the
event constants defined in the GrammarEvent
class.
GrammarEvent.DEFAULT_MASK is the initial value.
The event mask can be set and retrieved in any Recognizer
state.
This method affects filtering of all attached GrammarListener
s.
mask
- the mask for GrammarEvent
sGrammarEvent
,
GrammarEvent.DEFAULT_MASK
,
GrammarListener
,
getGrammarMask
|
JSAPI 2.0 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
JavaTM Speech API 2.0,
Final Release v2.0.6.
© 2008, Conversay and Sun Microsystems.