Skip to content

Commit 5330332

Browse files
committed
py2 test and cdl_sparql optimisation
1 parent b2ee1b3 commit 5330332

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

lib/bald/__init__.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,14 @@ def unpack_predicate(self, astring):
358358
result = astring.replace('{}__'.format(prefix),
359359
self.prefixes()[prefix])
360360
elif isinstance(astring, six.string_types):
361-
predicate_alias_query = ('prefix dct: <http://purl.org/dc/terms/> '
362-
'prefix owl: <http://www.w3.org/2002/07/owl#> '
363-
'select ?uri where '
364-
'{{?uri dct:identifier "{}" ; '
365-
' rdf:type ?type. '
366-
'FILTER(?type in (rdf:Property, owl:ObjectProperty) ) '
367-
'}}'.format(astring))
361+
qstr = ('prefix dct: <http://purl.org/dc/terms/> \n'
362+
'prefix owl: <http://www.w3.org/2002/07/owl#> \n'
363+
'select ?uri where \n'
364+
'{{?uri dct:identifier "{}" ; \n'
365+
' rdf:type ?type. \n'
366+
'FILTER(?type in (rdf:Property, owl:ObjectProperty) ) \n'
367+
'}}\n')
368+
predicate_alias_query = (six.text_type(qstr).format(astring))
368369

369370
qres = self.alias_graph.query(predicate_alias_query)
370371
results = list(qres)
@@ -389,12 +390,13 @@ def unpack_rdfobject(self, astring, predicate):
389390
# msg = 'predicate must be a http uri, not {}'.format(predicate)
390391
# raise ValueError(msg)
391392
# can be a file uri too
392-
rdfobj_alias_query = ('prefix dct: <http://purl.org/dc/terms/> '
393-
'select ?uri where '
394-
'{{ <{pred}> rdfs:range ?range . '
395-
'?uri dct:identifier "{id}" ; '
396-
' rdf:type ?range .'
397-
'}}'.format(pred=predicate, id=astring))
393+
qstr = ('prefix dct: <http://purl.org/dc/terms/> \n'
394+
'select ?uri where \n'
395+
'{{ <{pred}> rdfs:range ?range . \n'
396+
'?uri dct:identifier "{id}" ; \n'
397+
' rdf:type ?range .\n'
398+
'}}\n')
399+
rdfobj_alias_query = (six.text_type(qstr).format(pred=predicate, id=astring))
398400
# qres = self.alias_graph.query(rdfobj_alias_query)
399401
try:
400402
qres = self.alias_graph.query(rdfobj_alias_query)

lib/bald/tests/integration/test_cdl_sparql.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,29 @@
1010
from bald.tests import BaldTestCase
1111
from rdflib import Graph
1212

13+
# a module level graph, to share for memory and performance
14+
thisGraph = [Graph()]
15+
loaded_boolean = []
16+
1317

1418
class Test(BaldTestCase):
1519
def setUp(self):
1620
self.cdl_path = os.path.join(os.path.dirname(__file__), 'CDL')
17-
self.ttl_path = os.path.join(os.path.dirname(__file__), 'TTL')
18-
self.graph = Graph()
1921

20-
#load bald graphs from cdl
21-
for cdl_file in glob.glob(os.path.join(os.path.dirname(__file__), 'CDL', '*.cdl')):
22-
with self.temp_filename('.nc') as tfile:
23-
subprocess.check_call(['ncgen', '-o', tfile, cdl_file])
24-
root_container = bald.load_netcdf(tfile, cache=self.acache)
25-
curr_g = root_container.rdfgraph()
26-
27-
#merge into graph in test obj
28-
self.graph = self.graph + curr_g
22+
# Check to see if another test has already loaded the graph.
23+
if not loaded_boolean:
24+
# load bald graphs from cdl
25+
for cdl_file in glob.glob(os.path.join(self.cdl_path, '*.cdl')):
26+
with self.temp_filename('.nc') as tfile:
27+
subprocess.check_call(['ncgen', '-o', tfile, cdl_file])
28+
root_container = bald.load_netcdf(tfile, cache=self.acache)
29+
curr_g = root_container.rdfgraph()
30+
31+
#merge into graph in test obj
32+
thisGraph[0] = thisGraph[0] + curr_g
33+
print('setting loaded_boolean')
34+
loaded_boolean.append(True)
35+
self.graph = thisGraph[0]
2936

3037
def test_sparql_count_standard_names(self):
3138
#query standard_name values used and frequency

0 commit comments

Comments
 (0)