Exemplo n.º 1
0
    def __create_outer_quer_model(query_model1, query_model2):
        """
        Joins the prefixes, offset, limit, order and select clauses and variables of the two query models.
        Doesn't add the from clause from both variables
        :param query_model1:
        :param query_model2:
        :return: an outer query model
        """
        query_model = QueryModel()
        #query_model.from_clause.clear()
        query_model.add_prefixes(query_model1.prefixes)
        query_model.add_prefixes(query_model2.prefixes)

        query_model.set_offset(min(query_model1.offset, query_model2.offset))
        query_model.set_limit(max(query_model1.limit, query_model2.limit))
        query_model.add_order_columns(query_model1.order_clause)
        query_model.add_order_columns(query_model2.order_clause)

        # TODO: WHY do this here?
        # union the select columns
        if len(query_model1.select_columns) > 0 and len(query_model1.select_columns) > 0:
            query_model.select_columns = query_model1.select_columns.union(query_model2.select_columns)
        elif len(query_model1.select_columns) > 0:
            query_model.select_columns = query_model1.select_columns.union(query_model2.variables)
        elif len(query_model2.select_columns) > 0:
            query_model.select_columns = query_model1.variables.union(query_model2.select_columns)
        # union the variables
        query_model.variables = query_model1.variables.union(query_model2.variables)

        return query_model
Exemplo n.º 2
0
class Queue2QueryModelConverter(object):
    """
    Converts the query buffer to a query model
    """
    def __init__(self, dataset):
        self.dataset = dataset
        self.query_model = QueryModel()
        # add the graph URIs
        if self.dataset.graph.graphs is not None:
            self.query_model.add_graphs(self.dataset.graph.graphs.values())
        else:
            self.query_model.add_graphs([])
        # add the prefixes
        for prefixes in self.dataset.graph.graph_prefixes.values():
            self.query_model.add_prefixes(prefixes)

    def to_query_model(self):
        """
        converts the dataset to a query model
        :return: a query model
        """
        # traverse the query queue
        self.traverse_dataset()
        return self.query_model

    def traverse_dataset(self):
        self.__traverse_dataset(self.dataset)

    def __traverse_dataset(self, ds):
        # check if this ds is a grouped_ds, process the parent ds before
        if ds.type() == "GroupedDataset":
            self.__traverse_dataset(ds.parent_dataset)

        queue = ds.query_queue
        current_qm = self.query_model

        for node in queue.queue:
            current_ds, current_qm, grouped_ds = node.visit_node(
                current_qm, ds, node)
        self.query_model = current_qm
Exemplo n.º 3
0
    def __join_grouped_grouped(self, query_model1, query_model2):
        joined_query_model = QueryModel()

        joined_query_model.prefixes = copy.copy(
            query_model1.prefixes)  # all prefixes are already in query_model1
        joined_query_model.add_prefixes(query_model2.prefixes)

        joined_query_model.variables = copy.copy(
            query_model1.variables.union(query_model2.variables)
        )  # all prefixes are already in query_model1
        joined_query_model.from_clause = copy.copy(query_model1.from_clause)
        joined_query_model.select_columns = copy.copy(
            query_model1.select_columns.union(query_model2.select_columns))
        joined_query_model.offset = min(query_model1.offset,
                                        query_model2.offset)
        joined_query_model.limit = max(query_model1.limit, query_model2.limit)
        query_model1.order_clause.update(query_model2.order_clause)
        joined_query_model.order_clause = copy.copy(query_model1.order_clause)

        QueryModel.clean_inner_qm(query_model1)
        QueryModel.clean_inner_qm(query_model2)

        # add subqueries
        if self.join_type == JoinType.InnerJoin:
            joined_query_model.add_subquery(query_model1)
            joined_query_model.add_subquery(query_model2)
        elif self.join_type == JoinType.LeftOuterJoin:
            joined_query_model.add_subquery(query_model1)
            joined_query_model.add_optional_subquery(query_model2)
        elif self.join_type == JoinType.RightOuterJoin:
            joined_query_model.add_subquery(query_model2)
            joined_query_model.add_optional_subquery(query_model1)
        else:  # outer join
            joined_query_model.add_unions(query_model1)
            joined_query_model.add_unions(query_model2)
        return joined_query_model
Exemplo n.º 4
0
    subquery.add_aggregate_pair("tweet", "COUNT", "tweet_count", "distinct")
    subquery.add_having_condition("tweet_count", "< 300")
    subquery.add_having_condition("tweet_count", "> 250")
    subquery.add_select_column("tweep")

    twitterquery = QueryModel()
    prefixes = {
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "sioc": "http://rdfs.org/sioc/ns#",
        "sioct": "http://rdfs.org/sioc/types#",
        "to": "http://twitter.com/ontology/",
        "dcterms": "http://purl.org/dc/terms/",
        "xsd": "http://www.example.org/",
        "foaf": "http://xmlns.com/foaf/0.1/",
    }
    twitterquery.add_prefixes(prefixes)
    twitterquery.add_graphs(["http://twitter.com/"])
    twitterquery.add_variable("tweep")
    twitterquery.add_subquery(subquery)
    twitterquery.add_triple("tweet", "sioc:has_creater", "tweep")
    twitterquery.add_triple("tweet", " sioc:content", "text")
    twitterquery.add_optional_triples("tweet", "sioc:mentions", "mentions")
    twitterquery.add_triple("tweet", "to:hashashtag", "hashtag")
    twitterquery.add_triple("tweet", 'dcterms:created', 'date')
    twitterquery.add_triple("tweet", 'to:hasmedia', 'multimedia')
    twitterquery.add_order_columns([("tweep", "ASC")])
    twitterquery.add_select_column("tweet")
    twitterquery.add_select_column("tweep")
    twitterquery.add_select_column("text")
    twitterquery.add_select_column("date")
    twitterquery.add_select_column("hashtag")