abstract class RuleContainer extends java.lang.Object implements Scope, RuleListElement, DebugLevels
This implementation stores all rule types in a single list. A specialized
iterator is used to visit only the types of rules required for a particular
iteration (see rules
). This iterator is not accessed directly by
client code. Instead, a concrete subclass will call the apply(int)
method to iterate over the target set of rules and apply them in turn.
Named expressions support associates a library of aliased expression strings with a list of rules, such that any individual rule can access a pre-defined expression by its alias.
A pool of readable and writable user variables is registered with the expression engine, for use in rules contained directly or indirectly within this container. User variables are accessed directly by variable name in expressions. Duplicate variable names are not permitted.
The level of message output generated by the pattern engine as a whole is maintained by this class. It is accessible via static methods.
getDebugLevel()
,
setDebugLevel(int)
Modifier and Type | Field and Description |
---|---|
private static int |
debugLevel
Current debug level
|
private java.util.Map<java.lang.String,NamedFunction> |
functionLibrary
Library of named functions associated with this ruleset
|
private java.util.Set<RuleContainer> |
includes
Rule containers included for their expression libraries
|
private java.util.Map<java.lang.String,PatternWorker> |
namespaces
Pattern workers indexed by namespace alias
|
private RuleContainer |
parent
Parent container which provides enclosing scope
|
private boolean |
processingGetFunctionContainer
Flag to prevent direct or indirect recursion during search.
|
protected static int |
RULE_ASCENT
Constant indicating ascent-rule type
|
protected static int |
RULE_DESCENT
Constant indicating descent-rule type
|
protected static int |
RULE_INIT
Constant indicating init-rule type
|
protected static int |
RULE_NEXT_CHILD
Constant indicating next-child-rule type
|
protected static int |
RULE_NONE
Constant indicating no rule type
|
protected static int |
RULE_POST
Constant indicating post-rule type
|
protected static int |
RULE_WALK
Constant indicating walk-rule type
|
private java.util.List<RuleListElement> |
rules
List of all rules of all types
|
MSG_DEBUG, MSG_NONE, MSG_STATUS, MSG_TRACE
Modifier | Constructor and Description |
---|---|
protected |
RuleContainer(RuleContainer parent)
Constructor which assigns this container's enclosing scope.
|
Modifier and Type | Method and Description |
---|---|
Rule |
addRule(java.lang.String expr,
int type,
java.lang.String file,
int line)
Add a rule of the specified type to the container for later use.
|
RuleSet |
addRuleSet()
Add a new, empty ruleset to the end of the current pipeline.
|
void |
apply(int type)
Apply every rule of the specified type, in the order they were added
to this container.
|
void |
cleanup()
Termination hook to allow resources to be cleaned up when this object is
about to go out of service.
|
static int |
getDebugLevel()
Return the current debug level for the process.
|
Scope |
getEnclosingScope()
Return the object which defines the enclosing scope to this rule
container's scope.
|
(package private) NamedFunction |
getFunction(java.lang.String name)
Get a named function, given the alias which is used to uniquely
identify that function.
|
(package private) RuleContainer |
getFunctionContainer(java.lang.String name,
RuleContainer omit)
Get the container instance which contains the function named
name in its function library. |
(package private) PatternWorker |
getWorker(java.lang.String name)
Retrieve the pattern worker, if any, registered for the given
namespace.
|
boolean |
hasType(int type)
Indicates whether this object's internal list of rules contains any
rules of the specified type.
|
(package private) void |
include(RuleContainer included)
Add a rule container to this container's set of includes.
|
boolean |
isContainer()
Reports that this instance is a rule container.
|
void |
putFunction(java.lang.String name,
NamedFunction func)
Store a pre-defined function under the specified name.
|
void |
registerVariable(java.lang.String name,
java.lang.Class<?> type,
java.lang.String expression)
Register a new user variable with this container.
|
void |
registerVariableValue(java.lang.String name,
java.lang.Class<?> type,
java.lang.Object value)
Register a new user variable with this container.
|
boolean |
registerWorker(java.lang.String name,
PatternWorker worker)
Register a pattern worker implementation with this rule container,
such that its methods will be called by the engine at the appropriate
times.
|
(package private) boolean |
registerWorker(java.lang.String name,
java.lang.String className)
Instantiate and register a pattern worker implementation, given its
class name.
|
java.util.Iterator<RuleListElement> |
ruleList()
Return an iterator to the list of
RuleListElements , each
element of which can be either a Rule or a
RuleContainer instance. |
static void |
setDebugLevel(int debugLevel)
Set the current debug level for the process.
|
protected static final int RULE_NONE
protected static final int RULE_INIT
protected static final int RULE_WALK
protected static final int RULE_POST
protected static final int RULE_DESCENT
protected static final int RULE_NEXT_CHILD
protected static final int RULE_ASCENT
private static int debugLevel
private java.util.Map<java.lang.String,PatternWorker> namespaces
private RuleContainer parent
private java.util.List<RuleListElement> rules
private java.util.Set<RuleContainer> includes
private java.util.Map<java.lang.String,NamedFunction> functionLibrary
private boolean processingGetFunctionContainer
protected RuleContainer(RuleContainer parent)
parent
- Rule container which provides enclosing scope to this rule
container. May be null
.public static int getDebugLevel()
public static void setDebugLevel(int debugLevel)
debugLevel
- Debug message output level using MSG_*
constants.public Scope getEnclosingScope()
getEnclosingScope
in interface Scope
null
.public void registerVariable(java.lang.String name, java.lang.Class<?> type, java.lang.String expression)
type
and
expression
are provided, type
must match the
return type of expression
.name
- Name by which this variable will be referenced in expressions.type
- Data type of the variable. May be null
if and
only if expression
is not null
and expression
's return type is not
null
.expression
- Expression which will be used to initialize the variable each
time it is reset
by the pattern engine. May be null
if and only
if type
is not null
.SymbolException
- if the variable's name is already in use in this container's
scope.ExpressionException
- if type
is a class which is not assignable from
expression
's return type.java.lang.IllegalArgumentException
- if type
is not provided and it cannot be
determined from expression
.SymbolResolver.resetVariables(com.goldencode.expr.Scope, boolean)
public void registerVariableValue(java.lang.String name, java.lang.Class<?> type, java.lang.Object value)
type
and
value
are provided, type
must match the
return type of expression
.name
- Name by which this variable will be referenced in expressions.type
- Data type of the variable. May be null
if and
only if value
is not null
.value
- The specific instance to which the variable will be
will be initialized. Unless assigned explicitly from a rule
set, this instance will not be set to null
using reset
in the
pattern engine on subsequent runs. May be null
if
and only if type
is not null
.SymbolException
- if the variable's name is already in use in this container's
scope.java.lang.IllegalArgumentException
- if type
is not provided and it cannot be
determined from expression
.SymbolResolver.resetVariables(com.goldencode.expr.Scope, boolean)
public void putFunction(java.lang.String name, NamedFunction func)
evalLib
or execLib
user function to reference
the named function in rules.
Note that if any named function already exists in the current library
for this container with the alias name
at the time this
method is invoked, it will be replaced by func
.
name
- Alias under which to store the function.func
- The associated named function.public boolean registerWorker(java.lang.String name, PatternWorker worker)
PatternWorker.initialize()
method.
If an instance of the same pattern worker class is already registered,
worker
is discarded, and the previously registered
instance is associated with name
instead.
name
- Optional namespace which can be used as a qualifier in
expressions to indicate that this pattern worker should be
used to resolve a constant to a literal. Should be
non-null
for any pattern worker implementation
which implements PatternWorker.resolveConstant(java.lang.String)
directly; may be null
otherwise, however, only
one null
name is allowed per rule container.worker
- The pattern worker object to register.true
if the worker was successfully registered;
false
if a worker of the same type was already
registered. In either case, namespace
is mapped
to the registered worker upon return from this method.java.lang.IllegalArgumentException
- if name
is already in use or if it is an invalid
namespace identifier.public RuleSet addRuleSet()
public Rule addRule(java.lang.String expr, int type, java.lang.String file, int line)
expr
- Logical expression which will be compiled into the condition
expression of the rule.type
- Type of rule to add: init, walk, post, etc., using the
RULE_*
constants.file
- The file the rule is defined in, used for tracing.line
- The file line the rule is defined at, used for tracing.java.lang.IllegalArgumentException
- if the rule type is unrecognized.public java.util.Iterator<RuleListElement> ruleList()
RuleListElements
, each
element of which can be either a Rule
or a
RuleContainer
instance.public void cleanup()
Cascades to all contained RuleListElements
.
cleanup
in interface RuleListElement
public void apply(int type)
apply
in interface RuleListElement
type
- Type of rule to apply: init, walk, or post, using the
RULE_*
constants.public boolean isContainer()
isContainer
in interface RuleListElement
true
.public boolean hasType(int type)
hasType
in interface RuleListElement
type
- Type of rule for which to search: init, walk, or post, using
the RULE_*
constants.true
if the list of rules contains any rule of
the specified type, else false
.boolean registerWorker(java.lang.String name, java.lang.String className) throws java.lang.ClassNotFoundException, java.lang.InstantiationException, java.lang.IllegalAccessException
PatternWorker.initialize()
method
being invoked immediately after construction.
If an instance of the specified class is already registered, this request is ignored. There can be only one.
name
- Optional namespace which can be used as a qualifier in
expressions to indicate that this pattern worker should be
used to resolve a constant to a literal. Should be
non-null
for any pattern worker implementation
which implements PatternWorker.resolveConstant(java.lang.String)
directly; may be null
otherwise.className
- Fully qualified name of the pattern worker's class.java.lang.ClassNotFoundException
- if a class named className
is not found in the
JVM's classpath.java.lang.InstantiationException
- if an error occurs while instantiating the class (e.g., the
default constructor throws an exception).java.lang.IllegalAccessException
- if the target class or its default constructor is not
accessible.java.lang.ClassCastException
- if the target class does not implement the PatternWorker
interface.PatternWorker getWorker(java.lang.String name)
name
- Namespace identifier with which the requested pattern worker
is associated. May not be null
.null
if no match was found.void include(RuleContainer included)
included
- Rule container to include.NamedFunction getFunction(java.lang.String name)
name
- Alias which identifies the function.name
, or
null
if no function is found.RuleContainer getFunctionContainer(java.lang.String name, RuleContainer omit)
name
in its function library. This may be this container
or one that this container includes, or null
.name
- Name of function to look up.omit
- RuleContainer instance to omit from the list of includes to
check. This prevents circular references between parents and
included containers, or between included containers themselves.null
if neither this container nor any it
includes can resolve the given name to an function.