|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectrita.RiObject
rita.RiGrammar
public class RiGrammar
Implementation of a (probabilistic) context-free grammar (with specific literary extensions) that performs generation from user-specified grammars.
RiGrammar rg = new RiGrammar(this, "mygrammar.g");
System.out.println(rg.expand());
RiTa grammar files are plain text files (generally ending with the '.g'
extension and residing in the 'data' folder) that follow the format below:
{
<start>
<rule1> | <rule2> | <rule3>
}
{
<rule2>
terminal1 |
terminal2 | <rule1>
# this is a comment
}
...
Primary methods of interest:
expand() which simply begins at the <start> state and
generates a string of terminals from the grammar.
expandFrom(String) which begins with the argument
String (which can consist of both non-terminals and terminals,)
and expands from there. Notice that expand() is simply
a convenient version of expandFrom("<start>");.
expandWith(String, String) takes 2 String arguments, the 1st
(a terminal) is guaranteed to be substituted for the 2nd (a non-terminal). Once this
substitution is made, the algorithm then works backwards (up the tree from the leaf)
ensuring that the terminal (terminal1) appears in the output string.
For example, with the grammar fragment above, one might call:
grammar.expandWith(terminal1, "<rule2>");
assuring not only that <rule2>will be used at least
once in the generation process, but that when it is, it will be replaced
by the terminal "hello".
{
<rule2>
[2] terminal1 |
terminal2 | <rule1>
}
{
<rule2>
The cat ran after the `getRhyme("cat");` |
The <noun> ran after the `pluralize(<noun>);`
}
Any number of arguments may be passed in a callback, but for each call,
there must be a corresponding method(with the same number and type or
arguments) in the sketch, e.g.,
String pluralize(String s) {
...
}
| Field Summary | |
|---|---|
static java.lang.String |
DEFAULT_INDENT
|
static java.lang.String |
DEFAULT_LINEBREAK
|
Grammar |
grammar
|
| Fields inherited from interface processing.core.PConstants |
|---|
A, AB, ADD, AG, ALPHA, ALPHA_MASK, ALT, AMBIENT, AR, ARC, ARGB, ARROW, B, BACKSPACE, BASELINE, BEEN_LIT, BEVEL, BLEND, BLUE_MASK, BLUR, BOTTOM, BOX, BURN, CENTER, CENTER_DIAMETER, CENTER_RADIUS, CHATTER, CLOSE, CMYK, CODED, COMPLAINT, CONTROL, CORNER, CORNERS, CROSS, CUSTOM, DA, DARKEST, DB, DEG_TO_RAD, DELETE, DG, DIAMETER, DIFFERENCE, DILATE, DIRECTIONAL, DISABLE_ACCURATE_TEXTURES, DISABLE_DEPTH_SORT, DISABLE_DEPTH_TEST, DISABLE_OPENGL_2X_SMOOTH, DISABLE_OPENGL_ERROR_REPORT, DODGE, DOWN, DR, DXF, EB, EDGE, EG, ELLIPSE, ENABLE_ACCURATE_TEXTURES, ENABLE_DEPTH_SORT, ENABLE_DEPTH_TEST, ENABLE_NATIVE_FONTS, ENABLE_OPENGL_2X_SMOOTH, ENABLE_OPENGL_4X_SMOOTH, ENABLE_OPENGL_ERROR_REPORT, ENTER, EPSILON, ER, ERODE, ERROR_BACKGROUND_IMAGE_FORMAT, ERROR_BACKGROUND_IMAGE_SIZE, ERROR_PUSHMATRIX_OVERFLOW, ERROR_PUSHMATRIX_UNDERFLOW, ERROR_TEXTFONT_NULL_PFONT, ESC, EXCLUSION, G, GIF, GRAY, GREEN_MASK, HALF_PI, HAND, HARD_LIGHT, HINT_COUNT, HSB, IMAGE, INVERT, JAVA2D, JPEG, LEFT, LIGHTEST, LINE, LINES, LINUX, MACOSX, MAX_FLOAT, MAX_INT, MIN_FLOAT, MIN_INT, MITER, MODEL, MULTIPLY, NORMAL, NORMALIZED, NX, NY, NZ, OPAQUE, OPEN, OPENGL, ORTHOGRAPHIC, OTHER, OVERLAY, P2D, P3D, PATH, PDF, PERSPECTIVE, PI, platformNames, POINT, POINTS, POLYGON, POSTERIZE, PROBLEM, PROJECT, QUAD, QUAD_STRIP, QUADS, QUARTER_PI, R, RAD_TO_DEG, RADIUS, RECT, RED_MASK, REPLACE, RETURN, RGB, RIGHT, ROUND, SA, SB, SCREEN, SG, SHAPE, SHIFT, SHINE, SOFT_LIGHT, SPB, SPG, SPHERE, SPOT, SPR, SQUARE, SR, SUBTRACT, SW, TAB, TARGA, THIRD_PI, THRESHOLD, TIFF, TOP, TRIANGLE, TRIANGLE_FAN, TRIANGLE_STRIP, TRIANGLES, TWO_PI, TX, TY, TZ, U, UP, V, VERTEX_FIELD_COUNT, VW, VX, VY, VZ, WAIT, WHITESPACE, WINDOWS, X, Y, Z |
| Constructor Summary | |
|---|---|
RiGrammar()
|
|
RiGrammar(processing.core.PApplet parent)
Initialize a RiGrammar object with no grammar data. |
|
RiGrammar(processing.core.PApplet parent,
java.lang.String grammarFileName)
Specify a grammar file conforming to the example above. To specify a rule multiplier, prepend it with the desired # in square brackets: |
|
RiGrammar(java.lang.String grammarFileName)
|
|
| Method Summary | |
|---|---|
void |
addDefinition(java.lang.String name,
java.lang.String def)
Deprecated. |
void |
closeGrammarEditor()
Returns the current callback handler for 'exec' calls |
static void |
disableExec()
Call if you want to disable the exec() mechanism for callbacks (they are enabled by default). |
void |
dumpDefinitions()
Prints the definition map to the console. |
java.lang.String |
expand()
Expands a grammar from its '<start>' symbol |
java.lang.String |
expand(boolean preserveBuffer)
Expands a grammar from its '<start>' symbol using a temporary buffer when preserveBuffer
is true. |
java.lang.String[] |
expand(int numTimes)
Expands a grammar from its '<start>' symbol one or more times. |
java.lang.String |
expandFrom(java.lang.String toExpand)
Expands the grammar, starting from the given symbol. RiGrammar.expand() is equivalent to RiGrammar.expandFrom(' |
java.lang.String |
expandFrom(java.lang.String toExpand,
boolean preserveBuffer)
Expands the grammar from the given symbol, using a temporary buffer when preserveBuffer is true. |
java.lang.String |
expandWith(java.lang.String literalString,
java.lang.String productionName)
Expands the grammar after replacing an instance of the non-terminal productionName with the terminal in literalString. |
java.lang.String |
expandWith(java.lang.String literalString,
java.lang.String productionName,
boolean preserveBuffer)
|
java.lang.String |
getBuffer()
Return all the text generated since the last call to any of the expand() methods. |
Definition |
getDefinition(java.lang.String name)
Gets a production definition by name |
java.util.Map |
getDefinitions()
Returns a Map |
java.lang.String |
getGrammarFileName()
|
static boolean |
isExecEnabled()
Returns the state of the execEnabled flag. |
void |
loadGrammarFile(java.lang.String grammarFileName)
Loads a grammar from the file specified by grammarFileName,
replacing any existing grammar file. |
static void |
main(java.lang.String[] args)
|
static void |
mainX(java.lang.String[] xxx)
|
RiGrammarEditor |
openGrammarEditor()
Provides a live, editable view of a RiGrammar text file that can be dynamically loaded into a sketch without stopping and restarting it. |
RiGrammarEditor |
openGrammarEditor(int width,
int height)
Provides a live, editable view of a RiGrammar text file that can be dynamically loaded into a sketch without stopping and restarting it. |
void |
setBuffer(java.lang.CharSequence buffer)
Resets the contents of the current buffer |
void |
setDefinition(java.lang.String name,
java.lang.String def)
Adds a production definition by name, replacing the existing one it if it exists. |
static void |
setExecEnabled()
Deprecated. |
void |
setGrammar(java.lang.String grammarFileAsString)
Deprecated. |
void |
setGrammarFromString(java.lang.String grammarRulesAsString)
Initializes a grammar from a String containing the rules (rather than a file), replacing any existing grammar. |
void |
setIncludeSpaces(boolean includeSpaces)
Tells the grammar whether or not to add spaces between the terminals that are output (default=true). |
void |
setLineBreakCharacter(java.lang.String linebreak)
Tells the grammar what String to use for line-breaks default is '&break;'. |
| Methods inherited from class rita.RiObject |
|---|
dispose, getId, getPApplet, nextId |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final java.lang.String DEFAULT_LINEBREAK
public static final java.lang.String DEFAULT_INDENT
public Grammar grammar
| Constructor Detail |
|---|
public RiGrammar()
public RiGrammar(java.lang.String grammarFileName)
public RiGrammar(processing.core.PApplet parent,
java.lang.String grammarFileName)
{
|
[2] |
}
In the above case,
grammarFileName - grammar file in the 'data' directorypublic RiGrammar(processing.core.PApplet parent)
| Method Detail |
|---|
public java.lang.String getGrammarFileName()
getGrammarFileName in interface RiGrammarIFpublic void setGrammar(java.lang.String grammarFileAsString)
setGrammarFromString(String)public void setBuffer(java.lang.CharSequence buffer)
public java.lang.String getBuffer()
public void setGrammarFromString(java.lang.String grammarRulesAsString)
setGrammarFromString in interface RiGrammarIFpublic void loadGrammarFile(java.lang.String grammarFileName)
grammarFileName,
replacing any existing grammar file.
public void dumpDefinitions()
public java.util.Map getDefinitions()
public Definition getDefinition(java.lang.String name)
public void addDefinition(java.lang.String name,
java.lang.String def)
setDefinition(String, String)
public void setDefinition(java.lang.String name,
java.lang.String def)
public java.lang.String[] expand(int numTimes)
numTimes - # of times to do the expansion
public java.lang.String expand()
expand in interface RiGrammarIFpublic java.lang.String expand(boolean preserveBuffer)
preserveBuffer
is true.
preserveBuffer - when true, will use a temporary buffer and leave the current
buffer unchanged
public java.lang.String expandFrom(java.lang.String toExpand,
boolean preserveBuffer)
preserveBuffer is true.
preserveBuffer - when true, will use a temporary buffer and leave the current
buffer unchangedpublic java.lang.String expandFrom(java.lang.String toExpand)
expandFrom in interface RiGrammarIF
public java.lang.String expandWith(java.lang.String literalString,
java.lang.String productionName)
productionName with the terminal in literalString.
This method guarantees that literalString will be present
in the generated output, assuming at least one instance of productionName
exists in the grammar.
expandWith in interface RiGrammarIFpreserveBuffer - when true, will use a temporary buffer and leave the current
buffer unchanged
RiTaException - if productionName is not found
in the grammar.
public java.lang.String expandWith(java.lang.String literalString,
java.lang.String productionName,
boolean preserveBuffer)
public void setIncludeSpaces(boolean includeSpaces)
public void setLineBreakCharacter(java.lang.String linebreak)
public static void setExecEnabled()
disableExec()public static void disableExec()
Note: this must be called before loading a grammar file.
public static boolean isExecEnabled()
public void closeGrammarEditor()
public RiGrammarEditor openGrammarEditor()
public RiGrammarEditor openGrammarEditor(int width, int height)
public static void mainX(java.lang.String[] xxx)
throws java.lang.SecurityException,
java.lang.NoSuchMethodException,
java.lang.IllegalArgumentException,
java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException
java.lang.SecurityException
java.lang.NoSuchMethodException
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetExceptionpublic static void main(java.lang.String[] args)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||