Package pypln :: Package stores :: Module ontology
[hide private]

Source Code for Module pypln.stores.ontology

 1  #-*- coding:utf-8 -*- 
 2   
 3  """Store and load ontologies from MongoDB""" 
 4   
 5  __docformat__ = 'restructuredtext en' 
 6   
 7  import pymongo 
 8  import rdflib 
 9  import rdfextras 
10   
11   
12  rdfextras.registerplugins() 
13   
14 -class Ontology(object):
15 """Store and load ontologies from MongoDB""" 16
17 - def __init__(self, name, db, collection, host='127.0.0.1', port=27017):
18 self.name = name 19 self.graph = None 20 self.connection = pymongo.Connection(host=host, port=port) 21 self.collection = self.connection[db][collection]
22
23 - def import_rdfxml(self, uri):
24 """ Read RDF/XML from ``uri`` """ 25 self.graph = rdflib.Graph() 26 self.graph.parse(uri, format='application/rdf+xml')
27
28 - def store(self):
29 """ Store ontology in MongoDB (currently in n3 format) """ 30 data = self._serialize_to_n3() 31 self.collection.insert({'name': self.name, 'n3': data})
32
33 - def load(self, name):
34 """ Load ontology from database and set self.graph to it """ 35 data = self.collection.find_one({'name': name}) 36 self.graph = rdflib.Graph() 37 self.graph.parse(data, format='n3')
38
39 - def _serialize_to_n3(self):
40 """ Return graph serialized to JSON """ 41 if self.graph: 42 return self.graph.serialize(format='n3') 43 else: 44 return None
45