Example #1
0
def dict_to_logical(plan_dict):
    left = None
    right = None
    join = None

    for key, value in plan_dict.items():

        if key == 'right':
            right = dict_to_logical(plan_dict['right'])
        if key == 'left':
            left = dict_to_logical(plan_dict['left'])
        if key == 'type':
            if value == 'NLJ':
                join = Xnjoin
            else:
                join = Fjoin

        if key == 'tpf':
            arguments = value.split(" ")[:-1]
            # triple_pattern = " ".join(triples)
            triple_pattern = TriplePattern(Argument(arguments[0]),
                                           Argument(arguments[1]),
                                           Argument(arguments[2]))
            return LogicalPlan(triple_pattern)

    return LogicalPlan(left, right, join)
Example #2
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))
Example #3
0
def custom_plan(sources):

    tp_1 = TriplePattern(Argument("?v3"),
                         Argument("<http://schema.org/trailer>"),
                         Argument("?v5"))
    tp_2 = TriplePattern(
        Argument("?v3"),
        Argument("http://www.w3.org/1999/02/22-rdf-syntax-ns#type>"),
        Argument("<http://db.uwaterloo.ca/~galuc/wsdbm/ProductCategory2>"))
    tp_3 = TriplePattern(
        Argument("?v3"),
        Argument("<http://db.uwaterloo.ca/~galuc/wsdbm/hasGenre>"),
        Argument("?v0"))
    tps = [tp_1, tp_2, tp_3]

    # XNJoin = Nested Loop Join
    # FJoin: Hash Join
    l_plan = LogicalPlan(LogicalPlan(LogicalPlan(tp_1),
                                     LogicalPlan(tp_2),
                                     operator=Xnjoin),
                         LogicalPlan(tp_3),
                         operator=Xnjoin)

    plan = PhysicalPlan(sources, 2, l_plan, poly_operator=False)
    return plan
Example #4
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
Example #5
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
Example #6
0
    return count_requests


if __name__ == '__main__':
    from nlde.query import TriplePattern, Argument
    import logging
    from multiprocessing import Queue
    logging.basicConfig(level=logging.INFO)
    servers = [
        "http://fragments.dbpedia.org/2015-10/en",
        "https://query.wikidata.org/bigdata/ldf"
    ]
    servers = ["http://aifb-ls3-vm8.aifb.kit.edu:5000/db"]
    #servers = ["http://aifb-ls3-remus.aifb.kit.edu:5000/dbpedia2014", "http://aifb-ls3-remus.aifb.kit.edu:5000/dbpedia2014"] #http://aifb-ls3-remus.aifb.kit.edu:5000/dblp"]
    query = TriplePattern(
        Argument("http://dbpedia.org/resource/Aaron_Altaras"), Argument("?p"),
        Argument("http://wikidata.dbpedia.org/resource/Q301638"
                 ))  #Argument("http://www.wikidata.org/entity/Q986"))
    #query = TriplePattern(Argument("?x"),
    #                      Argument("http://www.w3.org/2002/07/owl#sameAs"), Argument("http://www.wikidata.org/entity/Q986"))
    #query = TriplePattern(Argument("?x"),
    #                      Argument("http://www.w3.org/2000/01/rdf-schema#label"), Argument("?y"))

    #query = TriplePattern(Argument("?s"), Argument("http://www.wikidata.org/prop/direct/P31"), Argument("http://www.wikidata.org/entity/Q6256"))
    #query = TriplePattern(Argument("http://www.wikidata.org/entity/Q31835482"), Argument("http://www.w3.org/2002/07/owl#sameAs"),
    #                      Argument("?o"))
    query = TriplePattern(
        Argument(
            "<http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugs/DB00201>"
        ), Argument("<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>"),
        Argument(
Example #7
0
        mgs = {"querypath": response.url}
        logger_debug.info(str(mgs))

    return count_requests


if __name__ == '__main__':

    from nlde.query import TriplePattern
    from nlde.query import Argument
    from multiprocessing import Queue

    queue = Queue()
    servers = ["http://aifb-ls3-merope.aifb.kit.edu:8891/sparql"]
    tp1 = TriplePattern(
        Argument("?x"),
        Argument("<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>"),
        Argument(
            "<http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugbank/drugs>"
        ))
    tp2 = TriplePattern(
        Argument("?x"),
        Argument("<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>"),
        Argument("?y"))

    instances = [{"x": "http://dbpedia.org/resource/Calonectris"}]
    res = contact_single_endpoint_server_bindings(servers[0], [tp2], instances,
                                                  queue, None)

    tuple = queue.get(True)
    while tuple != "EOF":
Example #8
0
            "s" : "http://dbpedia.org/resource/Zack_Space"
        },
        {
            "s" : "http://dbpedia.org/resource/Abraham_Gotthelf_K%C3%A4stner"
        }
    ]

    bindings = [
        {
            "o" : "http://dbpedia.org/resource/Stanford_University"
        }
    ]

    #bindings = None
    #servers = ["http://aifb-ls3-vm8.aifb.kit.edu:5000/dbpedia"]
    query = TriplePattern(Argument("http://dbpedia.org/resource/Frank_Church"), Argument("http://dbpedia.org/ontology/almaMater"), Argument("?o")) #Argument("http://www.wikidata.org/entity/Q986"))


    #card, stats = get_metadata_ldf_stats(servers, query)
    #print stats

    if True:
        vars = {
            "s" : ["?drug"],
            "p" : [],
            "o" : []
        }

        total = get_metadata_brtpf(servers, query)
        print total
        q = Queue()