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-2020, University of 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(rdf_compare,
   37          [ rdf_equal_graphs/3          % +Graph1, +Graph2, -Substitutions
   38          ]).   39:- use_module(library(semweb/rdf_db),[lang_equal/2,rdf_is_bnode/1]).   40:- autoload(library(apply),[partition/4,maplist/3]).   41:- autoload(library(debug),[debug/3]).   42:- autoload(library(lists),[select/3]).

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.
   69rdf_equal_graphs(A, B, Substitutions) :-
   70    sort(A, SA),
   71    sort(B, SB),
   72    partition(contains_bnodes, SA, VA, GA),
   73    partition(contains_bnodes, SB, VB, GB),
   74    (   GA == GB
   75    ->  true
   76    ;   maplist(compare_triple, GA, GB)
   77    ),
   78    compare_list(VA, VB, [], Substitutions),
   79    !.
   80
   81contains_bnodes(rdf(S,P,O)) :-
   82    (   node_id(S)
   83    ;   node_id(P)
   84    ;   node_id(O)
   85    ),
   86    !.
   87
   88compare_list([], [], S, S).
   89compare_list([H1|T1], In2, S0, S) :-
   90    select(H2, In2, T2),
   91    compare_triple(H1, H2, S0, S1),
   92    compare_list(T1, T2, S1, S).
   93
   94compare_triple(T1, T2) :-
   95    compare_triple(T1,T2,[],[]).
   96
   97compare_triple(rdf(Subj1,P1,O1), rdf(Subj2, P2, O2), S0, S) :-
   98    compare_field(Subj1, Subj2, S0, S1),
   99    compare_field(P1, P2, S1, S2),
  100    compare_field(O1, O2, S2, S).
  101
  102compare_field(X, X, S, S) :- !.
  103compare_field(literal(X), xml(X), S, S) :- !. % TBD
  104compare_field(literal(lang(L1,X)), literal(lang(L2,X)), S, S) :-
  105    !,
  106    lang_equal(L1, L2).
  107compare_field(X, Id, S, S) :-
  108    memberchk(X=Id, S),
  109    !.
  110compare_field(X, Y, S, [X=Y|S]) :-
  111    \+ memberchk(X=_, S),
  112    node_id(X),
  113    node_id(Y),
  114    debug(rdf_compare, 'Assume ~w = ~w~n', [X, Y]).
  115
  116node_id(node(_)) :- !.
  117node_id(X) :-
  118    rdf_is_bnode(X)