コード例 #1
0
    def test_other__service1(self):
        tc_desc = (
            "Test if a nested service pattern is properly translated "
            "into the query text. "
            "The query must also be executable and shall not violate any SPARQL query syntax."
        )
        try:
            query_tree = parser.parseQuery(self.query_text)
        except Exception as e:
            print(e)
            return Test(
                test_number=24,
                tc_desc=tc_desc,
                expected_result="0",
                actual_result="Not executable. Error returned from parseQuery",
            )
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=24,
            tc_desc=tc_desc,
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra,
        )

        return test
コード例 #2
0
    def test_functions__functional_forms(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)

        test = Test(
            test_number=1,
            tc_desc=
            'Test if functional forms are properly translated into the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        try:
            self.rdf_engine.get_data(self.query_from_query_from_algebra,
                                     yn_timestamp_query=False)
        except Exception as e:
            print(e)
            print(
                "The query must be executable. Otherwise, the test has failed."
            )
            return Test(test_number=test.test_number,
                        tc_desc=test.tc_desc,
                        expected_result="0",
                        actual_result="not_executable")

        return test
コード例 #3
0
    def test_other__service2(self):
        tc_desc = 'Test if "service" along with its service string is properly translated ' \
                  'into the query text. ' \
                  'The query must also be executable and shall not violate any SPARQL query syntax.'
        try:
            query_tree = parser.parseQuery(self.query_text)
        except Exception as e:
            print(e)
            return Test(test_number=25,
                        tc_desc=tc_desc,
                        expected_result="0",
                        actual_result=
                        "Not executable. Error returned from parseQuery().")
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(test_number=25,
                    tc_desc=tc_desc,
                    expected_result=self.query_from_algebra,
                    actual_result=self.query_from_query_from_algebra)

        return test
コード例 #4
0
    def test_property_path__negated_property_set(self):
        tc_desc = 'Test if a negated property set gets properly translated into the query text. ' \
                  'The query must also be executable and shall not violate any SPARQL query syntax.'
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        try:
            self.query_from_algebra = translateAlgebra(query_algebra)
        except TypeError as e:
            print(e)
            return Test(
                test_number=29,
                tc_desc=tc_desc,
                expected_result="0",
                actual_result=
                "Not executable. n3() method of NegatedPath class should be fixed. "
            )

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(test_number=29,
                    tc_desc=tc_desc,
                    expected_result=self.query_from_algebra,
                    actual_result=self.query_from_query_from_algebra)

        return test
コード例 #5
0
def parse_query(query, dataset, default_graph, server_url):
    """Parse a regular SPARQL query into a query execution plan"""
    logical_plan = translateQuery(parseQuery(query)).algebra
    cardinalities = list()
    iterator = parse_query_node(logical_plan, dataset, [default_graph],
                                server_url, cardinalities)
    return iterator, cardinalities
コード例 #6
0
ファイル: query_parser.py プロジェクト: momo54/sage-engine
def parse_query(query: str, dataset: Dataset,
                default_graph: str) -> Tuple[PreemptableIterator, dict]:
    """Parse a read-only SPARQL query into a physical query execution plan.

    For parsing SPARQL UPDATE query, please refers to the `parse_update` method.

    Args:
      * query: SPARQL query to parse.
      * dataset: RDF dataset on which the query is executed.
      * default_graph: URI of the default graph.

    Returns: A tuple (`iterator`, `cardinalities`) where:
      * `iterator` is the root of a pipeline of iterators used to execute the query.
      * `cardinalities` is the list of estimated cardinalities of all triple patterns in the query.

    Throws: `UnsupportedSPARQL` is the SPARQL query contains features not supported by the SaGe query engine.
    """
    # transaction timestamp
    start_timestamp = datetime.now()
    # rdflib has no tool for parsing both read and update query,
    # so we must rely on a try/catch dirty trick...
    try:
        logical_plan = translateQuery(parseQuery(query)).algebra
        cardinalities = list()
        iterator = parse_query_alt(logical_plan,
                                   dataset, [default_graph],
                                   cardinalities,
                                   as_of=start_timestamp)
        return iterator, cardinalities
    except ParseException:
        return parse_update(query,
                            dataset,
                            default_graph,
                            as_of=start_timestamp)
コード例 #7
0
    def test_query_optional(self):

        q = """
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX schema: <http://schema.org/>
            PREFIX dbr: <http://dbpedia.org/resource/>
            PREFIX dbo: <http://dbpedia.org/ontology/>
            SELECT ?leader ?label ?leaderobj 
            WHERE {
                ?leader rdfs:label ?label. 
                ?leader rdf:type schema:Person.
                OPTIONAL {?leaderobj dbo:leader ?leader}
            }
            """

        start_time = time()
        pq = parser.parseQuery(q)
        logging.debug ('parsing took %fs' % (time() - start_time))

        logging.debug(pq)
        tq = algebra.translateQuery(pq)

        self.sas.debug_log_algebra (tq)

        logging.debug(tq.algebra.__class__)
コード例 #8
0
def prepareQuery(queryString, initNs={}, base=None):
    """
    Parse and translate a SPARQL Query
    """
    ret = translateQuery(parseQuery(queryString), base, initNs)
    ret._original_args = (queryString, initNs, base)
    return ret
コード例 #9
0
ファイル: processor.py プロジェクト: drewp/rdflib
def prepareQuery(queryString, initNs={}, base=None):
    """
    Parse and translate a SPARQL Query
    """
    ret = translateQuery(parseQuery(queryString), base, initNs)
    ret._original_args = (queryString, initNs, base)
    return ret
コード例 #10
0
ファイル: discover.py プロジェクト: fserena/agora-gw
def extract_bgps(q, cache=None, init_ns={}):
    from agora.graph.evaluate import traverse_part
    from rdflib.plugins.sparql.algebra import translateQuery
    from rdflib.plugins.sparql.parser import parseQuery
    from rdflib.plugins.sparql.sparql import Query

    if cache is not None and q in cache:
        return cache[q]

    if not isinstance(q, Query):
        parsetree = parseQuery(q)
        query = translateQuery(parsetree, None, init_ns)
    else:
        query = q

    part = query.algebra
    filters = {}
    bgps = []

    for p in traverse_part(part, filters):
        bgps.append(p)

    if cache is not None:
        cache[q] = bgps, filters
    return bgps, filters
コード例 #11
0
ファイル: filter.py プロジェクト: sage-org/sage-engine
 def __init__(self, source: PreemptableIterator, expression: str, context: dict):
     super(FilterIterator, self).__init__()
     self._source = source
     self._raw_expression = expression
     # compile the expression using rdflib
     compiled_expr = parseQuery(f"SELECT * WHERE {{?s ?p ?o . FILTER({expression})}}")
     compiled_expr = translateQuery(compiled_expr)
     self._prologue = compiled_expr.prologue
     self._compiled_expression = compiled_expr.algebra.p.p.expr
コード例 #12
0
ファイル: filter.py プロジェクト: loic44650/sage-engine
 def __init__(self, source, expression):
     super(FilterIterator, self).__init__()
     self._source = source
     self._raw_expression = expression
     # compile the expression using rdflib
     compiled_expr = parseQuery("SELECT * WHERE {?s ?p ?o FILTER(" +
                                expression + ")}")
     compiled_expr = translateQuery(compiled_expr)
     self._compiled_expression = compiled_expr.algebra.p.p.expr
コード例 #13
0
    def test_solution_modifiers__slice(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=37,
            tc_desc="Test if slice get properly translated into the limit and offset. "
            "The query must also be executable and shall not violate any SPARQL query syntax.",
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra,
        )

        return test
コード例 #14
0
    def test_property_path__alternative_path(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=27,
            tc_desc="Test if an alternative path gets properly translated into the query text. "
            "The query must also be executable and shall not violate any SPARQL query syntax.",
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra,
        )

        return test
コード例 #15
0
    def test_operators__unary(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=23,
            tc_desc="Test if unary expressions are properly translated into the query text. "
            "The query must also be executable and shall not violate any SPARQL query syntax.",
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra,
        )

        return test
コード例 #16
0
    def test_functions__functions_on_dates_and_time(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=7,
            tc_desc=
            'Test if functions on dates and time are properly translated into the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        return test
コード例 #17
0
    def test_property_path__zero_or_one_path(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=33,
            tc_desc=
            'Test if a zeroOrOne path gets properly translated into the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        return test
コード例 #18
0
    def test_graph_patterns__left_join(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=16,
            tc_desc=
            'Test if "left join" gets properly translated into "OPTIONAL {...}" in the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        return test
コード例 #19
0
    def test_functions__functional_forms_not_exists(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=2,
            tc_desc=
            'Test if the not exists form is properly translated into the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        return test
コード例 #20
0
    def test_operators__conditional_or(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=21,
            tc_desc=
            'Test if "conditional ors (||)" are properly translated into the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        return test
コード例 #21
0
    def test_graph_patterns__bgp(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=9,
            tc_desc=
            'Test if basic graph patterns are properly translated into the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        return test
コード例 #22
0
    def test_graph_patterns__aggregate_join(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=8,
            tc_desc='Test if aggregate join including all aggregation functions '
            'are properly translated into the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        return test
コード例 #23
0
    def test_solution_modifiers__to_multiset(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=38,
            tc_desc=
            'Test if subqueries get properly translated into the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        return test
コード例 #24
0
    def test_other__values(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=26,
            tc_desc=
            'Test if "values" gets properly translated into the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        return test
コード例 #25
0
    def test_integration__complex_query1(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=39,
            tc_desc=
            'Test a query with multiple graph patterns and solution modifiers '
            'gets properly translated into the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        return test
コード例 #26
0
    def test_graph_patterns__extend(self):
        query_tree = parser.parseQuery(self.query_text)
        query_algebra = algebra.translateQuery(query_tree)
        self.query_from_algebra = translateAlgebra(query_algebra)

        query_tree_2 = parser.parseQuery(self.query_from_algebra)
        query_algebra_2 = algebra.translateQuery(query_tree_2)
        self.query_from_query_from_algebra = translateAlgebra(query_algebra_2)
        _pprint_query(self.query_from_query_from_algebra)

        test = Test(
            test_number=10,
            tc_desc=
            'Test if "extend" (=Bind explicitly or implicitly in projection) '
            'gets properly translated into the query text. '
            'The query must also be executable and shall not violate any SPARQL query syntax.',
            expected_result=self.query_from_algebra,
            actual_result=self.query_from_query_from_algebra)

        return test
コード例 #27
0
ファイル: evaluate.py プロジェクト: oeg-upm/agora-py
def extract_bgps(query, prefixes):
    parsetree = parseQuery(query)
    query = translateQuery(parsetree, initNs=prefixes)
    part = query.algebra
    filters = {}
    bgps = []

    for p in traverse_part(part, filters):
        bgps.append(p)

    for bgp in bgps:
        yield bgp, {v: filters[v] for v in bgp._vars if v in filters}
コード例 #28
0
def extract_bgps(query, prefixes):
    from rdflib.plugins.sparql.algebra import translateQuery
    from rdflib.plugins.sparql.parser import parseQuery

    parsetree = parseQuery(query)
    query = translateQuery(parsetree, initNs=prefixes)
    part = query.algebra
    filters = {}
    bgps = []

    for p in traverse_part(part, filters):
        bgps.append(p)

    for bgp in bgps:
        yield bgp, {v: filters[v] for v in bgp._vars if v in filters}
コード例 #29
0
ファイル: processor.py プロジェクト: 0038lana/Test-Task
    def query(
            self, strOrQuery, initBindings={},
            initNs={}, base=None, DEBUG=False):
        """
        Evaluate a query with the given initial bindings, and initial
        namespaces. The given base is used to resolve relative URIs in
        the query and will be overridden by any BASE given in the query.
        """

        if not isinstance(strOrQuery, Query):
            parsetree = parseQuery(strOrQuery)
            query = translateQuery(parsetree, base, initNs)
        else:
            query = strOrQuery

        return evalQuery(self.graph, query, initBindings, base)
コード例 #30
0
ファイル: processor.py プロジェクト: rac021/rdf-translator
    def query(
            self, strOrQuery, initBindings={},
            initNs={}, base=None, DEBUG=False):
        """
        Evaluate a query with the given initial bindings, and initial
        namespaces. The given base is used to resolve relative URIs in
        the query and will be overridden by any BASE given in the query.
        """

        if not isinstance(strOrQuery, Query):
            parsetree = parseQuery(strOrQuery)
            query = translateQuery(parsetree, base, initNs)
        else:
            query = strOrQuery

        return evalQuery(self.graph, query, initBindings, base)
コード例 #31
0
ファイル: sparqlalchemy.py プロジェクト: gooofy/sparqlalchemy
    def query(self, q):

        global engine

        logging.debug(q)

        start_time = time()
        pq = parser.parseQuery(q)
        logging.debug('parsing took %fs' % (time() - start_time))

        logging.debug(pq)
        tq = algebra.translateQuery(pq)

        self.debug_log_algebra(tq)

        # print 'tq.prologue:', tq.prologue

        return self.query_algebra(tq.algebra)
コード例 #32
0
ファイル: QueryManager.py プロジェクト: serrano-p/SWEEP
 def extractBGP(self, query):
     try:
         tree = parseQuery(query)
     except Exception as e:
         raise ParseQueryException(e.args)
     else:
         try:
             # pprint(tree)
             q = translateQuery(tree).algebra
             # pprint(q)
             #---
             assert q is not None
             #---
         except SPARQLError as e:
             raise SPARQLException(e.args)
         except Exception as e:
             m = e.__str__().split(':')
             if (m[0] == 'Unknown namespace prefix '):
                 pr = m[1].strip()
                 if (pr in self.defaultPrefixes):
                     n_query = 'PREFIX ' + pr + ': <' + self.defaultPrefixes[
                         pr] + '> #ADD by BE4DBPedia \n' + query
                     return self.extractBGP(n_query)
                 else:
                     raise NSException(e.args)
             else:
                 raise TranslateQueryException(e.args)
         else:
             try:
                 BGPSet = getBGP(q)
                 if valid(BGPSet):
                     if self.modeStat:
                         l = len(BGPSet)
                         if l > self.maxTP:
                             self.bgpStat.stdput('more')
                         else:
                             self.bgpStat.stdput(str(l))
                     else:
                         if len(BGPSet) == 0: parse('', q)
                     return (BGPSet, query)
                 else:
                     raise BGPUnvalidException('BGP Not Valid')
             except ValueError as e:
                 raise BGPException(e.args)
コード例 #33
0
ファイル: processor.py プロジェクト: 0038lana/Test-Task
def prepareQuery(queryString, initNs={}, base=None):
    """
    Parse and translate a SPARQL Query
    """
    return translateQuery(parseQuery(queryString), base, initNs)
コード例 #34
0
ファイル: test_dawg.py プロジェクト: afujii/rdflib
def query_test(t):
    uri, name, comment, data, graphdata, query, resfile, syntax = t

    # the query-eval tests refer to graphs to load by resolvable filenames
    rdflib_sparql_module.SPARQL_LOAD_GRAPHS = True

    if uri in skiptests:
        raise SkipTest()

    def skip(reason='(none)'):
        print "Skipping %s from now on." % uri
        f = open("skiptests.list", "a")
        f.write("%s\t%s\n" % (uri, reason))
        f.close()

    try:
        g = Dataset()
        if data:
            g.default_context.load(data, format=_fmt(data))

        if graphdata:
            for x in graphdata:
                g.load(x, format=_fmt(x))

        if not resfile:
            # no result - syntax test

            if syntax:
                translateQuery(parseQuery(
                    open(query[7:]).read()), base=urljoin(query, '.'))
            else:
                # negative syntax test
                try:
                    translateQuery(parseQuery(
                        open(query[7:]).read()), base=urljoin(query, '.'))

                    assert False, 'Query should not have parsed!'
                except:
                    pass  # it's fine - the query should not parse
            return

        # eval test - carry out query
        res2 = g.query(open(query[7:]).read(), base=urljoin(query, '.'))

        if resfile.endswith('ttl'):
            resg = Graph()
            resg.load(resfile, format='turtle', publicID=resfile)
            res = RDFResultParser().parse(resg)
        elif resfile.endswith('rdf'):
            resg = Graph()
            resg.load(resfile, publicID=resfile)
            res = RDFResultParser().parse(resg)
        elif resfile.endswith('srj'):
            res = Result.parse(open(resfile[7:]), format='json')
        elif resfile.endswith('tsv'):
            res = Result.parse(open(resfile[7:]), format='tsv')

        elif resfile.endswith('csv'):
            res = Result.parse(open(resfile[7:]), format='csv')

            # CSV is lossy, round-trip our own resultset to
            # lose the same info :)

            # write bytes, read strings...
            s = BytesIO()
            res2.serialize(s, format='csv')
            print s.getvalue()
            s = StringIO(s.getvalue().decode('utf-8'))  # hmm ?
            res2 = Result.parse(s, format='csv')

        else:
            res = Result.parse(open(resfile[7:]), format='xml')

        if not DETAILEDASSERT:
            eq(res.type, res2.type, 'Types do not match')
            if res.type == 'SELECT':
                eq(set(res.vars), set(res2.vars), 'Vars do not match')
                comp = bindingsCompatible(
                    set(res),
                    set(res2)
                )
                assert comp, 'Bindings do not match'
            elif res.type == 'ASK':
                eq(res.askAnswer, res2.askAnswer, 'Ask answer does not match')
            elif res.type in ('DESCRIBE', 'CONSTRUCT'):
                assert isomorphic(
                    res.graph, res2.graph), 'graphs are not isomorphic!'
            else:
                raise Exception('Unknown result type: %s' % res.type)
        else:
            eq(res.type, res2.type,
               'Types do not match: %r != %r' % (res.type, res2.type))
            if res.type == 'SELECT':
                eq(set(res.vars),
                   set(res2.vars), 'Vars do not match: %r != %r' % (
                   set(res.vars), set(res2.vars)))
                assert bindingsCompatible(
                    set(res),
                    set(res2)
                ), 'Bindings do not match: \n%s\n!=\n%s' % (
                    res.serialize(format='txt', namespace_manager=g.namespace_manager),
                    res2.serialize(format='txt', namespace_manager=g.namespace_manager))
            elif res.type == 'ASK':
                eq(res.askAnswer,
                   res2.askAnswer, "Ask answer does not match: %r != %r" % (
                   res.askAnswer, res2.askAnswer))
            elif res.type in ('DESCRIBE', 'CONSTRUCT'):
                assert isomorphic(
                    res.graph, res2.graph), 'graphs are not isomorphic!'
            else:
                raise Exception('Unknown result type: %s' % res.type)

    except Exception, e:

        if isinstance(e, AssertionError):
            failed_tests.append(uri)
            fails[str(e)] += 1
        else:
            error_tests.append(uri)
            errors[str(e)] += 1

        if DEBUG_ERROR and not isinstance(e, AssertionError) or DEBUG_FAIL:
            print "======================================"
            print uri
            print name
            print comment

            if not resfile:
                if syntax:
                    print "Positive syntax test"
                else:
                    print "Negative syntax test"

            if data:
                print "----------------- DATA --------------------"
                print ">>>", data
                print open(data[7:]).read()
            if graphdata:
                print "----------------- GRAPHDATA --------------------"
                for x in graphdata:
                    print ">>>", x
                    print open(x[7:]).read()

            print "----------------- Query -------------------"
            print ">>>", query
            print open(query[7:]).read()
            if resfile:
                print "----------------- Res -------------------"
                print ">>>", resfile
                print open(resfile[7:]).read()

            try:
                pq = parseQuery(open(query[7:]).read())
                print "----------------- Parsed ------------------"
                pprintAlgebra(translateQuery(pq, base=urljoin(query, '.')))
            except:
                print "(parser error)"

            print decodeStringEscape(unicode(e))

            import pdb
            pdb.post_mortem(sys.exc_info()[2])
            # pdb.set_trace()
            # nose.tools.set_trace()
        raise
コード例 #35
0
ファイル: CommandLine.py プロジェクト: nuoya/FuXi
def why(options, factGraph, network, nsBinds, ruleSet, workingMemory):
    if options.builtinTemplates:
        builtinTemplateGraph = Graph().parse(options.builtinTemplates,
                                             format='n3')
    else:
        builtinTemplateGraph = Graph()
    factGraph.templateMap = dict([
        (pred, template) for pred, _ignore, template in
        builtinTemplateGraph.triples((None, TEMPLATES.filterTemplate, None))])
    network.nsMap['pml'] = PML
    network.nsMap['gmp'] = GMP_NS
    network.nsMap['owl'] = OWL_NS
    nsBinds.update(network.nsMap)
    network.nsMap = nsBinds
    query = parseQuery(options.why)
    translateQuery(query, initNs=nsBinds)
    goals = sum([triple_block['triples'] for triple_block
                in query[1].where['part']], [])
    defaultBasePreds = []
    defaultDerivedPreds = set()
    hybridPredicates = []
    mapping = dict(factGraph.namespace_manager.namespaces())
    for edb in options.edb:
        pref, uri = edb.split(':')
        defaultBasePreds.append(URIRef(mapping[pref] + uri))
    noMagic = []
    for pred in options.noMagic:
        pref, uri = pred.split(':')
        noMagic.append(URIRef(mapping[pref] + uri))
    if options.ddlGraph:
        ddlGraph = Graph().parse(options.ddlGraph, format='n3')
        # @TODO: should also get hybrid predicates from DDL graph
        defaultDerivedPreds = IdentifyDerivedPredicates(
            ddlGraph, Graph(), ruleSet)
    else:
        for idb in options.idb:
            pref, uri = idb.split(':')
            defaultDerivedPreds.add(URIRef(mapping[pref] + uri))
        defaultDerivedPreds.update(
            set([p == RDF.type and o or p for s, p, o in goals]))
        for hybrid in options.hybridPredicate:
            pref, uri = hybrid.split(':')
            hybridPredicates.append(URIRef(mapping[pref] + uri))

    if options.method == 'gms':
        gms(goals, noMagic, factGraph, ruleSet, defaultBasePreds,
            defaultDerivedPreds, network, workingMemory,
            nameMap[options.strictness])
        if options.output == 'rif':
            print("Rules used for bottom-up evaluation")
            if network.rules:
                for clause in network.rules:
                    print(clause)
            else:
                for clause in factGraph.adornedProgram:
                    print(clause)
    if options.output == 'conflict':
        network.reportConflictSet()
    elif options.method == 'bfp':
        bfp(defaultDerivedPreds, options, factGraph, ruleSet, network,
            hybridPredicates)