Exemplo n.º 1
0
 def testRegex(self):
     # raise SkipTest("Known issue.")
     g = self.graph
     g.parse(data=testN3, format="n3")
     try:
         for s,p,o in g.triples((None,implies,None)):
             formulaA = s
             formulaB = o
         
         assert type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph
         a = URIRef('http://test/a')
         b = URIRef('http://test/b')
         c = URIRef('http://test/c')
         d = URIRef('http://test/d')
         
         universe = ConjunctiveGraph(g.store)
         
         #REGEX triple matching
         assert len(list(universe.triples((None,REGEXTerm('.*22-rdf-syntax-ns.*'),None))))==1
         assert len(list(universe.triples((None,REGEXTerm('.*'),None))))==3
         assert len(list(universe.triples((REGEXTerm('.*formula.*$'),None,None))))==1
         assert len(list(universe.triples((None,None,REGEXTerm('.*formula.*$')))))==1
         assert len(list(universe.triples((None,REGEXTerm('.*implies$'),None))))==1
         for s,p,o in universe.triples((None,REGEXTerm('.*test.*'),None)):
             assert s==a
             assert o==c
         
         for s,p,o in formulaA.triples((None,REGEXTerm('.*type.*'),None)):
             assert o!=c or isinstance(o,BNode)
         
         #REGEX context matching
         assert len(list(universe.contexts((None,None,REGEXTerm('.*schema.*')))))==1
         assert len(list(universe.contexts((None,REGEXTerm('.*'),None))))==3
         
         #test optimized interfaces
         assert len(list(g.store.subjects(RDF.type,[RDFS.Class,c])))==1
         for subj in g.store.subjects(RDF.type,[RDFS.Class,c]):
             assert isinstance(subj,BNode)
         
         assert len(list(g.store.subjects(implies,[REGEXTerm('.*')])))==1
         
         for subj in g.store.subjects(implies,[formulaB,RDFS.Class]):
             assert subj.identifier == formulaA.identifier
         
         assert len(list(g.store.subjects(REGEXTerm('.*'),[formulaB,c])))==2
         assert len(list(g.store.subjects(None,[formulaB,c])))==2
         assert len(list(g.store.subjects(None,[formulaB,c])))==2
         assert len(list(g.store.subjects([REGEXTerm('.*rdf-syntax.*'),d],None)))==2
         
         assert len(list(g.store.objects(None,RDF.type)))==1
         assert len(list(g.store.objects(a,[d,RDF.type])))==1
         assert len(list(g.store.objects(a,[d])))==1
         assert len(list(g.store.objects(a,None)))==1
         assert len(list(g.store.objects(a,[REGEXTerm('.*')])))==1
         assert len(list(g.store.objects([a,c],None)))==1
     
     except:
         g.store.destroy(configString)
         raise
Exemplo n.º 2
0
 def parse_n3(term_n3):
     ''' Disclaimer: Quick and dirty hack using the n3 parser. '''
     prepstr = ("@prefix  xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
                "<urn:no_use> <urn:no_use> %s.\n" % term_n3)
     g = ConjunctiveGraph()
     g.parse(data=prepstr, format='n3')
     return [t for t in g.triples((None, None, None))][0][2]
Exemplo n.º 3
0
 def test_01_query(self):
     g = ConjunctiveGraph(self.store)
     count = 0
     for statement in g.triples((None, None, None)):
         count += 1
         break
     assert count == 1, "Should have found at least one triple"
Exemplo n.º 4
0
 def parse_n3(term_n3):
     ''' Disclaimer: Quick and dirty hack using the n3 parser. '''
     prepstr = ("@prefix  xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
                "<urn:no_use> <urn:no_use> %s.\n" % term_n3)
     g = ConjunctiveGraph()
     g.parse(data=prepstr, format='n3')
     return [t for t in g.triples((None, None, None))][0][2]
Exemplo n.º 5
0
 def test_01_query(self):
     g = ConjunctiveGraph(self.store)
     count = 0
     for statement in g.triples((None, None, None)):
         count += 1
         break
     assert count == 1, "Should have found at least one triple"
Exemplo n.º 6
0
    def handle(self, *args, **options):
        username = options['username']
        project_title = options['project_title']
        res_uri = options['res_uri']
        if (not (username and project_title and res_uri)):
            print "Username, project, and res are required arguments."
            exit(0)
        user_uri = uris.uri('semantic_store_users', username=username)
        user_g = Graph(store=rdfstore(), identifier=user_uri)

        # Project title data is stored in the named graph for that project, so we need
        # to query those graphs rather than just the user graph
        projectsByTitle = self.project_uris_by_title(user_g, user_uri)

        print "projectsByTitle: %s" % projectsByTitle.items()

        if project_title not in projectsByTitle:
            print "No such project with title '%s' found for user '%s'" % \
                (project_title, username)
            print "User %s has projects entitled %s" % (username,
                                                        projectsByTitle.keys())
            exit(0)

        project_uris = projectsByTitle[project_title]
        if len(project_uris) > 1:
            print "More than one project with that title exists."
            exit(0)
        else:
            project_identifier = project_uris[0]

            with transaction.commit_on_success():
                uri = uris.uri('semantic_store_projects',
                               uri=project_identifier)
                project_g = Graph(store=rdfstore(), identifier=uri)
                project_g.add((project_identifier, NS.ore['aggregates'],
                               URIRef(res_uri)))
                main_g = ConjunctiveGraph(store=rdfstore(),
                                          identifier=default_identifier)
                for t in main_g.triples(
                    (URIRef(res_uri), NS.dc['title'], None)):
                    project_g.add(t)
                for t in main_g.triples(
                    (URIRef(res_uri), NS.ore['isDescribedBy'], None)):
                    project_g.add(t)
                for t in main_g.triples(
                    (URIRef(res_uri), NS.rdf['type'], None)):
                    project_g.add(t)
Exemplo n.º 7
0
def find_resource_by_id(resource_id):
    '''
        Returns the charme resource associated with the given resource_id
        * resource_id:String
        * return: an rdflib.Graph object
    '''
    g = ConjunctiveGraph(store=CharmeMiddleware.get_store())
    tmp_g = Graph()
    for res in g.triples((_formatResourceURIRef(resource_id), None, None)):
        tmp_g.add(res) 
    return tmp_g
Exemplo n.º 8
0
 def getRecentComments(self, n=10, notOlderThan=None, withSpam=False):
     self.mongo['comment'].ensure_index('created')
     spec = {}
     if not withSpam:
         spec = self.notSpam
     if notOlderThan is not None:
         now = datetime.datetime.now(tzlocal())
         spec['created'] = {
             '$gt' : now - datetime.timedelta(days=notOlderThan)}
     for doc in self.mongo['comment'].find(spec, limit=n,
                                           sort=[('created', -1)]):
         g = ConjunctiveGraph()
         g.parse(StringInputSource(doc['n3'].encode('utf8')), format='n3')
         parent, _, uri = g.triples((None, SIOC.has_reply, None)).next()
         created = g.value(uri, DCTERMS.created)
         content = g.value(uri, CONTENT.encoded)
         creator = g.value(uri, SIOC.has_creator)
         docId = str(doc['_id'])
         isSpam = doc.get('type', '')
         yield vars()
Exemplo n.º 9
0
 def handle(self, *args, **options):
     with transaction.commit_on_success():        
         main_graph = ConjunctiveGraph(rdfstore(), 
                                       identifier=default_identifier)
         for t in main_graph.triples((None, None, None)):
             main_graph.remove(t)
Exemplo n.º 10
0
def testFormulaStore(store="default", configString=None):
    try:
        g = ConjunctiveGraph(store=store)
    except ImportError:
        raise SkipTest("Dependencies for store '%s' not available!" % store)

    if configString:
        g.destroy(configString)
        g.open(configString)
    else:
        if store == 'SQLite':
            _, path = mkstemp(prefix='test', dir='/tmp', suffix='.sqlite')
            g.open(path, create=True)
        else:
            g.open(mkdtemp(), create=True)

    g.parse(data=testN3, format="n3")
    try:
        for s, p, o in g.triples((None, implies, None)):
            formulaA = s
            formulaB = o

        assert type(formulaA) == QuotedGraph and type(formulaB) == QuotedGraph
        # a = URIRef('http://test/a')
        b = URIRef('http://test/b')
        c = URIRef('http://test/c')
        d = URIRef('http://test/d')
        v = Variable('y')

        universe = ConjunctiveGraph(g.store)

        #test formula as terms
        assert len(list(universe.triples((formulaA, implies, formulaB)))) == 1

        #test variable as term and variable roundtrip
        assert len(list(formulaB.triples((None, None, v)))) == 1
        for s, p, o in formulaB.triples((None, d, None)):
            if o != c:
                assert isinstance(o, Variable)
                assert o == v
        s = list(universe.subjects(RDF.type, RDFS.Class))[0]
        assert isinstance(s, BNode)
        assert len(list(universe.triples((None, implies, None)))) == 1
        assert len(list(universe.triples((None, RDF.type, None)))) == 1
        assert len(list(formulaA.triples((None, RDF.type, None)))) == 1
        assert len(list(formulaA.triples((None, None, None)))) == 2
        assert len(list(formulaB.triples((None, None, None)))) == 2
        assert len(list(universe.triples((None, None, None)))) == 3
        assert len(list(formulaB.triples(
            (None, URIRef('http://test/d'), None)))) == 2
        assert len(list(universe.triples(
            (None, URIRef('http://test/d'), None)))) == 1

        # #context tests
        # #test contexts with triple argument
        # assert len(list(universe.contexts((a, d, c)))) == 1, \
        #                     [ct for ct in universe.contexts((a, d, c))]

        # FAIL: test.test_graph_formula.testFormulaStores('SQLite',)
        # --------------------------------------------------------------------
        # Traceback (most recent call last):
        #   File ".../site-packages/nose/case.py", line 197, in runTest
        #     self.test(*self.arg)
        #   File ".../test_graph_formula.py", line 80, in testFormulaStore
        #     [ct for ct in universe.contexts((a, d, c))]
        # AssertionError: [
        #     <Graph identifier=N52fd4417ef7641089b2e4045ef19ad87
        #        (<class 'rdflib.graph.Graph'>)>,
        #     <Graph identifier=_:Formula16 (<class 'rdflib.graph.Graph'>)>
        #     ]

        #Remove test cases
        universe.remove((None, implies, None))
        assert len(list(universe.triples((None, implies, None)))) == 0
        assert len(list(formulaA.triples((None, None, None)))) == 2
        assert len(list(formulaB.triples((None, None, None)))) == 2

        formulaA.remove((None, b, None))
        assert len(list(formulaA.triples((None, None, None)))) == 1
        formulaA.remove((None, RDF.type, None))
        assert len(list(formulaA.triples((None, None, None)))) == 0

        universe.remove((None, RDF.type, RDFS.Class))

        #remove_context tests
        universe.remove_context(formulaB)
        assert len(list(universe.triples((None, RDF.type, None)))) == 0
        assert len(universe) == 1
        assert len(formulaB) == 0

        universe.remove((None, None, None))
        assert len(universe) == 0

        g.close()
        if store == 'SQLite':
            os.unlink(path)
        else:
            g.store.destroy(configString)
    except:
        g.close()
        if store == 'SQLite':
            os.unlink(path)
        else:
            g.store.destroy(configString)
        raise
Exemplo n.º 11
0
def testN3Store(store="default", configString=None):
    storetest = True
    del storetest
    g = ConjunctiveGraph(store=store)
    if configString:
        # g.destroy(configString)
        g.open(configString, create=True)
    g.parse(data=testN3, format="n3")
    # op = g.serialize(format="n3")
    # print(op)
    formulaA = BNode()
    formulaB = BNode()
    try:
        for s, p, o in g.triples((None, implies, None)):
            formulaA = s
            formulaB = o

        assert type(formulaA) == QuotedGraph \
                and type(formulaB) == QuotedGraph
        a = URIRef('http://test/a')
        b = URIRef('http://test/b')
        c = URIRef('http://test/c')
        d = URIRef('http://test/d')
        v = Variable('y')

        universe = ConjunctiveGraph(g.store)

        #test formula as terms
        assert len(list(universe.triples((formulaA, implies, formulaB)))) == 1

        #test variable as term and variable roundtrip
        assert len(list(formulaB.triples((None, None, v)))) == 1
        for s, p, o in formulaB.triples((None, d, None)):
            if o != c:
                assert isinstance(o, Variable)
                assert o == v
        s = list(universe.subjects(RDF.type, RDFS.Class))[0]
        assert isinstance(s, BNode)
        assert len(list(universe.triples((None, implies, None)))) == 1
        assert len(list(universe.triples((None, RDF.type, None)))) == 1
        assert len(list(formulaA.triples((None, RDF.type, None)))) == 1
        assert len(list(formulaA.triples((None, None, None)))) == 2
        assert len(list(formulaB.triples((None, None, None)))) == 2
        assert len(list(universe.triples((None, None, None)))) == 3
        assert len(
            list(formulaB.triples((None, URIRef('http://test/d'), None)))) == 2
        assert len(
            list(universe.triples((None, URIRef('http://test/d'), None)))) == 1

        #context tests
        #test contexts with triple argument
        assert len(list(universe.contexts((a, d, c)))) == 1

        #Remove test cases
        universe.remove((None, implies, None))
        assert len(list(universe.triples((None, implies, None)))) == 0
        assert len(list(formulaA.triples((None, None, None)))) == 2
        assert len(list(formulaB.triples((None, None, None)))) == 2

        formulaA.remove((None, b, None))
        assert len(list(formulaA.triples((None, None, None)))) == 1
        formulaA.remove((None, RDF.type, None))
        assert len(list(formulaA.triples((None, None, None)))) == 0

        universe.remove((None, RDF.type, RDFS.Class))

        #remove_context tests
        universe.remove_context(formulaB)
        assert len(list(universe.triples((None, RDF.type, None)))) == 0
        assert len(universe) == 1
        assert len(formulaB) == 0

        universe.remove((None, None, None))
        assert len(universe) == 0

        g.store.destroy(configString)
    except:
        g.store.destroy(configString)
        raise
Exemplo n.º 12
0
def testN3Store(store="default", configString=None):
    storetest = True
    g = ConjunctiveGraph(store=store)
    if configString is not None:
        g.destroy(configString)
        g.open(configString, create=True)
    g.parse(data=testN3, format="n3")
    # op = g.serialize(format="n3")
    # print(op)
    formulaA = BNode()
    formulaB = BNode()
    try:
        for s,p,o in g.triples((None,implies,None)):
            formulaA = s
            formulaB = o
        
        assert type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph
        a = URIRef('http://test/a')
        b = URIRef('http://test/b')
        c = URIRef('http://test/c')
        d = URIRef('http://test/d')
        v = Variable('y')
        
        universe = ConjunctiveGraph(g.store)
        
        #test formula as terms
        assert len(list(universe.triples((formulaA,implies,formulaB))))==1
        
        #test variable as term and variable roundtrip
        assert len(list(formulaB.triples((None,None,v))))==1
        for s,p,o in formulaB.triples((None,d,None)):
            if o != c:
                assert isinstance(o,Variable)
                assert o == v
        s = list(universe.subjects(RDF.type, RDFS.Class))[0]
        assert isinstance(s,BNode)
        assert len(list(universe.triples((None,implies,None)))) == 1
        assert len(list(universe.triples((None,RDF.type,None)))) ==1
        assert len(list(formulaA.triples((None,RDF.type,None))))==1
        assert len(list(formulaA.triples((None,None,None))))==2
        assert len(list(formulaB.triples((None,None,None))))==2
        assert len(list(universe.triples((None,None,None))))==3
        assert len(list(formulaB.triples((None,URIRef('http://test/d'),None))))==2
        assert len(list(universe.triples((None,URIRef('http://test/d'),None))))==1
        
        #context tests
        #test contexts with triple argument
        assert len(list(universe.contexts((a,d,c))))==1
        
        #Remove test cases
        universe.remove((None,implies,None))
        assert len(list(universe.triples((None,implies,None))))==0
        assert len(list(formulaA.triples((None,None,None))))==2
        assert len(list(formulaB.triples((None,None,None))))==2
        
        formulaA.remove((None,b,None))
        assert len(list(formulaA.triples((None,None,None))))==1
        formulaA.remove((None,RDF.type,None))
        assert len(list(formulaA.triples((None,None,None))))==0
        
        universe.remove((None,RDF.type,RDFS.Class))
        
        #remove_context tests
        universe.remove_context(formulaB)
        assert len(list(universe.triples((None,RDF.type,None))))==0
        assert len(universe)==1
        assert len(formulaB)==0
        
        universe.remove((None,None,None))
        assert len(universe)==0
        
        g.store.destroy(configString)
    except:
        g.store.destroy(configString)
        raise
class py_drone_graph_core:
    '''
    sample instantiation,
    d_graph = ldg.py_drone_graph(ontology_myID, load_graph_file)
    where,
    1. ontology_myID, uuid for this drone
      e.g. "MjlmNmVmZTAtNGU1OS00N2I4LWI3MzYtODZkMDQ0MTRiNzcxCg=="
    2. load_graph_file, turtle file or (folder) for db initialization
      e.g. base.ttl

    has the following sections,
    1. initialization and graph i/o
    2. utility functions like generating uuid etc.
    '''
    #################
    # class variables
    #################
    g = None  # graph
    Id = None  # local drone id
    files_loaded = False  # flag to prevent ontology reload
    my_host_name = None  # host_name
    BASE = None  # base namespace

    # initialization and graph i/o #############################################
    #######################
    # class initialization
    #######################
    def __init__(self, ontology_myid, graph_dict, my_base, my_host_name):
        '''
        Args:
            ontology_myid (str):    uuid for this drone
            graph_dict (dict.):     configuration data
        '''
        # save hostname
        self.my_host_name = my_host_name + '/'

        # fix base
        self.BASE = rdflib.Namespace(my_base)

        # set base id
        self.Id = ontology_myid

        # load graph, include ttl to load if required
        self.setup_graph(graph_dict)

    ##########################
    # setup and load graph
    ##########################
    def setup_graph(self, graph_dict):
        '''
        Args:
            graph_dict (dict.):     configuration data
        '''
        # get config for graph name, physical db location and it's format
        # added extraction of load_graph_file
        self.graph_name = graph_dict.get('name', ontology_db)
        graph_location = graph_dict.get('db_location', ontology_db_location)
        graph_file_format = graph_dict.get('file_format',
                                           ontology_landrs_file_format)
        load_graph_file = graph_dict.get('file', ontology_db_file)

        # test created instances with pyshacl?
        pshac = graph_dict.get('pyshacl', 'False')
        if pshac == 'False':
            self.pyshacl = False
        else:
            self.pyshacl = True

        shacl_filename = graph_dict.get('shacl_filename', '*shape.ttl')
        ontology_filename = graph_dict.get('ontology_filename', 'ontology.ttl')

        # other shape files
        flight_shacl_filename = graph_dict.get('flight_shacl_filename',
                                               extra_shape_file)
        shacl_constraint_filename = graph_dict.get('shacl_constraint_filename',
                                                   '*shapes.ttl')

        # added file reload startegy
        graph_file_reload = graph_dict.get('file_reload', 'False')

        # does the db exist?
        reload_db = True
        if graph_file_reload == 'False' and os.path.isfile(graph_location +
                                                           '.sqlite'):
            reload_db = False

        # check any folders exist
        os.makedirs(os.path.dirname(graph_location), exist_ok=True)

        # store location
        uri = Literal("sqlite:///%(here)s/%(loc)s.sqlite" % {
            "here": os.getcwd(),
            "loc": graph_location
        })

        # create store
        store_ident = URIRef('store_' + self.graph_name)
        self.store = plugin.get("SQLAlchemy", Store)(identifier=store_ident)

        # was self.g.open
        self.store.open(uri, create=True)

        # and ConjunctiveGraph
        self.g = ConjunctiveGraph(self.store)

        # vars for first graph context
        ident = self.BASE.term(self.graph_name)

        # create and load graph
        self.g1 = Graph(self.store, identifier=ident)

        # vars for shape graph context
        ident2 = self.BASE.term(self.graph_name + '_shape')

        # create and load shape graph
        self.g2 = Graph(self.store, identifier=ident2)

        # vars for config shape graph context
        ident3 = self.BASE.term(self.graph_name + '_config')

        # create and load shape graph
        self.g_config = Graph(self.store, identifier=ident3)

        # print graphs
        print("Graphs")
        for c in self.g.contexts():
            print("-- %s " % c)

        # add LANDRS and other namespaces, this converts the pythonized names to
        # something more readable
        self.g.namespace_manager.bind('landrs', LANDRS)
        self.g.namespace_manager.bind('sosa', SOSA)
        #self.g.namespace_manager.bind('base', BASE)
        #self.g.namespace_manager.bind('qudt-unit-1-1', QUDT_UNIT)
        self.g.namespace_manager.bind('qudt-1-1', QUDT)
        self.g.namespace_manager.bind('geo', GEO)
        self.g.namespace_manager.bind('rdfg', RDFG)

        # Load graph?
        if load_graph_file and not self.files_loaded and reload_db:
            # folder or file?
            if os.path.isdir(load_graph_file):

                # get the list of files
                files_in_graph_folder = os.walk(load_graph_file)
                print("Folder provided for import.")
                # loop
                for (dirpath, dirnames, filenames) in files_in_graph_folder:
                    for file in filenames:
                        file_path = os.path.join(dirpath, file)
                        # each file if turtle
                        if os.path.splitext(file_path)[-1].lower(
                        ) == "." + graph_file_format:
                            if os.path.isfile(file_path):
                                print("file", file_path)
                                self.files_loaded = True
                                # load the individual file
                                try:
                                    # test for shacl files, seperate graph
                                    if fnmatch.fnmatch(
                                            os.path.basename(file_path),
                                            shacl_filename):
                                        self.g2.load(
                                            file_path,
                                            format=graph_file_format,
                                            publicID=self.my_host_name)
                                    else:
                                        self.g1.load(
                                            file_path,
                                            format=graph_file_format,
                                            publicID=self.my_host_name)
                                except Exception as ex:
                                    print("Could not load graph file: " +
                                          str(ex))

            else:
                print("File provided for import.")
                if os.path.isfile(load_graph_file):
                    self.files_loaded = True
                    # load the file
                    try:
                        self.g1.load(load_graph_file,
                                     format=graph_file_format,
                                     publicID=self.my_host_name)
                    except Exception as ex:
                        print("Could not load graph file: " + str(ex))

                    # turn off pyshacl if no seperate shape graph
                    self.pyshacl = False

            # additional config shape files, folder?
            if os.path.isdir(flight_shacl_filename):
                file_list = glob.glob(flight_shacl_filename +
                                      shacl_constraint_filename)
                for fn in file_list:
                    # extract target graph name
                    pos = fn.find(shacl_constraint_filename[1:]) - 1
                    pos2 = fn.rfind('/') + 1

                    # set name
                    g_name = fn[pos2:pos]
                    print("file", fn, g_name)

                    # create graph
                    # vars for config shape graph context
                    identn = self.BASE.term(g_name)

                    # create and load shape graph
                    self.g_tmp = Graph(self.store, identifier=identn)

                    # try to create
                    try:
                        self.g_tmp.load(fn,
                                        format=graph_file_format,
                                        publicID=self.my_host_name)
                    except Exception as ex:
                        print("Could not load shape file: " + str(ex))

            # stand alone?
            elif os.path.isfile(flight_shacl_filename):
                print("file", flight_shacl_filename)
                try:
                    self.g_config.load(flight_shacl_filename,
                                       format=graph_file_format,
                                       publicID=self.my_host_name)
                except Exception as ex:
                    print("Could not load shape file: " + str(ex))

    #############
    # create uuid
    #############
    def generate_uuid(self):
        return base64.urlsafe_b64encode(
            uuid.uuid4().bytes)[:-2].decode('utf-8')

    ######################
    # dump graph as turtle
    ######################
    def dump_graph(self, id):
        graph = self.g.get_context(self.BASE.term(id))
        if graph:
            return graph.serialize(format="turtle", base=self.my_host_name)
        else:
            return None

    #######################
    # dump graphs as turtle
    #######################
    def list_graphs(self):
        ret = '@prefix rdfg: <http://www.w3.org/2004/03/trix/rdfg-1/> .\n' + \
            '@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n' + \
            '@prefix rdflib: <http://rdflib.net/projects#> .\n\n'
        # loop over graphs and append
        for c in self.g.contexts():
            ret = ret + str(c) + '\n'

        # return it
        return ret

    ###########################################
    # find namespace for node from uuid
    # solves problem of having objects created
    # on ld.landrs.org OR the drone.
    # Also test existance.
    ###########################################
    def find_node_from_uuid(self, uuid, id_type=None):
        '''
        Args:
            uuid (str):    uuid to find

        Returns:
           URIRef: node associated with uuid
        '''
        # check drone definition exists and if it is local or on ld.landrs.org
        id_node = LDLBASE.term(uuid)
        if not (id_node, RDF.type, id_type) in self.g:
            # from myself?
            id_node = self.BASE.term(uuid)
            if not (id_node, RDF.type, id_type) in self.g:
                # return info
                return None

        # if here, exists and node resolved
        return id_node

    ##########################################
    # recursive drill down through blank nodes
    ##########################################
    def blank_node_recursion(self, blnk, grph):
        # check blank
        if isinstance(blnk, BNode):
            # get nodes
            for sn, pn, on in self.g.triples((blnk, None, None)):
                grph.add((sn, pn, on))
                # recurse
                self.blank_node_recursion(on, grph)

    #########################################
    # get graph with node and its blank nodes
    #########################################
    def get_graph_with_node(self, id_node):
        '''
        Args:
            id_node (str): node id to put into graph

        Returns:
           graph: graph of id_node
        '''
        node_graph = Graph()
        # get id's triples
        for s, p, o in self.g.triples((id_node, None, None)):
            node_graph.add((s, p, o))
            # if associated blank not, get its tripples
            self.blank_node_recursion(o, node_graph)

        # return the new graph
        return node_graph

    ##########################
    # get triples for an id
    ##########################
    def get_id_data(self, id, json=False):
        '''
        Args:
            id (str): uuid to query

        Returns:
           dict.: query result
        '''
        # dictionary
        id_data = {}

        # is the id a local graph?
        # if so return the graph as turtle
        g = self.g.get_context(self.BASE.term(id))
        if g:
            # return info
            return g.serialize(format="turtle",
                               base=self.my_host_name)  # id_data

        # check drone definition exists and if it is local or on ld.landrs.org
        # we will support ld.landrs.org ids due to potential connectivity problems
        id_node = self.find_node_from_uuid(id)
        if not id_node:
            # return info
            return {"status": "id: " + id + " not found."}

        if not json:
            node_graph = self.get_graph_with_node(id_node)

            # return info
            return node_graph.serialize(format="turtle",
                                        base=self.my_host_name)  # id_data
        else:
            # get id's triples
            for s, p, o in self.g.triples((id_node, None, None)):
                print("{} is a {}".format(p, o))
                id_data.update({p: o})

            # return json here
            return id_data

    ##################################
    # routine to convert graph to json
    ##################################
    def graph_to_json(self, g):
        """
        Pass in a rdflib.Graph and get back a chunk of JSON using
        the Talis JSON serialization for RDF:
        http://n2.talis.com/wiki/RDF_JSON_Specification
        """
        g_json = {}

        # go through all the triples in the graph
        for s, p, o in g:

            # initialize property dictionary if we've got a new subject
            if not s in g_json.keys():
                # if not json.has_key(s):
                g_json[s] = {}

            # initialize object list if we've got a new subject-property combo
            if not p in g_json[s].keys():
                # if not json[s].has_key(p):
                g_json[s][p] = []

            # determine the value dictionary for the object
            v = {'value': o}
            if isinstance(o, rdflib.URIRef):
                v['type'] = 'uri'
            elif isinstance(o, rdflib.BNode):
                v['type'] = 'bnode'
            elif isinstance(o, rdflib.Literal):
                v['type'] = 'literal'
                if o.language:
                    v['lang'] = o.language
                if o.datatype:
                    v['datatype'] = o.datatype

            # add the triple
            g_json[s][p].append(v)

        return json.dumps(g_json, indent=4)
Exemplo n.º 14
0
    def test_n3_store(self):
        # Thorough test suite for formula-aware store

        implies = URIRef("http://www.w3.org/2000/10/swap/log#implies")
        testN3 = """
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix : <http://test/> .
{:a :b :c;a :foo} => {:a :d :c,?y}.
_:foo a rdfs:Class.
:a :d :c."""

        g = self.open_graph()
        g.parse(data=testN3, format="n3")

        formulaA = BNode()
        formulaB = BNode()
        for s,o in g.subject_objects(predicate=implies):
            formulaA = s
            formulaB = o
        assert type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph

        a = URIRef('http://test/a')
        b = URIRef('http://test/b')
        c = URIRef('http://test/c')
        d = URIRef('http://test/d')
        v = Variable('y')
        
        universe = ConjunctiveGraph(g.store)
        
        # test formula as terms
        assert len(list(universe.triples((formulaA, implies, formulaB)))) == 1
        
        # test variable as term and variable roundtrip
        assert len(list(formulaB.triples((None,None,v)))) == 1
        for s,p,o in formulaB.triples((None,d,None)):
            if o != c:
                assert isinstance(o, Variable)
                assert o == v

        s = list(universe.subjects(RDF.type, RDFS.Class))[0]
        assert isinstance(s, BNode)
        assert len(list(universe.triples((None,implies,None)))) == 1
        assert len(list(universe.triples((None,RDF.type,None)))) == 1

        assert len(list(formulaA.triples((None,RDF.type,None)))) == 1
        assert len(list(formulaA.triples((None,None,None)))) == 2

        assert len(list(formulaB.triples((None,None,None)))) == 2
        assert len(list(formulaB.triples((None,d,None)))) == 2

        assert len(list(universe.triples((None,None,None)))) == 3 
        assert len(list(universe.triples((None,d,None)))) == 1
        
        # context tests
        # test contexts with triple argument
        assert len(list(universe.contexts((a,d,c))))==1
        
        # remove test cases
        universe.remove((None,implies,None))
        assert len(list(universe.triples((None,implies,None)))) == 0
        assert len(list(formulaA.triples((None,None,None)))) == 2
        assert len(list(formulaB.triples((None,None,None)))) == 2
        
        formulaA.remove((None,b,None))
        assert len(list(formulaA.triples((None,None,None)))) == 1

        formulaA.remove((None,RDF.type,None))
        assert len(list(formulaA.triples((None,None,None)))) == 0
        
        universe.remove((None,RDF.type,RDFS.Class))
        
        # remove_context tests
        universe.remove_context(formulaB)
        assert len(list(universe.triples((None,RDF.type,None)))) == 0
        assert len(universe) == 1
        assert len(formulaB) == 0
        
        universe.remove((None,None,None))
        assert len(universe) == 0
Exemplo n.º 15
0
class TestLevelDBConjunctiveGraphCore(unittest.TestCase):
    def setUp(self):
        store = "LevelDB"
        self.graph = ConjunctiveGraph(store=store)
        self.path = configString
        self.graph.open(self.path, create=True)

    def tearDown(self):
        self.graph.destroy(self.path)
        try:
            self.graph.close()
        except:
            pass
        if getattr(self, 'path', False) and self.path is not None:
            if os.path.exists(self.path):
                if os.path.isdir(self.path):
                    for f in os.listdir(self.path):
                        os.unlink(self.path + '/' + f)
                    os.rmdir(self.path)
                elif len(self.path.split(':')) == 1:
                    os.unlink(self.path)
                else:
                    os.remove(self.path)

    def test_namespaces(self):
        self.graph.bind("dc", "http://http://purl.org/dc/elements/1.1/")
        self.graph.bind("foaf", "http://xmlns.com/foaf/0.1/")
        self.assert_(len(list(self.graph.namespaces())) == 5)
        self.assert_(('foaf', rdflib.term.URIRef(u'http://xmlns.com/foaf/0.1/')
                      ) in list(self.graph.namespaces()))

    def test_readable_index(self):
        print(readable_index(111))

    def test_triples_context_reset(self):
        michel = rdflib.URIRef(u'michel')
        likes = rdflib.URIRef(u'likes')
        pizza = rdflib.URIRef(u'pizza')
        cheese = rdflib.URIRef(u'cheese')
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.commit()
        ntriples = self.graph.triples((None, None, None),
                                      context=self.graph.store)
        self.assert_(len(list(ntriples)) == 2)

    def test_remove_context_reset(self):
        michel = rdflib.URIRef(u'michel')
        likes = rdflib.URIRef(u'likes')
        pizza = rdflib.URIRef(u'pizza')
        cheese = rdflib.URIRef(u'cheese')
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.commit()
        self.graph.store.remove((michel, likes, cheese), self.graph.store)
        self.graph.commit()
        self.assert_(
            len(
                list(
                    self.graph.triples((None, None, None),
                                       context=self.graph.store))) == 1)

    def test_remove_db_exception(self):
        michel = rdflib.URIRef(u'michel')
        likes = rdflib.URIRef(u'likes')
        pizza = rdflib.URIRef(u'pizza')
        cheese = rdflib.URIRef(u'cheese')
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.commit()
        self.graph.store.__len__(context=self.graph.store)
        self.assert_(
            len(
                list(
                    self.graph.triples((None, None, None),
                                       context=self.graph.store))) == 2)
Exemplo n.º 16
0
 def handle(self, *args, **options):
     with transaction.commit_on_success():
         main_graph = ConjunctiveGraph(rdfstore(),
                                       identifier=default_identifier)
         for t in main_graph.triples((None, None, None)):
             main_graph.remove(t)
Exemplo n.º 17
0
# default rdflib parsers
#   https://rdflib.readthedocs.org/en/stable/plugin_parsers.html
# rdflib doesn't support JSON format without an extension
#   can use https://github.com/RDFLib/rdflib-jsonld to add that capability

g.parse("http://dbpedia.org/data/Python_(programming_language).ntriples", format="nt")

# for o,p,s in g:
#     print("o: ", o)
#     print("p: ", p)
#     print("s: ", s)
#
# for triple in g:
#     print(triple)

r = list(g.triples((None, rdflib.URIRef('rdf:about'), None)))

print(r)

out = g.serialize(format="pretty-xml")
print(out)

# need to write out in binary format - because of unicode?
#outfile = open("test.xml", 'wb')
#outfile.write(g.serialize(format="pretty-xml"))

q = prepareQuery('''
SELECT ?object WHERE
{ ?language dbpedia:influenced ?object. }''',
                initNs = {'dbpedia': 'http://dbpedia.org/ontology/'})
g = Graph()
Exemplo n.º 18
0
    def test_rdflib_mysql_test(self):
        """
        test taken from rdflib/test/test_mysql.py
        """
        implies = URIRef("http://www.w3.org/2000/10/swap/log#implies")
        testN3="""
        @prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
        @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
        @prefix : <http://test/> .
        {:a :b :c;a :foo} => {:a :d :c,?y}.
        _:foo a rdfs:Class.
        :a :d :c."""


        #Thorough test suite for formula-aware store
        g = self.rdflib_graph
        g.parse(data=testN3, format="n3")
        #print g.store
        for s,p,o in g.triples((None,implies,None)):
            formulaA = s
            formulaB = o

        self.assertTrue(type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph)
        a = URIRef('http://test/a')
        b = URIRef('http://test/b')
        c = URIRef('http://test/c')
        d = URIRef('http://test/d')
        v = Variable('y')

        universe = ConjunctiveGraph(g.store)

        #test formula as terms
        self.assertTrue(len(list(universe.triples((formulaA,implies,formulaB))))==1)

        #test variable as term and variable roundtrip
        self.assertTrue(len(list(formulaB.triples((None,None,v))))==1)
        for s,p,o in formulaB.triples((None,d,None)):
            if o != c:
                self.assertTrue(isinstance(o,Variable))
                self.assertTrue(o == v)
        s = list(universe.subjects(RDF.type, RDFS.Class))[0]
        self.assertTrue(isinstance(s,BNode))
        self.assertTrue( len(list(universe.triples((None,implies,None)))) == 1)
        self.assertTrue( len(list(universe.triples((None,RDF.type,None)))) ==1)
        self.assertTrue( len(list(formulaA.triples((None,RDF.type,None))))==1)
        self.assertTrue( len(list(formulaA.triples((None,None,None))))==2)
        self.assertTrue( len(list(formulaB.triples((None,None,None))))==2)
        self.assertTrue( len(list(universe.triples((None,None,None))))==3)
        self.assertTrue( len(list(formulaB.triples((None,URIRef('http://test/d'),None))))==2)
        self.assertTrue( len(list(universe.triples((None,URIRef('http://test/d'),None))))==1)

        #context tests
        #test contexts with triple argument
        self.assertTrue( len(list(universe.contexts((a,d,c))))==1)

        #Remove test cases
        universe.remove((None,implies,None))
        self.assertTrue( len(list(universe.triples((None,implies,None))))==0)
        self.assertTrue( len(list(formulaA.triples((None,None,None))))==2)
        self.assertTrue( len(list(formulaB.triples((None,None,None))))==2)

        formulaA.remove((None,b,None))
        self.assertTrue( len(list(formulaA.triples((None,None,None))))==1)
        formulaA.remove((None,RDF.type,None))
        self.assertTrue( len(list(formulaA.triples((None,None,None))))==0)

        universe.remove((None,RDF.type,RDFS.Class))


        #remove_context tests
        universe.remove_context(formulaB)
        self.assertTrue( len(list(universe.triples((None,RDF.type,None))))==0)
        self.assertTrue( len(universe)==1)
        self.assertTrue( len(list(formulaB.triples((None,None,None))))==0)

        universe.remove((None,None,None))
        self.assertTrue( len(universe)==0)
Exemplo n.º 19
0
class TestKyotoCabinetConjunctiveGraphCore(unittest.TestCase):
    def setUp(self):
        store = "KyotoCabinet"
        self.graph = ConjunctiveGraph(store=store)
        self.path = configString
        self.graph.open(self.path, create=True)

    def tearDown(self):
        self.graph.destroy(self.path)
        try:
            self.graph.close()
        except:
            pass
        if getattr(self, "path", False) and self.path is not None:
            if os.path.exists(self.path):
                if os.path.isdir(self.path):
                    for f in os.listdir(self.path):
                        os.unlink(self.path + "/" + f)
                    os.rmdir(self.path)
                elif len(self.path.split(":")) == 1:
                    os.unlink(self.path)
                else:
                    os.remove(self.path)

    def test_namespaces(self):
        self.graph.bind("dc", "http://http://purl.org/dc/elements/1.1/")
        self.graph.bind("foaf", "http://xmlns.com/foaf/0.1/")
        self.assert_(len(list(self.graph.namespaces())) == 5)
        self.assert_(("foaf", rdflib.term.URIRef(u"http://xmlns.com/foaf/0.1/")) in list(self.graph.namespaces()))

    def test_play_journal(self):
        self.assertRaises(NotImplementedError, self.graph.store.play_journal, {"graph": self.graph})

    def test_readable_index(self):
        print(readable_index(111))

    def test_triples_context_reset(self):
        michel = rdflib.URIRef(u"michel")
        likes = rdflib.URIRef(u"likes")
        pizza = rdflib.URIRef(u"pizza")
        cheese = rdflib.URIRef(u"cheese")
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.commit()
        ntriples = self.graph.triples((None, None, None), context=self.graph.store)
        self.assert_(len(list(ntriples)) == 2)

    def test_remove_context_reset(self):
        michel = rdflib.URIRef(u"michel")
        likes = rdflib.URIRef(u"likes")
        pizza = rdflib.URIRef(u"pizza")
        cheese = rdflib.URIRef(u"cheese")
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.commit()
        self.graph.store.remove((michel, likes, cheese), self.graph.store)
        self.graph.commit()
        self.assert_(len(list(self.graph.triples((None, None, None), context=self.graph.store))) == 1)

    def test_remove_db_exception(self):
        michel = rdflib.URIRef(u"michel")
        likes = rdflib.URIRef(u"likes")
        pizza = rdflib.URIRef(u"pizza")
        cheese = rdflib.URIRef(u"cheese")
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.commit()
        self.graph.store.__len__(context=self.graph.store)
        self.assert_(len(list(self.graph.triples((None, None, None), context=self.graph.store))) == 2)
Exemplo n.º 20
0
def clean_uri(uri):
    if uri.find('#') != -1:
        special_char = '#'
    else:
        special_char = '/'
    index = uri.rfind(special_char)
    return uri[index + 1:len(uri)]


keywords = []
links = []
cont_key = {}
resources = session.query(Resource).filter(Resource.context_id == context_id,
                                           Resource.potential == False)
for res in resources:
    #print(res.name)
    #print ("Triples in graph before add: ", len(graph))
    for s, p, o in graph.triples((None, None, Literal(res.name))):
        #print(s, p , o)
        keywords.append(res.name)
        links.append(s)
        for s2, p2, o2 in graph.triples((s, RDF.type, None)):
            # print(s2, p2, o2)
            cont_key.update({res.name: clean_uri(o2)})

list_sentences = []

print(keywords)
print(links)
Exemplo n.º 21
0
 def test_18_conjunctive(self):
     g = ConjunctiveGraph(store)
     for statement in g.triples((None, None, None)):
         break
     assert statement
Exemplo n.º 22
0
def testFormulaStore(store="default", configString=None):
    try: 
        g = ConjunctiveGraph(store=store)
    except ImportError: 
        raise SkipTest("Dependencies for store '%s' not available!"%store)
  



    if configString:
        g.destroy(configString)
        g.open(configString)
    else: 
        g.open(mkdtemp(), create=True)

    g.parse(data=testN3, format="n3")
    print g.store
    try:
        for s,p,o in g.triples((None,implies,None)):
            formulaA = s
            formulaB = o

        assert type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph
        a = URIRef('http://test/a')
        b = URIRef('http://test/b')
        c = URIRef('http://test/c')
        d = URIRef('http://test/d')
        v = Variable('y')

        universe = ConjunctiveGraph(g.store)

        #test formula as terms
        assert len(list(universe.triples((formulaA,implies,formulaB))))==1

        #test variable as term and variable roundtrip
        assert len(list(formulaB.triples((None,None,v))))==1
        for s,p,o in formulaB.triples((None,d,None)):
            if o != c:
                assert isinstance(o,Variable)
                assert o == v
        s = list(universe.subjects(RDF.type, RDFS.Class))[0]
        assert isinstance(s,BNode)
        assert len(list(universe.triples((None,implies,None)))) == 1
        assert len(list(universe.triples((None,RDF.type,None)))) ==1
        assert len(list(formulaA.triples((None,RDF.type,None))))==1
        assert len(list(formulaA.triples((None,None,None))))==2
        assert len(list(formulaB.triples((None,None,None))))==2
        assert len(list(universe.triples((None,None,None))))==3
        assert len(list(formulaB.triples((None,URIRef('http://test/d'),None))))==2
        assert len(list(universe.triples((None,URIRef('http://test/d'),None))))==1

        #context tests
        #test contexts with triple argument
        assert len(list(universe.contexts((a,d,c))))==1

        #Remove test cases
        universe.remove((None,implies,None))
        assert len(list(universe.triples((None,implies,None))))==0
        assert len(list(formulaA.triples((None,None,None))))==2
        assert len(list(formulaB.triples((None,None,None))))==2

        formulaA.remove((None,b,None))
        assert len(list(formulaA.triples((None,None,None))))==1
        formulaA.remove((None,RDF.type,None))
        assert len(list(formulaA.triples((None,None,None))))==0

        universe.remove((None,RDF.type,RDFS.Class))


        #remove_context tests
        universe.remove_context(formulaB)
        assert len(list(universe.triples((None,RDF.type,None))))==0
        assert len(universe)==1
        assert len(formulaB)==0

        universe.remove((None,None,None))
        assert len(universe)==0

        g.close()
        g.store.destroy(configString)
    except:
        g.close()
        g.store.destroy(configString)
        raise
Exemplo n.º 23
0
#   https://rdflib.readthedocs.org/en/stable/plugin_parsers.html
# rdflib doesn't support JSON format without an extension
#   can use https://github.com/RDFLib/rdflib-jsonld to add that capability

g.parse("http://dbpedia.org/data/Python_(programming_language).ntriples",
        format="nt")

# for o,p,s in g:
#     print("o: ", o)
#     print("p: ", p)
#     print("s: ", s)
#
# for triple in g:
#     print(triple)

r = list(g.triples((None, rdflib.URIRef('rdf:about'), None)))

print(r)

out = g.serialize(format="pretty-xml")
print(out)

# need to write out in binary format - because of unicode?
#outfile = open("test.xml", 'wb')
#outfile.write(g.serialize(format="pretty-xml"))

q = prepareQuery('''
SELECT ?object WHERE
{ ?language dbpedia:influenced ?object. }''',
                 initNs={'dbpedia': 'http://dbpedia.org/ontology/'})
g = Graph()