def spa_linkset_subset(specs, activated=False):

    if activated is True:

        check = Ls.run_checks(specs, check_type="subset")
        if check[St.result] != "GOOD TO GO":
            return check

        # THE LINKSET DOES NOT EXIT, LETS CREATE IT NOW
        print Ls.linkset_info(specs, specs[St.sameAsCount])

        ##########################################################
        """ 1. GENERATE SUBSET LINKSET INSERT QUERY            """
        ##########################################################
        insert_query = spa_subset_insert(specs)
        # print insert_query

        #############################################################
        """ 2. EXECUTING INSERT SUBSET LINKSET QUERY AT ENDPOINT  """
        #############################################################
        Qry.endpoint(insert_query)

        #############################################################
        """ 3. LINKSET SIZE (NUMBER OF TRIPLES)                   """
        #############################################################
        # LINKSET SIZE (NUMBER OF TRIPLES)
        specs[St.triples] = Qry.get_namedgraph_size(specs[St.linkset])
        print "\t>>> {} TRIPLES INSERTED".format(specs[St.triples])

        # NO MATCH FOUND
        if specs[St.triples] == "0":

            # logger.warning("WE DID NOT INSERT A METADATA AS NO TRIPLE WAS INSERTED.")
            print "WE DID NOT INSERT A METADATA AS NO TRIPLE WAS INSERTED."
            specs[St.insert_query] = insert_query
            # metadata = spa_subset_metadata(source, target, data, size)

            explain_q = "ask {{ GRAPH <{}> {{ ?s <{}> ?o }} }}".format(
                specs[St.linkset], specs[St.source][St.link_old])
            response = Qry.boolean_endpoint_response(explain_q)
            explain = True if response == "true" else False
            # print explain
            if explain is False:
                # logger.warning("{} DOES NOT EXIST IS {}.".format(data[St.link_old], source[St.graph]))
                print "{} DOES NOT EXIST IS {}.".format(
                    specs[St.source][St.link_old], specs[St.source][St.graph])

                message = "{} DOES NOT EXIST IS {}.".format(
                    specs[St.source][St.link_old], specs[St.source][St.graph])

                return {St.message: message, St.error_code: 1, St.result: None}

        # SOME MATCHES WHERE FOUND
        construct_query = "\n{}\n{}\n{}\n".format(
            "PREFIX predicate: <{}>".format(Ns.alivocab),
            "construct { ?x ?y ?z }",
            "where     {{ graph <{}> {{ ?x ?y ?z }} }}".format(
                specs[St.linkset]),
        )
        # print construct_query
        construct_response = Qry.endpointconstruct(construct_query)
        if construct_response is not None:
            construct_response = construct_response.replace(
                '{', "<{}>\n{{".format(specs[St.linkset]), 1)

        # GENERATE LINKSET SINGLETON METADATA QUERY
        singleton_metadata_query = "\n{}\n{}\n{}\n{}\n{}\n{}\n\n".format(
            "PREFIX singMetadata:   <{}>".format(Ns.singletons),
            "PREFIX predicate:      <{}>".format(Ns.alivocab),
            "PREFIX prov:           <{}>".format(Ns.prov),
            "PREFIX rdf:            <{}>".format(Ns.rdf),
            "construct { ?x ?y ?z }",
            "where     {{ graph <{}{}> {{ ?x ?y ?z }} }}".format(
                Ns.singletons, specs[St.linkset_name]),
        )
        # GET THE SINGLETON METADATA USING THE CONSTRUCT QUERY
        singleton_construct = Qry.endpointconstruct(singleton_metadata_query)
        if singleton_construct is not None:
            singleton_construct = singleton_construct.replace(
                '{', "singMetadata:{}\n{{".format(specs[St.linkset_name]), 1)

        #############################################################
        """ 4. LINKSET METADATA                                   """
        #############################################################
        # METADATA
        specs[St.insert_query] = insert_query
        metadata = Gn.spa_subset_metadata(specs)

        ###############################################################
        """ 5. EXECUTING INSERT LINKSET METADATA QUERY AT ENDPOINT  """
        ###############################################################
        # EXECUTING METADATA QUERY AT ENDPOINT
        Qry.endpoint(metadata)

        print "\t>>> WRITING TO FILE"
        write_to_file(graph_name=specs[St.linkset_name],
                      metadata=metadata.replace("INSERT DATA", ""),
                      correspondences=construct_response,
                      singletons=singleton_construct,
                      directory=DIRECTORY)

        print "\tLinkset created as [SUBSET]: ", specs[St.linkset]
        print "\t*** JOB DONE! ***"

        message = "The linkset was created as [{}] with {} triples found!".format(
            specs[St.linkset], specs[St.triples])

        return {
            St.message: message,
            St.error_code: 0,
            St.result: specs[St.linkset]
        }
Beispiel #2
0
def modified_value(early_version, late_version, count=False):
    count_c = "" if count is True else "# "
    select_c = "# " if count is True else ""
    modified_value_q_load = """
    DROP SILENT GRAPH <{2}_TEMP>;
    DROP SILENT GRAPH <{3}_TEMP>;

    INSERT
    {{
        GRAPH <{2}_TEMP>
        {{
            ?subj ?pred ?obj_1 .
        }}

        GRAPH <{3}_TEMP>
        {{
            ?subj ?pred ?obj_2  .
        }}
    }}

    WHERE
    {{
        {{
            GRAPH <{2}>
            {{
                ?subj ?pred ?obj_1 .
            }}

            GRAPH <{3}>
            {{
                ?subj ?pred ?obj_2  .
            }}

            FILTER(?obj_1 != ?obj_2)
        }}
    }} """.format(count_c, select_c, early_version, late_version)

    if count is False:
        print "\nLOADING VALUES TEMP GRAPH..."
        start = time.time()
        Qr.endpoint(modified_value_q_load)
        diff = str(datetime.timedelta(seconds=time.time() - start))
        print "\t{:50} [{}]".format("... values temp graph loaded in", diff)

    modified_value_q = """
    # PREDICATE MODIFIED
    {0}SELECT (COUNT(?obj_1) AS ?TOTAL)
    {1}SELECT DISTINCT ?subj ?pred ?obj_1 ?obj_2
    {{
        GRAPH <{2}_TEMP>
        {{
            ?subj ?pred ?obj_1 .
            FILTER NOT EXISTS {{ GRAPH <{3}_TEMP> {{?subj ?pred ?obj_1 .  }} }}
        }}

        GRAPH <{3}_TEMP>
        {{
            ?subj ?pred ?obj_2  .
            FILTER NOT EXISTS {{  GRAPH <{2}_TEMP> {{?subj ?pred ?obj_2 . }} }}
        }}

        #  FILTER NOT EXISTS {{
        #     GRAPH <{3}_TEMP> {{?subj ?pred ?obj_1 . }}
        #     GRAPH <{2}_TEMP> {{?subj ?pred ?obj_2 . }}
        # }}
    }}
    """.format(count_c, select_c, early_version, late_version)
    # print modified_predicate_q
    # if count is False:
    #     print "MATCHING TEMPS..."
    return modified_value_q
Beispiel #3
0
def reload():
    Qr.endpoint("DROP SILENT GRAPH <http://risis.eu/dataset/Test.v1>")
    Qr.endpoint("DROP SILENT GRAPH <http://risis.eu/dataset/Test.v2>")
    Qr.endpoint(test_v1)
    Qr.endpoint(test_v2)
Beispiel #4
0
def modified(early_version, late_version, stat=False, display=True, activated=False):

    if activated is False:
        print "\nTHE FUNCTION [removed] IS NOT ACTIVATED"
        return {St.subject: None, St.predicate: None, St.triples: None}

    if stat is False:

        # TRIPLES REMOVED
        mod_pred = modified_predicate(early_version, late_version, count=stat)

        # EXECUTING THE PREDICATE MODIFICATION QUERY
        start = time.time()
        resp_mod_pred = Qr.sparql_xml_to_matrix(mod_pred)
        matched_time_1 = str(datetime.timedelta(seconds=time.time() - start))
        print " \t{:50} [{}]".format("... predicate matched in", matched_time_1)

        # DROPPING THE TEMP GRAPH USED FOR THE QUERY
        # print "DROPPING TEMPS..."
        drop = """
        DROP SILENT GRAPH <{}_TEMP>;
        DROP SILENT GRAPH <{}_TEMP> """.format(early_version, late_version)
        start = time.time()
        Qr.endpoint(drop)
        dropped_time_1 = str(datetime.timedelta(seconds=time.time() - start))
        print "\t{:50} [{}]".format("... predicate temp graph dropped in", dropped_time_1)
        print "\t{:50} [{}]".format("... elapse time", str(datetime.timedelta(seconds=time.time() - start)))

        mod_val = modified_value(early_version, late_version, count=stat)

        # EXECUTING THE VALUE MODIFICATION QUERY
        start_2 = time.time()
        resp_mod_val = Qr.sparql_xml_to_matrix(mod_val)
        matched_time_2 = str(datetime.timedelta(seconds=time.time() - start_2))
        print "\t{:50} [{}]".format("... value matched in", matched_time_2)

        # DROPPING THE TEMP GRAPH USED FOR THE QUERY
        # print "DROPPING TEMPS..."
        drop = """
        DROP SILENT GRAPH <{}_TEMP>;
        DROP SILENT GRAPH <{}_TEMP> """.format(early_version, late_version)
        start_2 = time.time()
        Qr.endpoint(drop)
        dropped_time_2 = str(datetime.timedelta(seconds=time.time() - start_2))
        print "\t{:50} [{}]".format("... value temp graph dropped in", dropped_time_2)
        print "\t{:50} [{}]".format("... elapse time", str(datetime.timedelta(seconds=time.time() - start)))

        status = (resp_mod_val[St.result] is not None and len(resp_mod_val[St.result]) > 1) or \
                 (resp_mod_pred[St.result] is not None and len(resp_mod_pred[St.result]) > 1)

        if display is True:

            # DISPLAY THE RESULTS FOR VALUE REMOVED
            print "\n>>> DISPLAY THE RESULTS FOR VALUE MODIFIED"
            Qr.display_matrix(resp_mod_val, spacing=90, limit=10, is_activated=True)

            # DISPLAY THE RESULTS FOR VALUE REMOVED
            print "\n>>> DISPLAY THE RESULTS FOR PREDICATE MODIFIED"
            Qr.display_matrix(resp_mod_pred, spacing=90, limit=10, is_activated=True)

        return {"status": status,
                St.predicate: resp_mod_pred[St.result],
                St.triples: resp_mod_val[St.result]}

    else:

        mod_pred = modified_predicate(early_version, late_version, count=stat)
        mod_val = modified_value(early_version, late_version, count=stat)

        resp_mod_pred = Qr.sparql_xml_to_matrix(mod_pred)
        resp_mod_val = Qr.sparql_xml_to_matrix(mod_val)

        # resp_mod_val = {St.result: None}

        resp_mod_pred_bool = resp_mod_pred[St.result] is None
        resp_mod_val_bool = resp_mod_val[St.result] is None

        status = (resp_mod_pred_bool is not True and int(resp_mod_pred[St.result][1][0]) > 0) or \
                 (resp_mod_val_bool is not None and int(resp_mod_val[St.result][1][0]) > 0)

        return {"status": status,
                St.predicate: resp_mod_pred[St.result][1][0] if resp_mod_pred_bool is not True else None,
                St.triples: resp_mod_val[St.result][1][0] if resp_mod_val_bool is not True else None}