allURIs = getAllOntologyIRIs(snomedGraph);
162 |
163 | /**
164 | * Step 2: Get the property data for the term IRIs in batches of PAGE_SIZE
165 | */
166 | traverseAllTerms(allURIs,properties);
167 |
168 | System.out.printf("%d terms retrieved in %.2f s\n",allURIs.size(),(System.currentTimeMillis() - t0)/1000.0);
169 | }
170 | }
171 |
--------------------------------------------------------------------------------
/perl/sparql.pm:
--------------------------------------------------------------------------------
1 | # Obtained from https://github.com/swh/Perl-SPARQL-client-library
2 | # No license applies. There is no warranty for this free software.
3 | # About
4 | #
5 | # This module is an implementation of the SPARQL protocol and result format
6 | # with minimal dependencies. Specifically it should be possible to run this
7 | # module using only packages provided by your distribution, making it suitable
8 | # for corporate environments.
9 | #
10 | # The dependencies are:
11 | # LWP::UserAgent
12 | # LWP::ConnCache
13 | # URI::Escape
14 | # XML::Simple
15 | #
16 | # This module will also use Keepalive HTTP Connections where possible.
17 | #
18 | # Usage
19 | #
20 | # use sparql;
21 | #
22 | # my $sparql = sparql->new();
23 | #
24 | # my $res = $sparql->query('http://dbpedia.org/sparql',
25 | # 'SELECT * WHERE { ?s ?p ?o } LIMIT 10');
26 | #
27 | # for my $row (@{$res}) {
28 | # for my $col (keys %{$row}) {
29 | # print('?'.$col.' = '.$row->{$col}."\n");
30 | # }
31 | # print("\n");
32 | # }
33 | #
34 | # Methods
35 | #
36 | # new()
37 | #
38 | # Returns a SPARQL client object.
39 | #
40 | # query(endpoint, query string)
41 | #
42 | # Takes and endpoint URI, and a query, and returns a reference to an array of
43 | # hashes, for SELECT, and a string for CONSTRUCT etc. Each element in the
44 | # array (a hash) is a solution, the keys of the hash are the variable name
45 | # (without the ? or $), and the value is a Turtle literal representing the result
46 | # value.
47 | #
48 | # update(endpoint, update string)
49 | #
50 | # Takes an endpoint URI, and a query, and returns a string of the returned message.
51 | #
52 | # put(endpoint, graph uri, mime type, RDF text)
53 | #
54 | # Takes an endpoint URI, and graph to write to, a MIME type and some RDF
55 | # data, and writes the data into the graph at the endpoint.
56 | #
57 | # Example: $sparql->put('http://host.example/data', 'http://mygraph.example/data.rdf',
58 | # 'text/turtle', " .\n");
59 | #
60 | # Returns whatever text the endpoint returns as a result.
61 | #
62 | # chomp(turtle literal)
63 | #
64 | # Removes any Turtle-style gubbins from around a returned value, e.g. '"foo"' -> 'foo',
65 | # '' -> 'http://example.com/'.
66 | #
67 | # print(result)
68 | #
69 | # Formats query a result object and prints it to STDOUT.
70 | package sparql;
71 |
72 | use HTTP::Request::Common;
73 | use LWP::UserAgent;
74 | use LWP::ConnCache;
75 | use URI::Escape;
76 | use XML::Simple;
77 | use strict;
78 |
79 | sub new {
80 | my $class = shift;
81 | my $ua = LWP::UserAgent->new(keep_alive => 1);
82 | $ua->timeout(600);
83 | $ua->conn_cache(LWP::ConnCache->new());
84 | $ua->conn_cache->total_capacity(10);
85 | $ua->agent("sparql.pm/1.1");
86 | my $self = {
87 | "_ua" => $ua
88 | };
89 | bless $self, $class;
90 |
91 | return $self
92 | }
93 |
94 | sub query {
95 | my ($self, $uri, $tquery) = @_;
96 |
97 | if (!$uri) {
98 | warn "sparql_query() called without endpoint";
99 | return ();
100 | }
101 |
102 | my $query = uri_escape($tquery);
103 | my $ruri;
104 | if ($uri =~ /\?/) {
105 | $ruri = $uri.'&query='.$query;
106 | } else {
107 | $ruri = $uri.'?query='.$query;
108 | }
109 |
110 | my $ret;
111 | my $response = $self->{'_ua'}->get($ruri, 'Accept' => 'text/tab-separated-values');
112 | if ($response->is_success) {
113 | $ret = $response->decoded_content;
114 | } elsif ($response->header("Client-Aborted")) {
115 | warn "Response timeout";
116 |
117 | return ();
118 | } else {
119 | warn $response->status_line.": ".$tquery;
120 |
121 | return ();
122 | }
123 |
124 | my $tsv = ($response->header('Content-Type') =~ m/^text\/tab-separated-values/i);
125 | my $sr = ($response->header('Content-Type') =~ m/^application\/sparql-results\+xml/i);
126 | my @retrows = ();
127 | if ($tsv) {
128 | my @rows = split("\n", $ret);
129 | my $header = shift @rows;
130 | if (!$header) {
131 | warn("no header from query");
132 | return ();
133 | }
134 | $header =~ s/^\s+//;
135 | my @cols = grep(s/^[\?\$]//, split("\t", $header));
136 | while (my $row = shift @rows) {
137 | if ($row =~ /^#/) {
138 | warn("SPARQL warning: $row\n");
139 | next;
140 | }
141 | next if ($row =~ /^\s*$/);
142 | my @data = split("\t", $row);
143 | my %row = ();
144 | for my $h (@cols) {
145 | my $cell = shift @data;
146 | $row{$h} = $cell;
147 | }
148 | push(@retrows, \%row);
149 | }
150 |
151 | return \@retrows;
152 | } elsif ($sr) {
153 | my $tree = XMLin($ret, ForceArray => 1);
154 | #print Dumper($tree)."\n";
155 | my @retrows;
156 |
157 | my $rows = $tree->{'results'}->[0]->{'result'};
158 | for my $row (@{ $rows }) {
159 | my %row;
160 | for my $binding (keys %{$row->{'binding'}}) {
161 | #print($binding."\n");
162 | my $val = $row->{'binding'}->{$binding};
163 | if ($val->{'uri'}) {
164 | $row{$binding} = '<'.$val->{'uri'}->[0].'>';
165 | } elsif ($val->{'literal'}) {
166 | $row{$binding} = '"'.$val->{'literal'}->[0].'"';
167 | } elsif ($val->{'bnode'}) {
168 | $row{$binding} = '_:'.$val->{'literal'}->[0];
169 | } else {
170 | $row{$binding} = '???';
171 | }
172 | }
173 | push(@retrows, \%row);
174 | }
175 |
176 | return \@retrows;
177 | }
178 |
179 | return $ret;
180 | }
181 |
182 | sub update {
183 | my ($self, $uri, $tupdate) = @_;
184 |
185 | if (!$uri) {
186 | warn "SPARQL::update() called without endpoint";
187 | return ();
188 | }
189 |
190 | my $ret;
191 | my %form = ( 'update' => $tupdate );
192 | my $response = $self->{'_ua'}->post($uri, \%form);
193 | if ($response->is_success) {
194 | $ret = $response->decoded_content;
195 | } elsif ($response->header("Client-Aborted")) {
196 | warn "Response timeout";
197 |
198 | return undef;
199 | } else {
200 | warn $response->status_line.": ".$tupdate."\n".$response->decoded_content;
201 |
202 | return undef;
203 | }
204 |
205 | return $ret;
206 | }
207 |
208 | sub put {
209 | my ($self, $uri, $doc, $mime, $text) = @_;
210 |
211 | if (!$uri) {
212 | warn "SPARQL::update() called without endpoint";
213 | return undef;
214 | }
215 |
216 | if ($uri =~ /\?/) {
217 | $uri .= '&graph='.uri_escape($doc);
218 | } else {
219 | $uri .= '?graph='.uri_escape($doc);
220 | }
221 | my $response = $self->{'_ua'}->request(PUT $uri, 'Content-Type' => $mime, 'Content-Length' => length($text), 'Content' => $text);
222 | if ($response->is_success) {
223 | return $response->decoded_content;
224 | } elsif ($response->header("Client-Aborted")) {
225 | warn "Response timeout";
226 |
227 | return undef;
228 | }
229 | warn $response->status_line."\n".$response->decoded_content;
230 |
231 | return undef;
232 | }
233 |
234 | sub print {
235 | my ($self, $res) = @_;
236 |
237 | for my $row (@{$res}) {
238 | my $first = 1;
239 | for my $col (keys %{$row}) {
240 | if ($first) {
241 | $first = 0;
242 | } else {
243 | print(", ");
244 | }
245 | print('?'.$col.' = '.$row->{$col});
246 | }
247 | print("\n");
248 | }
249 | }
250 |
251 | sub chomp {
252 | my($self, $s) = @_;
253 |
254 | if (!$s) {
255 | return "";
256 | }
257 |
258 | if ($s =~ /^<(.*)>$/) {
259 | return $1;
260 | } elsif ($s =~ /"(.*)"/) {
261 | return $1;
262 | } elsif ($s =~ /"(.*)"^^<.*>$/) {
263 | return $1;
264 | }
265 |
266 | return $s;
267 | }
268 |
269 | 1;
270 |
271 | # vi:set expandtab sts=4 sw=4:
272 |
--------------------------------------------------------------------------------
/python/all_props:
--------------------------------------------------------------------------------
1 | ?p
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |