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, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(rdf_bnode,
   36          [ bnode_vars/3                % +Graph0, -VarGraph, -BNodeVars
   37          ]).   38:- use_module(library(semweb/rdf_db)).   39:- use_module(library(assoc)).   40
   41/** <module> RDF graph operations on bnodes
   42
   43This module operates on  RDF  graphs   represented  as  rdf(S,P,O)  that
   44contain blank nodes  (bnodes).  Bnodes   can  be  considered existential
   45variables in (sub-)graph,  which  motivates   replacing  them  by Prolog
   46variables.
   47*/
   48
   49%!  bnode_vars(+RDF, -RDFWithVars, -Vars) is det.
   50%
   51%   Consistently replace bnodes in  RDF   with  Prolog  variable and
   52%   unify Vars with a list of the  variables found. Note that, if we
   53%   perform matches with such graphs,   multiple variables may unify
   54%   to  the  same  concrete  resource.  One  might  consider  adding
   55%   constraints such as dif/2.
   56%
   57%   @param  RDF is a list rdf(S,P,O)
   58%   @param  Resolved is a list rdf(S,P,O), where resources may be
   59%           a variable
   60%   @param  NodeIDs is a list of variables representing the bnodes.
   61
   62bnode_vars(Graph0, Graph, NodeIDs) :-
   63    empty_assoc(Map0),              % BNodeID --> Var
   64    bnode_vars(Graph0, Graph, Map0, Map),
   65    assoc_to_values(Map, NodeIDs).
   66
   67bnode_vars([], [], Map, Map).
   68bnode_vars([rdf(S0,P0,O0)|T0], Graph, Map0, Map) :-
   69    (   rdf_is_bnode(S0)
   70    ;   rdf_is_bnode(P0)
   71    ;   rdf_is_bnode(O0)
   72    ),
   73    !,
   74    Graph = [rdf(S,P,O)|T],
   75    bnode_var(S0, S, Map0, Map1),
   76    bnode_var(P0, P, Map1, Map2),
   77    bnode_var(O0, O, Map2, Map3),
   78    bnode_vars(T0, T, Map3, Map).
   79bnode_vars([Triple|T0], [Triple|T], Map0, Map) :-
   80    bnode_vars(T0, T, Map0, Map).
   81
   82
   83bnode_var(R0, BNodeID, Map0, Map) :-
   84    rdf_is_bnode(R0),
   85    !,
   86    (   get_assoc(R0, Map0, BNodeID)
   87    ->  Map = Map0
   88    ;   put_assoc(R0, Map0, BNodeID, Map)
   89    ).
   90bnode_var(R, R, Map, Map)