Skip to content

Commit dd8dd1e

Browse files
committed
use blank nodes for references
1 parent d45cf26 commit dd8dd1e

File tree

8 files changed

+327
-540
lines changed

8 files changed

+327
-540
lines changed

lib/bald/__init__.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ def __init__(self, baseuri, relative_id, attrs=None, prefixes=None,
318318

319319
@property
320320
def identity(self):
321-
if self.relative_id:
321+
if self.relative_id is None:
322+
result = None
323+
elif self.relative_id:
322324
result = '/'.join([self.baseuri, self.relative_id])
323325
else:
324326
result = self.baseuri
@@ -531,7 +533,16 @@ def viewgraph(self):
531533
return html
532534

533535
def rdfnode(self, graph):
534-
selfnode = rdflib.URIRef(self.identity)
536+
"""
537+
Create an RDF Node,
538+
add it to the supplied graph,
539+
return the node.
540+
541+
"""
542+
if self.identity is None:
543+
selfnode = rdflib.BNode()
544+
else:
545+
selfnode = rdflib.URIRef(self.identity)
535546
for attr in self.attrs:
536547
list_items = []
537548
objs = self.attrs[attr]
@@ -542,9 +553,19 @@ def rdfnode(self, graph):
542553
if not (isinstance(objs, set) or isinstance(objs, list)):
543554
objs = set([objs])
544555
for obj in objs:
556+
if isinstance(obj, Subject):
557+
if obj.identity is None:
558+
node = obj.rdfnode(graph)
559+
else:
560+
obj_ref = rdflib.URIRef(obj.identity)
561+
if (obj_ref, None, None) not in graph:
562+
node = obj.rdfnode(graph)
563+
else:
564+
node = obj_ref
565+
545566
rdfpred = self.unpack_predicate(attr)
546567
if isinstance(obj, Subject):
547-
rdfobj = rdflib.URIRef(obj.identity)
568+
rdfobj = node #rdflib.URIRef(obj.identity)
548569
else:
549570
rdfobj = self.unpack_rdfobject(obj, rdfpred)
550571
if is_http_uri(rdfobj):
@@ -566,17 +587,12 @@ def rdfnode(self, graph):
566587
#graph.add((selfnode, rdfpred, rdfobj))
567588
elif isinstance(objs, list):
568589
list_items.append(rdfobj)
569-
if isinstance(obj, Subject):
570-
obj_ref = rdflib.URIRef(obj.identity)
571-
if (obj_ref, None, None) not in graph:
572-
graph = obj.rdfnode(graph)
573590
if list_items:
574591
list_name = rdflib.BNode()
575592
col = rdflib.collection.Collection(graph, list_name, list_items)
576-
577593
graph.add((selfnode, rdfpred, list_name))
578594

579-
return graph
595+
return selfnode
580596

581597
def rdfgraph(self):
582598
"""
@@ -603,10 +619,14 @@ def rdfgraph(self):
603619
uri = uri + '/'
604620
graph.bind(alias_name, uri)
605621

606-
graph = self.rdfnode(graph)
622+
self.rdfnode(graph)
607623

608624
return graph
609-
625+
626+
627+
class Reference(Subject):
628+
_rdftype = 'bald__Reference'
629+
610630

611631
class Array(Subject):
612632
_rdftype = 'bald__Array'
@@ -918,6 +938,7 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
918938
var = Subject(baseuri, name, sattrs, prefixes=prefixes,
919939
aliases=aliases, alias_graph=aliasgraph)
920940
root_container.attrs['bald__contains'].add(var)
941+
921942
file_variables[name] = var
922943

923944

@@ -1042,13 +1063,12 @@ def _make_ref_entities(var, fhandle, pref, name, baseuri,
10421063
try:
10431064
refset = var.attrs.get('bald__references', set())
10441065

1045-
identity = '{}_{}_ref'.format(name, pref)
1066+
identity = None
10461067
rattrs = {}
1047-
rattrs['rdf__type'] = 'bald__Reference'
10481068

10491069
reshapes = netcdf_shared_dimensions(fhandle.variables[name],
10501070
fhandle.variables[pref])
1051-
rattrs['bald__sourceShape'] = list(fhandle.variables[name].shape)
1071+
10521072
rattrs['bald__targetShape'] = list(fhandle.variables[pref].shape)
10531073
sourceReshape = [i[1] for i in reshapes['sourceReshape'].items()]
10541074
if sourceReshape != list(fhandle.variables[name].shape):
@@ -1057,12 +1077,11 @@ def _make_ref_entities(var, fhandle, pref, name, baseuri,
10571077
if targetReshape != list(fhandle.variables[pref].shape):
10581078
rattrs['bald__targetReshape'] = targetReshape
10591079
rattrs['bald__target'] = set((file_variables.get(pref),))
1060-
ref_node = Subject(baseuri, identity, rattrs,
1080+
ref_node = Reference(baseuri, identity, rattrs,
10611081
prefixes=prefixes,
10621082
aliases=aliases,
10631083
alias_graph=aliasgraph)
1064-
root_container.attrs['bald__contains'].add(ref_node)
1065-
file_variables[identity] = ref_node
1084+
10661085
refset.add(ref_node)
10671086
var.attrs['bald__references'] = refset
10681087
except ValueError:

lib/bald/tests/integration/CDL/ereefs_gbr4_ncld.cdl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ variables:
8282
salt:coordinates = "time zc latitude longitude" ;
8383
salt:units = "PSU" ;
8484
salt:long_name = "Salinity" ;
85-
salt:valid_range = 0.f, 40.f ;
8685
salt:missing_value = 1.e+35f ;
8786
salt:medium_id = "http://environment.data.gov.au/def/feature/ocean" ;
8887
salt:scaledQuantityKind_id = "http://environment.data.gov.au/def/property/practical_salinity" ;
@@ -93,7 +92,7 @@ variables:
9392
temp:coordinates = "time zc latitude longitude" ;
9493
temp:units = "degrees C" ;
9594
temp:long_name = "Temperature" ;
96-
temp:valid_range = 0.f, 40.f ;
95+
temp:valid_range = -3.f, 40.f ;
9796
temp:missing_value = 1.e+35f ;
9897
temp:medium_id = "http://environment.data.gov.au/def/feature/ocean" ;
9998
temp:scaledQuantityKind_id = "http://environment.data.gov.au/def/property/sea_water_temperature" ;

lib/bald/tests/integration/CDL/votemper.cdl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ dimensions:
44
dim1 = 180 ;
55
bnds = 2 ;
66
bnds_4 = 4 ;
7-
time = 3 ;
7+
time = 4 ;
88
deptht = 4 ;
99
percentiles = 19 ;
1010
variables:
@@ -59,7 +59,7 @@ variables:
5959
:Conventions = "CF-1.5" ;
6060

6161
data:
62-
time = 0, 24, 48 ;
62+
time = 0, 24, 48, 72 ;
6363
deptht = 0., 10., 100., 1000. ;
6464
percentiles = 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95 ;
6565

lib/bald/tests/integration/HTML/multi_array_reference.html

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -176,44 +176,30 @@
176176

177177
var file://CDL/multi_array_reference.cdl = instance('file://CDL/multi_array_reference.cdl:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
178178
var file://CDL/multi_array_reference.cdl/data_variable1 = instance('file://CDL/multi_array_reference.cdl/data_variable1:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17', 'long_name: Gerald', 'obtype: <a xlink:href="http://codes.wmo.int/common/observation-type/METCE/2013/SamplingObservation" xlink:show=new text-decoration="underline">metce__SamplingObservation</a>'], '#878800');
179-
var file://CDL/multi_array_reference.cdl/data_variable1_pdim0_ref = instance('file://CDL/multi_array_reference.cdl/data_variable1_pdim0_ref:<a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>, <a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/childBroadcast" xlink:show=new text-decoration="underline">bald__childBroadcast</a>: 1, 11'], '#878800');
180-
var file://CDL/multi_array_reference.cdl/data_variable1_pdim1_ref = instance('file://CDL/multi_array_reference.cdl/data_variable1_pdim1_ref:<a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>, <a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/childBroadcast" xlink:show=new text-decoration="underline">bald__childBroadcast</a>: 1, 17'], '#878800');
181179
var file://CDL/multi_array_reference.cdl/data_variable2 = instance('file://CDL/multi_array_reference.cdl/data_variable2:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17', 'long_name: Imelda', 'obtype: <a xlink:href="http://codes.wmo.int/common/observation-type/METCE/2013/SamplingObservation" xlink:show=new text-decoration="underline">metce__SamplingObservation</a>'], '#878800');
182-
var file://CDL/multi_array_reference.cdl/data_variable2_pdim0_ref = instance('file://CDL/multi_array_reference.cdl/data_variable2_pdim0_ref:<a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>, <a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/childBroadcast" xlink:show=new text-decoration="underline">bald__childBroadcast</a>: 1, 11'], '#878800');
183-
var file://CDL/multi_array_reference.cdl/data_variable2_pdim1_ref = instance('file://CDL/multi_array_reference.cdl/data_variable2_pdim1_ref:<a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>, <a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/childBroadcast" xlink:show=new text-decoration="underline">bald__childBroadcast</a>: 1, 17'], '#878800');
184180
var file://CDL/multi_array_reference.cdl/list_collection = instance('file://CDL/multi_array_reference.cdl/list_collection:<a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |'], '#878800');
185181
var file://CDL/multi_array_reference.cdl/location_reference_system = instance('file://CDL/multi_array_reference.cdl/location_reference_system:<a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['pcode: 4897'], '#878800');
186182
var file://CDL/multi_array_reference.cdl/location_variable = instance('file://CDL/multi_array_reference.cdl/location_variable:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
187-
var file://CDL/multi_array_reference.cdl/location_variable_pdim0_ref = instance('file://CDL/multi_array_reference.cdl/location_variable_pdim0_ref:<a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>, <a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/childBroadcast" xlink:show=new text-decoration="underline">bald__childBroadcast</a>: 1, 11'], '#878800');
188-
var file://CDL/multi_array_reference.cdl/location_variable_pdim1_ref = instance('file://CDL/multi_array_reference.cdl/location_variable_pdim1_ref:<a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>, <a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/childBroadcast" xlink:show=new text-decoration="underline">bald__childBroadcast</a>: 1, 17'], '#878800');
189-
var file://CDL/multi_array_reference.cdl/pdim0 = instance('file://CDL/multi_array_reference.cdl/pdim0:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11'], '#878800');
190-
var file://CDL/multi_array_reference.cdl/pdim1 = instance('file://CDL/multi_array_reference.cdl/pdim1:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 17'], '#878800');
183+
var file://CDL/multi_array_reference.cdl/pdim0 = instance('file://CDL/multi_array_reference.cdl/pdim0:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11'], '#878800');
184+
var file://CDL/multi_array_reference.cdl/pdim1 = instance('file://CDL/multi_array_reference.cdl/pdim1:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 17'], '#878800');
191185
var file://CDL/multi_array_reference.cdl/set_collection = instance('file://CDL/multi_array_reference.cdl/set_collection:<a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |'], '#878800');
192186
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/data_variable1, 'bald__contains', 'top', true);
193-
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/data_variable1_pdim0_ref, 'bald__contains', 'top', true);
194-
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/data_variable1_pdim1_ref, 'bald__contains', 'top', true);
195187
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/data_variable2, 'bald__contains', 'top', true);
196-
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/data_variable2_pdim0_ref, 'bald__contains', 'top', true);
197-
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/data_variable2_pdim1_ref, 'bald__contains', 'top', true);
198188
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/list_collection, 'bald__contains', 'top', true);
199189
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/location_reference_system, 'bald__contains', 'top', true);
200190
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/location_variable, 'bald__contains', 'top', true);
201-
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/location_variable_pdim0_ref, 'bald__contains', 'top', true);
202-
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/location_variable_pdim1_ref, 'bald__contains', 'top', true);
203191
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/pdim0, 'bald__contains', 'top', true);
204192
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/pdim1, 'bald__contains', 'top', true);
205193
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/set_collection, 'bald__contains', 'top', true);
206-
link(file://CDL/multi_array_reference.cdl/data_variable1, file://CDL/multi_array_reference.cdl/data_variable1_pdim0_ref, 'bald__references');
207-
link(file://CDL/multi_array_reference.cdl/data_variable1, file://CDL/multi_array_reference.cdl/data_variable1_pdim1_ref, 'bald__references');
194+
link(file://CDL/multi_array_reference.cdl/data_variable1, None, 'bald__references');
195+
link(file://CDL/multi_array_reference.cdl/data_variable1, None, 'bald__references');
208196
link(file://CDL/multi_array_reference.cdl/data_variable1, file://CDL/multi_array_reference.cdl/location_variable, 'bald__references');
209-
link(file://CDL/multi_array_reference.cdl/data_variable2, file://CDL/multi_array_reference.cdl/data_variable2_pdim0_ref, 'bald__references');
210-
link(file://CDL/multi_array_reference.cdl/data_variable2, file://CDL/multi_array_reference.cdl/data_variable2_pdim1_ref, 'bald__references');
197+
link(file://CDL/multi_array_reference.cdl/data_variable2, None, 'bald__references');
198+
link(file://CDL/multi_array_reference.cdl/data_variable2, None, 'bald__references');
211199
link(file://CDL/multi_array_reference.cdl/data_variable2, file://CDL/multi_array_reference.cdl/location_variable, 'bald__references');
200+
link(file://CDL/multi_array_reference.cdl/location_variable, None, 'bald__references');
201+
link(file://CDL/multi_array_reference.cdl/location_variable, None, 'bald__references');
212202
link(file://CDL/multi_array_reference.cdl/location_variable, file://CDL/multi_array_reference.cdl/location_reference_system, 'bald__references');
213-
link(file://CDL/multi_array_reference.cdl/location_variable, file://CDL/multi_array_reference.cdl/location_variable_pdim0_ref, 'bald__references');
214-
link(file://CDL/multi_array_reference.cdl/location_variable, file://CDL/multi_array_reference.cdl/location_variable_pdim1_ref, 'bald__references');
215-
link(file://CDL/multi_array_reference.cdl/pdim0, file://CDL/multi_array_reference.cdl/pdim0, 'bald__array', 'bottom');
216-
link(file://CDL/multi_array_reference.cdl/pdim1, file://CDL/multi_array_reference.cdl/pdim1, 'bald__array', 'bottom');
217203
joint.layout.DirectedGraph.layout(graph, { setLinkVertices: false,
218204
nodeSep: 150, rankSep: 100,
219205
marginX: 100, marginY: 100,

0 commit comments

Comments
 (0)