Esempio n. 1
0
    def get_sparql(self, include_prefixes, is_subquery):
        """
        Get the SPARQL query corresponding to the current state of the QueryBuilder.

        :param include_prefixes: indicates whether or not prefixes should be included in the query
        :param is_subquery: indicates whether or not the query is considered to be a sub-query
        :return: the SPARQL query as a string
        """
        if is_subquery:
            return self.__get_query(False)  # create subquery

        prefixes = self.prefix_string if include_prefixes else ''
        outer_query_closing_braces = ''.join([
            "}\n" if self.subquery is not None else '',
            "}" if self.get_triple_patterns() != ''
            and self.subquery is not None else '',
            "}" if self.get_triple_patterns() != '' else ''
        ])
        selective_closing_braces = "}}" if self.include_selectivity and self.target_query is not None else ''

        if len(self.constraints) == 1 and isinstance(
                self.constraints[0], MaxOnlyConstraint
        ) and self.constraints[0].get_shape_ref() is None:
            target_node = ''
            if self.include_selectivity and self.target_query is not None:
                target_node = get_target_node_statement(
                    self.target_query) + ".\n"
            return ''.join([
                prefixes,
                self.__get_projection_string(), " WHERE {\n", target_node,
                "{ SELECT ?",
                VariableGenerator.get_focus_node_var(), " COUNT(?",
                self.triples[0].rsplit("?", 1)[1][:-1], " AS ?cnt) WHERE {\n",
                'OPTIONAL { ', self.triples[0], ' }\n} GROUP BY ?',
                VariableGenerator.get_focus_node_var(), ' HAVING (COUNT(?',
                self.triples[0].rsplit("?", 1)[1][:-1], ') > ',
                str(self.constraints[0].max), ')', '\n}}',
                " ORDER BY ?" + VariableGenerator.get_focus_node_var()
                if self.include_ORDERBY else ''
            ])

        query = ''.join([
            prefixes,
            self.__get_selective(),
            self.__get_query(include_prefixes),
            self.subquery if self.subquery is not None else '',
            outer_query_closing_braces, selective_closing_braces,
            " ORDER BY ?" + VariableGenerator.get_focus_node_var()
            if self.include_ORDERBY else ''
        ])
        return query
Esempio n. 2
0
    def get_sparql(self, include_prefixes, is_subquery):
        """
        Get the SPARQL query corresponding to the current state of the QueryBuilder.

        :param include_prefixes: indicates whether or not prefixes should be included in the query
        :param is_subquery: indicates whether or not the query is considered to be a sub-query
        :return: the SPARQL query as a string
        """
        if is_subquery:
            return self.__get_query(False)  # create subquery

        prefixes = get_prefix_string() if include_prefixes else ''
        outer_query_closing_braces = ''.join([
            "}\n" if self.subquery is not None else '',
            "}" if self.get_triple_patterns() != ''
            and self.subquery is not None else '',
            "}" if self.get_triple_patterns() != '' else ''
        ])
        selective_closing_braces = "}}" if self.include_selectivity and self.target_query is not None else ''

        return ''.join([
            prefixes,
            self.__get_selective(),
            self.__get_query(include_prefixes),
            self.subquery if self.subquery is not None else '',
            outer_query_closing_braces, selective_closing_braces,
            " ORDER BY ?" + VariableGenerator.get_focus_node_var()
            if self.include_ORDERBY else ''
        ])
Esempio n. 3
0
    def add_triple(self, path, obj):
        """
        Adds a triple pattern to the constraint query.
        No subject is needed as in a constraint query all triple patterns share the same subject.

        :param path: predicate of the triple pattern
        :param obj: object of the triple pattern
        """
        self.triples.append("?" + VariableGenerator.get_focus_node_var() +
                            " " + path + " " + obj + ".")
Esempio n. 4
0
    def compute_rule_pattern(constraints, id_):
        """
        Computes the query rule patterns for the given constraints.

        :param constraints: a list of constraints to compute the rule patterns for
        :param id_: the internal constraint query name associated with the constraints
        :return: query rule pattern for the constraints
        """
        body = []
        for c in constraints:
            body = body + c.compute_rule_pattern_body()

        return RulePattern(head=(id_, VariableGenerator.get_focus_node_var(),
                                 True),
                           body=body)
Esempio n. 5
0
    def _plain_target_query(target_query, include_prefixes, include_order_by):
        """
        Generates a simple target query from the parsed target query.

        :param target_query: target query string parsed from the input file
        :param include_prefixes: indicates whether or not prefixes should be included in the query
        :param include_order_by: indicates whether or not the ORDER BY clause will be added
        :return: simple target query
        """
        prefixes = get_prefix_string() if include_prefixes else ''
        focus_var = VariableGenerator.get_focus_node_var()
        return ''.join([
            prefixes, target_query,
            " ORDER BY ?" + focus_var if include_order_by else ''
        ])
Esempio n. 6
0
    def parse_constraints(self, array, target_def, constraints_id):
        """
        Parses all constraints of a shape.

        :param array: list of constraints belonging to the shape
        :param target_def: the target definition of the shape
        :param constraints_id: suffix for the constraint IDs
        :return: list of constraints in internal constraint representation
        """
        var_generator = VariableGenerator()
        return [
            self.parse_constraint(var_generator, array[0][i],
                                  constraints_id + "_c" + str(i + 1),
                                  target_def) for i in range(len(array[0]))
        ]
Esempio n. 7
0
    def generate_local_subquery(positive_constraints):
        """
        Generates a local sub-query.

        :param positive_constraints: positive constraints
        :return: sub-query including the constraints
        """
        local_pos_constraints = [
            c for c in positive_constraints if c.get_shape_ref() is None
        ]

        if len(local_pos_constraints) == 0:
            return None  # optional empty

        builder = QueryBuilder("tmp", None,
                               VariableGenerator.get_focus_node_var(), False)

        for c in local_pos_constraints:
            builder.build_clause(c)

        return builder.get_sparql(False, True)
Esempio n. 8
0
    def _filter_not_in_target_query(self, constraint, target_query,
                                    include_prefixes, include_order_by):
        """
        Generates a target query including a FILTER NOT IN clause to filter based on invalid instances.

        :param constraint: constraint that refers to this target query
        :param target_query: target query string parsed from the input file
        :param include_prefixes: indicates whether or not prefixes should be included in the query
        :param include_order_by: indicates whether or not the ORDER BY clause will be added
        :return: target query with FILTER NOT IN clause
        """
        prefixes = self.shape.get_prefix_string() if include_prefixes else ''
        ref_path = constraint[0].path
        focus_var = VariableGenerator.get_focus_node_var()
        target_node = get_target_node_statement(target_query)
        query = ''.join([
            prefixes, "SELECT DISTINCT ?" + focus_var + " WHERE {\n",
            "?" + focus_var + " " + ref_path + " ?inst.\n", target_node + "\n",
            "FILTER (?inst NOT IN ( $instances_to_add$ )). }\n",
            " ORDER BY ?" + focus_var if include_order_by else ''
        ])
        return Query(None, None, query)
Esempio n. 9
0
 def __get_disjunct_rp_body(self):
     focus_node_var = VariableGenerator.get_focus_node_var()
     min_query = [(self.minQuery.get_id(), focus_node_var, True)]
     max_queries = [(s, focus_node_var, False)
                    for s in [q.get_id() for q in self.maxQueries]]
     return min_query + max_queries
Esempio n. 10
0
    def __compute_rule_pattern(self):
        """ Computes shape rule pattern """
        focus_node_var = VariableGenerator.get_focus_node_var()
        head = (self.id, focus_node_var, True)

        return RulePattern(head, self.__get_disjunct_rp_body())