Пример #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 specification_2_linkset_subset(specs, activated=False):

    if activated is True:
        print Ut.headings("EXECUTING LINKSET SUBSET SPECS...")
    else:
        print Ut.headings(
            "THE FUNCTION [specification_2_linkset_subset] IS NOT ACTIVATED")
        return {St.message: Ec.ERROR_CODE_0, St.error_code: 0, St.result: None}

    # ACCESS THE TASK SPECIFIC PREDICATE COUNT
    specs[St.sameAsCount] = Qry.get_same_as_count(specs[St.mechanism])

    # UPDATE THE QUERY THAT IS GOING TO BE EXECUTED
    if specs[St.sameAsCount]:

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

        # UPDATE THE SPECS OF SOURCE AND TARGETS
        update_specification(source)
        update_specification(target)

        # GENERATE THE NAME OF THE LINKSET
        Ls.set_subset_name(specs)

        # SETTING SOME GENERIC METADATA INFO
        specs[St.link_name] = "same"
        specs[St.linkset_name] = specs[St.linkset_name]
        specs[St.link] = "http://risis.eu/linkset/predicate/{}".format(
            specs[St.link_name])
        specs[
            St.
            link_subpropertyof] = "http://risis.eu/linkset/predicate/{}".format(
                specs[St.link_name])
        specs[St.linkset] = "{}{}".format(Ns.linkset, specs[St.linkset_name])
        specs[St.assertion_method] = "{}{}".format(Ns.method,
                                                   specs[St.linkset_name])
        specs[St.justification] = "{}{}".format(Ns.justification,
                                                specs[St.linkset_name])

        # COMMENT ON THE LINK PREDICATE
        specs[St.link_comment] = "The predicate <{}> is used in replacement of the linktype <{}> used in the " \
                                 "original <{}> dataset.".format(
            specs[St.link], specs[St.source][St.link_old], specs[St.source][St.graph])

        # COMMENT ON THE JUSTIFICATION FOR THIS LINKSET
        specs[St.justification_comment] = "In OrgRef's a set of entities are linked to GRID. The linking method " \
                                          "used by OrgRef is unknown. Here we assume that it is a curated work " \
                                          "and extracted it as a linkset.",

        # COMMENT ON THE LINKSET ITSELF
        specs[St.linkset_comment] = "The current linkset is a subset of the <{0}> dataset that links <{0}> to " \
                                    "<{1}>. The methodology used by <{0}> to generate this builtin linkset in " \
                                    "unknown.".format(specs[St.source][St.graph], specs[St.target][St.graph])

        source[St.entity_ns] = str(source[St.entity_datatype]).replace(
            source[St.entity_name], '')
        target[St.entity_ns] = str(target[St.entity_datatype]).replace(
            target[St.entity_name], '')

        # GENERATE THE LINKSET
        inserted_linkset = spa_linkset_subset(specs, activated)
        # print "LINKSET SUBSET RESULT:", inserted_linkset

        if inserted_linkset[St.message].__contains__("ALREADY EXISTS"):
            return inserted_linkset

        if specs[St.triples] > "0":

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

        return inserted_linkset

    else:
        print Ec.ERROR_CODE_1
        return {St.message: Ec.ERROR_CODE_1, St.error_code: 5, St.result: None}
Пример #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 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"
    }
Пример #5
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.")
Пример #6
0
        Ut.batch_load(writers[St.batch_output_path])
    # inserted = Qry.insert_size(specs[St.linkset], isdistinct=False)

    metadata = Gn.linkset_metadata(specs, display=False).replace("INSERT DATA", "")
    writers[St.meta_writer].write(to_unicode(metadata))

    if int(specs[St.triples]) > 0:
        Qry.boolean_endpoint_response(metadata)
        writers[St.meta_writer].close()

        # REGISTER THE ALIGNMENT
        # if check[St.result].__contains__("ALREADY EXISTS"):
        #     Urq.register_alignment_mapping(specs, created=False)
        # else:
        #     Urq.register_alignment_mapping(specs, created=True)
        Urq.register_alignment_mapping(specs, created=False)
        # WRITE TO FILE
        # check_rdf_file(writers[St.crpdce_writer_path])
        # check_rdf_file(writers[St.meta_writer_path])
        # check_rdf_file(writers[St.singletons_writer_path])
    else:
        writers[St.meta_writer].close()

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

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

else:
    print "NO LINK WAS CREATED"
Пример #7
0
def intersecting(specs, activated=False):

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

    print Ut.headings("EXECUTING INTERSECTION SPECS...")

    # 1. GENERATE THE LENS NAME
    lens_name = generate_lens_name(specs['datasets'], operator="intersection")
    specs[St.lens] = "{}{}".format(Ns.lens, lens_name['name'])
    Ut.update_specification(specs)

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

    # 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:

        try:
            specs[St.lens_target_triples] = ""

            # DOCUMENTING START TIME
            lens_start = time.time()

            print "\n4. GENERATE THE INSERT QUERY"
            specs[St.insert_query] = intersection_extended(
                specs, lens_name=specs[St.lens_name])
            print specs[St.insert_query]

            print "\n5. >>> LOOKING FOR INTERSECTING LINKS"
            print "\t", Qry.boolean_endpoint_response(specs[St.insert_query])

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

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

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

                for linkset in specs[St.datasets]:
                    specs[St.lens_target_triples] += \
                        "\n\t        void:target                         <{}> ;".format(linkset)

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

                # print "\n8. WRITING TO 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 "The linkset was not generated as no match could be found"
                print "\t*** JOB DONE! ***"
                return {
                    St.message:
                    "The linkset was not generated as no match could be found",
                    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
            }


# specs = {
#     'lens_operation': u'intersection',
#     'datasets': [u'http://risis.eu/linkset/grid_20170712_eter_2014_approxStrSim_Organization_altLabel_P1079405301',
#                  u'http://risis.eu/linkset/grid_20170712_eter_2014_approxStrSim_Organization_altLabel_P1661430032',
#                  u'http://risis.eu/linkset/grid_20170712_eter_2014_approxStrSim_Organization_label_N1860664105'],
#     'researchQ_URI': u'http://risis.eu/activity/idea_67a6ce'}

# specs_2 = {'lens_operation': u'intersection',
#     'datasets': [u'http://risis.eu/lens/union_Grid_20170712_Eter_2014_N291690309',
#               u'http://risis.eu/lens/union_Orgreg_20170718_Eter_2014_P1061032980',
#               u'http://risis.eu/lens/union_Orgreg_20170718_Grid_20170712_N1966224323'],
#     'researchQ_URI': u'http://risis.eu/activity/idea_67a6ce'}
#
# specs_3 = {'lens_operation': u'intersection',
#            'datasets': [
#                u'http://risis.eu/linkset/orgreg_20170718_grid_20170712_approxStrSim_University_Entity_current_name_English_N682223883',
#                u'http://risis.eu/linkset/orgreg_20170718_grid_20170712_approxStrSim_University_Entity_current_name_English_P2117262605',
#                u'http://risis.eu/lens/union_Grid_20170712_Eter_2014_N291690309',
#                u'http://risis.eu/lens/union_Orgreg_20170718_Eter_2014_P1061032980',
#                u'http://risis.eu/lens/union_Orgreg_20170718_Grid_20170712_N1966224323'],
#            'researchQ_URI': u'http://risis.eu/activity/idea_67a6ce'}
#
#
#
# print intersection_extended(specs_3, "lens_name", display=False)

# import Alignments.Manage.AdminGraphs as adm
# adm.drop_a_lens("http://risis.eu/lens/intersection_Grid_20170712_Eter_2014_P1326988364", display=True, activated=True)
# print intersecting(specs, activated=True)
Пример #8
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)
Пример #9
0
}

dbpEnArtwork_ProPath_2 = {
    St.graph:
    dataset_en,
    St.entity_datatype:
    artwork,
    St.aligns:
    "<http://dbpedia.org/ontology/author>/<http://www.w3.org/2000/01/rdf-schema#label>"
}

################################################################################################
""" 1: REGISTER YOUR RESEARCH QUESTION """
################################################################################################
question = "1 - What paintings did Rembrandt paint?"
research_qst = Urq.register_research_question(question)
research_uri = research_qst[St.result]
# research_uri = "http://risis.eu/activity/idea_030dae]"

################################################################################################
""" 2: REGISTER A DATASET MAPPING: A PAIR OF [DATASET|ENTITY TYPE] """
################################################################################################
ds_mapping = {
    "http://mytest.org/dbpedia-en": [
        "http://dbpedia.org/ontology/Artist",
        "http://dbpedia.org/ontology/Artwork"
    ],
    "http://mytest.org/dbpedia-nl":
    ["http://dbpedia.org/ontology/Artist", "http://schema.org/Painting"]
}
Urq.register_dataset_mapping(question_uri=research_uri,