/* Author: Jan Wielemaker E-mail: J.Wielemaker@cs.vu.nl WWW: http://www.swi-prolog.org Copyright (C): 2013, University of Amsterdam, VU University Amsterdam This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA As a special exception, if you link this library with other files, compiled with a Free Software compiler, to produce an executable, this library does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */ :- module(graph_consistency, [ consistent_graph/2, % +GraphName, +Props graph_value_space/3, % +Graph, -ValueSpaceGraph, +Props literal_value/3, % +Literal, -Canonical, +Props literal_value/2, % +Literal, -Canonical xsd_datatype/1, % ?URI xsd_subtype_of/2 % ?Sub, ?Super ]). :- use_module(library(apply)). :- use_module(library(xsdp_types)). :- use_module(library(memfile)). :- use_module(library(sgml)). :- use_module(library(semweb/rdf_db)). %% consistent_graph(+Name, +Props) % % True if the named graph Name does not contain any % inconsistencies. consistent_graph(Graph, Props) :- consistent_literals(Graph, Props), consistent_datatype_declarations(Graph, Props). consistent_literals(Graph, Props) :- forall(( rdf(_,_,L,Graph), L = literal(_) ), literal_value(L, _, Props)). consistent_datatype_declarations(Graph, _) :- rdf(S,P,O,Graph), invalid_datatype_assertion(S,P,O), !, fail. consistent_datatype_declarations(_, _). %% invalid_datatype_assertion(+S,+P,+O) is semidet. % % True if {S,P,O} makes an invalid assertion on XSD datatypes. We % consider an rdfs:subClassOf statement invalid of both subject % and object are known datatypes and the subClassOf relation is % not defined in XSD. :- rdf_meta invalid_datatype_assertion(r,r,r). invalid_datatype_assertion(S, rdfs:subClassOf, O) :- xsd_datatype(S), xsd_datatype(O), \+ xsd_subtype_of(S, O). term_expansion(xsd_datatype(_), [ xsd_datatype(LangString) | Clauses ]) :- rdf_equal(rdf:langString, LangString), findall(xsd_datatype(DataType), ( xsdp_type(Type), rdf_global_id(xsd:Type, DataType) ), Clauses). term_expansion(xsd_subtype_of(_,_), Clauses) :- findall(xsd_subtype_of(Sub, Super), ( xsdp_subtype_of(SubL, SuperL), rdf_global_id(xsd:SubL, Sub), rdf_global_id(xsd:SuperL, Super) ), Clauses). xsd_datatype(_). xsd_subtype_of(_,_). %% literal_value(+Literal, -Value) is semidet. %% literal_value(+Literal, -Value, +Props) is semidet. % % True when Value is the canonical representation of Literal in % the value space. Defined values are: % % * float(Float) % * double(Float) % * boolean(Boolean) % * decimal(Rational) % * lang(Lang, String) % * type(Type, String) literal_value(literal(Data), Value) :- literal_value2(Data, Value, mf{datatypes:dt{}, regime:rdfs}). % BUG: loops literal_value(literal(Data), Value, Props) :- ( get_dict(datatypes, Props, DT) -> true ; DT = dt{} ), literal_value2(Data, Value, DT). literal_value2(type(Type, String), Value, Props) :- xsdp_numeric_uri(Type, Base), \+ ( rdf_global_id(xsd:Local, Type), get_dict(Local, Props, false) ), !, numeric_value(Base, String, Value). literal_value2(type(Type, String), boolean(Value), Props) :- \+ get_dict(boolean, Props, false), rdf_equal(Type, xsd:boolean), !, bool(String, Value). literal_value2(type(XMLLiteral, String), xml(DOM), Props) :- rdf_equal(rdf:'XMLLiteral', XMLLiteral), \+ get_dict('XMLLiteral', Props, false), !, atom_xml_dom(String, DOM). literal_value2(type(Type, String), type(Type, String), _) :- !. literal_value2(lang(Lang,String), lang(CanonicalLang, String), _) :- !, downcase_atom(Lang, CanonicalLang). literal_value2(Plain, type(XsdString, Plain), _) :- rdf_equal(XsdString, xsd:string). :- rdf_meta numeric_value(r, +, -). numeric_value(xsd:float, String, float(Value)) :- number_string(V0, String), Value is float(V0). numeric_value(xsd:double, String, double(Value)) :- number_string(V0, String), Value is float(V0). numeric_value(xsd:integer, String, decimal(Integer)) :- decimal_value(String, Integer). numeric_value(xsd:decimal, String, decimal(Decimal)) :- decimal_value(String, Decimal). decimal_value(String, Value) :- split_string(String, ".", "", [IntS|Rest]), number_string(Int, IntS), integer(Int), ( Rest = [PartS] -> number_string(Part, PartS), integer(Part), string_length(PartS, Digits), Value is Int + Part rdiv (10**Digits) ; Rest = [], Value = Int ). bool(false, false). bool(true, true). bool('0', false). bool('1', true). %% atom_xml_dom(+String, -XMLDom) is semidet. % % Run XML parser on String. Fails if there are errors in the % string. atom_xml_dom(String, DOM) :- setup_call_cleanup( atom_to_memory_file(String, MF), setup_call_cleanup( open_memory_file(MF, read, In), catch(load_xml(In, DOM, [ max_errors(0) ]), _, (nodebug, fail)), % BUG: Should not start debug mode close(In)), free_memory_file(MF)). %% graph_value_space(+Graph, -ValueSpacedGraph, Props) is semidet. % % Convert a graph into a its canonical value space. Fails % if some triple has an inconsistent literal. graph_value_space(Graph, ValueSpaceGraph, Props) :- maplist(triple_value_space(Props), Graph, ValueSpaceGraph0), sort(ValueSpaceGraph0, ValueSpaceGraph). triple_value_space(Props, rdf(S,P,L0), rdf(S,P,L)) :- nonvar(L0), L0 = literal(_), !, literal_value(L0, L, Props). triple_value_space(_, Triple, Triple).