def main(): parser = argparse.ArgumentParser( description='OMIA integration test', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument( '--input', '-i', type=str, required=True, help='Location of input ttl file') args = parser.parse_args() graph = ConjunctiveGraph() graph.parse(args.input, format=rdflib_util.guess_format(args.input)) model_of = URIRef('http://purl.obolibrary.org/obo/RO_0003301') models = graph.subject_objects(model_of) model_len = len(list(models)) if model_len < EXPECTED_PAIRS: logger.error("Not enough model_of predicates in graph:" " {} expected {} check omia log for" " warnings".format(model_len, EXPECTED_PAIRS)) exit(1) omim_diseases = graph.objects( subject=URIRef('https://monarchinitiative.org/model/OMIA-breed:18'), predicate=model_of ) if list(omim_diseases) != [URIRef('http://purl.obolibrary.org/obo/OMIM_275220')]: logger.error("Missing breed to omim triple for {}".format('OMIA-breed:18')) exit(1) logger.info("PASSED")
def update_list(): """ Reads the triple store looking for twitter usernames and adds them to the dev8d list. """ g = ConjunctiveGraph("Sleepycat") g.open("store") # get oauth client in order credentials = get_credentials() consumer = oauth.Consumer(credentials['key'], credentials['secret']) client = oauth.Client(consumer, credentials['access_token']) list_update_url = 'http://api.twitter.com/1/%s/%s/members.json' % \ (credentials['list_owner'], credentials['list_name']) # create the list if necessary create_list(credentials['list_owner'], credentials['list_name'], client) # look at all the twitter usernames and add them to the list for twitter_username in g.objects(predicate=w['Twitter']): id = twitter_user_id(twitter_username, client) if id: logging.info("adding %s (%s) to list" % (twitter_username, id)) body = "id=%s" % id resp, content = client.request(list_update_url, 'POST', body=body) else: logging.error("unable to get twitter id for %s" % twitter_username) g.close()
class TestSparqlOPT_FILTER(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_OPT_FILTER(self): xd3Objs = [o for o in self.graph.objects(subject=exNS.xd3, predicate=exNS.p)] self.failUnless(xd3Objs[0].datatype == XSD.double, "Expecting %r, got instead : %r"%(double1, xd3Objs[0]))
class TestSparqlOPT_FILTER(unittest.TestCase): def setUp(self): self.graph = ConjunctiveGraph() self.graph.load(StringIO(testContent), format='n3') def test_OPT_FILTER(self): xd3Objs = [ o for o in self.graph.objects(subject=exNS.xd3, predicate=exNS.p) ] self.failUnless( xd3Objs[0].datatype == XSD.double, "Expecting %r, got instead : %r" % (double1, xd3Objs[0]))
def main(): parser = argparse.ArgumentParser( description='OMIA integration test', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument( '--input', '-i', type=str, required=True, help='Location of input ttl file') args = parser.parse_args() graph = ConjunctiveGraph() graph.parse(args.input, format=rdflib_util.guess_format(args.input)) # "is model of": "RO:0003301" # is_model_of = URIRef('OBO:RO_0003301') is_model_of = URIRef('http://purl.obolibrary.org/obo/RO_0003301') # if we curie_map & globaltt here we could ... # (pfx lcl) = globaltt["is model of"].split(':') # iri = curie_map[pfx] + '_'.join((pfx, lcl)) # is_model_of = URIRef(iri) models = graph.subject_objects(is_model_of) model_len = len(set(list(models))) if model_len < EXPECTED_PAIRS: LOG.error( "Not enough <RO:is model of> predicates in graph: found {}, " "expected {} check omia log for warnings".format( model_len, EXPECTED_PAIRS)) exit(1) # else: # LOG.info( # "Found {} model_of predicates in graph, expected at least: {}".format( # model_len, EXPECTED_PAIRS)) breed = 'https://monarchinitiative.org/model/OMIA-breed:758' disease = 'http://omim.org/entry/305100' omim_diseases = graph.objects( subject=URIRef(breed), predicate=is_model_of ) if list(omim_diseases) != [URIRef(disease)]: LOG.error("Missing breed to omim triple for %s", breed) LOG.error(list(omim_diseases)) exit(1) LOG.info("PASSED")
def main(): parser = argparse.ArgumentParser( description='OMIA integration test', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('--input', '-i', type=str, required=True, help='Location of input ttl file') args = parser.parse_args() graph = ConjunctiveGraph() graph.parse(args.input, format=rdflib_util.guess_format(args.input)) # "is model of": "RO:0003301" # is_model_of = URIRef('OBO:RO_0003301') is_model_of = URIRef('http://purl.obolibrary.org/obo/RO_0003301') # if we curie_map & globaltt here we could ... # (pfx lcl) = globaltt["is model of"].split(':') # iri = curie_map[pfx] + '_'.join((pfx, lcl)) # is_model_of = URIRef(iri) models = graph.subject_objects(is_model_of) model_len = len(set(list(models))) if model_len < EXPECTED_PAIRS: LOG.error("Not enough <RO:is model of> predicates in graph: found {}, " "expected {} check omia log for warnings".format( model_len, EXPECTED_PAIRS)) exit(1) # else: # LOG.info( # "Found {} model_of predicates in graph, expected at least: {}".format( # model_len, EXPECTED_PAIRS)) breed = 'https://monarchinitiative.org/model/OMIA-breed:758' disease = 'http://omim.org/entry/305100' omim_diseases = graph.objects(subject=URIRef(breed), predicate=is_model_of) if list(omim_diseases) != [URIRef(disease)]: LOG.error("Missing breed to omim triple for %s", breed) LOG.error(list(omim_diseases)) exit(1) LOG.info("PASSED")
def main(): parser = argparse.ArgumentParser( description='OMIA integration test', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('--input', '-i', type=str, required=True, help='Location of input ttl file') args = parser.parse_args() graph = ConjunctiveGraph() graph.parse(args.input, format=rdflib_util.guess_format(args.input)) model_of = URIRef('http://purl.obolibrary.org/obo/RO_0003301') models = graph.subject_objects(model_of) model_len = len(list(models)) if model_len < EXPECTED_PAIRS: logger.error("Not enough model_of predicates in graph:" " {} expected {} check omia log for" " warnings".format(model_len, EXPECTED_PAIRS)) exit(1) omim_diseases = graph.objects( subject=URIRef('https://monarchinitiative.org/model/OMIA-breed:18'), predicate=model_of) if list(omim_diseases) != [ URIRef('http://purl.obolibrary.org/obo/OMIM_275220') ]: logger.error( "Missing breed to omim triple for {}".format('OMIA-breed:18')) exit(1) logger.info("PASSED")
primer.add((myNS['pat'], myNS['age'], long(24))) # Now, with just that, lets see how the system # recorded *way* too many details about what # you just asserted as fact. # from pprint import pprint pprint(list(primer)) # just think .whatever((s, p, o)) # here we report on what we know pprint(list(primer.subjects())) pprint(list(primer.predicates())) pprint(list(primer.objects())) # and other things that make sense # what do we know about pat? pprint(list(primer.predicate_objects(myNS.pat))) # who is what age? pprint(list(primer.subject_objects(myNS.age))) # Okay, so lets now work with a bigger # dataset from the example, and start # with a fresh new graph. primer = ConjunctiveGraph()
def generictest(testFile): func_name = __name__ = __doc__ = id = 'test_sparql.' + \ os.path.splitext(testFile)[0][8:].translate( maketrans('-/','__')) 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=URIRef(TEST_BASE), format='n3') manifestData = manifestG.query( MANIFEST_QUERY, processor='sparql', 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 # log.debug("## Source: %s ##"%source) # log.debug("## Test: %s ##"%testCaseName) # log.debug("## Result: %s ##"%expectedRT) #Expected results if expectedRT: store = plugin.get(STORE,Store)() store.open(configString,create=False) resultG=ConjunctiveGraph(store).default_context log.debug("###"*10) log.debug("parsing: %s" % open(expectedRT).read()) log.debug("###"*10) assert len(store) == 0 # log.debug("## Parsing (%s) ##"%(expectedRT)) if not trialAndErrorRTParse(resultG,expectedRT,DEBUG): log.debug( "Unexpected result format (for %s), skipping" % \ (expectedRT)) store.rollback() store.close() continue log.debug("## 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 rbinds = [bindingDict[vName] for vName in rtVars] # print("Rbinds", rbinds) if len(rbinds) > 1 and ( isinstance(rbinds, list) or isinstance(rbinds, tuple)): bindings.append(frozenset(rbinds)) elif len(rbinds) == 1 and ( isinstance(rbinds, list) or isinstance(rbinds, tuple)): bindings.append(rbinds[0]) else: bindings.append(rbinds) # bindings.append(tuple([bindingDict[vName] for vName in rtVars])) log.debug(open(expectedRT).read()) store.rollback() store.close() if testFile in tests2Skip.keys(): log.debug("Skipping test (%s) %s\n" % \ (testFile, tests2Skip[testFile])) raise SkipTest("Skipping test (%s) %s\n" % \ (testFile, tests2Skip[testFile])) query = open(testFile).read() log.debug("### %s (%s) ###" % (testCaseName,testFile)) log.debug(query) p = parse(query)#,DEBUG_PARSE) log.debug(p) if EVALUATE and source: log.debug("### Source Graph: ###") log.debug(open(source).read()) store = plugin.get(STORE,Store)() store.open(configString,create=False) g = ConjunctiveGraph(store) try: g.parse(open(source),format='n3') except: log.debug("Unexpected data format (for %s), skipping" % \ (source)) store.rollback() store.close() continue rt = g.query(query, processor='sparql', DEBUG = False) if expectedRT: try: result = rt.result except AttributeError: result = rt if isinstance(result, Graph): resgraph = open(graphtests[testFile]).read() store = plugin.get(STORE,Store)() store.open(configString,create=False) g = ConjunctiveGraph(store) g.parse(data=resgraph,format="n3") assert result == g, \ "### Test Failed: ###\n\nB:\n%s\n\nR:\n%s\n\n" % \ (g.serialize(format="n3"), result.serialize(format="n3")) else: # result = [r[0] for r in result if isinstance(r, (tuple, list))] def stab(r): if isinstance(r, (tuple, list)): return frozenset(r) else: return r results = set( [stab(r) for r in result]) assert set(bindings).difference(results) == set([]) or set(bindings) == results, \ "### Test Failed: ###\n\nB:\n%s\n\nR:\n%s\n\n" % \ (set(bindings), results) log.debug("### Test Passed: ###") store.rollback()
#!/usr/bin/env python from rdflib.graph import ConjunctiveGraph from rdflib.namespace import Namespace dct = Namespace('http://purl.org/dc/terms/') g = ConjunctiveGraph('Sleepycat') g.open('store') subjects = {} for o in g.objects(predicate=dct['subject']): sub = o.split('/')[-1] subjects[sub] = subjects.get(sub, 0) + 1 sorted_keys = subjects.keys() sorted_keys.sort(lambda a, b: cmp(subjects[b], subjects[a])) for subject in sorted_keys: print subject, "\t", subjects[subject]
# Now, with just that, lets see how the system # recorded *way* too many details about what # you just asserted as fact. # from pprint import pprint pprint(list(primer)) # just think .whatever((s, p, o)) # here we report on what we know pprint(list(primer.subjects())) pprint(list(primer.predicates())) pprint(list(primer.objects())) # and other things that make sense # what do we know about pat? pprint(list(primer.predicate_objects(myNS.pat))) # who is what age? pprint(list(primer.subject_objects(myNS.age))) # Okay, so lets now work with a bigger # dataset from the example, and start # with a fresh new graph.
def generictest(testFile): func_name = __name__ = __doc__ = id = 'test_sparql.' + \ os.path.splitext(testFile)[0][8:].translate( maketrans('-/','__')) 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( MANIFEST_QUERY, processor='sparql', 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 # log.debug("## Source: %s ##"%source) # log.debug("## Test: %s ##"%testCaseName) # log.debug("## Result: %s ##"%expectedRT) #Expected results if expectedRT: store = plugin.get(STORE, Store)() store.open(configString, create=False) resultG = ConjunctiveGraph(store).default_context log.debug("###" * 10) log.debug("parsing: %s" % open(expectedRT).read()) log.debug("###" * 10) assert len(store) == 0 # log.debug("## Parsing (%s) ##"%(expectedRT)) if not trialAndErrorRTParse(resultG, expectedRT, DEBUG): log.debug( "Unexpected result format (for %s), skipping" % \ (expectedRT)) store.rollback() store.close() continue log.debug("## 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 rbinds = [bindingDict[vName] for vName in rtVars] # print("Rbinds", rbinds) if len(rbinds) > 1 and (isinstance(rbinds, list) or isinstance(rbinds, tuple)): bindings.append(frozenset(rbinds)) elif len(rbinds) == 1 and (isinstance(rbinds, list) or isinstance(rbinds, tuple)): bindings.append(rbinds[0]) else: bindings.append(rbinds) # bindings.append(tuple([bindingDict[vName] for vName in rtVars])) log.debug(open(expectedRT).read()) store.rollback() store.close() if testFile in tests2Skip.keys(): log.debug("Skipping test (%s) %s\n" % \ (testFile, tests2Skip[testFile])) raise SkipTest("Skipping test (%s) %s\n" % \ (testFile, tests2Skip[testFile])) query = open(testFile).read() log.debug("### %s (%s) ###" % (testCaseName, testFile)) log.debug(query) p = parse(query) #,DEBUG_PARSE) log.debug(p) if EVALUATE and source: log.debug("### Source Graph: ###") log.debug(open(source).read()) store = plugin.get(STORE, Store)() store.open(configString, create=False) g = ConjunctiveGraph(store) try: g.parse(open(source), format='n3') except: log.debug("Unexpected data format (for %s), skipping" % \ (source)) store.rollback() store.close() continue rt = g.query(query, processor='sparql', DEBUG=False) if expectedRT: try: result = rt.result except AttributeError: result = rt if isinstance(result, Graph): resgraph = open(graphtests[testFile]).read() store = plugin.get(STORE, Store)() store.open(configString, create=False) g = ConjunctiveGraph(store) g.parse(data=resgraph, format="n3") assert result == g, \ "### Test Failed: ###\n\nB:\n%s\n\nR:\n%s\n\n" % \ (g.serialize(format="n3"), result.serialize(format="n3")) else: # result = [r[0] for r in result if isinstance(r, (tuple, list))] def stab(r): if isinstance(r, (tuple, list)): return frozenset(r) else: return r results = set([stab(r) for r in result]) assert set(bindings).difference(results) == set([]) or set(bindings) == results, \ "### Test Failed: ###\n\nB:\n%s\n\nR:\n%s\n\n" % \ (set(bindings), results) log.debug("### Test Passed: ###") store.rollback()