ilog.rules.brl.syntaxtree

Class IlrSyntaxTree

  • java.lang.Object
    • ilog.rules.brl.syntaxtree.IlrSyntaxTree
  • All Implemented Interfaces:
    ilog.rules.brl.brldf.IlrGrammarConstants, java.io.Serializable, java.lang.Cloneable


    public class IlrSyntaxTree
    extends java.lang.Object
    implements ilog.rules.brl.brldf.IlrGrammarConstants, java.lang.Cloneable, java.io.Serializable
    IlrSyntaxTree implements the abstract syntax tree that represents the current state of a BRLDF defined rule instance.

    Code Example

    The following code example shows how to print the 'LOCALE VAR' action in a custom BRL.

      public void printSimpleAction(IlrSimpleActionStatement stmt, IlrAbstractCodeGenerator codeGenerator) {
        IlrIRLCodeGenerator irlCodeGenerator = (IlrIRLCodeGenerator) codeGenerator;
        IlrSyntaxTree.Node node = stmt.getNode();
        switch (stmt.getKind()) {
          case ExtendedBALNodeTranslators.LOCAL_VAR:
            IlrSyntaxTree.Node targetNode = node.getSubNode(1);
            IlrConcept concept = IlrTranslationHelper.getConcept(targetNode, codeGenerator.getVocabulary());
            irlCodeGenerator.print(irlCodeGenerator.getTranslationSupport().getTypeString(concept));
            irlCodeGenerator.print(" ");
            String varName = IlrBRL.getVariableName(node.getSubNode(0));
            irlCodeGenerator.print(IlrTranslationHelper.makeIdentifier(varName));
            break;
        }
      }
     

    About syntax trees

    A syntax tree represents a business rule. It is made of nodes. Each node represents an operator in the grammar. A syntax tree contains one root node. Use this class to:

    • navigate through syntax tree elements and their children
    • retrieve the text contents of an element

    You use an iterator to filter nodes in the syntax tree.

    A syntax tree can be created from the text of the rule instance using a BRL parser. For more information, see IlrBRLParser.

    Since:
    JRules 4.0
    See Also:
    IlrBRLParser, Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      static int BOTTOM_UP_LEFT_TO_RIGHT
      A static constant to create an Iterator which iterates over the nodes of a tree, from left to right and from bottom to top.
      static int BOTTOM_UP_RIGHT_TO_LEFT
      A static constant to create an Iterator which iterates over the nodes of a tree, from right to left and from bottom to top.
      static int DOWN
      A static constant to create an Iterator which goes down the syntax tree.
      static int LEFT
      A static constant to create an Iterator which iterates over the preceding siblings of the current node.
      static int RIGHT
      A static constant to create an Iterator which iterates over the next siblings of the current node.
      static int SUBNODES
      A static constant to create an Iterator which iterates over the subnodes of the current node.
      static int UP
      A static constant to create an Iterator which goes up the syntax tree.
    • Constructor Summary

      Constructors 
      Constructor and Description
      IlrSyntaxTree(IlrBRLDefinition brlDefinition, IlrVocabulary vocabulary)
      Makes an instance of IlrSyntaxTree, with the given business rule language definition and vocabulary.
    • Field Detail

      • UP

        public static final int UP
        A static constant to create an Iterator which goes up the syntax tree. It iterates over the supernode of the current node, until the root of the tree is found.
        See Also:
        Constant Field Values
      • DOWN

        public static final int DOWN
        A static constant to create an Iterator which goes down the syntax tree. It iterates over the subnodes of the current node, in a depth-first manner.
        See Also:
        Constant Field Values
      • LEFT

        public static final int LEFT
        A static constant to create an Iterator which iterates over the preceding siblings of the current node.
        See Also:
        Constant Field Values
      • RIGHT

        public static final int RIGHT
        A static constant to create an Iterator which iterates over the next siblings of the current node.
        See Also:
        Constant Field Values
      • SUBNODES

        public static final int SUBNODES
        A static constant to create an Iterator which iterates over the subnodes of the current node.
        See Also:
        Constant Field Values
      • BOTTOM_UP_RIGHT_TO_LEFT

        public static final int BOTTOM_UP_RIGHT_TO_LEFT
        A static constant to create an Iterator which iterates over the nodes of a tree, from right to left and from bottom to top.
        See Also:
        Constant Field Values
      • BOTTOM_UP_LEFT_TO_RIGHT

        public static final int BOTTOM_UP_LEFT_TO_RIGHT
        A static constant to create an Iterator which iterates over the nodes of a tree, from left to right and from bottom to top.
        See Also:
        Constant Field Values
    • Constructor Detail

      • IlrSyntaxTree

        public IlrSyntaxTree(IlrBRLDefinition brlDefinition,
                     IlrVocabulary vocabulary)
        Makes an instance of IlrSyntaxTree, with the given business rule language definition and vocabulary.
        Parameters:
        brlDefinition - The business rule language definition associated to this syntax tree.
        vocabulary - The vocabulary associated to this syntax tree.
    • Method Detail

      • clone

        public java.lang.Object clone()
        Clones this syntax tree. The cloned and the original syntax tree will share the same IlrBRLDefinition.
        Overrides:
        clone in class java.lang.Object
        Since:
        JRules 4.5
        Returns:
        A clone of this syntax tree.
      • getEditedElement

        public IlrBRLElement getEditedElement()
        Allows retrieving the edited element associated to this syntax tree. If this syntax tree is the result of a parsing or editing through a grammar token model, it is associated to the edited rule element. In this case the edited element is an instance of IlrBRLRuleElement. If this syntax tree is the result of the deserialization of an AST file, this method returns null. If an editing context has been set on this AST, the edited element of this context is returned.
        Since:
        JRules 6.0
        Returns:
        The associated edited element, or null.
      • getEditingContext

        public IlrBRLRuleEditingContext getEditingContext()
        This method retrieve the Editing Context that may have been set to the syntax tree.
        Since:
        JRules 6.6
        Returns:
        the editing context set on the syntax tree or null if none has been set
      • getBRLDefinition

        public IlrBRLDefinition getBRLDefinition()
        Returns the business rule language definition from which this syntax tree is an instance.
        Returns:
        The business rule language definition corresponding to this syntax tree.
      • getVocabulary

        public IlrVocabulary getVocabulary()
        Returns the vocabulary associated to this syntax tree.
        Returns:
        The vocabulary associated to this syntax tree.
      • hasPlaceHolders

        public boolean hasPlaceHolders()
        Returns true if the syntax tree has nodes that are placeholders, false otherwise.
        Returns:
        true if the syntax tree has nodes that are placeholders.
      • hasErrorRecovery

        public boolean hasErrorRecovery()
        Returns true if the syntax tree has nodes that are in error recovery, false otherwise. Error recovery nodes are created by the parser when facing syntactic errors or other kind of errors that prevent from building a consistent AST.
        Returns:
        true if the syntax tree has nodes that are in error recovery.
      • setRoot

        public void setRoot(IlrSyntaxTree.Node root)
        Sets the root node of this syntax tree.
        Parameters:
        root - The Node object that is the root of the syntax tree.
      • getRoot

        public IlrSyntaxTree.Node getRoot()
        Gets the root node of this syntax tree.
        Returns:
        The Node object that is the root of the syntax tree.
      • setProperty

        public void setProperty(java.lang.String propName,
                       java.lang.Object value)
        Sets a property on this instance of the syntax tree.
        Parameters:
        propName - The property key.
        value - The value of the property.
      • getProperty

        public java.lang.Object getProperty(java.lang.String propName)
        Retrieves a property that has been set on this instance of a syntax tree.
        Parameters:
        propName - The property key.
        Returns:
        The value of the property, or null if the property is not registered.
      • clearProperties

        public void clearProperties()
        Clears all properties that have been set on this instance of a syntax tree.
      • removeProperty

        public void removeProperty(java.lang.String propName)
        Removes a given property from a list of properties stored on a syntax tree.
        Parameters:
        propName - The property key.
      • pushNode

        public IlrSyntaxTree.Node pushNode(java.lang.String name)
        Makes a new node called name, and adds it as a subnode of the current node. This method is used when building a syntax tree. If there is no current node, this node becomes the root of the syntax tree. This node then becomes the current node.
        Since:
        JRules 4.5
        Parameters:
        name - The name of the new node.
        Returns:
        The new node.
      • popNode

        public IlrSyntaxTree.Node popNode()
        Pops one node off the syntax tree stack, so that the supernode of the current node becomes the current node. This method is used when building a syntax tree.
        Since:
        JRules 4.5
        Returns:
        The new current node.
      • getCurrentNode

        public IlrSyntaxTree.Node getCurrentNode()
        Returns the current node being built. This method is used when building a syntax tree.
        Since:
        JRules 4.5
        Returns:
        The current node.
      • setCurrentNode

        public void setCurrentNode(IlrSyntaxTree.Node node)
        Sets the current node to be built. This method is used when building a syntax tree.
        Since:
        JRules 4.5
        Parameters:
        node - The new current node.
      • iterator

        public IlrSyntaxTree.Iterator iterator()
        Gets an iterator over this syntax tree. The iterator navigates through the children of the root node in a depth-first manner.
        Since:
        JRules 6.0
        Returns:
        An Iterator object.
      • iterator

        public IlrSyntaxTree.Iterator iterator(int direction)
        Gets an iterator that navigates through the syntax tree, starting from its root node, in the specified direction.

        The direction must be one of the following: UP, DOWN,LEFT,RIGHT,SUBNODES.

        Since:
        JRules 6.0
        Parameters:
        direction - The direction the iterator takes when it navigates through the syntax tree.
        Returns:
        An Iterator object.
      • print

        public void print(java.io.OutputStream os)
        Prints an XML representation of this syntax tree in the specified output stream. This prints each node on a new line, with an indentation that corresponds to the node depth in the tree.
        Parameters:
        os - The output stream.
      • print

        public void print(java.io.Writer writer)
        Prints an XML representation of this syntax tree in the given writer. This prints each node on a new line, with an indentation that corresponds to the node depth.
        Parameters:
        writer - The writer that handles the printing.
      • getXMLString

        public java.lang.String getXMLString()
        Returns a XML string representation of this syntax tree.
        See Also:
        print(Writer)
      • visit

        public final void visit(IlrSyntaxTree.Visitor visitor)
        Applies a visitor to this syntax tree. This method calls the method visit of the root node of this syntax tree with the visitor as argument.
        Parameters:
        visitor - The visitor.

© Copyright IBM Corp. 1987, 2017