File: rdf_void.sql

package info (click to toggle)
virtuoso-opensource 7.2.5.1%2Bdfsg1-0.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 285,240 kB
  • sloc: ansic: 641,220; sql: 490,413; xml: 269,570; java: 83,893; javascript: 79,900; cpp: 36,927; sh: 31,653; cs: 25,702; php: 12,690; yacc: 10,227; lex: 7,601; makefile: 7,129; jsp: 4,523; awk: 1,697; perl: 1,013; ruby: 1,003; python: 326
file content (469 lines) | stat: -rw-r--r-- 16,058 bytes parent folder | download | duplicates (2)
1
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
--
--  $Id$
--
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--
--  RDF Schema objects, generator of RDF Views
--
--  Copyright (C) 1998-2018 OpenLink Software
--
--  This project is free software; you can redistribute it and/or modify it
--  under the terms of the GNU General Public License as published by the
--  Free Software Foundation; only version 2 of the License, dated June 1991.
--
--  This program is distributed in the hope that it will be useful, but
--  WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
--  General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along
--  with this program; if not, write to the Free Software Foundation, Inc.,
--  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--
--
-- drop table DB.DBA.RDF_VOID_GRAPH;
-- drop table DB.DBA.RDF_VOID_GRAPH_MEMBER;

create table DB.DBA.RDF_VOID_GRAPH (
  RVG_IID IRI_ID_8 not null primary key,
  RVG_IRI varchar not null,
  RVG_VOID_IRI varchar null,
  RVG_COMMENT varchar
  )
alter index RDF_VOID_GRAPH on DB.DBA.RDF_VOID_GRAPH partition (RVG_IID int (0hexffff00))
create index RDF_VOID_GRAPH_IRI on DB.DBA.RDF_VOID_GRAPH (RVG_IRI) partition (RVG_IRI varchar)
;

create table DB.DBA.RDF_VOID_GRAPH_MEMBER (
  RVGM_GROUP_IID IRI_ID_8 not null,
  RVGM_MEMBER_IID IRI_ID_8 not null,
  primary key (RVGM_GROUP_IID, RVGM_MEMBER_IID)
  )
alter index RDF_VOID_GRAPH_MEMBER on DB.DBA.RDF_VOID_GRAPH_MEMBER partition (RVGM_GROUP_IID int (0hexffff00))
;


create procedure RDF_VOID_INIT ()
{
  XML_REMOVE_NS_BY_PREFIX ('scovo', 2);
  XML_REMOVE_NS_BY_PREFIX ('void', 2);
  XML_SET_NS_DECL ('scovo', 'http://purl.org/NET/scovo#', 2);
  XML_SET_NS_DECL ('void', 'http://rdfs.org/ns/void#', 2);
}
;

RDF_VOID_INIT ()
;

create procedure RDF_VOID_SPLIT_IRI (in rel varchar, out pref varchar, out name varchar)
{
      declare delim1, delim2, delim3, pos int;
      delim1 := coalesce (strrchr (rel, '/'), -1);
      delim2 := coalesce (strrchr (rel, '#'), -1);
      delim3 := coalesce (strrchr (rel, ':'), -1);
      pos := __max (delim1, delim2, delim3);

      name := subseq (rel, pos + 1);
      pref := subseq (rel, 0, pos);
}
;

create procedure RDF_VOID_STORE (in graph varchar, in to_graph_name varchar := null, in src varchar := null)
{
  declare ses any;
  declare host varchar;

  if (src is null)
    ses := RDF_VOID_GEN (graph);
  else
    ses := src;
  if (to_graph_name is null)
    {
      host := virtuoso_ini_item_value ('URIQA','DefaultHost');
      to_graph_name := 'http://' || host || '/stats/void#';
    }
  exec (sprintf ('sparql delete from <%s> { ?s1 ?p1 ?s2 } from <%s> where { <%s#Dataset> void:statItem ?s1 . ?s1 ?p1 ?s2 }',
	to_graph_name, to_graph_name, graph));
  TTLP (ses, graph, to_graph_name, 185);
  return;
}
;

create procedure RDF_VOID_ALL_GEN (in target_graph varchar, in details int := 0) -- 1: e.g. http://log.openlinksw.com/void/ 2: to make distincts
{
  declare total, subset, ns_ctr int;
  declare ses, hf any;
  declare host varchar;

  ses := string_output (http_strses_memory_size ());

  host := null;

  if (is_http_ctx ())
    host := http_request_header(http_request_header (), 'Host', null, null);
  if (host is null)
    host := virtuoso_ini_item_value ('URIQA','DefaultHost');
  if (host is null)
    {
      hf := WS.WS.PARSE_URI (target_graph);
      host := hf[1];
    }

  -- add dataset for all
  ns_ctr := 1;
  total := 0;
  target_graph := rtrim (target_graph, '/#') || '/';
  RDF_VOID_NS (ses);
  http (sprintf ('\n'), ses);
  http (sprintf ('@prefix ns%d: <%s> .\n', ns_ctr, target_graph), ses); -- put NS prefix here
  http (sprintf ('ns%d:Dataset a void:Dataset ; \n', ns_ctr), ses);
  http (sprintf (' void:sparqlEndpoint <http://%s/sparql> . \n', host), ses);
  for select RVG_IID, RVG_IRI, RVG_COMMENT from RDF_VOID_GRAPH where RVG_IRI like target_graph || '%' do
    {
       -- add subset for group to all here
       declare gr_pref_ctr, grp_cnt int;
       ns_ctr := ns_ctr + 1;
       grp_cnt := 0;
       RVG_IRI := rtrim (RVG_IRI, '/#') || '/';
       http (sprintf ('@prefix ns%d: <%s> .\n', ns_ctr, RVG_IRI), ses); -- put NS prefix here
       http (sprintf ('ns%d:Dataset a void:Dataset . \n', ns_ctr), ses);
       http (sprintf ('ns1:Dataset void:subset ns%d:Dataset . \n', ns_ctr), ses);
       gr_pref_ctr := ns_ctr;
       for select RVGM_MEMBER_IID from RDF_VOID_GRAPH_MEMBER where RVGM_GROUP_IID = RVG_IID do
         {
	   -- add subset for graph to group here
	   ns_ctr := ns_ctr + 1;
	   RDF_VOID_GEN_1 (id_to_iri (RVGM_MEMBER_IID), null, sprintf ('ns%d', ns_ctr),
	       target_graph || rtrim (id_to_iri (RVGM_MEMBER_IID), '/#') || '/',
	       ses, grp_cnt, 1);
	   http (sprintf ('ns%d:Dataset void:subset ns%d:Dataset . \n', gr_pref_ctr, ns_ctr), ses);
         }
       http (sprintf ('ns%d:Dataset void:statItem ns%d:Stat . \n', gr_pref_ctr, gr_pref_ctr), ses);
       http (sprintf ('ns%d:Stat a scovo:Item ; \n rdf:value %d ; \n', gr_pref_ctr, grp_cnt), ses);
       http (sprintf (' scovo:dimension void:numOfTriples . \n'), ses);
       http (sprintf ('\n'), ses);
       total := total + grp_cnt;
    }
  http (sprintf ('ns1:Dataset void:statItem ns1:Stat . \n'), ses);
  http (sprintf ('ns1:Stat a scovo:Item ; \n rdf:value %ld ; \n', total), ses);
  http (sprintf (' scovo:dimension void:numOfTriples . \n'), ses);
  http (sprintf ('\n'), ses);
  return ses;
}
;

create procedure RDF_VOID_NS (inout ses any)
{
  http ('@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n', ses);
  http ('@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n', ses);
  http ('@prefix owl: <http://www.w3.org/2002/07/owl#> .\n', ses);
  --http ('@prefix dc: <http://purl.org/dc/elements/1.1/> .\n', ses);
  --http ('@prefix scovo: <http://purl.org/NET/scovo#> .\n', ses);
  http ('@prefix void: <http://rdfs.org/ns/void#> .\n', ses);
}
;

create procedure RDF_VOID_GEN (in graph varchar, in gr_name varchar := null)
{
  declare ses any;
  declare dummy int;
  dummy := 0;
  ses := string_output (http_strses_memory_size ());
  RDF_VOID_NS (ses);
  http (sprintf ('\n'), ses);
  RDF_VOID_GEN_1 (graph, gr_name, 'this', '', ses, dummy, 1);
  return ses;
}
;

create function RDF_VOID_CHECK_GRAPH (in graph varchar) returns float
{
  --pl_debug+
  declare _total_quad_count_estimate, _graph_quad_count_estimate int;
  _total_quad_count_estimate := (select count(*) from DB.DBA.RDF_QUAD table option (index RDF_QUAD_GS, index_only));
  _graph_quad_count_estimate := (select count(*) from DB.DBA.RDF_QUAD table option (index RDF_QUAD_GS, index_only) where G = __i2id(graph));
  return cast(_graph_quad_count_estimate as float) / cast(_total_quad_count_estimate as float) * 100;
}
;

create procedure RDF_VOID_GEN_IMPL_SMALL_GRAPH (in graph varchar,
                                                out cnt int, out cnt_subj int, out cnt_obj int,
                                                out n_classes int, out n_entities int, out n_properties int)
	{
  --pl_debug+
  declare exit handler for sqlstate '*' { goto end1; };
  cnt := 0;
  cnt_subj := 0;
  cnt_obj := 0;
  n_classes := 0;
  n_entities := 0;
  n_properties := 0;

  cnt := (select count(*)
	  from DB.DBA.RDF_QUAD table option (index RDF_QUAD_GS)
	  where G = __i2id(graph));

  cnt_subj := (select count(distinct S)
               from DB.DBA.RDF_QUAD table option (index RDF_QUAD_GS)
               where G = __i2id (graph));

  cnt_obj  := (select count(distinct O)
	       from RDF_QUAD table option (index RDF_QUAD_GS)
	       where G = __i2id (graph));

  -- number of classes
  n_classes := (select count (distinct O)
                from DB.DBA.RDF_QUAD table option (index RDF_QUAD_POGS)
                where G = __i2id (graph)
                  and P = __i2id ('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'));

  -- number of entities
  n_entities := (select count (distinct S)
                 from DB.DBA.RDF_QUAD table option (index RDF_QUAD)
                 where G = __i2id (graph)
                   and P = __i2id ('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'));

  -- number of properties
  n_properties := (select count (distinct P)
                   from DB.DBA.RDF_QUAD table option (index RDF_QUAD_GS)
                   where G = __i2id (graph));
end1:;
    }
;

create procedure RDF_VOID_GEN_IMPL_LARGE_GRAPH (in graph varchar,
                                                out cnt int, out cnt_subj int, out cnt_obj int,
                                                out n_classes int, out n_entities int, out n_properties int)
{
  --pl_debug+
  declare exit handler for sqlstate '*' { goto end1; };
  cnt := 0;
  cnt_subj := 0;
  cnt_obj := 0;
  n_classes := 0;
  n_entities := 0;
  n_properties := 0;

  cnt := (select count(*)
          from DB.DBA.RDF_QUAD table option (index RDF_QUAD)
          where G = __i2id (graph));

  cnt_subj := (select count(distinct S)
               from DB.DBA.RDF_QUAD table option (index RDF_QUAD_GS)
               where G = __i2id (graph));

  cnt_obj  := (select count (distinct O)
               from RDF_QUAD table option (index RDF_QUAD_POGS)
               where G = __i2id (graph));

  -- number of classes
  n_classes := (select count (distinct O)
                from DB.DBA.RDF_QUAD table option (index RDF_QUAD_POGS)
                where G = __i2id (graph)
                  and P = __i2id ('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'));

  -- number of entities
  n_entities := (select count (distinct S)
                 from DB.DBA.RDF_QUAD table option (index RDF_QUAD)
                 where G = __i2id (graph)
                   and P = __i2id ('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'));

  -- number of properties
  n_properties := (select count (distinct P)
                   from DB.DBA.RDF_QUAD table option (index RDF_QUAD)
                   where G = __i2id (graph));

end1:;
}
;

create procedure RDF_VOID_GEN_1 (in graph varchar, in gr_name varchar := null,
				in ns_pref varchar := 'this', in this_ns varchar := '',
        inout ses any, inout total int, in ep int := 1)
{
  --pl_debug+
  declare _cnt, _cnt_subj, _cnt_obj, _n_classes, _n_entities, _n_properties, has_links int;
  declare preds, dict any;
  declare pref, name, pred, host varchar;
  declare nam, inx any;

  host := null;
  if (is_http_ctx ())
    host := http_request_header(http_request_header (), 'Host', null, null);
  if (host is null)
    host := virtuoso_ini_item_value ('URIQA','DefaultHost');

  -- check if this is a small or large graph.
  if (RDF_VOID_CHECK_GRAPH (graph) < 1)
    RDF_VOID_GEN_IMPL_SMALL_GRAPH (graph, _cnt, _cnt_subj, _cnt_obj, _n_classes, _n_entities, _n_properties);
  else
    RDF_VOID_GEN_IMPL_LARGE_GRAPH (graph, _cnt, _cnt_subj, _cnt_obj, _n_classes, _n_entities, _n_properties);

  total := total + _cnt;

  http (sprintf ('@prefix %s: <%s> .\n', ns_pref, this_ns), ses); -- put NS prefix here

  http (sprintf ('\n'), ses);

  http (sprintf ('%s:Dataset a void:Dataset ; \n', ns_pref), ses);
  http (sprintf (' rdfs:seeAlso <%s> ; \n', graph), ses);
  if (gr_name is not null)
    http (sprintf (' rdfs:label "%s" ; \n', gr_name), ses);
  if (ep)
    http (sprintf (' void:sparqlEndpoint <http://%s/sparql> ; \n', host), ses);

  http (sprintf (' void:triples %ld ; \n', _cnt), ses);
  http (sprintf (' void:classes %ld ; \n', _n_classes), ses);
  http (sprintf (' void:entities %ld ; \n', _n_entities), ses);
  http (sprintf (' void:distinctSubjects %ld ; \n', _cnt_subj), ses);
  http (sprintf (' void:properties %ld ; \n', _n_properties), ses);
  http (sprintf (' void:distinctObjects %ld . \n', _cnt_obj), ses);

  http (sprintf ('\n'), ses);

  preds := vector ('owl:sameAs', 'rdfs:seeAlso');
  foreach (any rel in preds) do
  {
      RDF_VOID_SPLIT_IRI (rel, pref, name);
      pred := __xml_get_ns_uri (pref, 2) || name;

      _cnt := (sparql
               define input:storage ""
               select count(*)
               where { graph `iri (?:graph)`
                       { ?s `iri (?:pred)` ?o .
                         filter (?o != iri (?:graph))
  }
                      });
      if (_cnt > 0)
  {
        http (sprintf ('%s:%sLinks a void:Linkset ;\n', ns_pref, name), ses);
        http (sprintf (' void:inDataset %s:Dataset ; \n', ns_pref), ses);
        http (sprintf (' void:triples %ld ; \n', _cnt), ses);
        http (sprintf (' void:linkPredicate %s . \n', rel), ses);
        http (sprintf ('\n'), ses);
      }
    }

  return ses;
  }
;



create procedure void_ins (inout gs any, inout iris any, in fill int)
  {
  if (fill < 10000)
      {
    iris := subseq (iris, 0, fill);
    gs := subseq (gs, 0, fill);
      }
  set non_txn_insert = 1;
  for vectored (in g iri_id_8 := gs, in iri varchar := iris)
      {
		   insert into DB.DBA.RDF_VOID_GRAPH ( RVG_IID, RVG_IRI ) values (g, iri);
      }
  }
;

create procedure void_distinct_graphs ()
{
  declare g_iid, prev_g iri_id;
  declare iri varchar;
  declare gs, iris any;
  declare fill int;
  fill := 0;
 iris := make_array (10000, 'any');
 gs := make_array (10000, 'any');
  declare cr cursor for select G, __id2i (g) from DB.DBA.RDF_QUAD table option (index RDF_QUAD_GS, index_only);
  whenever not found goto nf;
  open cr;
  while (1)
      {
      fetch cr into g_iid, iri;
      if (g_iid <> prev_g)
	{
	  gs[fill] := g_iid;
	  iris[fill] := iri;
	fill := fill + 1;
	prev_g := g_iid;
	  if (fill >= 10000)
	    {
	      void_ins (gs, iris, fill);
	    fill := 0;
	    }
      }
  }
nf:
  close cr;
  void_ins (gs, iris, fill);
}
;

create procedure RDF_DCAT_GEN (in graph varchar,
                               in gr_name varchar := null,
                               in ns_pref varchar := 'this',
                               in this_ns varchar := '',
                               inout ses any)
{
  --pl_debug+
  declare _cnt, _cnt_subj, _cnt_obj, _n_classes, _n_entities, _n_properties, has_links int;
  declare preds, dict any;
  declare pref, name, pred, host varchar;
  declare nam, inx any;

  host := null;
  if (is_http_ctx ())
    host := http_request_header(http_request_header (), 'Host', null, null);
  if (host is null)
    host := virtuoso_ini_item_value ('URIQA','DefaultHost');

  http (sprintf ('@prefix %s: <%s> .\n', ns_pref, this_ns), ses); -- put NS prefix here

  http (sprintf ('\n'), ses);

  http (sprintf ('%s:Dataset a void:Dataset ; \n', ns_pref), ses);
  http (sprintf (' rdfs:seeAlso <%s> ; \n', graph), ses);
  if (gr_name is not null)
    http (sprintf (' rdfs:label "%s" ; \n', gr_name), ses);
-- XXX: no endpoint  
--  if (ep)
--    http (sprintf (' void:sparqlEndpoint <http://%s/sparql> ; \n', host), ses);

  http (sprintf (' void:triples %ld ; \n', _cnt), ses);
  http (sprintf (' void:classes %ld ; \n', _n_classes), ses);
  http (sprintf (' void:entities %ld ; \n', _n_entities), ses);
  http (sprintf (' void:distinctSubjects %ld ; \n', _cnt_subj), ses);
  http (sprintf (' void:properties %ld ; \n', _n_properties), ses);
  http (sprintf (' void:distinctObjects %ld . \n', _cnt_obj), ses);

  http (sprintf ('\n'), ses);

  preds := vector ('owl:sameAs', 'rdfs:seeAlso');
  foreach (any rel in preds) do
    {
      RDF_VOID_SPLIT_IRI (rel, pref, name);
      pred := __xml_get_ns_uri (pref, 2) || name;

      _cnt := (sparql
               define input:storage ""
               select count(*)
               where { graph `iri (?:graph)`
                       { ?s `iri (?:pred)` ?o .
                         filter (?o != iri (?:graph))
                        }
                      });
      if (_cnt > 0)
      {
	  http (sprintf ('%s:%sLinks a void:Linkset ; \n', ns_pref, name), ses);
        http (sprintf (' void:inDataset %s:Dataset ; \n', ns_pref), ses);
        http (sprintf (' void:triples %ld ; \n', _cnt), ses);
	  http (sprintf (' void:linkPredicate %s .\n', rel), ses);
	  http (sprintf ('\n'), ses);
    }
    }

  return ses;
}
;