Пример #1
0
    def execute_old(self, variables, instances, outputqueue, p_list=None):
        self.q = Queue()
        # Copy the query array and obtain variables.
        query = [
            self.query.subject.value, self.query.predicate.value,
            self.query.object.value
        ]
        variables = list(variables)

        # Instantiate variables in the query.
        inst = {}
        for i in variables:
            inst.update({i: instances[i]})
            #inst_aux = str(instances[i]).replace(" ", "%%%")
            # Remove the %%% replacement as it does not work with the current LDF Server implementation
            inst_aux = str(instances[i])
            for j in (0, 1, 2):
                if query[j] == "?" + i:
                    query[j] = inst_aux

        tp = TriplePattern(Argument(query[0]), Argument(query[1]),
                           Argument(query[2]))
        tp.sources = self.query.sources

        # We need to handle the case that all variables are instatiated
        vars = None
        if tp.variable_position == 0:
            vars = self.query.variables_dict

        # Create process to contact sources.
        aux_queue = Queue()
        self.p = Process(target=contact_source,
                         args=(self.query.sources, tp, aux_queue, vars))

        self.p.start()
        sources = self.sources.keys()

        if p_list:
            p_list.put(self.p.pid)

        # Ready and done vectors.
        ready = self.sources_desc[self.sources.keys()[0]]
        done = 0

        # Get answers from the sources.
        data = aux_queue.get(True)
        while data != "EOF":
            # TODO: Check why this is needed.
            data.update(inst)

            # Create tuple and put it in output queue.
            outputqueue.put(Tuple(data, ready, done, sources))

            # Get next answer.
            data = aux_queue.get(True)

        # Close the queue
        aux_queue.close()
        self.p.terminate()
        outputqueue.put(Tuple("EOF", ready, done, sources))
Пример #2
0
def dict_to_logical(plan_dict, sources):
    left = None
    right = None
    join = None

    for key, value in plan_dict.items():
        if key == 'right':
            right = dict_to_logical(plan_dict['right'], sources)
        if key == 'left':
            left = dict_to_logical(plan_dict['left'], sources)
        if key == 'type':
            if value == 'NLJ':
                join = Xnjoin
            else:
                join = Fjoin

        if key == 'tpf':
            pattern_var = re.compile(r'\?\w+')
            pattern_uri = re.compile(r'\<[^<^>]+\>')
            pattern_literal = re.compile(r'[\'"].*[\'"]@?\w*')

            matches_var = pattern_var.finditer(value)
            matches_uri = pattern_uri.finditer(value)
            matches_literal = pattern_literal.finditer(value)

            matches_var = [(m.start(), m.group(0)) for m in matches_var]
            matches_uri = [(m.start(), m.group(0)) for m in matches_uri]
            matches_literal = [(m.start(), m.group(0))
                               for m in matches_literal]

            arguments = [matches_var, matches_uri, matches_literal]

            arguments = proc_arguments(arguments)

            triple_pattern = TriplePattern(Argument(arguments[0]),
                                           Argument(arguments[1]),
                                           Argument(arguments[2]))
            cardinality = int(plan_dict.get("cardinality", 0))
            triple_pattern.cardinality = cardinality
            triple_pattern.sources = {sources[0]: cardinality}
            print('--- Now printing Triple Pattern: ---')
            print(triple_pattern)
            print('------')
            return LogicalPlan(triple_pattern)

    print plan_dict
    logical_plan = LogicalPlan(left, right, join)
    logical_plan.cardinality = int(plan_dict.get("estimated_cardinality", 0))
    return logical_plan
Пример #3
0
def contact_single_tpf_server_binding(server,
                                      triple_pattern,
                                      queue,
                                      binding,
                                      vars=None):

    variables = vars  # list(query.variables)

    query = [
        triple_pattern.subject.value, triple_pattern.predicate.value,
        triple_pattern.object.value
    ]

    if len(binding) > 1:
        raise Exception("Too many bindings for TPF interface")

    binding = binding[0]

    # Instantiate variables in the query.
    inst = {}
    for i in variables:
        inst.update({i: binding[i]})
        inst_aux = str(binding[i])
        if inst_aux.startswith("http://"):
            inst_aux = "<{}>".format(inst_aux)
        for j in (0, 1, 2):
            if query[j] == "?" + i:
                query[j] = inst_aux

    tp = TriplePattern(Argument(query[0]), Argument(query[1]),
                       Argument(query[2]))
    tp.sources = triple_pattern.sources
    vars = None
    if tp.variable_position == 0:
        #vars = list(triple_pattern.get_variables())
        vars = triple_pattern.variables_dict

    count_requests = contact_single_tpf_server(server, tp, queue, vars,
                                               binding)
    return count_requests