Ejemplo n.º 1
0
 def __init__(self, endpoint):
     """
     Constructs an instance of the client class
     :param endpoint: string of the SPARQL endpoint's URI hostname:port
     :type endpoint: string
     """
     if not is_uri(endpoint):
         raise Exception("endpoint is not a valid URI")
     self.endpoint_url = None
     self.set_endpoint(endpoint)
Ejemplo n.º 2
0
    def add_variable(self, col_name):
        """
        add a variable (column name) to the list of the variables of a single SPARQL query (mainly to represent
         Select variables) .
         :param col_name: represents the column name after being parced from the corresponding DAG node.

        """
        if not is_uri(col_name):
            if col_name.find(":") < 0:
                self.variables.add(col_name)
            elif col_name[:col_name.find(":")] not in self.prefixes:
                self.variables.add(col_name)
Ejemplo n.º 3
0
 def __add_patterns(self):
     where_string = ""
     for triple in self.query_model.triples:
         triple0 = triple[0]
         triple1 = triple[1]
         triple2 = triple[2]
         if not is_uri(triple[0]) and triple[0].find(":") < 0:
             triple0 = "?" + triple[0]
         if not is_uri(triple[1]) and triple[1].find(":") < 0:
             triple1 = "?" + triple[1]
         if not is_uri(triple[2]) and triple[2].find(":") < 0:
             triple2 = "?" + triple[2]
         where_string += '\n\t{} {} {}'.format(triple0, triple1,
                                               triple2) + " ."
     if len(self.query_model.graph_triples) > 0:
         for graph, triples in self.query_model.graph_triples.items():
             if len(triples) > 0:
                 graph_triples_string = "GRAPH <{}> ".format(graph) + "{\n"
                 for triple in triples:
                     triple0 = triple[0]
                     triple1 = triple[1]
                     triple2 = triple[2]
                     if not is_uri(triple[0]) and triple[0].find(":") < 0:
                         triple0 = "?" + triple[0]
                     if not is_uri(triple[1]) and triple[1].find(":") < 0:
                         triple1 = "?" + triple[1]
                     if not is_uri(triple[2]) and triple[2].find(":") < 0:
                         triple2 = "?" + triple[2]
                     graph_triples_string += '\t{} {} {}'.format(
                         triple0, triple1, triple2) + " .\n"
                 graph_triples_string += " }\n"
                 where_string += "\n" + '\t'.join(
                     ('\n' +
                      graph_triples_string.lstrip()).splitlines(True))
     if len(self.query_model.filter_clause) > 0:
         filter_string = self.add_filter_clause()
         where_string += '\t'.join(
             ('\n' + filter_string.lstrip()).splitlines(True))
     if len(self.query_model.subqueries) > 0:
         subqueries_string = self.add_subqueries()
         where_string += '\t'.join(
             ('\n' + subqueries_string.lstrip()).splitlines(True))
     if len(self.query_model.optionals) > 0:
         optional_string = self.add_optional_clause()
         where_string += '\t'.join(
             ('\n' + optional_string.lstrip()).splitlines(True))
     if len(self.query_model.unions) > 0:
         union_string = self.add_union_query()
         where_string += '\t'.join(
             ('\n' + union_string.lstrip()).splitlines(True))
     if len(self.query_model.optional_subqueries) > 0:
         where_string += "\t" + self.add_optional_subqueries()
     if where_string != "":
         where_string = where_string.rstrip('\n')
     return where_string
Ejemplo n.º 4
0
 def add_column(self, column):
     if not is_uri(column) and column.find(":") < 0:
         self.columns.append(column)
Ejemplo n.º 5
0
    def validate(self):
        """
        validate the columns and parameters data in the query model and reports and inconsistencies.
        1) validate the namespance in the preidcate to match the given in the graphs' prefixes
        2)
        :return: True if valid and False if not
        """
        ## add the aggregation to expandable group

        ## group by in expandable dataset, raise exception if the select cols not in the group by
        ### validate the prefix in the triple
        if self.parent_query_model is None:
            for triple in self.triples:
                if not is_uri(triple[1]):
                    if triple[1].find(":") >= 0:
                        prefix = triple[1].split(":")
                        if (len(prefix) >= 1):
                            if not self.is_valid_prefix(prefix[0]):
                                raise Exception(
                                    "Not a valid Prefix in triple {}".format(
                                        triple))
                    else:
                        # predicate is a variable
                        pass

        if self.parent_query_model is None:
            for col_name in self.filter_clause:
                if col_name.find(':') != -1:
                    prefix = col_name.split(":")
                    if (len(prefix) >= 1):
                        if not self.is_valid_prefix(prefix[0]):
                            raise Exception(
                                "Not a valid Prefix in filter {}".format(
                                    col_name))

        for subquery in self.subqueries:
            subquery_variables_set = set(subquery.variables)
            my_variables_set = set(self.variables)
            intersection_variables = my_variables_set.intersection(
                subquery_variables_set)
            if len(intersection_variables) < 0:
                raise Exception(
                    "No common variables between the main query and the subquery"
                )

        all_vars = self.all_variables()
        missing_vars = set()

        for sel_col in self.select_columns:
            if sel_col not in all_vars:
                missing_vars.add(sel_col)

        if len(missing_vars) > 0:
            raise Exception(
                'Variables {} are not defined in the query\'s body'.format(
                    ', '.join(missing_vars)))

        # filter_clause validation
        for col_name in self.filter_clause:
            if col_name not in all_vars:
                raise Warning(
                    'Cannot add filter on {}, is not part of the query variables'
                    .format(col_name))

        for col in self.order_clause:
            if col not in self.variables:
                raise Warning(
                    '{} cannot be a sorting column, it should be part of variables'
                    .format(col))

        for col_name in self.having_clause:
            if not self.is_aggregate_col(col_name):
                raise Warning(
                    '{} is not an aggregate column, cannot be added to having clause'
                    .format(col_name))