当前位置: 首页>>代码示例>>Python>>正文


Python Graph.commit方法代码示例

本文整理汇总了Python中rdflib.graph.Graph.commit方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.commit方法的具体用法?Python Graph.commit怎么用?Python Graph.commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rdflib.graph.Graph的用法示例。


在下文中一共展示了Graph.commit方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GraphTest

# 需要导入模块: from rdflib.graph import Graph [as 别名]
# 或者: from rdflib.graph.Graph import commit [as 别名]
class GraphTest(test.TestCase):
    """
    Testing the basic graph functionality.

    Heavily based on https://github.com/RDFLib/rdflib-postgresql/blob/master/test/graph_case.py
    """
    store_name = "Django"
    storetest = True
    path = ''
    create = True

    michel = URIRef(u'michel')
    tarek = URIRef(u'tarek')
    bob = URIRef(u'bob')
    likes = URIRef(u'likes')
    hates = URIRef(u'hates')
    pizza = URIRef(u'pizza')
    cheese = URIRef(u'cheese')

    def setUp(self):
        self.graph = Graph(store=self.store_name)
        self.graph.destroy(self.path)
        self.graph.open(self.path, create=self.create)

    def tearDown(self):
        self.graph.destroy(self.path)
        self.graph.close()

    def addStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.add((tarek, likes, pizza))
        self.graph.add((tarek, likes, cheese))
        self.graph.add((michel, likes, pizza))
        self.graph.add((michel, likes, cheese))
        self.graph.add((bob, likes, cheese))
        self.graph.add((bob, hates, pizza))
        self.graph.add((bob, hates, michel))
        self.graph.commit()

    def removeStuff(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese

        self.graph.remove((tarek, likes, pizza))
        self.graph.remove((tarek, likes, cheese))
        self.graph.remove((michel, likes, pizza))
        self.graph.remove((michel, likes, cheese))
        self.graph.remove((bob, likes, cheese))
        self.graph.remove((bob, hates, pizza))
        self.graph.remove((bob, hates, michel))

    def testAdd(self):
        self.addStuff()

    def testRemove(self):
        self.addStuff()
        self.removeStuff()

    def testTriples(self):
        tarek = self.tarek
        michel = self.michel
        bob = self.bob
        likes = self.likes
        hates = self.hates
        pizza = self.pizza
        cheese = self.cheese
        triples = self.graph.triples
        Any = None

        self.addStuff()

        # unbound subjects
        self.assertEquals(len(list(triples((Any, likes, pizza)))), 2)
        self.assertEquals(len(list(triples((Any, hates, pizza)))), 1)
        self.assertEquals(len(list(triples((Any, likes, cheese)))), 3)
        self.assertEquals(len(list(triples((Any, hates, cheese)))), 0)

        # unbound objects
        self.assertEquals(len(list(triples((michel, likes, Any)))), 2)
        self.assertEquals(len(list(triples((tarek, likes, Any)))), 2)
        self.assertEquals(len(list(triples((bob, hates, Any)))), 2)
        self.assertEquals(len(list(triples((bob, likes, Any)))), 1)

        # unbound predicates
        self.assertEquals(len(list(triples((michel, Any, cheese)))), 1)
        self.assertEquals(len(list(triples((tarek, Any, cheese)))), 1)
        self.assertEquals(len(list(triples((bob, Any, pizza)))), 1)
        self.assertEquals(len(list(triples((bob, Any, michel)))), 1)
#.........这里部分代码省略.........
开发者ID:DarioGT,项目名称:rdflib-django,代码行数:103,代码来源:test_rdflib.py

示例2: TestKyotoCabinetGraphCore

# 需要导入模块: from rdflib.graph import Graph [as 别名]
# 或者: from rdflib.graph.Graph import commit [as 别名]
class TestKyotoCabinetGraphCore(unittest.TestCase):
    def setUp(self):
        store = "KyotoCabinet"
        self.graph = Graph(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_create_db(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.close()
        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)
        self.graph.store.open(self.path, create=True)
        ntriples = self.graph.triples((None, None, None))
        self.assert_(len(list(ntriples)) == 0)

    def test_missing_db_exception(self):
        self.graph.store.close()
        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)
        self.graph.store.open(self.path, create=True)
        ntriples = self.graph.triples((None, None, None))
        self.assert_(len(list(ntriples)) == 0)

    def test_reopening_db(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.close()
        self.graph.store.open(self.path, create=False)
        ntriples = self.graph.triples((None, None, None))
        self.assert_(len(list(ntriples)) == 2)

    def test_reopening_missing_db(self):
        self.graph.store.close()
        self.assertRaises(ValueError, self.graph.store.open, ("/tmp/NotAnExistingDB"), create=False)

    def test_isopen_db(self):
        self.assert_(self.graph.store.is_open() == True)
        self.graph.store.close()
        self.assert_(self.graph.store.is_open() == False)
开发者ID:pebbie,项目名称:rdflib-kyotocabinet,代码行数:98,代码来源:test_corekc.py

示例3: __init__

# 需要导入模块: from rdflib.graph import Graph [as 别名]
# 或者: from rdflib.graph.Graph import commit [as 别名]
class RDFStore:
	configString = "/var/tmp/rdfstore"
	def __init__(self):
		self.nsDict = {}
		pass
	
	def build_graph(self):
				
		self.nsDict['blog'] = Namespace('http://rdflib.net/blog/') 
		self.graph.bind("blog", "http://rdflib.net/blog/")				
		self.nsDict['blogger_id'] = Namespace('http://rdflib.net/blog/blogger_id/')
		self.graph.bind("blogger_id", "http://rdflib.net/blog/blogger_id/")	
		self.nsDict['blog_url'] = Namespace('http://rdflib.net/blog/blog_url/')
		self.graph.bind("blog_url", "http://rdflib.net/blog/blog_url/")	
		self.nsDict['dbpprop'] = Namespace('http://dbpedia.org/property/')
		self.graph.bind("dbpprop", "http://dbpedia.org/property/")		
		self.nsDict['foaf'] = Namespace('http://xmlns.com/foaf/0.1/')
		self.graph.bind("foaf", "http://xmlns.com/foaf/0.1/")	
		self.nsDict['owl']  = Namespace('http://www.w3.org/2002/07/owl#')
		self.graph.bind("owl", "http://www.w3.org/2002/07/owl#")		
		self.nsDict['dcterms'] = Namespace('http://purl.org/dc/terms/')
		self.graph.bind("dcterms", "http://purl.org/dc/terms/")		
		self.nsDict['dbpowl'] = Namespace('http://dbpedia.org/ontology/')
		self.graph.bind("dbpowl", "http://dbpedia.org/ontology/")		
		self.nsDict['rdfs']  = Namespace('http://www.w3.org/2000/01/rdf-schema#')
		self.graph.bind("rdfs", "http://www.w3.org/2000/01/rdf-schema#")		
		self.nsDict['rdf'] = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
		self.graph.bind("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
		 		
		#Prabhjeet
		blogger_id = self.nsDict['blogger_id']
		rdf = self.nsDict['rdf'] 
		owl = self.nsDict['owl'] 
		blog = self.nsDict['blog']
		dbpowl = self.nsDict['dbpowl']
		foaf = self.nsDict['foaf'] 
		self.graph.add((blogger_id, rdf['type'], owl['DatatypeProperty']))
		self.graph.add((blog[blogger_id], foaf['name'], Literal('name')))
		self.graph.add((blog[blogger_id], rdf['type'], owl['DatatypeProperty']))
		self.graph.add((dbpowl['blog'], rdf['type'], owl['ObjectProperty']))

		self.graph.commit()
	
	def parse_data(self,filename):
		#xmldoc = open(filename, "rU")
		#rss = parse(xmldoc)
                blog = self.nsDict['blog']
                foaf = self.nsDict['foaf']
                dcterms = self.nsDict['dcterms'] 
		ldict  = None
		with open(filename,'r') as f:
			ldict = json.loads(f.read())
		for post in ldict:
			text = post['post']
			id = post['id']
			blogger_id1 = post['id']
			#name1 = post['tags']
			date = post['date']
			title = post['title']
                        self.graph.add((blog,blogger_id1,Literal(blogger_id1)))
			#self.graph.add((blog[blogger_id1],foaf['name'],Literal(name1)))
			self.graph.add((blog[blogger_id1],dcterms['title'],Literal(title)))
			self.graph.add((blog[blogger_id1],dcterms['date'],Literal(date)))
		'''	
		for place in rss.getElementsByTagName('place'):
			name1 = place.getElementsByTagName('name1')[0].firstChild.nodeValue
			blogger_id1 = place.getElementsByTagName('blogger-id')[0].firstChild.nodeValue
			title1 = place.getElementsByTagName('title')[0].firstChild.nodeValue
			url1 = place.getElementsByTagName('url')[0].firstChild.nodeValue
			date1 = place.getElementsByTagName('date')[0].firstChild.nodeValue
			expense = place.getElementsByTagName('expense')[0].firstChild.nodeValue
			
			
			blog  = self.nsDict['blog']
			foaf = self.nsDict['foaf']
			dcterms = self.nsDict['dcterms'] 
			self.graph.add((blog,blogger_id1,Literal(blogger_id1)))
			self.graph.add((blog[blogger_id1],foaf['name'],Literal(name1)))
			self.graph.add((blog[blogger_id1],dcterms['title'],Literal(title1)))
			self.graph.add((blog[blogger_id1],dcterms['date'],Literal(date1)))
		'''
			
		self.graph.commit()

	def open_store(self):
		default_graph_uri = "http://rdflib.net/rdfstore"
		# open existing store or create new one
		#store = getStore() # if store does not exist, then new store returned
		
		# RDF store section:
		configString = "/var/tmp/rdfstore"
		
		# Get the Sleepycat plugin.
		store = plugin.get('Sleepycat', Store)('rdfstore')		
		# Open previously created store, or create it if it doesn't exist yet
		path = mkdtemp()
		rt = store.open('rdfstore', create=False)
		#print rt
		#print path
		
#.........这里部分代码省略.........
开发者ID:shreeshga,项目名称:BlogCrawler,代码行数:103,代码来源:rdflibmethods.py

示例4: ATS

# 需要导入模块: from rdflib.graph import Graph [as 别名]
# 或者: from rdflib.graph.Graph import commit [as 别名]

#.........这里部分代码省略.........
        owl = Namespace('http://www.w3.org/2002/07/owl#')
        graph.bind("owl", "http://www.w3.org/2002/07/owl#")
        
        dcterms = Namespace('http://purl.org/dc/terms/')
        graph.bind("dcterms", "http://purl.org/dc/terms/")
        
        dbpowl = Namespace('http://dbpedia.org/ontology/')
        graph.bind("dbpowl", "http://dbpedia.org/ontology/")

        rdfs = Namespace('http://www.w3.org/2000/01/rdf-schema#')
        graph.bind("rdfs", "http://www.w3.org/2000/01/rdf-schema#")
        
        rdf = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
        graph.bind("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")    

                #Prabhjeet
        graph.add((blogger_id, rdf['type'], owl['DatatypeProperty']))
        graph.add((blog[blogger_id], foaf['name'], Literal('name')))
        graph.add((blog[blogger_id], rdf['type'], owl['DatatypeProperty']))
        graph.add((dbpowl['blog'], rdf['type'], owl['ObjectProperty']))
        
                
        xmldoc = open("/Users/prabhjeet/Desktop/WIS/myproject/Demo-Sample.xml", "rU")
        rss = parse(xmldoc)
        for place in rss.getElementsByTagName('place'):
                name1 = place.getElementsByTagName('name1')[0].firstChild.nodeValue
                blogger_id1 = place.getElementsByTagName('blogger-id')[0].firstChild.nodeValue
                title1 = place.getElementsByTagName('title')[0].firstChild.nodeValue
                url1 = place.getElementsByTagName('url')[0].firstChild.nodeValue
                date1 = place.getElementsByTagName('date')[0].firstChild.nodeValue
                expense = place.getElementsByTagName('expense')[0].firstChild.nodeValue
                
                graph.add((blog,blogger_id,Literal(blogger_id1)))
                graph.add((blog[blogger_id1],foaf['name'],Literal(name1)))
                graph.add((blog[blogger_id1],dcterms['title'],Literal(title1)))
                graph.add((blog[blogger_id1],dcterms['date'],Literal(date1)))
                
        
        '''
        for place in rss.getElementsByTagName('place'):
                   pprint (place)
                   print (place.getElementsByTagName('name1')[0].firstChild.nodeValue)
                   print (place.getElementsByTagName('blogger-id')[0].firstChild.nodeValue)
                   print (place.getElementsByTagName('title')[0].firstChild.nodeValue)
                   print (place.getElementsByTagName('url')[0].firstChild.nodeValue)
                   print (place.getElementsByTagName('date')[0].firstChild.nodeValue)
                   print (place.getElementsByTagName('expense')[0].firstChild.nodeValue)
                   #graph.add((   
        '''
        '''
                # Fill in ontology data here:    
                #graph.add((rdflib['game:1'], rdflib['name'], Literal('Half-Life')))
                graph.add((game[gameObject.id], rdf['type'], dbpowl['VideoGame']))
                graph.add((dbpowl['abstract'], rdf['type'], owl['DatatypeProperty']))
                graph.add((foaf['name'], rdf['type'], owl['DatatypeProperty']))
                graph.add((game['id'], rdf['type'], owl['DatatypeProperty']))
                graph.add((game['reviewer'], rdf['type'], owl['DatatypeProperty']))
                graph.add((game['rating'], rdf['type'], owl['DatatypeProperty']))

                graph.add((developer['devid'], rdf['type'], owl['DatatypeProperty']))
                graph.add((developer['devname'], rdf['type'], owl['DatatypeProperty']))
                graph.add((developer['devurl'], rdf['type'], owl['DatatypeProperty']))
                graph.add((dbpowl['developer'], rdf['type'], owl['ObjectProperty']))

                graph.add((publisher['pubid'], rdf['type'], owl['DatatypeProperty']))
                graph.add((publisher['pubname'], rdf['type'], owl['DatatypeProperty']))
                graph.add((publisher['puburl'], rdf['type'], owl['DatatypeProperty']))
                graph.add((dbpowl['publisher'], rdf['type'], owl['ObjectProperty']))

                graph.add((game[gameObject.id], game['id'], Literal(gameObject.id)))
                graph.add((game[gameObject.id], foaf['name'], Literal(gameObject.name)))
                graph.add((game[gameObject.id], dbpowl['abstract'], Literal(gameObject.deck)))
                graph.add((game[gameObject.id], game['rating'], Literal(gameObject.reviewscore)))
                graph.add((game[gameObject.id], game['reviewer'], Literal(gameObject.reviewer)))

                #Adding all developers and publishers here:
                for dev in gameObject.devlist:
                                #graph.add((game[gameObject.id], dbpowl['developer'], Literal(developer.name)))
                                graph.add((developer[dev.id], rdf['type'], dbpowl['Company']))
                                
                                graph.add((game[gameObject.id], dbpowl['developer'], developer[dev.id]))
                                graph.add((developer[dev.id], developer['devid'], Literal(dev.id)))
                                graph.add((developer[dev.id], developer['devname'], Literal(dev.name)))
                                graph.add((developer[dev.id], developer['devurl'], Literal(dev.url)))


                for pub in gameObject.publisherlist:
                                #graph.add((game[gameObject.id], dbpowl['publisher'], Literal(publisher.name)))
                                graph.add((publisher[pub.id], rdf['type'], dbpowl['Company']))

                                graph.add((game[gameObject.id], dbpowl['publisher'], publisher[pub.id]))
                                graph.add((publisher[pub.id], publisher['pubid'], Literal(pub.id)))
                                graph.add((publisher[pub.id], publisher['pubname'], Literal(pub.name)))
                                graph.add((publisher[pub.id], publisher['puburl'], Literal(pub.url)))
        '''
        print_RDF_triples(graph)
        #print_RDF_XML(graph)
        graph.commit()
        #create_RDF_File(graph)
        graph.close(commit_pending_transaction=True)
开发者ID:shreeshga,项目名称:BlogCrawler,代码行数:104,代码来源:rdflibmethods_back.py


注:本文中的rdflib.graph.Graph.commit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。