# Path: hq/hquery/evaluation_error.py
# class HqueryEvaluationError(RuntimeError):
# 
#     @classmethod
#     def must_be_node_set(cls, obj):
#         if not is_node_set(obj):
#             raise HqueryEvaluationError('Expected a node set, but found a(n) {0}'.format(obj.__class__.__name__))
# 
#     @classmethod
#     def must_be_node_set_or_sequence(cls, obj):
#         if not (is_node_set(obj) or is_sequence(obj)):
#             raise HqueryEvaluationError('Expected a node set or sequence, but found a(n) {0}'.format(
#                 obj.__class__.__name__
#             ))
# 
# Path: hq/hquery/object_type.py
# def debug_dump_anything(obj):
#     if is_any_node(obj):
#         result = debug_dump_node(obj)
#     elif is_boolean(obj) or is_number(obj) or is_hash(obj) or is_array(obj):
#         result = repr(obj)
#     elif is_string(obj):
#         result = u'string("{0}")'.format(obj)
#     elif is_node_set(obj):
#         result = u'node-set({0})'.format(', '.join(truncate_string(debug_dump_node(node), 20) for node in obj))
#     elif is_sequence(obj):
#         result = u'sequence({0})'.format(', '.join(truncate_string(debug_dump_anything(item), 20) for item in obj))
#     else:
#         raise RuntimeError("debug_dump_anything doesn't know how to handle {0}".format(obj.__class__.__name__))
#     return debug_dump_long_string(result)
# 
# Path: hq/hquery/sequences.py
# def make_sequence(sequence):
#     if not isinstance(sequence, list):
#         sequence = [sequence]
#     return sequence
# 
# def sequence_concat(first, second):
#     first = make_sequence(first)
#     first.extend(make_sequence(second))
#     return first
# 
# Path: hq/hquery/variables.py
# def push_variable(name, value):
#     global variable_stack
#     verbose_print(lambda: u'Pushing variable onto stack: let ${0} := {1}'.format(name, debug_dump_anything(value)))
#     variable_stack.append((name, value))
# 
# class variable_scope:
#     def __enter__(self):
#         self.mark = len(variable_stack)
# 
#     def __exit__(self, *args):
#         del variable_stack[self.mark:]
# 
# Path: hq/verbosity.py
# def verbose_print(text, indent_after=False, outdent_before=False):
#     if settings.VERBOSE:
#         if outdent_before:
#             pop_indent()
#         if not is_a_string(text):
#             text = text()
#         print(u'{0}{1}'.format(' ' * indent_level, text), file=sys.stderr)
#         if indent_after:
#             push_indent()


from hq.hquery.evaluation_error import HqueryEvaluationError
from hq.hquery.object_type import debug_dump_anything
from hq.hquery.sequences import make_sequence, sequence_concat
from hq.hquery.variables import push_variable, variable_scope
from hq.verbosity import verbose_print


class UnionDecomposition:

    def __init__(self):
        self.mapping_generators = None
        self.union_expression = None


    def __str__(self):
        union_str = ' | '.join('<expr>' * len(self.mapping_generators))
        return '{0} => {0}'.format(union_str)


    def evaluate(self):
        verbose_print('Evaluating union decomposition ({} clauses)'.format(len(self.mapping_generators)),
                      indent_after=True)

        sequence = make_sequence(self.union_expression())
        result = []

        for item in sequence:

# Path: hq/hquery/evaluation_error.py
# class HqueryEvaluationError(RuntimeError):
# 
#     @classmethod
#     def must_be_node_set(cls, obj):
#         if not is_node_set(obj):
#             raise HqueryEvaluationError('Expected a node set, but found a(n) {0}'.format(obj.__class__.__name__))
# 
#     @classmethod
#     def must_be_node_set_or_sequence(cls, obj):
#         if not (is_node_set(obj) or is_sequence(obj)):
#             raise HqueryEvaluationError('Expected a node set or sequence, but found a(n) {0}'.format(
#                 obj.__class__.__name__
#             ))
# 
# Path: hq/hquery/object_type.py
# def debug_dump_anything(obj):
#     if is_any_node(obj):
#         result = debug_dump_node(obj)
#     elif is_boolean(obj) or is_number(obj) or is_hash(obj) or is_array(obj):
#         result = repr(obj)
#     elif is_string(obj):
#         result = u'string("{0}")'.format(obj)
#     elif is_node_set(obj):
#         result = u'node-set({0})'.format(', '.join(truncate_string(debug_dump_node(node), 20) for node in obj))
#     elif is_sequence(obj):
#         result = u'sequence({0})'.format(', '.join(truncate_string(debug_dump_anything(item), 20) for item in obj))
#     else:
#         raise RuntimeError("debug_dump_anything doesn't know how to handle {0}".format(obj.__class__.__name__))
#     return debug_dump_long_string(result)
# 
# Path: hq/hquery/sequences.py
# def make_sequence(sequence):
#     if not isinstance(sequence, list):
#         sequence = [sequence]
#     return sequence
# 
# def sequence_concat(first, second):
#     first = make_sequence(first)
#     first.extend(make_sequence(second))
#     return first
# 
# Path: hq/hquery/variables.py
# def push_variable(name, value):
#     global variable_stack
#     verbose_print(lambda: u'Pushing variable onto stack: let ${0} := {1}'.format(name, debug_dump_anything(value)))
#     variable_stack.append((name, value))
# 
# class variable_scope:
#     def __enter__(self):
#         self.mark = len(variable_stack)
# 
#     def __exit__(self, *args):
#         del variable_stack[self.mark:]
# 
# Path: hq/verbosity.py
# def verbose_print(text, indent_after=False, outdent_before=False):
#     if settings.VERBOSE:
#         if outdent_before:
#             pop_indent()
#         if not is_a_string(text):
#             text = text()
#         print(u'{0}{1}'.format(' ' * indent_level, text), file=sys.stderr)
#         if indent_after:
#             push_indent()


# Path: hq/hquery/union_decomposition.py
from hq.hquery.evaluation_error import HqueryEvaluationError
from hq.hquery.object_type import debug_dump_anything
from hq.hquery.sequences import make_sequence, sequence_concat
from hq.hquery.variables import push_variable, variable_scope
from hq.verbosity import verbose_print



class UnionDecomposition:

    def __init__(self):
        self.mapping_generators = None
        self.union_expression = None


    def __str__(self):
        union_str = ' | '.join('<expr>' * len(self.mapping_generators))
        return '{0} => {0}'.format(union_str)


    def evaluate(self):
        verbose_print('Evaluating union decomposition ({} clauses)'.format(len(self.mapping_generators)),
                      indent_after=True)

        sequence = make_sequence(self.union_expression())
        result = []

        for item in sequence:

            verbose_print(lambda: u'Visiting item {0}'.format(debug_dump_anything(item)), indent_after=True)