/* Part of ClioPatria SeRQL and SPARQL server 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(rdf_mt, [ load_manifest/1, run_tests/0, run_test/1, % +Test list_test/0, list_data/0, list_entailed_data/0, list_result/0 ]). :- use_module(library(debug)). :- use_module(library(lists)). :- use_module(library(semweb/rdf_db)). :- use_module(library(semweb/rdf_turtle_write)). :- use_module(library(http/http_open)). :- use_module(library(http/http_ssl_plugin)). :- use_module(library(rdf_mt/earl)). :- use_module(library(rdf_mt/manifest)). :- use_module(library(rdf_mt/rdf_reasoner)). :- use_module(library(rdf_mt/mt_simple)). :- use_module(library(rdf_mt/mt_rdf)). :- use_module(library(rdf_mt/mt_rdfs)). :- use_module(library(rdf_mt/graph_list)). :- use_module(library(rdf_mt/graph_properties)). :- use_module(library(rdf_mt/graph_consistency)). :- dynamic test_result/3. % Test, Time, Result default_manifest( local, 'rdf-ff6d041b8705/rdf-mt/tests/manifest.ttl'). default_manifest( remote, 'https://dvcs.w3.org/hg/rdf/raw-file/default/rdf-mt/tests/manifest.ttl'). :- initialization debug(mt(_)). %% load_manifest(+Type) % % Load default test manifest % % @arg Type is one of =local= or =remote= load_manifest(Type) :- clean_manifests, default_manifest(Type, MF), load_manifest(MF, []). %% run_tests % % Run all tests from the loaded manifests run_tests :- rdf_reset_db, retractall(test_result(_,_,_)), forall(mf_test(Test), run_test(Test)), test_report. %% test_report % % Generate an EARL report test_report :- findall(result{test:Test, date:Date, outcome:Result}, test_result(Test, Date, Result), Results0), reverse(Results0, Results), % get in manifest order partition(passed, Results, Passed, Failed), length(Passed, CountPassed), length(Failed, CountFailed), debug(mt(result), 'Test result: passed: ~D, failed: ~D', [CountPassed, CountFailed]), earl_report(Results, [ project('earl_header.ttl'), save('earl-results.ttl') ]). passed(Dict) :- Dict.outcome == passed. %% run_test(+Test) % % Run test Test. Test is either the URI of a test or the name. run_test(Test) :- rdf_retractall(_,_,_,_), announce_test(Test), !, load_test_data(Test), test_entailment_properties(Test, Props), run_entailment(Props), assess_result(Test, Props). run_test(Name) :- mf_rdf(Test, mf:name, literal(Name)), !, run_test(Test). %% announce_test(+Test) % % Report we are about to start the test announce_test(Test) :- mf_rdf(Test, mf:name, Name), text_of(Name, Text), format('Test ~q ...~n', [Text]). text_of(literal(lang(_,Text)), Text) :- !. text_of(literal(Text), Text) :- atomic(Text). %% load_test_data(+Test) % % Load test data into the RDF store if applicable. Test is % loaded into the graph =data=. load_test_data(Test) :- forall(mf_rdf(Test, mf:action, Data), load_data(Data, data)). %% load_test_result(+Test) % % Load the expected result of Test into the graph =result=. load_test_result(Test) :- forall(mf_rdf(Test, mf:result, Data), load_data(Data, result)). load_data(URL, Graph) :- uri_is_global(URL), !, setup_call_cleanup( http_open(URL, In, [ cert_verify_hook(ssl_verify) ]), rdf_load(In, [ silent(true), graph(Graph) ]), close(In)). load_data(URL, Graph) :- rdf_load(URL, [ silent(true), graph(Graph) ]). :- public ssl_verify/5. %% ssl_verify(+SSL, +ProblemCert, +AllCerts, +FirstCert, +Error) % % Accept all certificates. ssl_verify(_SSL, _ProblemCertificate, _AllCertificates, _FirstCertificate, _Error). false_result_test(Test) :- mf_rdf(Test, mf:result, literal(type(_,false))). %% test_entailment_properties(+Test, -Props) % % Props is a dict containing the entailment properties test_entailment_properties(Test, Props) :- findall(Name-Value, test_entailment_property(Test, Name, Value), Pairs), dict_pairs(Props, mf, Pairs). test_entailment_property(Test, regime, Regime) :- mf_rdf(Test, mf:entailmentRegime, literal(Regime0)), downcase_atom(Regime0, Regime). test_entailment_property(Test, datatypes, Dict) :- ( mf_rdf(Test, mf:recognizedDatatypes, Recognised) -> findall(DT-true, dt_member(DT, Recognised), Pairs, Negative) ; Pairs = Negative ), ( mf_rdf(Test, mf:unrecognizedDatatypes, UnRecognised) -> findall(DT-false, dt_member(DT, UnRecognised), Negative) ; Negative = [] ), dict_pairs(Dict, dt, Pairs). dt_member(DT, Collection) :- mf_member(DTURI, Collection), ( rdf_global_id(_:DT, DTURI) -> true ; DT = DTURI ). %% run_entailment(+Props) % % Run the entailment process run_entailment(Props) :- debug(mt(entailment), 'Entailment using ~q', [Props]), compute_closure(Props.regime, Props.put(graph, data)). %% assess_result(+Test, +Properties) :- % % Assess the result :- meta_predicate assert_result(1, +). assess_result(Test, Props) :- mf_rdf(Test, rdf:type, mf:'PositiveEntailmentTest'), !, debug(mt(result), 'Evaluating positive entailment test', []), assert_result(assess_result(positive, Props), Test). assess_result(Test, Props) :- mf_rdf(Test, rdf:type, mf:'NegativeEntailmentTest'), !, debug(mt(result), 'Evaluating negative entailment test', []), assert_result(assess_result(negative, Props), Test). assess_result(_Test, _) :- assertion(false). assert_result(Goal, Test) :- ( catch(call(Goal, Test), E, (print_message(error, E), fail)) -> get_time(Now), asserta(test_result(Test, Now, passed)) ; get_time(Now), asserta(test_result(Test, Now, failed)), once(mf_rdf(Test, mf:name, Name)), text_of(Name, Text), debug(mt(failed), 'FAILED: ~q', [Text]) ). %% assess_result(+Props, +PosNeg, +Test) % % True if the test has passed. assess_result(positive, Props, Test) :- false_result_test(Test), !, \+ consistent_graph(data, Props). assess_result(positive, Props, Test) :- !, load_test_result(Test), graph_value_list(data, Data, Props), graph_value_list(result, Result, Props), simply_entails(Data, Result). assess_result(negative, Props, Test) :- false_result_test(Test), !, consistent_graph(data, Props). assess_result(negative, Props, Test) :- load_test_result(Test), \+ ( graph_value_list(data, Data, Props), graph_value_list(result, Result, Props), simply_entails(Data, Result) ). %% graph_value_list(+GraphName, -Triples, +Props) is semidet. % % True when Triples is a list of rdf(S,P,O) terms, where all % literals are in canonical value space representation. Fails if % some literal is inconsistent. graph_value_list(Graph, ValueListGraph, Props) :- graph_list(Graph, List), graph_value_space(List, ValueListGraph, Props). /******************************* * DEBUGGING HELP * *******************************/ %% list_test % % Display info on last test list_test :- test_result(Test, _Date, _OutCome), !, list_test(Test). list_test(Test) :- list_comment(Test), list_data(Test), format('~N~n*** Expected result ***~n'), list_result(Test). list_comment(Test) :- forall(mf_rdf(Test, rdfs:comment, literal(Comment)), writeln(Comment)). %% list_data % % List data from the graph =data= list_data :- test_result(Test, _Date, _OutCome), !, list_data(Test). list_data(Test) :- mf_rdf(Test, mf:action, Data), atom(Data), uri_file_name(Data, File), !, setup_call_cleanup( open(File, read, In), copy_stream_data(In, current_output), close(In)). %% list_entailed_data % % List data from the graph =data= after entailment. list_entailed_data :- test_result(Test, _Date, _OutCome), !, list_entailed_data(Test). list_entailed_data(Test) :- ( mf_rdf(Test, mf:action, Data) -> format('Data from: ~q~n', [Data]) ; true ), rdf_save_turtle(stream(current_output), [ graph(data)/*, only_known_prefixes(true)*/ ]). %% list_result % % List data from the graph =result= list_result :- test_result(Test, _Date, _OutCome), !, list_result(Test). list_result(Test) :- ( mf_rdf(Test, mf:result, literal(type(_, false))) -> format('Expected outcome: false~n') ; mf_rdf(Test, mf:result, Data), format('Result from: ~q~n', [Data]), rdf_save_turtle(stream(current_output), [ graph(result)/*, only_known_prefixes(true)*/ ]) ).