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 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 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 testBasic(DEBUG = False): from glob import glob from sre import sub for testFile in glob('data/examples/*.rq'):#glob('data/*/*.rq'): store = plugin.get(STORE,Store)() bootStrapStore(store) store.commit() prefix = testFile.split('.rq')[-1] manifestPath = '/'.join(testFile.split('/')[:-1]+['manifest.n3']) manifestPath2 = '/'.join(testFile.split('/')[:-1]+['manifest.ttl']) queryFileName = testFile.split('/')[-1] store = plugin.get(STORE,Store)() store.open(configString,create=False) assert len(store) == 0 manifestG=ConjunctiveGraph(store) if not os.path.exists(manifestPath): assert os.path.exists(manifestPath2) manifestPath = manifestPath2 manifestG.default_context.parse(open(manifestPath),publicID=TEST_BASE,format='n3') manifestData = \ manifestG.query( PARSED_MANIFEST_QUERY, initBindings={'?query' : TEST_BASE[queryFileName]}, initNs=manifestNS, DEBUG = False) store.rollback() store.close() for source,testCaseName,testCaseComment,expectedRT in manifestData: if expectedRT: expectedRT = '/'.join(testFile.split('/')[:-1]+[expectedRT.replace(TEST_BASE,'')]) if source: source = '/'.join(testFile.split('/')[:-1]+[source.replace(TEST_BASE,'')]) testCaseName = testCaseComment and testCaseComment or testCaseName print "## Source: %s ##"%source print "## Test: %s ##"%testCaseName print "## Result: %s ##"%expectedRT #Expected results if expectedRT: store = plugin.get(STORE,Store)() store.open(configString,create=False) resultG=ConjunctiveGraph(store).default_context # if DEBUG: # print "###"*10 # print "parsing: ", open(expectedRT).read() # print "###"*10 assert len(store) == 0 print "## Parsing (%s) ##"%(expectedRT) if not trialAndErrorRTParse(resultG,expectedRT,DEBUG): if DEBUG: print "Unexpected result format (for %s), skipping"%(expectedRT) store.rollback() store.close() continue if DEBUG: print "## Done .. ##" rtVars = [rtVar for rtVar in resultG.objects(None,RESULT_NS.resultVariable)] bindings = [] resultSetNode = resultG.value(predicate=RESULT_NS.value,object=RESULT_NS.ResultSet) for solutionNode in resultG.objects(resultSetNode,RESULT_NS.solution): bindingDict = dict([(key,None) for key in rtVars]) for bindingNode in resultG.objects(solutionNode,RESULT_NS.binding): value = resultG.value(subject=bindingNode,predicate=RESULT_NS.value) name = resultG.value(subject=bindingNode,predicate=RESULT_NS.variable) bindingDict[name] = value bindings.append(tuple([bindingDict[vName] for vName in rtVars])) if DEBUG: print "Expected bindings: ", bindings print open(expectedRT).read() store.rollback() store.close() if testFile.startswith('data/NegativeSyntax'): try: query = open(testFile).read() p = Parse(query,DEBUG) except: continue else: raise Exception("Test %s should have failed!"%testFile) if testFile in tests2Skip: print "Skipping test (%s)"%testCaseName continue query = open(testFile).read() print "### %s (%s) ###"%(testCaseName,testFile) print query p = Parse(query,DEBUG_PARSE) if DEBUG: print p if EVALUATE and source: if DEBUG: print "### Source Graph: ###" print open(source).read() store = plugin.get(STORE,Store)() store.open(configString,create=False) g=ConjunctiveGraph(store) try: g.parse(open(source),format='n3') except: print "Unexpected data format (for %s), skipping"%(source) store.rollback() store.close() continue #print store rt = g.query(p,DEBUG = DEBUG) if expectedRT: if rt != bindings and Set([Set(i) for i in rt]) != Set([Set(i) for i in bindings]):#unorderedComparison(rt,bindings): print "### Expected Result (%s) ###"%expectedRT pprint(bindings) print "### Actual Results ###" pprint(rt) raise Exception("### TEST FAILED!: %s ###"%testCaseName) else: print "### TEST PASSED!: %s ###"%testCaseName store.rollback()