:- module(ascii_simplify, [simplify/0, remove_updated/0 ]). :- use_module(library(semweb/rdf_db)). :- use_module(library(semweb/rdf_label)). :- use_module(library(semweb/rdf_turtle_write)). :- use_module(library(xmlrdf/rdf_rewrite)). user:file_search_path(ascii_simple_rdf, '../rdf/aers_ascii_simple'). quarter('04Q1'). quarter('04Q2'). quarter('04Q3'). quarter('04Q4'). quarter('05Q1'). quarter('05Q2'). quarter('05Q3'). quarter('05Q4'). quarter('06Q1'). quarter('06Q2'). quarter('06Q3'). quarter('06Q4'). quarter('07Q1'). quarter('07Q2'). quarter('07Q3'). quarter('07Q4'). quarter('08Q1'). quarter('08Q2'). quarter('08Q3'). quarter('08Q4'). quarter('09Q1'). quarter('09Q2'). quarter('09Q3'). quarter('09Q4'). :- debug(simplify). simplify :- ( quarter(Q), atom_concat(aers, Q, Graph), atom_concat(Graph, '.ttl', RDF_FileName), absolute_file_name(ascii_rdf(RDF_FileName), RDF_File), absolute_file_name(ascii_simple_rdf(RDF_FileName), RDF_Simple_File), debug(simplify, 'load ~w into ~w', [RDF_File, Graph]), rdf_load(RDF_File, [graph(Graph)]), debug(simplify, 'simplify ~w', [Graph]), simplify_graph(Graph), debug(simplify, 'save simplified graph to ~w', [RDF_Simple_File]), rdf_save_turtle(RDF_Simple_File, [graph(Graph)]), rdf_retractall(_,_,_,Graph), fail ; true ). simplify_graph(Graph) :- rdf_rewrite(Graph). report @@ {S, rdf:type, aers:'Report'}\ {S, aers:followup_status, _}?, {S, aers:foll_seq, _}?, {S, aers:mfr_dt, _}?, %{S, aers:report_type, _}?, {S, aers:mfr_num, _}?, {S, aers:mfr_sndr, _}?, {S, aers:e_sub, _}?, {S, aers:rept_dt, _}?, {S, aers:reporter_type, _}?, {S, aers:source, _}?, {S, aers:to_mfr, _}?, {S, aers:confid, _} <=> true. patient @@ {_, aers:patient, P}\ {P, aers:age_type, _}?, {P, aers:weight_type, _}?, {P, aers:death_dt, _}? <=> true. drug @@ {_, aers:drug, D}\ %{D, aers:name_type, _}?, %{D, aers:dose_vbm, _}?, {D, aers:dechal, _}?, {D, aers:rechal, _}?, {D, aers:lot_num, _}?, {D, aers:exp_dt, _}?, {D, aers:nda_num, _}? <=> true. therapy @@ {_, aers:therapy, T}, {T, _, _} <=> true. % TBD load all files then remove the updated and save the update graphs remove_updated :- ( rdf(_,aers:case,Case), remove_updated(Case), fail ; true ). %save_quarters. remove_updated(Case) :- findall(S,rdf(S,aers:case,Case),Reports), Reports = [_,_|_], sort_by_id(Reports, Sorted), reverse(Sorted, [_Latest|Updated]), length(Updated, Count), literal_text(Case, CaseNumber), debug(simplify, 'found ~w update(s) in case ~w', [Count,CaseNumber]), maplist(recursive_retract, Updated). sort_by_id(Reports, Sorted) :- maplist(add_id_key, Reports, Pairs0), keysort(Pairs0, Pairs), pairs_values(Pairs, Sorted). add_id_key(Report, Id-Report) :- rdf(Report, aers:isr, literal(Id)), !. recursive_retract(S) :- atom(S), !, forall(rdf(S, _, O), recursive_retract(O)), rdf_retractall(S, _, _). recursive_retract(_). save_quarters :- ( quarter(Q), atom_concat(aers, Q, Graph), atom_concat(Graph, '.ttl', RDF_FileName), absolute_file_name(ascii_simple_rdf(RDF_FileName), RDF_File), rdf_save_turtle(RDF_File, [graph(Graph)]), fail ; true ).