1/* Part of ClioPatria SeRQL and SPARQL server 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@cs.vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (C): 2010, VU University Amsterdam 7 8 This program is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public License 10 as published by the Free Software Foundation; either version 2 11 of the License, or (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 22 As a special exception, if you link this library with other files, 23 compiled with a Free Software compiler, to produce an executable, this 24 library does not by itself cause the resulting executable to be covered 25 by the GNU General Public License. This exception does not however 26 invalidate any other reasons why the executable file might be covered by 27 the GNU General Public License. 28*/ 29 30:- module(rdf_bnode, 31 [ bnode_vars/3 % +Graph0, -VarGraph, -BNodeVars 32 ]). 33:- use_module(library(semweb/rdf_db)). 34:- use_module(library(assoc)). 35 36/** <module> RDF graph operations on bnodes 37 38This module operates on RDF graphs represented as rdf(S,P,O) that 39contain blank nodes (bnodes). Bnodes can be considered existential 40variables in (sub-)graph, which motivates replacing them by Prolog 41variables. 42*/ 43 44%% bnode_vars(+RDF, -RDFWithVars, -Vars) is det. 45% 46% Consistently replace bnodes in RDF with Prolog variable and 47% unify Vars with a list of the variables found. Note that, if we 48% perform matches with such graphs, multiple variables may unify 49% to the same concrete resource. One might consider adding 50% constraints such as dif/2. 51% 52% @param RDF is a list rdf(S,P,O) 53% @param Resolved is a list rdf(S,P,O), where resources may be 54% a variable 55% @param NodeIDs is a list of variables representing the bnodes. 56 57bnode_vars(Graph0, Graph, NodeIDs) :- 58 empty_assoc(Map0), % BNodeID --> Var 59 bnode_vars(Graph0, Graph, Map0, Map), 60 assoc_to_values(Map, NodeIDs). 61 62bnode_vars([], [], Map, Map). 63bnode_vars([rdf(S0,P0,O0)|T0], Graph, Map0, Map) :- 64 ( rdf_is_bnode(S0) 65 ; rdf_is_bnode(P0) 66 ; rdf_is_bnode(O0) 67 ), !, 68 Graph = [rdf(S,P,O)|T], 69 bnode_var(S0, S, Map0, Map1), 70 bnode_var(P0, P, Map1, Map2), 71 bnode_var(O0, O, Map2, Map3), 72 bnode_vars(T0, T, Map3, Map). 73bnode_vars([Triple|T0], [Triple|T], Map0, Map) :- 74 bnode_vars(T0, T, Map0, Map). 75 76 77bnode_var(R0, BNodeID, Map0, Map) :- 78 rdf_is_bnode(R0), !, 79 ( get_assoc(R0, Map0, BNodeID) 80 -> Map = Map0 81 ; put_assoc(R0, Map0, BNodeID, Map) 82 ). 83bnode_var(R, R, Map, Map)