Пример #1
0
def run_checks_cluster(specs, check_type):

    heading = "RUNNING LINKSET SPECS CHECK" \
              "\n======================================================" \
              "========================================================"
    print heading

    ask = "ASK {{ <#> ?p ?o . }}"
    linkset = specs[St.refined] if St.refined in specs else specs[St.linkset]
    # print linkset
    """
    # CHECK WHETHER THE GRAPHS EXIST
    """
    g_exist_q = "ASK { GRAPH <@> {?s ?p ?o} }"
    response = "false"
    for target in specs[St.targets]:
        # query = g_exist_q.replace("@", target[St.graph])
        # print query
        response = Qry.boolean_endpoint_response(
            g_exist_q.replace("@", target[St.graph]))
        if response == "false":
            break
    # print response

    if response == "false":
        print Ec.ERROR_CODE_10
        return {
            St.message: Ec.ERROR_CODE_10,
            St.error_code: 10,
            St.result: None
        }
    """
    # CHECK THE TASK SPECIFIC PREDICATE COUNT
    """
    if specs[St.sameAsCount] is None:
        print Ec.ERROR_CODE_1
        return {St.message: Ec.ERROR_CODE_1, St.error_code: 1, St.result: None}
    # print "sameAsCount", specs[St.sameAsCount]
    """
    # CHECK WHETHER THE LINKSET WAS ALREADY CREATED AND ITS ALTERNATIVE NAME REPRESENTATION
    """

    # CHECK WHETHER THE CURRENT LINKSET NAME EXIST
    print "CHECK WHETHER THE CURRENT LINKSET NAME EXIST"
    print ask.replace("#", linkset)
    ask_1 = Qry.boolean_endpoint_response(ask.replace("#", linkset))
    # print ask_1

    # THE CURRENT AME EXIST
    if ask_1 == "true":
        # print specs[St.linkset_name]
        print "ASK_1: YES, THE CURRENT NAME EXIST"
        message = Ec.ERROR_CODE_2.replace('#', linkset)

        if St.refined in specs:

            # GET LINKSET DERIVED FROM
            subjects_target = linkset_wasderivedfrom(linkset)
            # CHECK THE RESULT OF THE DIFFERENCE AND OUT PUT BOTH THE REFINED AND THE DIFFERENCE
            if subjects_target is not None:

                print "\t>>> NOT GOOD TO GO, IT ALREADY EXISTS"

                diff_lens_specs = {
                    St.researchQ_URI:
                    specs[St.researchQ_URI],
                    # THE OBJECT IS THE LINKSET THE REFINED LINKSET WAS DERIVED FROM
                    St.subjectsTarget:
                    subjects_target,
                    # THE TARGET IS THE REFINED LINKSET
                    St.objectsTarget:
                    linkset
                }

                Lu.diff_lens_name(diff_lens_specs)
                message2 = Ec.ERROR_CODE_7.replace('#',
                                                   diff_lens_specs[St.lens])
                refined = {
                    St.message: message,
                    St.error_code: 0,
                    St.result: linkset
                }
                difference = {
                    St.message: message2,
                    St.error_code: 7,
                    St.result: diff_lens_specs[St.lens]
                }
                # print "difference", difference

                # REGISTER THE ALIGNMENT
                if refined[St.message].__contains__("ALREADY EXISTS"):
                    Ura.register_alignment_mapping(specs, created=False)
                else:
                    Ura.register_alignment_mapping(specs, created=True)

                # REGISTER THE LENS
                Ura.register_lens(diff_lens_specs, is_created=False)

                return {
                    St.message: "NOT GOOD TO GO",
                    'refined': refined,
                    'difference': difference
                }
        print message
        return {
            St.message: message.replace("\n", "<br/>"),
            St.error_code: 2,
            St.result: linkset
        }

    # CHECK WHETHER THE ALTERNATIVE LINKSET NAME EXIST
    # elif ask_1 == "false" and str(check_type).lower() != "subset":
    #
    # print "ASK 2: CHECK WHETHER THE ALTERNATIVE LINKSET NAME EXIST"
    # if St.refined not in specs:
    #     print "\t- NOT REFINED"
    #     GENERATE ALTERNATIVE NAME. THIS DOS NOT APPLY TO SUBSET BECAUSE WE ASSUME
    #     A LINKSET BY SUBSET DOES NOT NEED THE TARGET ALIGNS TO BE SET AS IT IS OFTEN UNKNOWN
    #     counter_check = set_linkset_name(specs, inverse=True)
    #
    #     CHECK WHETHER THE CURRENT LINKSET EXIST UNDER A DIFFERENT NAME
    #     ask_2 = Qry.boolean_endpoint_response(ask.replace("#", counter_check))
    #
    #     if ask_2 == "true":
    #         message = Ec.ERROR_CODE_3.replace('#', linkset).replace("@", counter_check)
    #         print "\n>>> NOT GOOD TO GO, IT ALREADY EXISTS UNDER THE NAME {}".format(counter_check)
    #         print message
    #         return {St.message: message.replace("\n", "<br/>"), St.error_code: 3, St.result: counter_check}

    print "\t>>> NO IT DOES NOT EXIST YET..."
    if str(check_type).lower() == "subset":
        print "\nSETTING THE LINKSET SUBSET NAME"
        set_subset_name(specs, inverse=False)
    elif str(check_type).lower() == "linkset":
        print "\nSETTING THE LINKSET NAME"
        set_cluster_linkset_name(specs)
    elif str(check_type).lower() == "refine":
        print "\nSETTING THE LINKSET REFINE NAME"
        set_refined_name(specs)
    # print specs[St.linkset_name]
    print "\n>>> GOOD TO GO !!!"
    return {
        St.message: "GOOD TO GO",
        St.error_code: 0,
        St.result: "GOOD TO GO"
    }
Пример #2
0
def run_checks_id(specs):

    heading = "\n======================================================" \
              "========================================================"\
              "\nRUNNING LINKSET SPECS CHECK" \
              "\n======================================================" \
              "========================================================"
    print heading

    ask = "ASK {{ <#> ?p ?o . }}"

    linkset = specs[St.refined] if St.refined in specs else specs[St.linkset]
    # print linkset
    """
    # CHECK WHETHER THE SOURCE & TARGET GRAPHS EXIST
    """
    g_exist_q = "ASK { GRAPH <@> {?s ?p ?o} }"
    src_g_exist = Qry.boolean_endpoint_response(
        g_exist_q.replace("@", specs[St.source][St.graph]))
    trg_g_exist = Qry.boolean_endpoint_response(
        g_exist_q.replace("@", specs[St.target][St.graph]))
    if (src_g_exist == "false") or (trg_g_exist == "false"):
        print Ec.ERROR_CODE_10
        return {
            St.message: Ec.ERROR_CODE_10,
            St.error_code: 10,
            St.result: None
        }
    """
    # CHECK THE TASK SPECIFIC PREDICATE COUNT
    """
    if specs[St.sameAsCount] is None:
        print Ec.ERROR_CODE_1
        return {St.message: Ec.ERROR_CODE_1, St.error_code: 1, St.result: None}
    """
    # CHECK WHETHER THE LINKSET WAS ALREADY CREATED AND ITS ALTERNATIVE NAME REPRESENTATION
    """

    # CHECK WHETHER THE CURRENT LINKSET NAME EXIST
    ask_1 = Qry.boolean_endpoint_response(ask.replace("#", linkset))
    if ask_1 == "true":

        # print "ASK_1"
        message = Ec.ERROR_CODE_2.replace('#', linkset)

        if St.refined in specs:

            # GET LINKSET DERIVED FROM
            subjects_target = linkset_wasderivedfrom(linkset)
            # CHECK THE RESULT OF THE DIFFERENCE AND OUT PUT BOTH THE REFINED AND THE DIFFERENCE
            if subjects_target is not None:

                diff_lens_specs = {
                    St.researchQ_URI:
                    specs[St.researchQ_URI],
                    # THE OBJECT IS THE LINKSET THE REFINED LINKSET WAS DERIVED FROM
                    St.subjectsTarget:
                    subjects_target,
                    # THE TARGET IS THE REFINED LINKSET
                    St.objectsTarget:
                    linkset
                }

                Lu.diff_lens_name(diff_lens_specs)
                message2 = Ec.ERROR_CODE_7.replace('#',
                                                   diff_lens_specs[St.lens])
                refined = {
                    St.message: message,
                    St.error_code: 0,
                    St.result: linkset
                }
                difference = {
                    St.message: message2,
                    St.error_code: 7,
                    St.result: diff_lens_specs[St.lens]
                }

                # REGISTER THE ALIGNMENT
                if refined[St.message].__contains__("ALREADY EXISTS"):
                    Ura.register_alignment_mapping(specs, created=False)
                else:
                    Ura.register_alignment_mapping(specs, created=True)

                # REGISTER THE LENS
                Ura.register_lens(diff_lens_specs, is_created=False)

                print "\n>>> NOT GOOD TO GO, IT ALREADY EXISTS"
                return {
                    St.message: "NOT GOOD TO GO",
                    'refined': refined,
                    'difference': difference
                }
        print message
        return {
            St.message: message.replace("\n", "<br/>"),
            St.error_code: 2,
            St.result: linkset
        }

    # CHECK WHETHER THE alternative LINKSET NAME EXIST
    elif ask_1 == "false" and str(linkset).__contains__("subset") is False:

        # print "ASK 2"
        if St.refined not in specs:

            # GENERATE ALTERNATIVE NAME. THIS DOS NOT APPLY JTO SUBSET BECAUSE WE ASSUME
            # A LINKSET BY SUBSET DOES NEEDS NOT THE TARGET ALIGNS TO BE SET AS IT IS OFTEN UNKNOWN
            counter_check = set_linkset_identity_name(specs, inverse=True)

            # CHECK WHETHER THE CURRENT LINKSET EXIST UNDER A DIFFERENT NAME
            ask_2 = Qry.boolean_endpoint_response(
                ask.replace("#", counter_check))

            if ask_2 == "true":
                message = Ec.ERROR_CODE_3.replace('#', linkset).replace(
                    "@", counter_check)
                print "\n>>> NOT GOOD TO GO, IT ALREADY EXISTS UNDER THE NAME {}".format(
                    counter_check)
                print message
                return {
                    St.message: message.replace("\n", "<br/>"),
                    St.error_code: 3,
                    St.result: counter_check
                }

    print "NO PROBLEM"
    set_linkset_identity_name(specs, inverse=False)
    print "NAME: " + specs[St.linkset]
    print "\n>>> GOOD TO GO !!!"
    return {
        St.message: "GOOD TO GO",
        St.error_code: 0,
        St.result: "GOOD TO GO"
    }
Пример #3
0
def union(specs, activated=False):

    if activated is False:
        # logger.warning("THE FUNCTION IS NOT ACTIVATED")
        print("THE FUNCTION IS NOT ACTIVATED")
        return {
            St.message: "THE FUNCTION IS NOT ACTIVATED.",
            St.error_code: 1,
            St.result: None
        }

    print "\nEXECUTING UNION SPECS" \
          "\n======================================================" \
          "========================================================"
    """
    THE generate_lens_name FUNCTION RETURNS THE NAME OF THE UNION AND A
    QUERY THAT ALLOWS TO ASk WHETHER THE LENS TO BE CREATED EXIST BY CHECKING
    WHETHER THERE EXISTS A LENS WITH THE SAME COMPOSITION IN TERMS GRAPHS USED FOR THE UNION
    """

    # SET THE NAME OF THE UNION-LENS
    print "1. DATASETS:", len(specs[St.datasets])
    for ds in specs[St.datasets]:
        print "\t- {}".format(ds)
    info = Lu.generate_lens_name(specs[St.datasets])

    specs[St.lens] = "{}{}".format(Ns.lens, info["name"])
    print "\n2. LENS: ", info["name"]

    # CHECK WHETHER THE LENS EXISTS
    check = run_checks(specs, info["query"])
    if check[St.result] != "GOOD TO GO":
        if check[St.message].__contains__("ALREADY EXISTS"):
            Urq.register_lens(specs, is_created=False)
        return check
    # print "AFTER CHECK"

    # PREPARATION FOR THE CREATION OF THE LENS
    specs[St.lens_target_triples] = ""
    specs[St.expectedTriples] = 0
    specs[St.insert_query] = ""
    lens = specs[St.lens]
    source = "{}{}".format(Ns.tmpgraph, "load00")
    message_2 = Ec.ERROR_CODE_8.replace("#", specs[St.lens])
    count = -1
    insert_ans = False

    try:

        # GO THROUGH THE LINKSETS/LENSES IN THE LENS
        #   1-SUM UP THE EXPECTED NUMBER OF TRIPLES
        #   2-GENERATE THE TRIPLES REPRESENTATION OF GHE GRAPHS COMPOSING THIS LENS
        #   3-GENERATE THE INSERT QUERY FOR MOVING BOTH LINKSET AND SINGLETON GRAPHS TO THE UNION GRAPH
        total_size = 0

        # LOAD ALL GRAPHS IN LOAD00
        specs[St.insert_query] += "DROP SILENT GRAPH <{}{}> ;\n".format(
            Ns.tmpgraph, "load00")

        # ITERATE THROUGH THE PROVIDED GRAPHS
        for linkset in specs[St.datasets]:

            # print "TARGET: ", linkset
            count += 1

            # GET THE TOTAL NUMBER OF CORRESPONDENCE TRIPLES INSERTED
            curr_triples = Qry.get_triples(linkset)
            # PROBABLY THE LINKSET HAS NO SUCH PROPERTY " void:triples  ?triples ."

            if curr_triples is None:
                curr_triples = Qry.get_triples_count(linkset)

            total_size += int(curr_triples)
            print "{} Contains {} triples".format(linkset, curr_triples)

            if curr_triples is not None:
                specs[St.expectedTriples] += int(curr_triples)
            else:
                # THE IS A PROBLEM WITH THE GRAPH FOR SEVERAL POSSIBLE REASONS
                return {
                    St.message: message_2.replace("\n", "<br/>"),
                    St.error_code: 1,
                    St.result: None
                }

            # GENERATE TRIPLES OUT OF THE TARGETS
            specs[
                St.
                lens_target_triples] += "\n\t        void:target                         <{}> ;".format(
                    linkset)

            # GET THE INSERT QUERY
            # BOTH THE LINKSET AND THE SINGLETONS ARE MOVED TO A SINGLE GRAPH
            partial_query = Qry.q_copy_graph(source, source, linkset)
            if count == 0:
                specs[St.insert_query] += partial_query
            else:
                specs[St.insert_query] += " ;\n{}".format(partial_query)

        # INTERSECTION MANIPULATION OVER THE UNION (SOURCE)
        insert_query = union_insert_q(lens, source, specs[St.lens_name])
        # print "manipulation:", manipulation
        specs[St.insert_query] += " ;\n{}".format(insert_query)

        # GENERATE THE LENS UNION
        if activated is True:

            # print specs[St.insert_query]
            insert_ans = Qry.boolean_endpoint_response(specs[St.insert_query])

            specs[St.triples] = Qry.get_namedgraph_size(lens, isdistinct=False)
            if specs[St.triples] == "0":
                message = Ec.ERROR_CODE_9
                print message
                # return None
                return {
                    St.message: message.replace("\n", "<br/>"),
                    St.error_code: 1,
                    St.result: None
                }

            # CHECK WHETHER THE RESULT CONTAINS DUPLICATES
            contains_duplicated = Qry.contains_duplicates(lens)
            print "Contains Opposite Direction Duplicated:", contains_duplicated

            # IF IT DOES, REMOVE THE DUPLICATES
            if contains_duplicated is True:
                # logger.warning("THE LENS CONTAINS DUPLICATES.")
                print "THE LENS CONTAINS DUPLICATES."
                Qry.remove_duplicates(lens)
                # logger.warning("THE DUPLICATES ARE NOW REMOVED.")
                print "THE DUPLICATES ARE NOW REMOVED."

            print "Number of triples loaded              : {}".format(
                total_size)

            specs[St.triples] = Qry.get_namedgraph_size(lens, isdistinct=False)
            print "\t>>> INSERTED:  {}\n\t>>> INSERTED TRIPLES: {}".format(
                insert_ans, specs[St.triples])

            print "Inserted : {}".format(specs[St.triples])
            print "Removed  : {}".format(total_size - int(specs[St.triples]))

            # LOAD THE METADATA
            # NOT GOOD AS THE LENS ALSO HAS A SINGLETON GRAPH
            # inserted_correspondences = int(Qry.get_union_triples(lens))
            inserted_correspondences = int(specs[St.triples])
            # print "inserted_correspondences:", inserted_correspondences
            specs[St.removedDuplicates] = specs[
                St.expectedTriples] - inserted_correspondences
            metadata = Gn.union_meta(specs)
            # print "METADATA:", metadata
            meta_ans = Qry.boolean_endpoint_response(metadata)
            print "\t>>> IS THE METADATA GENERATED AND INSERTED?  {}".format(
                meta_ans)

        construct_response = Qry.get_constructed_graph(specs[St.lens])
        if construct_response is not None:
            print "\t>>> WRITING TO FILE"
            construct_response = construct_response.replace(
                '{', "<{}>\n{{".format(specs[St.lens]), 1)
            write_to_file(graph_name=specs[St.lens_name],
                          metadata=None,
                          correspondences=construct_response,
                          directory=DIRECTORY)
        print "\tLens created as : ", specs[St.lens]

        # REGISTER THE LINKSET
        Urq.register_lens(specs, is_created=True)

        # return specs[St.lens]
        message = "THE LENS WAS CREATED as {}. " \
                  "With initially {} triples loaded, {} duplicated triples were found and removed.".\
            format(specs[St.lens], total_size, total_size - int(specs[St.triples]))

        print "\t*** JOB DONE! ***"
        return {
            St.message: message,
            St.error_code: 0,
            St.result: specs[St.lens]
        }

    except Exception as err:
        # logger.warning(err)
        if insert_ans == "true":
            "DROP THE INSERTED UNION"
            drop_linkset(lens, activated=True)

        print "ERROR IN UNION LENS CREATION:", err
        return {St.message: ERROR_CODE_11, St.error_code: 11, St.result: None}
Пример #4
0
def lens_transitive(specs, activated=False):

    # CHECK BOTH DATASETS FOR SAME MECHANISM
    print "GENERATE THE LENS NAME"
    Lu.composition_lens_name(specs)

    print "GET THE SAME AS COUNT"
    specs[St.sameAsCount] = Qry.get_same_as_count(specs[St.lens_operation])
    # print same_as_count

    # GENERATE THE INSERT QUERY FOR TRANSITIVITY
    # transitive_analyses = lens_transitive_query(specs)
    # if transitive_analyses is None:
    #     return
    # specs[St.insert_query] = transitive_analyses[1]
    # print insert_query
    # exit(0)
    # specs['is_transitive_by'] = transitive_analyses[0]
    ln = get_uri_local_name(specs[St.lens])
    sg = specs[St.subjectsTarget]
    tg = specs[St.objectsTarget]
    ssg = "{}{}".format(Ns.singletons, get_uri_local_name(sg))
    tsg = "{}{}".format(Ns.singletons, get_uri_local_name(tg))

    print "SOURCE: {}".format(sg)
    print "TARGET: {}".format(tg)
    print "1. GENERATING THE INSERT QUERY"
    specs[St.insert_query] = transitive_insert_query(ln, sg, tg, ssg, tsg)

    if activated is True:

        # RUN THE QUERY AT THE END POINT
        print "2. RUNNING THE INSERT QUERY"
        Qry.boolean_endpoint_response(specs[St.insert_query])

        # GET THE SIZE OF THE LENS JUST CREATED ABOVE
        print "3. ETTING THE SIZE OF THE LENS JUST INSERTED"
        size = Qry.get_namedgraph_size(specs[St.lens], isdistinct=False)

        # IF ACTIVATED, INSERT THE METADATA
        if size > 0:

            # GENERATE THE METADATA ABOUT THE LENS JUST CREATED
            print "4. SOME {} TRANSITIVE TRIPLES WERE FOUND".format(size)
            metadata = transitive_metadata(specs, size)
            # print metadata

            print "5. INSERTING THE METADATA"
            Qry.boolean_endpoint_response(metadata)

            print "6. REGISTER THE LENS"
            Urq.register_lens(specs, is_created=True)

            # RUN A CORRESPONDENCE CONSTRUCT QUERY FOR BACKING UP THE DATA TO DISC
            print "7. GENERATE THE CONSTRUCT FOR FILE DUMP"
            construct_correspondence = Qry.endpointconstruct(
                Qry.construct_namedgraph(specs[St.lens]))

            if construct_correspondence is not None:
                construct_correspondence = construct_correspondence.replace(
                    '{', "<{}>\n{{".format(specs[St.lens]), 1)

            # RUN A SINGLETON METADATA CONSTRUCT QUERY FOR BACKING UP THE DATA TO DISC
            construct_singletons = Qry.endpointconstruct(
                Qry.construct_namedgraph("{}{}".format(Ns.singletons,
                                                       specs[St.lens_name])))

            if construct_singletons is not None:
                construct_singletons = construct_singletons. \
                    replace('{', "<{}{}>\n{{".format(Ns.singletons, specs[St.lens_name]), 1)

            # WRITE TO FILE
            print "WRITING TO FILE"
            write_to_file(graph_name=ln,
                          metadata=metadata,
                          directory=DIRECTORY,
                          correspondences=construct_correspondence,
                          singletons=construct_singletons)

            # return specs[St.lens]
            message = "THE LENS WAS CREATED!<br/>URI = {}".format(
                specs[St.lens])
            print message
            print "\t*** JOB DONE! ***"
            return {
                St.message: message,
                St.error_code: 0,
                St.result: specs[St.lens]
            }

    if activated is False:
        logger.warning(
            "THE FUNCTION IS NOT ACTIVATED BUT THE METADATA THAT IS "
            "SUPPOSED TO BE ENTERED IS WRITEN TO THE CONSOLE.")
Пример #5
0
def refine_lens(specs, activated=False, check_file=False):

    try:

        message = Ec.ERROR_CODE_0.replace('\n', "<br/>")
        if activated is False:
            print Ut.headings("THE FUNCTION [refine_lens] IS NOT ACTIVATED")
            return {St.message: message, St.error_code: 4, St.result: None}

        # 1. UPDATING THE SPECS BY CHANGING LINKSET TO TENS
        specs[St.refined] = specs['linkset']
        specs.pop('linkset')
        Ut.update_specification(specs)

        # CHECKING WHETHER THE LENS IS REFINENABLE
        # Refine.is_refinable(specs[St.refined])

        # PRINTING THE SPECIFICATIONS
        # lensUt.print_specs(specs)

        # ASSIGN THE SAME AS COUNT
        specs[St.sameAsCount] = Qry.get_same_as_count(specs[St.mechanism])

        message = Ec.ERROR_CODE_4.replace('\n', "<br/>")
        if specs[St.sameAsCount]:

            source = specs[St.source]
            target = specs[St.target]

            # 2. SET THE LENS NAME
            # *******************************
            print "\n2. SET THE LENS NAME"
            # *******************************
            lensUt.lens_refine_name(specs, 'refine')

            #*******************************
            # GOOD TO GO CHECK
            # *******************************
            query = """
        SELECT *
        {{
            <{}> ?predicate ?object .
        }}
            """.format(specs[St.lens])
            check = Lens_Union.run_checks(specs, query, operator="refine")

            # NOT GOOD TO GO, IT ALREADY EXISTS
            if check[St.message].__contains__("ALREADY EXISTS"):
                return {
                    St.message: check[St.message],
                    St.error_code: 71,
                    St.result: specs[St.lens]
                }

            # *******************************
            # GOOD TO GO
            # *******************************
            else:

                lens_start = time.time()
                # UPDATE THE SPECIFICATION
                Ut.update_specification(specs[St.source])
                Ut.update_specification(specs[St.target])

                # PRINTING THE SPECIFICATIONS
                lensUt.print_specs(specs)

                ########################################################################
                print """\n4. EXECUTING THE GEO-MATCH                                """
                ########################################################################
                geo_match(specs)

                ########################################################################
                print """\n5. EXTRACT THE NUMBER OF TRIPLES                          """
                ########################################################################
                specs[St.triples] = Qry.get_namedgraph_size("{0}{1}".format(
                    Ns.lens, specs[St.lens_name]))

                ########################################################################
                print """\n6. ASSIGN THE SPARQL INSERT QUERY                         """
                ########################################################################
                specs[St.insert_query] = "{} ;\n{};\n{}".format(
                    geo_load_query(specs, True), geo_load_query(specs, False),
                    geo_match_query(specs))

                lens_end = time.time()
                diff = lens_end - lens_start
                print "\n\t>>> Executed so far in    : {:<14}".format(
                    str(datetime.timedelta(seconds=diff)))

                if int(specs[St.triples]) > 0:

                    ########################################################################
                    print """\n4. INSERTING THE GENERIC METADATA                         """
                    ########################################################################
                    metadata = Gn.lens_refine_geo_metadata(specs)
                    Qry.boolean_endpoint_response(metadata)

                    ########################################################################
                    print """\n5. WRITING TO FILE                                        """
                    ########################################################################
                    src = [source[St.graph_name], "", source[St.entity_ns]]
                    trg = [target[St.graph_name], "", target[St.entity_ns]]

                    # linkset_path = "D:\datasets\Linksets\ExactName"
                    linkset_path = DIRECTORY
                    writelinkset(src,
                                 trg,
                                 specs[St.lens_name],
                                 linkset_path,
                                 metadata,
                                 check_file=check_file)
                    server_message = "Linksets created as: {}".format(
                        specs[St.lens])
                    message = "The linkset was created as [{}] with {} triples found!".format(
                        specs[St.lens], specs[St.triples])

                    print "\n\t", server_message

                    Urq.register_lens(specs, is_created=True)

                    ls_end_2 = time.time()
                    diff = ls_end_2 - lens_end
                    print ">>> Executed in    : {:<14}".format(
                        str(datetime.timedelta(seconds=diff)))

                    print "\t*** JOB DONE! ***"

                    return {
                        St.message: message,
                        St.error_code: 0,
                        St.result: specs[St.lens]
                    }

                else:
                    print "\tThe linkset was not generated as no match could be found"
                    print "\t*** JOB DONE! ***"
                    return {
                        St.message: message,
                        St.error_code: 4,
                        St.result: None
                    }

    except Exception as err:
        traceback.print_exc()
        return {St.message: Ec.ERROR_CODE_1, St.error_code: 5, St.result: None}
        # print geo_load_query(specs, is_source=True)
        # print geo_load_query(specs, is_source=False)
        # geo_match_query(specs)

        # traceback.print_exception()


# import Alignments.Manage.AdminGraphs as adm
# adm.drop_a_lens("http://risis.eu/lens/refine_union_Grid_20170712_Eter_2014_N291690309", display=True, activated=True)
# refine_lens(specs_example, activated=True, check_file=False)
#
# adm.drop_a_lens("http://risis.eu/lens/refine_union_Orgreg_20170718_Eter_2014_P1061032980", display=True, activated=True)
# refine_lens(specs_example_2, activated=True, check_file=False)
#
# adm.drop_a_lens("http://risis.eu/lens/refine_union_Orgreg_20170718_Grid_20170712_N1966224323", display=True, activated=True)
# refine_lens(specs_example_3, activated=True, check_file=False)
Пример #6
0
def difference(specs, save=False, activated=False):

    # print "LINKSET FUNCTION ACTIVATED: {}".format(activated)
    if activated is False:
        print "THE FUNCTION IS NOT ACTIVATED" \
              "\n======================================================" \
              "========================================================"
        return {St.message: "THE FUNCTION IS NOT ACTIVATED.", St.error_code: 1, St.result: None}
    else:
        print "COMPUTING THE DIFFERENCE BETWEEN:\n\t{}\n\t{}".format(specs[St.subjectsTarget], specs[St.objectsTarget])

    Lu.diff_lens_name(specs)

    # GENERATE THE LINKSET OF THE DIFFERENCE
    diff_insert_query = """
        PREFIX prov: <{0}>
        PREFIX specific:<{8}>
        ### Difference between
        ### <{1}> and
        ### <{2}>
        INSERT
        {{
            GRAPH <{3}>
            {{
                ?subject        ?newSingletons          ?object .
            }}
            GRAPH specific:{9}
            {{
                ?newSingletons	prov:wasDerivedFrom 	?predicate1 .
                ?predicate1	    ?pre 			        ?obj .
            }}
        }}
        WHERE
        {{
            {{
                GRAPH <{4}>
                {{
                    ?subject ?predicate1 ?object .
                    bind( iri(replace("{5}{6}_#", "#",  strafter(str(uuid()), "uuid:") )) as ?newSingletons )
                }}
                GRAPH ?g
                {{
                    ?predicate1 ?pre ?obj .
                }}
            }}
            MINUS
            {{
                GRAPH <{7}>
                {{
                    ?subject ?predicate2 ?object .
                }}
            }}
        }}
        """.format(Ns.prov, specs[St.subjectsTarget], specs[St.objectsTarget], specs[St.lens],
                   specs[St.subjectsTarget], Ns.alivocab, "diff", specs[St.objectsTarget], Ns.singletons,
                   specs[St.lens_name])
    # print diff_insert_query
    # print specs[St.lens_name]

    specs[St.insert_query] = diff_insert_query

    # RUN THE INSERT QUERY
    insertion_result = Qry.boolean_endpoint_response(diff_insert_query)
    print "\tDIFFERENCE INSERTED: {}".format(insertion_result)

    # RUN THE METADATA
    metadata = Gn.diff_meta(specs)

    # print metadata

    if int(specs[St.triples]) > 0:

        insertion_metadata = Qry.boolean_endpoint_response(metadata)
        print "\tDIFFERENCE INSERTED METADATA: {}".format(insertion_metadata)

        if save is True:

            # GENERATE LINKSET CONSTRUCT QUERY
            construct_query = "\n{}\n{}\n{}\n".format(
                "PREFIX alivocab:<{}>".format(Ns.alivocab),
                "construct { ?x ?y ?z }",
                "where     {{ graph <{}> {{ ?x ?y ?z }} }}".format(specs[St.lens]),
            )
            # print construct_query

            # GET THE SINGLETON METADATA USING THE CONSTRUCT QUERY
            construct_response = Qry.endpointconstruct(construct_query)
            if construct_response is not None:
                construct_response = construct_response.replace('{', "<{}>\n{{".format(specs[St.lens]), 1)
                # print construct_response

            # WRITE TO FILE
            print "\t>>> WRITING THE DIFFERENCE TO FILE"
            write_to_file(graph_name=specs[St.lens_name], metadata=metadata.replace("INSERT DATA", ""),
                          correspondences=construct_response, singletons=None, directory=DIRECTORY)

        server_message = "LENS created as: {}".format(specs[St.lens])
        message = "The LENS DIFFERENCE was created as<br/> {}" \
                  "<br/>with {} CORRESPONDENCES INSERTED AS THE DIFFERENCE".format(specs[St.lens], specs[St.triples])
        print "\t", server_message
        print "\t*** JOB DONE! ***"

        # REGISTER THE LENS
        register_lens(specs, is_created=True)

        return {St.message: message, St.error_code: 0, St.result: specs[St.lens]}
    else:
        return {St.message: Ec.ERROR_CODE_4, St.error_code: 4, St.result: None}