def check_type_definitions(vocabfile): graph = Graph() try: graph.parse(vocabfile) except: return False all_definitions = True testo = vocab_type_definitions_test['rdfs'] subjects = [] subs = graph.subjects(namespaces['rdf']['type'], URIRef(testo)) for s in subs: subjects.append(s) if subjects: objects = vocab_type_definitions_rdfs else: objects = vocab_type_definitions_owl for o in objects: subs = graph.subjects(namespaces['rdf']['type'], o) done = [] for s in subs: if s in done: continue done.append(s) definition = False vals = graph.objects(s, namespaces['rdfs']['isDefinedBy']) for val in vals: definition = True all_definitions = all_definitions and definition return all_definitions
def check_propeties(vocabfile): graph = Graph() try: graph.parse(vocabfile) except: return {} subject = none for s in graph.subjects(namespaces['dc']['title'], None): subject = s if not subject: for s in graph.subjects(namespaces['dcterms']['title'], None): subject = s if not subject: for s in graph.subjects(namespaces['dc']['creator'], None): subject = s if not subject: for s in graph.subjects(namespaces['dcterms']['creator'], None): subject = s properties = {} #Identifier identifier = [] ids = graph.objects(subject, namespaces['dc']['identifier']) for id in ids: identifier = id if not identifier: ids = graph.objects(subject, namespaces['dcterms']['identifier']) for id in ids: identifier.append(id) properties['identifier'] = [id] #hasFormat format = False prop1 = True prop2 = False prop3 = False properties['format'] = [] for fm in graph.objects(subject, namespaces['dcterms']['hasFormat']): properties['format'].append(fm) bnodes = graph.objects(fm, namespaces['dc']['format']) for b in bnodes: for value in graph.objects(b, namespaces['rdf']['value']): prop1 = True for value in graph.objects(b, namespaces['rdfs']['label']): prop2 = True for value in graph.objects(b, namespaces['rdfs']['type']): prop3 = True if not 'all' in properties: properties['all'] = prop1 and prop2 and prop3 else: properties['all'] = properties['all'] and prop1 and prop2 and prop3 conforms = False if identifier and properties['format'] and properties['all']: conforms = True return (conforms, properties)
def check_type_definitions(vocabfile): graph = Graph() try: graph.parse(vocabfile) except: return False all_definitions = True testo = vocab_type_definitions_test['rdfs'] subjects = [] subs = graph.subjects(namespaces['rdf']['type'], URIRef(testo)) for s in subs: subjects.append(s) if subjects: objects = vocab_type_definitions_rdfs else: objects = vocab_type_definitions_owl for o in objects: subs = graph.subjects(namespaces['rdf']['type'], o) done = [] for s in subs: if s in done: continue done.append(s) definition = False vals = graph.objects(s, namespaces['rdfs']['isDefinedBy']) for val in vals: definition = True all_definitions = all_definitions and definition return all_definitions
def check_propeties(vocabfile): graph = Graph() try: graph.parse(vocabfile) except: return {} subject = none for s in graph.subjects(namespaces['dc']['title'], None): subject = s if not subject: for s in graph.subjects(namespaces['dcterms']['title'], None): subject = s if not subject: for s in graph.subjects(namespaces['dc']['creator'], None): subject = s if not subject: for s in graph.subjects(namespaces['dcterms']['creator'], None): subject = s properties = {} #Identifier identifier = [] ids = graph.objects(subject, namespaces['dc']['identifier']) for id in ids: identifier = id if not identifier: ids = graph.objects(subject, namespaces['dcterms']['identifier']) for id in ids: identifier.append(id) properties['identifier'] = [id] #hasFormat format = False prop1 = True prop2 = False prop3 = False properties['format'] = [] for fm in graph.objects(subject, namespaces['dcterms']['hasFormat']): properties['format'].append(fm) bnodes = graph.objects(fm, namespaces['dc']['format']) for b in bnodes: for value in graph.objects(b, namespaces['rdf']['value']): prop1 = True for value in graph.objects(b, namespaces['rdfs']['label']): prop2 = True for value in graph.objects(b, namespaces['rdfs']['type']): prop3 = True if not 'all' in properties: properties['all'] = prop1 and prop2 and prop3 else: properties['all'] = properties['all'] and prop1 and prop2 and prop3 conforms = False if identifier and properties['format'] and properties['all']: conforms = True return (conforms, properties)
class RdfParser(object): """A basic wrapper for RdfLib's RDF parser. This class aims to accomplish easier parsing, extraction of Models, etc.""" def __init__(self, rdf, format='guess'): """Init the parser with the graph string.""" self.graph = Graph() if format == 'guess': format = self.__guess_format(rdf) print 'RdfParser guesses format to be: %s' % format try: self.graph.load(StringIO(rdf), format=format) except: print "Failed to parse RDF:" print rdf[0:100] def extract(self, datatype): """Extract all of the data of a given datatype.""" data = [] ns = RdfSerializer.NAMESPACES['sylph'] # TODO: Awkward. for sub in self.graph.subjects(RDF.type, ns[datatype]): idx = str(sub) item = {'uri': idx} for pred, obj in self.graph.predicate_objects(sub): if pred == RDF.type: continue if obj == ns['None']: obj = None elif type(obj) == URIRef: obj = unicode(obj) elif type(obj) == Literal: obj = obj.toPython() if type(obj) == Literal: # Don't be silly, RdfLib! obj = unicode(obj) predstr = str(pred).rpartition('#')[2].rpartition('/')[2] item[predstr] = obj data.append(item) return data @staticmethod def __guess_format(st): """Guess the format of the input string.""" # TODO: At present, it can only guess between XML and n3, even # then this is a vague heuristic. if st.startswith('<'): return 'xml' return 'n3'
def schemadoc(uris): G = Graph() for uri in uris: G.parse(uri) print """ <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Schema Documentation</title> <style type="text/css"> body { margin: 1em; font-family: Georgia, sans-serif; } h1 { font-family: Tahoma, sans-serif; } h2, h3, h4, h5, h6 { font-family: Arial, sans-serif; } a { font-weight: bold; color: #036; } dt.class { margin-top: 0.75em; } dt.property { margin-top: 0.75em; } address { padding-top: 0.35em; border-top: 2px solid #369; } </style> </head> <body> <h1>Schema Documentation</h1> """ classes = [] for metaclass in [RDFS.Class, OWL.Class]: for uri in G.subjects(RDF.type, metaclass): if not isinstance(uri, URIRef): continue c = Class(uri) c.classes = [Class(u) for u in G.objects(uri, RDFS.subClassOf) if isinstance(u, URIRef)] for prop in G.subjects(RDFS.domain, uri): p = Property(prop) ranges = [Class(u) for u in G.objects(prop, RDFS.range)] c.properties.append((p, ranges)) # c.properties = [Property(u) for u in G.subjects(RDFS.domain, uri)] c.comments = [str(s) for s in G.objects(uri, RDFS.comment)] classes.append(c) print '<h2>Classes</h2>' print '<ul>' for c in sorted(classes): print '<li>' print '<dl>' print '<dt class="class">' sys.stdout.write(c.name()) if c.classes: o = ', '.join(cls.name(format='text') for cls in sorted(c.classes)) print '(' + o + ')' else: print print '</dt>' for comment in c.comments: print '<dd>' print comment print '</dd>' for prop, ranges in sorted(c.properties): print '<dd>' print ' ' + prop.name() if ranges: print ' => ' + ', '.join(range.name() for range in ranges) print '</dd>' print '</dt>' print '</li>' print '</ul>' print '<h2>Properties</h2>' properties = [] print '<dl>' for propclass in [RDF.Property, OWL.FunctionalProperty, OWL.InverseFunctionalProperty]: for uri in G.subjects(RDF.type, propclass): if not isinstance(uri, URIRef): continue p = Property(uri) properties.append(p) p.kind = Class(propclass) p.domains = [Class(u) for u in G.objects(uri, RDFS.domain) if isinstance(u, URIRef)] p.ranges = [Class(u) for u in G.objects(uri, RDFS.range) if isinstance(u, URIRef)] p.comments = [str(s) for s in G.objects(uri, RDFS.comment)] for p in sorted(properties): print '<dt class="property">' print p.name() + ' (' + p.kind.name(format='text') + ')' print '</dt>' for comment in p.comments: print '<dd>' print comment print '</dd>' if p.domains: print '<dd>domain: ' print ', '.join(domain.name() for domain in p.domains) print '</dd>' if p.ranges: print '<dd>range: ' print ', '.join(range.name() for range in p.ranges) print '</dd>' print '</dl>' print '<address>' print 'Generated by <a href="http://inamidst.com/proj/sdoc/"' print '>Schemadoc</a>' print '</address>' print '</body>' print '</html>'
def testN3Store(store="default", configString=None): g = ConjunctiveGraph(store=store) if configString: g.destroy(configString) g.open(configString) g.parse(StringInputSource(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.store.destroy(configString) except: g.store.destroy(configString) raise
def schemadoc(uris): G = Graph() for uri in uris: G.parse(uri) print """ <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Schema Documentation</title> <style type="text/css"> body { margin: 1em; font-family: Georgia, sans-serif; } h1 { font-family: Tahoma, sans-serif; } h2, h3, h4, h5, h6 { font-family: Arial, sans-serif; } a { font-weight: bold; color: #036; } dt.class { margin-top: 0.75em; } dt.property { margin-top: 0.75em; } address { padding-top: 0.35em; border-top: 2px solid #369; } </style> </head> <body> <h1>Schema Documentation</h1> """ classes = [] for metaclass in [RDFS.Class, OWL.Class]: for uri in G.subjects(RDF.type, metaclass): if not isinstance(uri, URIRef): continue c = Class(uri) c.classes = [ Class(u) for u in G.objects(uri, RDFS.subClassOf) if isinstance(u, URIRef) ] for prop in G.subjects(RDFS.domain, uri): p = Property(prop) ranges = [Class(u) for u in G.objects(prop, RDFS.range)] c.properties.append((p, ranges)) # c.properties = [Property(u) for u in G.subjects(RDFS.domain, uri)] c.comments = [str(s) for s in G.objects(uri, RDFS.comment)] classes.append(c) print '<h2>Classes</h2>' print '<ul>' for c in sorted(classes): print '<li>' print '<dl>' print '<dt class="class">' sys.stdout.write(c.name()) if c.classes: o = ', '.join(cls.name(format='text') for cls in sorted(c.classes)) print '(' + o + ')' else: print print '</dt>' for comment in c.comments: print '<dd>' print comment print '</dd>' for prop, ranges in sorted(c.properties): print '<dd>' print ' ' + prop.name() if ranges: print ' => ' + ', '.join(range.name() for range in ranges) print '</dd>' print '</dt>' print '</li>' print '</ul>' print '<h2>Properties</h2>' properties = [] print '<dl>' for propclass in [ RDF.Property, OWL.FunctionalProperty, OWL.InverseFunctionalProperty ]: for uri in G.subjects(RDF.type, propclass): if not isinstance(uri, URIRef): continue p = Property(uri) properties.append(p) p.kind = Class(propclass) p.domains = [ Class(u) for u in G.objects(uri, RDFS.domain) if isinstance(u, URIRef) ] p.ranges = [ Class(u) for u in G.objects(uri, RDFS.range) if isinstance(u, URIRef) ] p.comments = [str(s) for s in G.objects(uri, RDFS.comment)] for p in sorted(properties): print '<dt class="property">' print p.name() + ' (' + p.kind.name(format='text') + ')' print '</dt>' for comment in p.comments: print '<dd>' print comment print '</dd>' if p.domains: print '<dd>domain: ' print ', '.join(domain.name() for domain in p.domains) print '</dd>' if p.ranges: print '<dd>range: ' print ', '.join(range.name() for range in p.ranges) print '</dd>' print '</dl>' print '<address>' print 'Generated by <a href="http://inamidst.com/proj/sdoc/"' print '>Schemadoc</a>' print '</address>' print '</body>' print '</html>'
def testN3Store(store="default", configString=None): g = ConjunctiveGraph(store=store) if configString: g.destroy(configString) g.open(configString) g.parse(StringInputSource(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.store.destroy(configString) except: g.store.destroy(configString) raise
def update_rdf_for_conversion(prefix, vocab_properties, rdf_vocab_properties): #(id, base, prefix) = get_vocab_base(vocabfile) html_vocab_properties = {} html_vocab_properties['format'] = 'text/html' html_vocab_properties['name'] = "%s.html" % os.path.splitext( rdf_vocab_properties['name'])[0] html_vocab_properties['path'] = rdf_vocab_properties['path'].replace( rdf_vocab_properties['name'], html_vocab_properties['name']) html_vocab_properties['uri'] = rdf_vocab_properties['uri'].replace( rdf_vocab_properties['name'], html_vocab_properties['name']) newrdf_vocab_properties = {} newrdf_vocab_properties['format'] = 'application/rdf+xml' newrdf_vocab_properties['name'] = "%s_modified.rdf" % os.path.splitext( rdf_vocab_properties['name'])[0] newrdf_vocab_properties['path'] = rdf_vocab_properties['path'].replace( rdf_vocab_properties['name'], newrdf_vocab_properties['name']) newrdf_vocab_properties['uri'] = rdf_vocab_properties['uri'].replace( rdf_vocab_properties['name'], newrdf_vocab_properties['name']) graph = Graph() graph.parse(rdf_vocab_properties['path']) subject = None for s in graph.subjects(namespaces['rdf']['type'], URIRef(namespaces['owl']['Ontology'])): subject = s #graph2 = Graph() graph_ns = [] for nsprefix, nsurl in graph.namespaces(): graph_ns.append(str(nsurl)) for prefix, url in namespaces.iteritems(): if not str(url) in graph_ns: graph.bind(prefix, URIRef(url)) #properties = get_vocab_properties(prefix) #subject = None #for s in graph.subjects(namespaces['dc']['title'], None): # subject = s #if not subject: # for s in graph.subjects(namespaces['dcterms']['title'], None): # subject = s #if not subject: # for s in graph.subjects(namespaces['dc']['creator'], None): # subject = s #if not subject: # for s in graph.subjects(namespaces['dcterms']['creator'], None): # subject = s formatNode1 = BNode() formatNode2 = BNode() #Add vocabulary properties identifier and format graph.add((subject, namespaces['dc']['identifier'], URIRef(rdf_vocab_properties['uri']))) graph.add((subject, namespaces['dcterms']['isVersionOf'], URIRef(vocab_properties['preferredNamespaceUri']))) graph.add((subject, namespaces['dcterms']['hasFormat'], URIRef(rdf_vocab_properties['uri']))) graph.add((subject, namespaces['dcterms']['hasFormat'], URIRef(html_vocab_properties['uri']))) graph.add((subject, namespaces['vann']['preferredNamespaceUri'], URIRef(vocab_properties['preferredNamespaceUri']))) graph.add((subject, namespaces['vann']['preferredNamespacePrefix'], URIRef(vocab_properties['preferredNamespacePrefix']))) graph.add((URIRef(html_vocab_properties['uri']), namespaces['rdf']['type'], URIRef(namespaces['dctype']['Text']))) graph.add((URIRef(html_vocab_properties['uri']), namespaces['dc']['format'], formatNode1)) graph.add((formatNode1, namespaces['rdf']['value'], Literal('text/html'))) graph.add((formatNode1, namespaces['rdfs']['label'], Literal('HTML'))) graph.add((formatNode1, namespaces['rdf']['type'], URIRef(namespaces['dcterms']['IMT']))) graph.add((URIRef(rdf_vocab_properties['uri']), namespaces['rdf']['type'], URIRef(namespaces['dctype']['Text']))) graph.add((URIRef(rdf_vocab_properties['uri']), namespaces['dc']['format'], formatNode2)) graph.add((formatNode2, namespaces['rdf']['value'], Literal('application/rdf+xml'))) graph.add((formatNode2, namespaces['rdfs']['label'], Literal('RDF'))) graph.add((formatNode2, namespaces['rdf']['type'], URIRef(namespaces['dcterms']['IMT']))) #Add rdfs:isDefinedBy for each class / property / term of the vocabulary #Find if schema is rdfs / owl. This defines the possible types (rdf:type) for each class / property / term #testo = vocab_type_definitions_test['rdfs'] #subjects = [] #subs = graph.subjects(namespaces['rdf']['type'], URIRef(testo)) #for s in subs: # subjects.append(s) #if subjects: # objects = vocab_type_definitions_rdfs #else: # objects = vocab_type_definitions_owl #For all subjects that are of the type found above, add rdfs:isDefinedBy #for o in objects: # subs = graph.subjects(namespaces['rdf']['type'], o) # for s in subs: # graph.add((s, namespaces['rdfs']['isDefinedBy'], URIRef(vocab_properties['preferredNamespaceUri']))) list_of_terms = get_terms(rdf_vocab_properties['path']) for s in list_of_terms: graph.add((URIRef(s), namespaces['rdfs']['isDefinedBy'], URIRef(vocab_properties['preferredNamespaceUri']))) rdf_str = None rdf_str = graph.serialize(format="pretty-xml") #f = codecs.open(newrdf_vocab_properties['path'], 'w', 'utf-8') f = codecs.open(newrdf_vocab_properties['path'], 'w') f.write(rdf_str) f.close() return (newrdf_vocab_properties, html_vocab_properties)
def update_rdf_for_conversion(prefix, vocab_properties, rdf_vocab_properties): #(id, base, prefix) = get_vocab_base(vocabfile) html_vocab_properties = {} html_vocab_properties['format'] = 'text/html' html_vocab_properties['name'] = "%s.html"%os.path.splitext(rdf_vocab_properties['name'])[0] html_vocab_properties['path'] = rdf_vocab_properties['path'].replace(rdf_vocab_properties['name'], html_vocab_properties['name']) html_vocab_properties['uri'] = rdf_vocab_properties['uri'].replace(rdf_vocab_properties['name'], html_vocab_properties['name']) newrdf_vocab_properties = {} newrdf_vocab_properties['format'] = 'application/rdf+xml' newrdf_vocab_properties['name'] = "%s_modified.rdf"%os.path.splitext(rdf_vocab_properties['name'])[0] newrdf_vocab_properties['path'] = rdf_vocab_properties['path'].replace(rdf_vocab_properties['name'], newrdf_vocab_properties['name']) newrdf_vocab_properties['uri'] = rdf_vocab_properties['uri'].replace(rdf_vocab_properties['name'], newrdf_vocab_properties['name']) graph = Graph() graph.parse(rdf_vocab_properties['path']) subject = None for s in graph.subjects(namespaces['rdf']['type'], URIRef(namespaces['owl']['Ontology'])): subject = s #graph2 = Graph() graph_ns = [] for nsprefix, nsurl in graph.namespaces(): graph_ns.append(str(nsurl)) for prefix, url in namespaces.iteritems(): if not str(url) in graph_ns: graph.bind(prefix, URIRef(url)) #properties = get_vocab_properties(prefix) #subject = None #for s in graph.subjects(namespaces['dc']['title'], None): # subject = s #if not subject: # for s in graph.subjects(namespaces['dcterms']['title'], None): # subject = s #if not subject: # for s in graph.subjects(namespaces['dc']['creator'], None): # subject = s #if not subject: # for s in graph.subjects(namespaces['dcterms']['creator'], None): # subject = s formatNode1 = BNode() formatNode2 = BNode() #Add vocabulary properties identifier and format graph.add((subject, namespaces['dc']['identifier'], URIRef(rdf_vocab_properties['uri']))) graph.add((subject, namespaces['dcterms']['isVersionOf'], URIRef(vocab_properties['preferredNamespaceUri']))) graph.add((subject, namespaces['dcterms']['hasFormat'], URIRef(rdf_vocab_properties['uri']))) graph.add((subject, namespaces['dcterms']['hasFormat'], URIRef(html_vocab_properties['uri']))) graph.add((subject, namespaces['vann']['preferredNamespaceUri'], URIRef(vocab_properties['preferredNamespaceUri']))) graph.add((subject, namespaces['vann']['preferredNamespacePrefix'], URIRef(vocab_properties['preferredNamespacePrefix']))) graph.add((URIRef(html_vocab_properties['uri']), namespaces['rdf']['type'], URIRef(namespaces['dctype']['Text']))) graph.add((URIRef(html_vocab_properties['uri']), namespaces['dc']['format'], formatNode1)) graph.add((formatNode1, namespaces['rdf']['value'], Literal('text/html'))) graph.add((formatNode1, namespaces['rdfs']['label'], Literal('HTML'))) graph.add((formatNode1, namespaces['rdf']['type'], URIRef(namespaces['dcterms']['IMT']))) graph.add((URIRef(rdf_vocab_properties['uri']), namespaces['rdf']['type'], URIRef(namespaces['dctype']['Text']))) graph.add((URIRef(rdf_vocab_properties['uri']), namespaces['dc']['format'], formatNode2)) graph.add((formatNode2, namespaces['rdf']['value'], Literal('application/rdf+xml'))) graph.add((formatNode2, namespaces['rdfs']['label'], Literal('RDF'))) graph.add((formatNode2, namespaces['rdf']['type'], URIRef(namespaces['dcterms']['IMT']))) #Add rdfs:isDefinedBy for each class / property / term of the vocabulary #Find if schema is rdfs / owl. This defines the possible types (rdf:type) for each class / property / term #testo = vocab_type_definitions_test['rdfs'] #subjects = [] #subs = graph.subjects(namespaces['rdf']['type'], URIRef(testo)) #for s in subs: # subjects.append(s) #if subjects: # objects = vocab_type_definitions_rdfs #else: # objects = vocab_type_definitions_owl #For all subjects that are of the type found above, add rdfs:isDefinedBy #for o in objects: # subs = graph.subjects(namespaces['rdf']['type'], o) # for s in subs: # graph.add((s, namespaces['rdfs']['isDefinedBy'], URIRef(vocab_properties['preferredNamespaceUri']))) list_of_terms = get_terms(rdf_vocab_properties['path']) for s in list_of_terms: graph.add((URIRef(s), namespaces['rdfs']['isDefinedBy'], URIRef(vocab_properties['preferredNamespaceUri']))) rdf_str = None rdf_str = graph.serialize(format="pretty-xml") #f = codecs.open(newrdf_vocab_properties['path'], 'w', 'utf-8') f = codecs.open(newrdf_vocab_properties['path'], 'w') f.write(rdf_str) f.close() return (newrdf_vocab_properties, html_vocab_properties)