View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2009-2013, University of 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_compare,
   36          [ rdf_equal_graphs/3          % +Graph1, +Graph2, -Substitutions
   37          ]).   38:- use_module(library(semweb/rdf_db)).   39:- use_module(library(apply)).   40:- use_module(library(debug)).

Compare RDF graphs

This library provides predicates that compare RDF graphs. The current version only provides one predicate: rdf_equal_graphs/3 verifies that two graphs are identical after proper labeling of the blank nodes.

Future versions of this library may contain more advanced operations, such as diffing two graphs. */

 rdf_equal_graphs(+GraphA, +GraphB, -Substition) is semidet
True if GraphA and GraphB are the same under Substition. Substition is a list of BNodeA = BNodeB, where BNodeA is a blank node that appears in GraphA and BNodeB is a blank node that appears in GraphB.
Arguments:
GraphA- is a list of rdf(S,P,O) terms
GraphB- is a list of rdf(S,P,O) terms
Substition- is a list if NodeA = NodeB terms.
To be done
- The current implementation is rather naive. After dealing with the subgraphs that contain no bnodes, it performs a fully non-deterministic substitution.
   68rdf_equal_graphs(A, B, Substitutions) :-
   69    sort(A, SA),
   70    sort(B, SB),
   71    partition(contains_bnodes, SA, VA, GA),
   72    partition(contains_bnodes, SB, VB, GB),
   73    (   GA == GB
   74    ->  true
   75    ;   maplist(compare_triple, GA, GB)
   76    ),
   77    compare_list(VA, VB, [], Substitutions),
   78    !.
   79
   80contains_bnodes(rdf(S,P,O)) :-
   81    (   node_id(S)
   82    ;   node_id(P)
   83    ;   node_id(O)
   84    ),
   85    !.
   86
   87compare_list([], [], S, S).
   88compare_list([H1|T1], In2, S0, S) :-
   89    select(H2, In2, T2),
   90    compare_triple(H1, H2, S0, S1),
   91    compare_list(T1, T2, S1, S).
   92
   93compare_triple(T1, T2) :-
   94    compare_triple(T1,T2,[],[]).
   95
   96compare_triple(rdf(Subj1,P1,O1), rdf(Subj2, P2, O2), S0, S) :-
   97    compare_field(Subj1, Subj2, S0, S1),
   98    compare_field(P1, P2, S1, S2),
   99    compare_field(O1, O2, S2, S).
  100
  101compare_field(X, X, S, S) :- !.
  102compare_field(literal(X), xml(X), S, S) :- !. % TBD
  103compare_field(literal(lang(L1,X)), literal(lang(L2,X)), S, S) :-
  104    !,
  105    lang_equal(L1, L2).
  106compare_field(X, Id, S, S) :-
  107    memberchk(X=Id, S),
  108    !.
  109compare_field(X, Y, S, [X=Y|S]) :-
  110    \+ memberchk(X=_, S),
  111    node_id(X),
  112    node_id(Y),
  113    debug(rdf_compare, 'Assume ~w = ~w~n', [X, Y]).
  114
  115node_id(node(_)) :- !.
  116node_id(X) :-
  117    rdf_is_bnode(X)