Class: ScopedSearch::QueryLanguage::AST::LogicalOperatorNode

Inherits:
OperatorNode
  • Object
show all
Defined in:
lib/scoped_search/query_language/ast.rb

Overview

AST class for representing AND or OR constructs. Logical constructs can be simplified resulting in a less complex AST.

Instance Attribute Summary

Attributes inherited from OperatorNode

#children, #operator

Instance Method Summary collapse

Methods inherited from OperatorNode

#[], #empty?, #eql?, #infix?, #initialize, #lhs, #prefix?, #rhs, #to_a

Methods inherited from Node

#inspect

Constructor Details

This class inherits a constructor from ScopedSearch::QueryLanguage::AST::OperatorNode

Instance Method Details

#compatible_with(node) ⇒ Object

Checks whether another node is comparable so that it can be used for tree simplification. A node can only be simplified if the logical operator is equal.



133
134
135
# File 'lib/scoped_search/query_language/ast.rb', line 133

def compatible_with(node)
  node.kind_of?(LogicalOperatorNode) && node.operator == self.operator
end

#simplifyObject

Simplifies nested AND and OR constructs to single constructs with multiple arguments: e.g. (a AND (b AND c)) -> (a AND b AND c)



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/scoped_search/query_language/ast.rb', line 139

def simplify
  if children.length == 1
    # AND or OR constructs do nothing if they only have one operand
    # So remove the logal operator from the AST by simply using the opeand
    return children.first.simplify
  else
    # nested AND or OR constructs can be combined into one construct
    @children = children.map { |c| c.simplify }.map { |c| self.compatible_with(c) ? c.children : c }.flatten
    return self
  end
end