def test_convert_simple_value_boolean_query_to_and_boolean_queries():
    parse_tree = \
        parser.SimpleQuery(
            parser.SpiresKeywordQuery(
                parser.InspireKeyword('author'),
                parser.Value(
                    parser.SimpleValueBooleanQuery(
                        parser.SimpleValue('foo'),
                        parser.And(),
                        parser.SimpleValueBooleanQuery(
                            parser.SimpleValue('bar'),
                            parser.Or(),
                            parser.SimpleValueNegation(parser.SimpleValue('foobar'))
                        )
                    )
                )
            )
        )

    expected_parse_tree = \
        AndOp(
            KeywordOp(Keyword('author'), Value('foo')),
            OrOp(
                KeywordOp(Keyword('author'), Value('bar')),
                NotOp(KeywordOp(Keyword('author'), Value('foobar')))
            )
        )

    restructuring_visitor = RestructuringVisitor()
    parse_tree = parse_tree.accept(restructuring_visitor)

    assert parse_tree == expected_parse_tree
示例#2
0
    def _create_keyword_op_node(value_node):
        """Creates a KeywordOp node, with the given Keyword argument and the given Value node of the closure."""
        if isinstance(value_node, NotOp):
            keyword_op_node = NotOp(KeywordOp(keyword, value_node.op))
        else:
            keyword_op_node = KeywordOp(keyword, value_node)

        return keyword_op_node
def test_foo_bar():
    query_str = 'find j Nucl.Phys. and not vol A531 and a ellis'
    print("Parsing: " + query_str)
    stateful_parser = StatefulParser()
    restructuring_visitor = RestructuringVisitor()
    _, parse_tree = stateful_parser.parse(query_str, parser.Query)
    parse_tree = parse_tree.accept(restructuring_visitor)
    expected_parse_tree = AndOp(
        KeywordOp(Keyword('journal'), Value('Nucl.Phys.')),
        KeywordOp(Keyword('author'), Value('ellis')))

    assert parse_tree == expected_parse_tree
    def _create_operator_node(value_node):
        """Creates a KeywordOp or a ValueOp node."""
        base_node = value_node.op if isinstance(value_node,
                                                NotOp) else value_node
        updated_base_node = KeywordOp(
            keyword, base_node) if keyword else ValueOp(base_node)

        return NotOp(updated_base_node) if isinstance(
            value_node, NotOp) else updated_base_node
示例#5
0
    def visit_spires_keyword_query(self, node):
        """Transform a :class:`SpiresKeywordQuery` into a :class:`KeywordOp`.

        Notes:
            In case the value being a :class:`SimpleValueBooleanQuery`, the subtree is transformed to chained
            :class:`AndOp` queries containing :class:`KeywordOp`, whose keyword is the keyword of the current node and
            values, all the :class:`SimpleValueBooleanQuery` values (either :class:`SimpleValues` or
            :class:`SimpleValueNegation`.)
        """
        keyword = node.left.accept(self)
        value = node.right.accept(self)

        if isinstance(value, SimpleValueBooleanQuery):
            return _convert_simple_value_boolean_query_to_and_boolean_queries(
                value, keyword)

        return KeywordOp(keyword, value)
示例#6
0
    def visit_invenio_keyword_query(self, node):
        """Transform an :class:`InvenioKeywordQuery` into a :class:`KeywordOp`.

        Notes:
            In case the value being a :class:`SimpleValueBooleanQuery`, the subtree is transformed to chained
            :class:`AndOp` queries containing :class:`KeywordOp`, whose keyword is the keyword of the current node and
            values, all the :class:`SimpleValueBooleanQuery` values (either :class:`SimpleValues` or
            :class:`SimpleValueNegation`.)
        """
        try:
            keyword = node.left.accept(self)
        except AttributeError:
            # The keywords whose values aren't an InspireKeyword are simple strings.
            keyword = Keyword(node.left)

        value = node.right.accept(self)

        if isinstance(value, SimpleValueBooleanQuery):
            return _convert_simple_value_boolean_query_to_and_boolean_queries(
                value, keyword)

        return KeywordOp(keyword, value)
from inspire_query_parser.ast import (
    AndOp, EmptyQuery, ExactMatchValue, GreaterEqualThanOp, GreaterThanOp,
    Keyword, KeywordOp, LessEqualThanOp, LessThanOp, MalformedQuery,
    NestedKeywordOp, NotOp, OrOp, PartialMatchValue, QueryWithMalformedPart,
    RangeOp, RegexValue, Value, ValueOp)
from inspire_query_parser.stateful_pypeg_parser import StatefulParser
from inspire_query_parser.visitors.restructuring_visitor import \
    RestructuringVisitor


@pytest.mark.parametrize(
    ['query_str', 'expected_parse_tree'],
    [
        # Find keyword combined with other production rules
        ('FIN author:\'ellis\'',
         KeywordOp(Keyword('author'), PartialMatchValue('ellis'))),
        ('Find author "ellis"',
         KeywordOp(Keyword('author'), ExactMatchValue('ellis'))),
        ('f author ellis', KeywordOp(Keyword('author'), Value('ellis'))),

        # Invenio like search
        ('author:ellis and title:boson',
         AndOp(KeywordOp(Keyword('author'), Value('ellis')),
               KeywordOp(Keyword('title'), Value('boson')))),
        ('unknown_keyword:\'bar\'',
         KeywordOp(Keyword('unknown_keyword'), PartialMatchValue('bar'))),
        ('dotted.keyword:\'bar\'',
         KeywordOp(Keyword('dotted.keyword'), PartialMatchValue('bar'))),

        # Boolean operator testing (And/Or)
        ('author ellis and title \'boson\'',