View source with formatted comments or as raw
    1/*  Part of ClioPatria SeRQL and SPARQL server
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2018, University of Amsterdam,
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(api_rdflib,
   37          [ library_ontology/1          % -Name
   38          ]).   39:- use_module(user(user_db)).   40:- use_module(library(http/http_dispatch)).   41:- use_module(library(http/http_parameters)).   42:- use_module(library(http/html_write)).   43:- use_module(library(semweb/rdf_library)).   44:- use_module(rdfql(rdf_io)).   45:- use_module(sesame).   46:- use_module(components(messages)).   47
   48/** <module> Provide access to the ontology library
   49
   50@see library(semweb/rdf_library)
   51*/
   52
   53:- http_handler(sesame('loadLibraryOntology'),   load_library_ontology,
   54                [time_limit(infinite)]).   55:- http_handler(sesame('listLibraryOntologies'), list_library_ontologies, []).   56
   57%!  load_library_ontology(+Request)
   58%
   59%   Load a named ontology from the ontology library.
   60%
   61%   @tbd    Cannot use concurrent loading as the load as a whole is
   62%           inside an rdf transaction.
   63
   64load_library_ontology(Request) :-
   65    http_parameters(Request,
   66                    [ repository(Repository),
   67                      ontology(Ontology, []),
   68                      resultFormat(Format)
   69                    ],
   70                    [ attribute_declarations(attribute_decl)
   71                    ]),
   72    authorized(write(Repository, load(library_ontology(Ontology)))),
   73    prepare_ontology_library,
   74    api_action(Request,
   75               rdf_load_library(Ontology, []),
   76               Format,
   77               \loaded_library_ontology(Ontology)).
   78
   79loaded_library_ontology(Id) -->
   80    html('Loaded RDF library '),
   81    (   { rdf_library_index(Id, title(Title)) }
   82    ->  html([Id, ' -- ', Title])
   83    ;   html(Id)
   84    ).
   85
   86
   87%!  list_library_ontologies(+Request)
   88%
   89%   Reply with a list of available base ontologies
   90
   91list_library_ontologies(Request) :-
   92    authorized(read(status, listBaseOntologies)),
   93    http_parameters(Request,
   94                    [ resultFormat(Format),
   95                      serialization(Serialization)
   96                    ],
   97                    [ attribute_declarations(attribute_decl)
   98                    ]),
   99    catch(findall(row(O), library_ontology(O), Rows0), _,
  100          Rows0 = []),
  101    sort(Rows0, Rows),
  102    write_table(Rows,
  103                [ result_format(Format),
  104                  serialization(Serialization),
  105                  variables(varnames(ontology))
  106                ]).
  107
  108
  109%!  library_ontology(-Name) is nondet.
  110%
  111%   True if Name is the name of an ontology from the library.
  112
  113library_ontology(O) :-
  114    prepare_ontology_library,
  115    rdf_library_index(O, title(_Title)).
  116
  117
  118%!  prepare_ontology_library is det.
  119%
  120%   Load RDF library manifests from   directories  defined using the
  121%   file_search_path/2 =ontology= alias.
  122
  123prepare_ontology_library :-
  124    (   absolute_file_name(rdf(.), Dir,
  125                           [ file_type(directory),
  126                             solutions(all)
  127                           ]),
  128        rdf_attach_library(Dir),
  129        fail
  130    ;   true
  131    ).
  132
  133%!  attribute_decl(+Name, -Options)
  134%
  135%   Copied from Sesame
  136
  137attribute_decl(repository,
  138               [ optional(true),
  139                 description('Name of the repository (ignored)')
  140               ]).
  141attribute_decl(resultFormat,
  142               [ default(xml),
  143                 type(oneof([ xml,
  144                              html,
  145                              rdf
  146                            ])),
  147                 description('Serialization format of the result')
  148               ]).
  149attribute_decl(ontology,
  150               [ description('Name of the ontology to load')
  151               ])