Created
April 15, 2017 16:07
-
-
Save berezovskyi/0fc83585f9c1074062a35422f9b09349 to your computer and use it in GitHub Desktop.
Rudimentary RDF parser for http://stackoverflow.com/questions/42741218/using-python-to-query-ibm-rational-change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rdflib | |
from rdflib import URIRef | |
from rdflib.namespace import RDF | |
g=rdflib.Graph() | |
g.load('https://example.com/pror/services/catalog/singleton') | |
def compact_predicate(p): | |
table = { | |
"http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf:", | |
"http://open-services.net/ns/core#": "oslc_core:", | |
"http://purl.org/dc/terms/": "dcterms:" | |
} | |
p_str = str(p) | |
for k,v in table.items(): | |
if p_str.startswith(k): | |
return p_str.replace(k, v) | |
class ServiceProviderCatalog(): | |
TYPE_URI = URIRef(u"http://open-services.net/ns/core#ServiceProviderCatalog") | |
def __init__(self, s, graph_tuples): | |
self.tuples = {} | |
self.tuples_compact = {} | |
for p,o in graph_tuples: | |
self.tuples[p] = o | |
pred = compact_predicate(p) | |
# TODO handle multiple objects automatically | |
if pred != "oslc_core:serviceProvider": | |
self.tuples_compact[pred] = o | |
else: | |
# TODO Recursively construct the RDF resource tree | |
if pred in self.tuples_compact: | |
self.tuples_compact[pred].append(o) | |
else: | |
self.tuples_compact[pred] = [o] | |
def __str__(self): | |
# TODO Handle missing predicates | |
s = "Service Provider Catalog '%s'\n" % self.tuples_compact["dcterms:title"] | |
for v in self.tuples_compact["oslc_core:serviceProvider"]: | |
s += "\t Service Provider '%s'\n" % (v) | |
for k, v in self.tuples_compact.items(): | |
if k!= "oslc_core:serviceProvider": | |
s += "\t %s = %s\n" % (k, v) | |
return s | |
for s,p,o in g.triples( (None, RDF.type, ServiceProviderCatalog.TYPE_URI) ): | |
print(ServiceProviderCatalog(s, g.predicate_objects(s))) | |
# for s,p,o in g: | |
# print(s,p,o) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python main.py | |
Service Provider Catalog 'Service Provider Catalog' | |
Service Provider 'https://example.com/pror/services/rm-services/AAA.reqif' | |
Service Provider 'https://example.com/pror/services/rm-services/BBB.reqif' | |
dcterms:title = Service Provider Catalog | |
dcterms:description = | |
oslc_core:domain = http://open-services.net/ns/rm# | |
rdf:type = http://open-services.net/ns/core#ServiceProviderCatalog |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment