View source with formatted comments or as raw
    1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2017, VU University Amsterdam
    7			 CWI 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(swish_hdt,
   37          [ rdf/4,                      % ?Subject, ?Pred, ?Object, ?Graph
   38
   39            rdf_subject/2,              % ?Subject, ?Graph
   40            rdf_predicate/2,            % ?Predicate, ?Graph
   41            rdf_object/2,               % ?Object, ?Graph
   42            rdf_shared/2,               % ?IRI, ?Graph
   43
   44            rdf_suggestions/5,		% +Base, +Role, +MaxCount, -List, +Graph
   45            rdf_graph_property/2,	% -Property, +Graph
   46
   47            rdf_meta/1,                 % +Declarations
   48            rdf_prefix/2,               % +Prefix, +URL
   49
   50            op(110, xfx, @),            % must be above .
   51            op(650, xfx, ^^),           % must be above :
   52            op(1150, fx, rdf_meta)
   53
   54          ]).
   55:- use_module(library(hdt)).   56:- use_module(library(error)).   57:- use_module(library(semweb/rdf11),
   58              except([ rdf/4,
   59                       rdf_graph_property/2
   60                     ])).   61
   62:- rdf_register_prefix(hdt, 'hdt://data/').   63:- rdf_meta
   64    rdf(r,r,o,r),
   65    rdf_subject(r,r),
   66    rdf_predicate(r,r),
   67    rdf_object(r,r),
   68    rdf_shared(r,r),
   69    rdf_suggestions(+,+,+,-,r),
   70    rdf_graph_property(-,r).   71
   72/** <module> Access RDF through HDTs
   73*/
   74
   75%!  rdf(?S,?P,?O,+G)
   76%
   77%   True when <S,P,O> is a  triple  in   G.  G  typically takes the form
   78%   `hdt:Name`, searching the HDT file Name.hdt
   79
   80rdf(S,P,O,G) :-
   81    graph_hdt(G, HDT),
   82    hdt_search(HDT, S, P, O).
   83
   84%!  rdf_subject(?S,+G)
   85%
   86%   True when S is a subject in the graph G.
   87
   88rdf_subject(S,G) :-
   89    graph_hdt(G, HDT),
   90    hdt_subject(HDT, S).
   91
   92%!  rdf_predicate(?P,+G)
   93%
   94%   True when P is a predicate in the graph G.
   95
   96rdf_predicate(P,G) :-
   97    graph_hdt(G, HDT),
   98    hdt_predicate(HDT, P).
   99
  100%!  rdf_object(?O,+G)
  101%
  102%   True when O is a object in the graph G.
  103
  104rdf_object(O,G) :-
  105    graph_hdt(G, HDT),
  106    hdt_object(HDT, O).
  107
  108%!  rdf_shared(?IRI,+G)
  109%
  110%   True when IRI is both a subject and object in the graph G.
  111
  112rdf_shared(S,G) :-
  113    graph_hdt(G, HDT),
  114    hdt_shared(HDT, S).
  115
  116%!  rdf_suggestions(+Base, +Role, +MaxCount, -List, +Graph)
  117%
  118%   True when Results is a list of   suggestions  for Base in the triple
  119%   role Role. Some experimentation suggests it  performs a prefix match
  120%   on the internal string representation.   This  implies that literals
  121%   are only found if the first character of Base is `"`.
  122%
  123%   @arg Base is a string or atom
  124%   @arg Role is one of `subject`, `predicate` or `object`
  125
  126rdf_suggestions(Base, Role, MaxCount, List, Graph) :-
  127    graph_hdt(Graph, HDT),
  128    hdt_suggestions(HDT, Base, Role, MaxCount, List).
  129
  130%!  rdf_graph_property(-Property, +Graph) is nondet.
  131%
  132%   True when Property is a property of Graph
  133
  134rdf_graph_property(Property, Graph) :-
  135    graph_hdt(Graph, HDT),
  136    hdt_property(HDT, Property).
  137
  138
  139:- dynamic
  140    hdt_graph/2.  141
  142graph_hdt(G, HDT) :-
  143    must_be(ground, G),
  144    hdt_graph(G, HDT0),
  145    !,
  146    HDT = HDT0.
  147graph_hdt(hdt:Local, HDT) :-
  148    !,
  149    rdf_global_id(hdt:Local, Global),
  150    graph_hdt(Global, HDT).
  151graph_hdt(G, HDT) :-
  152    atom_concat('hdt://data/', File, G),
  153    \+ sub_atom(File, _, _, _, '..'),
  154    absolute_file_name(hdt(File), Path,
  155                       [ extensions([hdt]),
  156                         access(read)
  157                       ]),
  158    hdt_open(HDT0, Path),
  159    assertz(hdt_graph(G, HDT0)),
  160    HDT = HDT0.
  161
  162:- multifile sandbox:safe_primitive/2.  163
  164sandbox:safe_primitive(swish_hdt:graph_hdt(_,_)).
  165sandbox:safe_primitive(hdt:hdt_search(_,_,_,_)).
  166sandbox:safe_primitive(system:hdt_column_(_,_,_)).
  167sandbox:safe_primitive(system:hdt_suggestions(_,_,_,_,_)).
  168sandbox:safe_primitive(system:hdt_property_(_,_))