|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object jess.Jesp
The Jess language parser. You can parse Jess language code directly with this class. Simply loading the contents of a file (or any other data source that can be supplied as a java.io.Reader is very easy:
Rete engine = new Rete(); FileReader file = new FileReader("myfile.clp"); try { Jesp parser = new Jesp(file, engine); parser.parse(false); } finally { file.close(); }
But this class's public interface is much richer than that. If
you want to, you can parse the file one expression at a time,
and have access to the parsed data. The method
parseExpression(jess.Context, boolean)
returns java.lang.Object,
and the returned object can either be a Value
or
one of the Jess classes that represent a construct
(Defrule
, Deftemplate
, etc.) In
addition, you can choose to have the parser execute function
calls as it parses them, or simply return them to you
unexecuted (this is controlled by the second argument to
parseExpression.
Rete engine = new Rete(); FileReader file = new FileReader("myfile.clp"); Context context = engine.getGlobalContext(); try { Jesp parser = new Jesp(file, engine); Object result = Funcall.TRUE; while (!result.equals(Funcall.EOF)) { result = parser.parseExpression(context, false); // Here you can use instanceof to determine what sort // of object "result" is, and process it however you want } } finally { file.close(); }
There are also methods in this class to control whether comments should be returned from parseExpression or just skipped, methods to fetch various pieces of information about the parsing process, and a mechanism for controlling whether warnings or just errors should be reported.
Instances of Jesp are not serializable, as they hold a reference to an input stream.
(C) 2006 Sandia National Laboratories
Field Summary | |
static java.lang.String |
PROMPT
|
Constructor Summary | |
Jesp(java.io.Reader reader,
Rete engine)
Construct a Jesp object. |
|
Jesp(Tokenizer tokenizer,
Rete engine)
Construct a Jesp object. |
Method Summary | |
void |
addArgumentChecker(java.lang.String name,
ArgumentChecker checker)
Add an ArgumentChecker to this parser. |
void |
clear()
Flush any partially-parsed information, probably to the next ')'. |
void |
clearWarnings()
Clear any pending parser warnings. |
void |
eatWhitespace()
Consumes all whitespace up to the next non-whitespace token. |
void |
eatWhitespaceAndComments()
Consumes all whitespace and comments up to the next non-whitespace, non-comment token. |
void |
error(java.lang.String routine,
java.lang.String msg,
int code,
JessToken errorToken)
|
void |
error(java.lang.String routine,
java.lang.String msg,
int code,
JessToken errorToken,
Named construct)
|
void |
error(java.lang.String routine,
java.lang.String msg,
java.lang.String[] alternatives,
int code,
JessToken errorToken)
|
void |
error(java.lang.String routine,
java.lang.String msg,
java.lang.String[] alternatives,
int code,
JessToken errorToken,
Named construct)
|
Rete |
getEngine()
Return the rule engine this parser is attached to . |
int |
getStreamPos()
Wrapper for JessTokenStream.getStreamPos() . |
java.util.List |
getWarnings()
Get the list of currently applicable warnings; each warning is an instance of ParseException . |
static boolean |
isAConstructName(java.lang.String name)
|
Value |
loadFacts(Context c)
Parses an input file containing only facts, asserts each one. |
Value |
parse(boolean prompt)
Parses an input file. |
Value |
parse(boolean prompt,
Context context)
Parses an input file by calling promptAndParseOneExpression(boolean, jess.Context) in a loop. |
Deffacts |
parseDeffacts(Rete engine,
jess.JessTokenStream jts)
parseDeffacts Syntax: (deffacts |
Deffunction |
parseDeffunction(Rete engine,
jess.JessTokenStream jts)
Parses a deffunction contstuct to create a Deffunction object. |
Defmodule |
parseDefmodule(jess.JessTokenStream jts)
Parse a defmodule construct and return a Defmodule object. |
Defquery |
parseDefquery(Context context,
Rete engine,
jess.JessTokenStream jts)
Parses a defquery construct and returns a Defquery object. |
Defrule |
parseDefrule(Context context,
Rete engine,
jess.JessTokenStream jts)
Parses a defrule construct and creates a Defrule object. |
Deftemplate |
parseDeftemplate(Context context,
Rete engine,
jess.JessTokenStream jts)
Parses a deftemplate construct and returns the parsed Deftemplate object. |
java.lang.Object |
parseExpression(Context context,
boolean executeFuncalls)
Parse and return a single expression. |
java.lang.Object |
parseExpression(Context context,
boolean executeFuncalls,
jess.JessTokenStream jts)
Parse and return a single expression from the given token stream. |
Funcall |
parseFuncall(Rete engine,
jess.JessTokenStream jts)
Parses a function call and returns the parsed Funcall object. |
Value |
promptAndParseOneExpression(boolean prompt,
Context context)
Parse and return a single expression. |
void |
setFileName(java.lang.String fileName)
Set the filename used for error reporting. |
void |
setIssueWarnings(boolean required)
Turn parser warnings on (true) or off (false). |
void |
warning(java.lang.String routine,
java.lang.String msg,
java.lang.String[] alternatives,
int code,
JessToken errorToken)
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final java.lang.String PROMPT
Constructor Detail |
public Jesp(java.io.Reader reader, Rete engine)
reader
- The Reader from which this Jesp should get its inputengine
- The engine that the parsed commands go topublic Jesp(Tokenizer tokenizer, Rete engine)
tokenizer
- The Tokenizer from which this Jesp should get its inputengine
- The engine that the parsed commands go toMethod Detail |
public void addArgumentChecker(java.lang.String name, ArgumentChecker checker)
ArgumentChecker.check(jess.Funcall, jess.JessToken, jess.ErrorSink)
method will be called whenever a matching function call is parsed.
name
- the name of the function for which this ArgumentChecker should be invokedchecker
- the ArgumentChecker to usepublic Rete getEngine()
getEngine
in interface ErrorSink
public void setIssueWarnings(boolean required)
required
- true if warnings should be issuedpublic void clearWarnings()
public java.util.List getWarnings()
ParseException
.
public int getStreamPos()
JessTokenStream.getStreamPos()
.
public void eatWhitespace() throws JessException
JessException
- if an input error occurspublic void eatWhitespaceAndComments() throws JessException
JessException
- if an input error occurspublic Value parse(boolean prompt) throws JessException
prompt
- True if a prompt should be printed.
JessException
- If anything goes wrong.public Value parse(boolean prompt, Context context) throws JessException
promptAndParseOneExpression(boolean, jess.Context)
in a loop.
Argument is true if a prompt should be printed (to the
Rete object's standard output), false for no prompt. Uses the given context to resolve variables.
prompt
- True if a prompt should be printed.
JessException
- If anything goes wrong.public Value promptAndParseOneExpression(boolean prompt, Context context) throws JessException
prompt
- True if a prompt should be printed.
JessException
- If anything goes wrong.public void clear()
public Value loadFacts(Context c) throws JessException
JessException
- If an error occurspublic java.lang.Object parseExpression(Context context, boolean executeFuncalls) throws JessException
context
- an execution contextexecuteFuncalls
- true if function calls should be executed immediately
JessException
- If anything goes wrong.public java.lang.Object parseExpression(Context context, boolean executeFuncalls, jess.JessTokenStream jts) throws JessException
context
- an execution contextexecuteFuncalls
- true if function calls should be executed immediatelyjts
- a token stream from which to parse an expression
JessException
- If anything goes wrong.public void setFileName(java.lang.String fileName)
fileName
- the filename thisk parser will report in error messagespublic Defmodule parseDefmodule(jess.JessTokenStream jts) throws JessException
jts
- the token stream
JessException
- if there is a syntax error.public Funcall parseFuncall(Rete engine, jess.JessTokenStream jts) throws JessException
JessException
- if there is a syntax error.public Deffacts parseDeffacts(Rete engine, jess.JessTokenStream jts) throws JessException
JessException
- if there is a syntax error.public Deftemplate parseDeftemplate(Context context, Rete engine, jess.JessTokenStream jts) throws JessException
JessException
- if there is a syntax error.public Defrule parseDefrule(Context context, Rete engine, jess.JessTokenStream jts) throws JessException
JessException
- if there is a syntax error.public Defquery parseDefquery(Context context, Rete engine, jess.JessTokenStream jts) throws JessException
JessException
- if there is a syntax error.public Deffunction parseDeffunction(Rete engine, jess.JessTokenStream jts) throws JessException
JessException
- if there is a syntax error.public void error(java.lang.String routine, java.lang.String msg, int code, JessToken errorToken) throws JessException
error
in interface ErrorSink
JessException
public void error(java.lang.String routine, java.lang.String msg, int code, JessToken errorToken, Named construct) throws JessException
error
in interface ErrorSink
JessException
public void error(java.lang.String routine, java.lang.String msg, java.lang.String[] alternatives, int code, JessToken errorToken) throws JessException
error
in interface ErrorSink
JessException
public void error(java.lang.String routine, java.lang.String msg, java.lang.String[] alternatives, int code, JessToken errorToken, Named construct) throws JessException
error
in interface ErrorSink
JessException
public void warning(java.lang.String routine, java.lang.String msg, java.lang.String[] alternatives, int code, JessToken errorToken)
warning
in interface ErrorSink
public static boolean isAConstructName(java.lang.String name)
|
© 2006 Sandia Corporation | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |