Skip to content

Commit d450dc0

Browse files
committed
rdf collection partial
1 parent b3a0df7 commit d450dc0

File tree

3 files changed

+76
-40
lines changed

3 files changed

+76
-40
lines changed

lib/bald/__init__.py

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import numpy as np
1111
import pyparsing
1212
import rdflib
13+
import rdflib.collection
1314
import requests
1415
import six
1516

@@ -524,10 +525,12 @@ def viewgraph(self):
524525
def rdfnode(self, graph):
525526
selfnode = rdflib.URIRef(self.identity)
526527
for attr in self.attrs:
528+
list_items = []
527529
objs = self.attrs[attr]
528530
if(isinstance(objs, np.ndarray)):
529531
#try to convert np.ndarray to a list
530532
objs = objs.tolist()
533+
531534
if not (isinstance(objs, set) or isinstance(objs, list)):
532535
objs = set([objs])
533536
for obj in objs:
@@ -546,16 +549,24 @@ def rdfnode(self, graph):
546549
else:
547550
rdfobj = rdflib.Literal(rdfobj)
548551
rdfpred = rdflib.URIRef(rdfpred)
549-
try:
550-
graph.add((selfnode, rdfpred, rdfobj))
551-
552-
except AssertionError:
553-
554-
graph.add((selfnode, rdfpred, rdfobj))
552+
if isinstance(objs, set):
553+
try:
554+
graph.add((selfnode, rdfpred, rdfobj))
555+
556+
except AssertionError:
557+
pass
558+
#graph.add((selfnode, rdfpred, rdfobj))
559+
elif isinstance(objs, list):
560+
list_items.append(rdfobj)
555561
if isinstance(obj, Subject):
556562
obj_ref = rdflib.URIRef(obj.identity)
557563
if (obj_ref, None, None) not in graph:
558564
graph = obj.rdfnode(graph)
565+
if list_items:
566+
list_name = rdflib.BNode()
567+
col = rdflib.collection.Collection(graph, list_name, list_items)
568+
569+
graph.add((selfnode, rdfpred, list_name))
559570

560571
return graph
561572

@@ -887,10 +898,18 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
887898
'filter(?rtype = owl:Class) '
888899
'}')
889900

901+
qstr = ('prefix bald: <http://binary-array-ld.net/latest/> '
902+
'prefix skos: <http://www.w3.org/2004/02/skos/core#> '
903+
'prefix owl: <http://www.w3.org/2002/07/owl#> '
904+
'select ?s '
905+
'where { '
906+
' ?s rdfs:range ?type . '
907+
'filter(?type in (rdfs:Literal, skos:Concept)) '
908+
'}')
909+
890910
refs = reference_graph.query(qstr)
891911

892-
ref_prefs = [str(ref[0]) for ref in list(refs)]
893-
912+
non_ref_prefs = [str(ref[0]) for ref in list(refs)]
894913

895914
# cycle again and find references
896915
for name in fhandle.variables:
@@ -906,30 +925,29 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
906925

907926
# for sattr in sattrs:
908927
for sattr in (sattr for sattr in sattrs if
909-
root_container.unpack_predicate(sattr) in ref_prefs):
910-
# if sattr == 'coordinates':
911-
# import pdb; pdb.set_trace()
928+
root_container.unpack_predicate(sattr) not in non_ref_prefs):
912929

913930
if (isinstance(sattrs[sattr], six.string_types) and
914931
file_variables.get(sattrs[sattr])):
915932
# next: remove all use of set, everything is dict or orderedDict
916933
var.attrs[sattr] = set((file_variables.get(sattrs[sattr]),))
917934
elif isinstance(sattrs[sattr], six.string_types):
918-
potrefs_list = sattrs[sattr].split(',')
919-
potrefs_set = sattrs[sattr].split(' ')
920-
if len(potrefs_list) > 1:
921-
refs = np.array([file_variables.get(pref) is not None
922-
for pref in potrefs_list])
923-
if np.all(refs):
924-
var.attrs[sattr] = [file_variables.get(pref)
925-
for pref in potrefs_list]
926-
927-
elif len(potrefs_set) > 1:
928-
refs = np.array([file_variables.get(pref) is not None
929-
for pref in potrefs_set])
930-
if np.all(refs):
931-
var.attrs[sattr] = set([file_variables.get(pref)
932-
for pref in potrefs_set])
935+
if sattrs[sattr].startswith('(') and sattrs[sattr].endswith(')'):
936+
potrefs_list = sattrs[sattr].lstrip('( ').rstrip(' )').split(' ')
937+
if len(potrefs_list) > 1:
938+
refs = np.array([file_variables.get(pref) is not None
939+
for pref in potrefs_list])
940+
if np.all(refs):
941+
var.attrs[sattr] = [file_variables.get(pref)
942+
for pref in potrefs_list]
943+
else:
944+
potrefs_set = sattrs[sattr].split(' ')
945+
if len(potrefs_set) > 1:
946+
refs = np.array([file_variables.get(pref) is not None
947+
for pref in potrefs_set])
948+
if np.all(refs):
949+
var.attrs[sattr] = set([file_variables.get(pref)
950+
for pref in potrefs_set])
933951

934952
# coordinate variables are bald__references except for
935953
# variables that already declare themselves as bald__Reference

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,11 @@ variables:
126126
:metadata_link = "http://marlin.csiro.au/geonetwork/srv/eng/search?&uuid=72020224-f086-434a-bbe9-a222c8e5cf0d" ;
127127
:rdf__type = "bald__Container" ;
128128
:bald__isPrefixedBy = "prefix_list" ;
129-
:bald__isAliasedBy = "alias_list" ;
130129

131130
group: prefix_list {
132131
// group attributes:
133132
:bald__ = "http://binary-array-ld.net/latest/" ;
134133
:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ;
135134
} // group bald__prefix_list
136-
137-
group: alias_list {
138-
// group attributes:
139-
:qudt = "http://qudt.org/1.1/schema/qudt";
140-
:ed_gov_au_op = "http://environment.data.gov.au/def/op" ;
141-
142-
} // group bald__alias_list
143135

144136
}

lib/bald/tests/integration/test_cdl_rdfgraph.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,40 @@ def test_gems_co2(self):
9090
expected_ttl = sf.read()
9191
self.assertEqual(expected_ttl, ttl)
9292

93+
def test_ProcessChain0300(self):
94+
with self.temp_filename('.nc') as tfile:
95+
name = 'ProcessChain0300'
96+
cdl_file = os.path.join(self.cdl_path, '{}.cdl'.format(name))
97+
subprocess.check_call(['ncgen', '-o', tfile, cdl_file])
98+
cdl_file_uri = 'file://CDL/{}.cdl'.format(name)
99+
alias_dict = {'CFTerms': 'http://def.scitools.org.uk/CFTerms',
100+
'cf_sname': 'http://vocab.nerc.ac.uk/standard_name/'
101+
}
102+
alias_dict = {}
103+
root_container = bald.load_netcdf(tfile, baseuri=cdl_file_uri,
104+
alias_dict=alias_dict, cache=self.acache)
105+
ttl = root_container.rdfgraph().serialize(format='n3').decode("utf-8")
106+
# with open(os.path.join(self.ttl_path, '{}.ttl'.format(name)), 'w') as sf:
107+
# sf.write(ttl)
108+
with open(os.path.join(self.ttl_path, '{}.ttl'.format(name)), 'r') as sf:
109+
expected_ttl = sf.read()
110+
self.assertEqual(expected_ttl, ttl)
111+
93112
def test_ereefs(self):
94113
with self.temp_filename('.nc') as tfile:
95-
cdl_file = os.path.join(self.cdl_path, 'ereefs_gbr4_ncld.cdl')
114+
name = 'ereefs_gbr4_ncld'
115+
cdl_file = os.path.join(self.cdl_path, '{}.cdl'.format(name))
96116
subprocess.check_call(['ncgen', '-o', tfile, cdl_file])
97117
root_container = bald.load_netcdf(tfile, cache=self.acache)
98-
try:
99-
g = root_container.rdfgraph()
100-
ttl = g.serialize(format='n3').decode("utf-8")
101-
except TypeError:
102-
self.fail("Test case could not convert ereefs CDL to RDF")
118+
# try:
119+
# g = root_container.rdfgraph()
120+
# ttl = g.serialize(format='n3').decode("utf-8")
121+
# except TypeError:
122+
# self.fail("Test case could not convert ereefs CDL to RDF")
103123

124+
ttl = root_container.rdfgraph().serialize(format='n3').decode("utf-8")
125+
# with open(os.path.join(self.ttl_path, '{}.ttl'.format(name)), 'w') as sf:
126+
# sf.write(ttl)
127+
with open(os.path.join(self.ttl_path, '{}.ttl'.format(name)), 'r') as sf:
128+
expected_ttl = sf.read()
129+
self.assertEqual(expected_ttl, ttl)

0 commit comments

Comments
 (0)