Package dev.roanh.gmark.ast
Class QueryTree
java.lang.Object
dev.roanh.gmark.ast.QueryTree
Representation of an Abstract Syntax Tree (AST) for queries.
-
Field Summary
Modifier and TypeFieldDescriptionprivate final QueryFragment
The query fragment this query tree node was derived from.private final QueryTree
For unary and binary operations the left input argument, for atoms null.private final QueryTree
For binary operations the right input argument, for unary operations and atoms null. -
Constructor Summary
ModifierConstructorDescriptionprivate
QueryTree
(QueryTree left, QueryTree right, QueryFragment fragment) Constructs a new query tree from the given input. -
Method Summary
Modifier and TypeMethodDescriptiongetLeft()
Gets the left child node of this AST node if any.Gets the operation represented by this AST node.If the operation for this AST node isOperationType.EDGE
, returns the edge label or predicate associated with edge at this AST node.getRight()
Gets the right child node of this AST node if any.boolean
isBinary()
Checks if the operation for this AST node is a binary operation, meaning it takes exactly two operands and has exactly two child nodes in the query AST.boolean
isLeaf()
Checks if this AST node is a leaf node.boolean
isUnary()
Checks if the operation for this AST node is a unary operation, meaning it takes exactly one operand and has exactly one child node in the query AST.static QueryTree
ofAtom
(QueryFragment fragment) Constructs an AST node for a query fragment representing an atomic operation.static QueryTree
ofBinary
(QueryTree left, QueryTree right, QueryFragment fragment) Constructs an AST node for a query fragment representing a binary operation.static QueryTree
ofUnary
(QueryTree left, QueryFragment fragment) Constructs an AST node for a query fragment representing a unary operation.stream()
Returns a stream over all the AST nodes in the sub-tree rooted at this AST node, including this AST node itself.toString()
private void
writeAST
(IndentWriter writer) Writes this AST in list from to the given indent writer.
-
Field Details
-
left
For unary and binary operations the left input argument, for atoms null. -
right
For binary operations the right input argument, for unary operations and atoms null. -
fragment
The query fragment this query tree node was derived from.
-
-
Constructor Details
-
QueryTree
Constructs a new query tree from the given input.- Parameters:
left
- The left input argument for unary and binary operations.right
- The right input argument binary operations.fragment
- The query fragment this AST node was derived from.
-
-
Method Details
-
isLeaf
public boolean isLeaf()Checks if this AST node is a leaf node. This implies that the operation for this AST node is an atom and that this node has no child nodes or inputs.- Returns:
- True if this AST node is a leaf node.
- See Also:
-
isUnary
public boolean isUnary()Checks if the operation for this AST node is a unary operation, meaning it takes exactly one operand and has exactly one child node in the query AST. The child node of the input for the operation in this node is given bygetLeft()
.- Returns:
- True if the operation for this AST node is unary.
- See Also:
-
isBinary
public boolean isBinary()Checks if the operation for this AST node is a binary operation, meaning it takes exactly two operands and has exactly two child nodes in the query AST. The two inputs for the operation in this node are given in order bygetLeft()
andgetRight()
.- Returns:
- True if the operation for this AST node is binary.
- See Also:
-
getOperation
Gets the operation represented by this AST node.- Returns:
- The operation for this AST node.
- See Also:
-
getPredicate
If the operation for this AST node isOperationType.EDGE
, returns the edge label or predicate associated with edge at this AST node.- Returns:
- The label at this AST node.
- Throws:
IllegalStateException
- When the operation for this AST node is not equal toOperationType.EDGE
.- See Also:
-
getLeft
Gets the left child node of this AST node if any. This child node is only present if the operation for this AST node is unary or binary and for binary operation represents the first input argument.- Returns:
- The left child node of this AST node or null if this node is a leaf.
- See Also:
-
getRight
Gets the right child node of this AST node if any. This child node is only present if the operation for this AST node is binary and represents the second input argument.- Returns:
- The right child node of this AST node of null if the operation for this node is not binary.
- See Also:
-
stream
Returns a stream over all the AST nodes in the sub-tree rooted at this AST node, including this AST node itself. The tree will be traversed in a top down manner with left child nodes appearing before right child nodes.- Returns:
- A stream over all AST nodes in the sub-tree rooted at this AST node.
-
writeAST
Writes this AST in list from to the given indent writer.- Parameters:
writer
- The indent writer to write to.
-
toString
-
ofAtom
Constructs an AST node for a query fragment representing an atomic operation.- Parameters:
fragment
- The query fragment.- Returns:
- The AST for the given query fragment.
- See Also:
-
ofUnary
Constructs an AST node for a query fragment representing a unary operation.- Parameters:
left
- The AST representing the input to the operation in the given query fragment.fragment
- The query fragment.- Returns:
- The AST for the given query fragment.
- See Also:
-
ofBinary
Constructs an AST node for a query fragment representing a binary operation.- Parameters:
left
- The AST representing the first input argument to the operation in the given query fragment.right
- The AST representing the second input argument to the operation in the given query fragment.fragment
- The query fragment.- Returns:
- The AST for the given query fragment.
- See Also:
-