Package ru.ispras.fortress.transformer
Class NodeTransformer
- java.lang.Object
-
- ru.ispras.fortress.transformer.NodeTransformer
-
- All Implemented Interfaces:
ExprTreeVisitor
,TreeVisitor
public class NodeTransformer extends java.lang.Object implements ExprTreeVisitor
NodeTransformer
is an expression tree visitor with bottom-up substitution policy. Substitutions take place accordingly to set of rules passed to transformer before traversal.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface ru.ispras.fortress.util.TreeVisitor
TreeVisitor.Status
-
-
Constructor Summary
Constructors Constructor Description NodeTransformer()
Create new transformer instance containing no substitution rules.NodeTransformer(java.util.Map<java.lang.Enum<?>,TransformerRule> rules)
Create new transformer instance with given substitutions rules.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addRule(java.lang.Enum<?> opId, TransformerRule rule)
Add substitution rule.void
clearRules(java.lang.Enum<?> opId)
Removed all the registered rules for the specified operation.int[]
getOperandOrder()
Returns an array of operand indexes that specify in which order the operands of the currently visited operator should be visited.java.util.List<Node>
getResult()
Get collection of expression trees resulting from substitutions done during traversals.TreeVisitor.Status
getStatus()
Returns the current status of the visitor.void
onBegin()
Notifies that processing of a hierarchical structure has been started.void
onBindingBegin(NodeBinding node)
Starts visiting a binding node.void
onBindingEnd(NodeBinding node)
Finishes visiting a binding node.void
onBindingListEnd(NodeBinding node)
Notifies that visiting a bound variables list finished.void
onBoundVariableBegin(NodeBinding node, NodeVariable variable, Node value)
Notifies that visiting a bound variable has started.void
onBoundVariableEnd(NodeBinding node, NodeVariable variable, Node value)
Notifies that visiting a bound variable has finished.void
onEnd()
Notifies that processing of a hierarchical structure has been finished.void
onOperandBegin(NodeOperation expr, Node operand, int index)
Notifies that visiting an expression operand has started.void
onOperandEnd(NodeOperation expr, Node operand, int index)
Notifies that visiting an expression operand has finished.void
onOperationBegin(NodeOperation expr)
Starts visiting an operation node.void
onOperationEnd(NodeOperation expr)
Finishes visiting an operation node.void
onValue(NodeValue value)
Notifies that a value node has been visited.void
onVariable(NodeVariable variable)
Notifies that a variable node has been visited.void
reset()
Reset transformer to initial state keeping all rules.void
walk(java.util.Collection<? extends Node> trees)
Traverse and apply substitutions to expression forest.void
walk(Node root)
Traverse and apply substitutions to single expression tree.
-
-
-
Constructor Detail
-
NodeTransformer
public NodeTransformer()
Create new transformer instance containing no substitution rules.
-
NodeTransformer
public NodeTransformer(java.util.Map<java.lang.Enum<?>,TransformerRule> rules)
Create new transformer instance with given substitutions rules.- Parameters:
rules
- Map of rules. SeeaddRule()
for details.
-
-
Method Detail
-
walk
public void walk(Node root)
Traverse and apply substitutions to single expression tree. Resulting expression can be acquired viagetResult()
.- Parameters:
root
- Root of a tree to be traversed.
-
walk
public void walk(java.util.Collection<? extends Node> trees)
Traverse and apply substitutions to expression forest. Resulting expression can be acquired viagetResult()
.- Parameters:
trees
- Collection of root nodes of trees to be traversed.
-
reset
public void reset()
Reset transformer to initial state keeping all rules.
-
addRule
public void addRule(java.lang.Enum<?> opId, TransformerRule rule)
Add substitution rule.For rule to be applied to node in expression tree several conditions needs to be hold: 1) either node is NodeOperation with opId operation or node is a Node subclass which kind is opId; 2) rule.isApplicable() should be true for node given.
- Parameters:
opId
- Target node kind identifier.rule
- Rule to be added.- Throws:
java.lang.IllegalArgumentException
- if any of the parameters isnull
.
-
clearRules
public void clearRules(java.lang.Enum<?> opId)
Removed all the registered rules for the specified operation.- Parameters:
opId
- The operation identifier.
-
getResult
public java.util.List<Node> getResult()
Get collection of expression trees resulting from substitutions done during traversals.- Returns:
- List of results in order of base forest iteration.
-
getStatus
public TreeVisitor.Status getStatus()
Description copied from interface:TreeVisitor
Returns the current status of the visitor. The status guides further actions of the walker.- Specified by:
getStatus
in interfaceTreeVisitor
- Returns:
- Current visitor status.
-
onBegin
public void onBegin()
Description copied from interface:TreeVisitor
Notifies that processing of a hierarchical structure has been started.- Specified by:
onBegin
in interfaceTreeVisitor
-
onEnd
public void onEnd()
Description copied from interface:TreeVisitor
Notifies that processing of a hierarchical structure has been finished.- Specified by:
onEnd
in interfaceTreeVisitor
-
onOperationBegin
public void onOperationBegin(NodeOperation expr)
Description copied from interface:ExprTreeVisitor
Starts visiting an operation node.- Specified by:
onOperationBegin
in interfaceExprTreeVisitor
- Parameters:
expr
- Operation node.
-
onOperationEnd
public void onOperationEnd(NodeOperation expr)
Description copied from interface:ExprTreeVisitor
Finishes visiting an operation node.- Specified by:
onOperationEnd
in interfaceExprTreeVisitor
- Parameters:
expr
- Operation node.
-
getOperandOrder
public int[] getOperandOrder()
Description copied from interface:ExprTreeVisitor
Returns an array of operand indexes that specify in which order the operands of the currently visited operator should be visited. If the order is standard (i.e. [0..N-1]),null
is returned.- Specified by:
getOperandOrder
in interfaceExprTreeVisitor
- Returns:
- Array of operand indexes or
null
for the standard order.
-
onOperandBegin
public void onOperandBegin(NodeOperation expr, Node operand, int index)
Description copied from interface:ExprTreeVisitor
Notifies that visiting an expression operand has started.- Specified by:
onOperandBegin
in interfaceExprTreeVisitor
- Parameters:
expr
- Operation node.operand
- Operand node.index
- Operand index.
-
onOperandEnd
public void onOperandEnd(NodeOperation expr, Node operand, int index)
Description copied from interface:ExprTreeVisitor
Notifies that visiting an expression operand has finished.- Specified by:
onOperandEnd
in interfaceExprTreeVisitor
- Parameters:
expr
- Operation node.operand
- Operand node.index
- Operand index.
-
onValue
public void onValue(NodeValue value)
Description copied from interface:ExprTreeVisitor
Notifies that a value node has been visited.- Specified by:
onValue
in interfaceExprTreeVisitor
- Parameters:
value
- Value node.
-
onVariable
public void onVariable(NodeVariable variable)
Description copied from interface:ExprTreeVisitor
Notifies that a variable node has been visited.- Specified by:
onVariable
in interfaceExprTreeVisitor
- Parameters:
variable
- Variable node.
-
onBindingBegin
public void onBindingBegin(NodeBinding node)
Description copied from interface:ExprTreeVisitor
Starts visiting a binding node.- Specified by:
onBindingBegin
in interfaceExprTreeVisitor
- Parameters:
node
- Binding node.
-
onBindingListEnd
public void onBindingListEnd(NodeBinding node)
Description copied from interface:ExprTreeVisitor
Notifies that visiting a bound variables list finished.- Specified by:
onBindingListEnd
in interfaceExprTreeVisitor
- Parameters:
node
- Bounding node.
-
onBindingEnd
public void onBindingEnd(NodeBinding node)
Description copied from interface:ExprTreeVisitor
Finishes visiting a binding node.- Specified by:
onBindingEnd
in interfaceExprTreeVisitor
- Parameters:
node
- Binding node.
-
onBoundVariableBegin
public void onBoundVariableBegin(NodeBinding node, NodeVariable variable, Node value)
Description copied from interface:ExprTreeVisitor
Notifies that visiting a bound variable has started. Bound value expression will be visited next as general expression.Bound variables are not visited at all.
- Specified by:
onBoundVariableBegin
in interfaceExprTreeVisitor
- Parameters:
node
- Binding node.variable
- Bound variable reference.value
- Bound value expression.
-
onBoundVariableEnd
public void onBoundVariableEnd(NodeBinding node, NodeVariable variable, Node value)
Description copied from interface:ExprTreeVisitor
Notifies that visiting a bound variable has finished.- Specified by:
onBoundVariableEnd
in interfaceExprTreeVisitor
- Parameters:
node
- Binding node.variable
- Bound variable reference.value
- Bound value expression.
-
-