예제 #1
0
    def testNtriples(self):        
        #test character escaping 
        s1 = r'''bug: File "g:\_dev\rx4rdf\rx\Server.py", '''
        n1 = r'''_:x1f6051811c7546e0a91a09aacb664f56x142 <http://rx4rdf.sf.net/ns/archive#contents> "bug: File \"g:\\_dev\\rx4rdf\\rx\\Server.py\", ".'''
        [(subject, predicate, object, objectType, scope)] = [x for x in parseTriples([n1])]
        self.failUnless(s1 == object)
        #test xml:lang support
        n2 = r'''_:x1f6051811c7546e0a91a09aacb664f56x142 <http://rx4rdf.sf.net/ns/archive#contents> "english"@en-US.'''
        [(subject, predicate, object, objectType, scope)] = [x for x in parseTriples([n2])]
        self.failUnless(object=="english" and objectType == 'en-US')
        #test datatype support
        n3 = r'''_:x1f6051811c7546e0a91a09aacb664f56x142 <http://rx4rdf.sf.net/ns/archive#contents>'''\
        ''' "1"^^http://www.w3.org/2001/XMLSchema#int.'''
        [(subject, predicate, object, objectType, scope)] = [x for x in parseTriples([n3])]
        self.failUnless(object=="1" and objectType == 'http://www.w3.org/2001/XMLSchema#int')

        sio = cStringIO.StringIO()
        writeTriples( [Statement('test:s', 'test:p', u'\x10\x0a\\\u56be',
                                 OBJECT_TYPE_LITERAL)], sio, 'ascii')
        self.failUnless(sio.getvalue() == r'<test:s> <test:p> "\u0010\n\\\u56BE" .'
                        '\n')                      

        #test URI validation when writing triples
        out = cStringIO.StringIO()
        self.failUnlessRaises(RuntimeError, lambda:
            writeTriples( [Statement(BNODE_BASE+'foo bar', 'http://foo bar', 
                'http://foo bar')], out) )
        writeTriples( [Statement(BNODE_BASE+'foobar', 'http://foo', 
                'http://foo bar')], out)         
        self.failUnlessRaises(RuntimeError, lambda:
            writeTriples( [Statement(BNODE_BASE+'foobar', 'http://foo', 
                'http://foo bar',OBJECT_TYPE_RESOURCE)], out) )
예제 #2
0
 def DeserializeFromN3File(n3filepath, driver=Memory, dbName='', create=0, defaultScope='',
                         modelName='default', model=None):
     if not model:
         if create:
             db = driver.CreateDb(dbName, modelName)
         else:
             db = driver.GetDb(dbName, modelName)
         db.begin()
         model = Model4Suite(db)
     else:
         db = model._driver
         
     if isinstance(n3filepath, ( type(''), type(u'') )):
         stream = file(n3filepath, 'r+')
     else:
         stream = n3filepath
         
     #bNodeMap = {}
     #makebNode = lambda bNode: bNodeMap.setdefault(bNode, generateBnode(bNode))
     makebNode = lambda bNode: BNODE_BASE + bNode
     for stmt in parseTriples(stream,  makebNode):
         if stmt[0] is Removed:            
             stmt = stmt[1]
             scope = stmt[4] or defaultScope
             model.remove( FtStatement(stmt[0], stmt[1], stmt[2], '', scope, stmt[3]) )
         else:
             scope = stmt[4] or defaultScope
             model.add( FtStatement(stmt[0], stmt[1], stmt[2], '', scope, stmt[3]) )                
     #db.commit()
     return model, db