DualLoop / data / loader.py
loader.py
Raw
#
#     dualloop
#
#        File:  loader.py
#
#     Authors: Deleted for purposes of anonymity
#
#     Proprietor: Deleted for purposes of anonymity --- PROPRIETARY INFORMATION
#
# The software and its source code contain valuable trade secrets and shall be maintained in
# confidence and treated as confidential information. The software may only be used for
# evaluation and/or testing purposes, unless otherwise explicitly stated in the terms of a
# license agreement or nondisclosure agreement with the proprietor of the software.
# Any unauthorized publication, transfer to third parties, or duplication of the object or
# source code---either totally or in part---is strictly prohibited.
#
#     Copyright (c) 2019 Proprietor: Deleted for purposes of anonymity
#     All Rights Reserved.
#
# THE PROPRIETOR DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE AND THE WARRANTY AGAINST LATENT
# DEFECTS, WITH RESPECT TO THE PROGRAM AND ANY ACCOMPANYING DOCUMENTATION.
#
# NO LIABILITY FOR CONSEQUENTIAL DAMAGES:
# IN NO EVENT SHALL THE PROPRIETOR OR ANY OF ITS SUBSIDIARIES BE
# LIABLE FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES
# FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF INFORMATION, OR
# OTHER PECUNIARY LOSS AND INDIRECT, CONSEQUENTIAL, INCIDENTAL,
# ECONOMIC OR PUNITIVE DAMAGES) ARISING OUT OF THE USE OF OR INABILITY
# TO USE THIS PROGRAM, EVEN IF the proprietor HAS BEEN ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGES.
#
# For purposes of anonymity, the identity of the proprietor is not given herewith.
# The identity of the proprietor will be given once the review of the
# conference submission is completed.
#
# THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
#
import os
import requests
import rdflib
import sys
from SPARQLWrapper import SPARQLWrapper, JSON
import config

os.environ['NO_PROXY'] = '192.168.0.16'

datadir = config.DATA_FOLDER 
rdf4j_server = config.RDF4J_SERVER

def get_immediate_subdirectories(a_dir):
    return [name for name in os.listdir(a_dir)
            if os.path.isdir(os.path.join(a_dir, name))]

def create_triple_config(ontology_name):
    config =  """ 
        @prefix sail:  <http://www.openrdf.org/config/sail#> .
        @prefix ms:    <http://www.openrdf.org/config/sail/memory#> .
        @prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
        @prefix rep:   <http://www.openrdf.org/config/repository#> .
        @prefix sr:    <http://www.openrdf.org/config/repository/sail#> .
            [a rep:Repository ;
                    rdfs:label          "%(label)s" ;
                    rep:repositoryID    "%(rid)s" ;
                    rep:repositoryImpl  [ 
                        rep:repositoryType  "openrdf:SailRepository" ;
                        sr:sailImpl    [ sail:sailType  "openrdf:NativeStore" ;
                                ms:persist     true ;
                                ms:syncDelay   120
                        ]
                    ]] . 
        """ % {"label": ontology_name, "rid": ontology_name} 

    return config


def load_all_datasets():
    datadir = config.DATA_FOLDER
    rdf4j_server = config.RDF4J_SERVER    

    subfolders = get_immediate_subdirectories(datadir)

    for subfolder in subfolders:
        groupfolder = datadir + '/' + subfolder
        ontologylist = get_immediate_subdirectories(groupfolder) 

        for ontology in ontologylist:
            if ontology == "baselines" or ontology == "groundtruth":
                print("Skipping", ontology)
                continue
            # create an empty repository for each ontology
            headers = {'content-type': 'text/turtle'}
            payload = create_triple_config(ontology)
            requests.put(rdf4j_server + '/repositories/' + ontology, data=payload, verify=False, headers=headers)

            ontologyfolder = groupfolder + '/' + ontology

            # import all ontology files under this ontology folder into the newly created repository
            for subdir, dirs, files in os.walk(ontologyfolder):
                for filename in files:
                    if filename.endswith(".rdf") or filename.endswith(".owl"):
                        ontologyfile = os.path.join(subdir, filename)
                        print('Loaded ', ontologyfile)
                        with open(ontologyfile, 'rb') as rdf_file:
                            payload = rdf_file.read()
                            headers = {'content-type': 'application/rdf+xml'}
                            requests.post(rdf4j_server + '/repositories/' + ontology + '/statements', data=payload, verify=False, headers=headers)

def fetch_repository_list():
    sparql = SPARQLWrapper(rdf4j_server + "/repositories")    
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()

    ontology_list = []

    for repository in results['results']['bindings']:
        #print(repository)
        ontology = {}
        ontology['id'] = repository['id']['value']
        ontology['title'] = repository['title']['value']        
        ontology['uri'] = repository['uri']['value']

        ontology_list.append(ontology)
        
    return ontology_list

def delete_one_repository(name):
    requests.delete(rdf4j_server + '/repositories/' + name)

def delete_all_repositories():
    all_repository_list = fetch_repository_list()
    for repository in all_repository_list:
        delete_one_repository(repository['id'])
        print("delete ", repository['id'])

def main():
    parameter_list = sys.argv[1:]

    if len(parameter_list)==0:
        print("please use the parameter: [load|clean]")
    else:
        if parameter_list[0] == 'clean':
            delete_all_repositories()
        if parameter_list[0] == 'load':
            load_all_datasets()

if __name__ == "__main__":
    main()