% Generated by rdf-prolog-roundtrip.

% RDF terms use rdf(Subject, Predicate, Object, Graph).

rdf(iri('https://example.org/evidence/study/study-a'), iri('https://example.org/vocab/statement'), triple(iri('https://example.org/evidence/intervention/therapy-x'), iri('https://example.org/vocab/reduces'), iri('https://example.org/evidence/outcome/inflammation-marker')), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-a'), iri('https://example.org/vocab/direction'), iri('https://example.org/evidence/direction/supports'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-a'), iri('https://example.org/vocab/design'), iri('https://example.org/evidence/design/randomized-trial'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-a'), iri('https://example.org/vocab/institution'), iri('https://example.org/evidence/institution/lab-a'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-a'), iri('https://example.org/vocab/sampleSize'), literal('240', datatype('http://www.w3.org/2001/XMLSchema#integer')), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-a'), iri('https://example.org/vocab/peerReviewed'), iri('https://example.org/evidence/value/yes'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-b'), iri('https://example.org/vocab/statement'), triple(iri('https://example.org/evidence/intervention/therapy-x'), iri('https://example.org/vocab/reduces'), iri('https://example.org/evidence/outcome/inflammation-marker')), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-b'), iri('https://example.org/vocab/direction'), iri('https://example.org/evidence/direction/supports'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-b'), iri('https://example.org/vocab/design'), iri('https://example.org/evidence/design/randomized-trial'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-b'), iri('https://example.org/vocab/institution'), iri('https://example.org/evidence/institution/lab-b'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-b'), iri('https://example.org/vocab/sampleSize'), literal('310', datatype('http://www.w3.org/2001/XMLSchema#integer')), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-b'), iri('https://example.org/vocab/peerReviewed'), iri('https://example.org/evidence/value/yes'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-c'), iri('https://example.org/vocab/statement'), triple(iri('https://example.org/evidence/intervention/therapy-x'), iri('https://example.org/vocab/reduces'), iri('https://example.org/evidence/outcome/inflammation-marker')), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-c'), iri('https://example.org/vocab/direction'), iri('https://example.org/evidence/direction/contradicts'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-c'), iri('https://example.org/vocab/design'), iri('https://example.org/evidence/design/observational'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-c'), iri('https://example.org/vocab/institution'), iri('https://example.org/evidence/institution/lab-c'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-c'), iri('https://example.org/vocab/sampleSize'), literal('40', datatype('http://www.w3.org/2001/XMLSchema#integer')), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-c'), iri('https://example.org/vocab/peerReviewed'), iri('https://example.org/evidence/value/yes'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-d'), iri('https://example.org/vocab/statement'), triple(iri('https://example.org/evidence/intervention/therapy-x'), iri('https://example.org/vocab/improves'), iri('https://example.org/evidence/outcome/survival')), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-d'), iri('https://example.org/vocab/direction'), iri('https://example.org/evidence/direction/supports'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-d'), iri('https://example.org/vocab/design'), iri('https://example.org/evidence/design/randomized-trial'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-d'), iri('https://example.org/vocab/institution'), iri('https://example.org/evidence/institution/lab-a'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-d'), iri('https://example.org/vocab/sampleSize'), literal('180', datatype('http://www.w3.org/2001/XMLSchema#integer')), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-d'), iri('https://example.org/vocab/peerReviewed'), iri('https://example.org/evidence/value/yes'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-e'), iri('https://example.org/vocab/statement'), triple(iri('https://example.org/evidence/intervention/therapy-x'), iri('https://example.org/vocab/improves'), iri('https://example.org/evidence/outcome/survival')), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-e'), iri('https://example.org/vocab/direction'), iri('https://example.org/evidence/direction/contradicts'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-e'), iri('https://example.org/vocab/design'), iri('https://example.org/evidence/design/randomized-trial'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-e'), iri('https://example.org/vocab/institution'), iri('https://example.org/evidence/institution/lab-b'), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-e'), iri('https://example.org/vocab/sampleSize'), literal('210', datatype('http://www.w3.org/2001/XMLSchema#integer')), iri('https://example.org/evidence/graph/evidence')).
rdf(iri('https://example.org/evidence/study/study-e'), iri('https://example.org/vocab/peerReviewed'), iri('https://example.org/evidence/value/yes'), iri('https://example.org/evidence/graph/evidence')).

% Rules
% Flagship example: a scientific evidence graph with RDF 1.2 triple terms.
% Studies make statements about candidate claims without turning every claim into
% accepted truth. Prolog combines study design, sample size, independence, and
% conflicting evidence into explicit evidence states.

%% goal: evidence_state(X0, X1)
%% goal: supporting_study(X0, X1)
%% goal: counterevidence(X0, X1, X2)
%% goal: evidence_reason(X0, X1)

v(statement,iri('https://example.org/vocab/statement')).
v(direction,iri('https://example.org/vocab/direction')).
v(design,iri('https://example.org/vocab/design')).
v(institution,iri('https://example.org/vocab/institution')).
v(sample_size,iri('https://example.org/vocab/sampleSize')).
v(peer_reviewed,iri('https://example.org/vocab/peerReviewed')).
g(evidence,iri('https://example.org/evidence/graph/evidence')).

yes(iri('https://example.org/evidence/value/yes')).
supports(iri('https://example.org/evidence/direction/supports')).
contradicts(iri('https://example.org/evidence/direction/contradicts')).
randomized(iri('https://example.org/evidence/design/randomized-trial')).

claim(marker_reduction,iri('https://example.org/evidence/claim/marker-reduction'),triple(iri('https://example.org/evidence/intervention/therapy-x'),iri('https://example.org/vocab/reduces'),iri('https://example.org/evidence/outcome/inflammation-marker'))).
claim(survival_benefit,iri('https://example.org/evidence/claim/survival-benefit'),triple(iri('https://example.org/evidence/intervention/therapy-x'),iri('https://example.org/vocab/improves'),iri('https://example.org/evidence/outcome/survival'))).

study(study_a,iri('https://example.org/evidence/study/study-a')). study(study_b,iri('https://example.org/evidence/study/study-b')). study(study_c,iri('https://example.org/evidence/study/study-c')).
study(study_d,iri('https://example.org/evidence/study/study-d')). study(study_e,iri('https://example.org/evidence/study/study-e')).
integer_literal(literal(Text,datatype('http://www.w3.org/2001/XMLSchema#integer')),N):-atom_chars(Text,Cs),number_chars(N,Cs).

study_statement(Study,Claim,Direction,Institution,Design,N):-
 study(Study,S),claim(Claim,_ClaimResource,C),g(evidence,G),v(statement,PS),v(direction,PD),v(institution,PI),v(design,PDe),v(sample_size,PN),v(peer_reviewed,PP),
 rdf(S,PS,C,G),rdf(S,PD,Direction,G),rdf(S,PI,Institution,G),rdf(S,PDe,Design,G),rdf(S,PN,L,G),integer_literal(L,N),yes(Y),rdf(S,PP,Y,G).

high_quality(Study,Claim,Direction,Institution):-study_statement(Study,Claim,Direction,Institution,Design,N),randomized(R),Design=R,N >= 100.
independent_support_pair(Claim,S1,S2):-supports(S),high_quality(S1,Claim,S,I1),high_quality(S2,Claim,S,I2),S1 @< S2,I1 \= I2.
high_quality_counterevidence(Claim,Study):-contradicts(C),high_quality(Study,Claim,C,_).

supporting_study(Claim,Study):-supports(S),high_quality(Study,Claim,S,_).
counterevidence(Claim,Study,low_quality):-contradicts(C),study_statement(Study,Claim,C,_I,_D,_N),\+ high_quality(Study,Claim,C,_).
counterevidence(Claim,Study,high_quality):-high_quality_counterevidence(Claim,Study).

evidence_state(Claim,supported):-independent_support_pair(Claim,_S1,_S2),\+ high_quality_counterevidence(Claim,_Counter).
evidence_state(Claim,contested):-supporting_study(Claim,_),high_quality_counterevidence(Claim,_).

evidence_reason(marker_reduction,"Two independent randomized studies with sample size >= 100 support the claim; the contradictory observational study is retained as lower-quality counterevidence."):-evidence_state(marker_reduction,supported).
evidence_reason(survival_benefit,"High-quality randomized evidence exists on both sides, so the claim remains contested instead of being collapsed to a single truth value."):-evidence_state(survival_benefit,contested).

result_rdf(ClaimResource,iri('https://example.org/vocab/evidenceState'),State,iri('https://example.org/evidence/graph/derived')):-claim(Claim,ClaimResource,_Statement),evidence_state(Claim,S),state_iri(S,State).
state_iri(supported,iri('https://example.org/evidence/state/supported')). state_iri(contested,iri('https://example.org/evidence/state/contested')).

write_results:-result_rdf(S,P,O,G),write_term(rdf(S,P,O,G),[quoted(true)]),write('.'),nl,fail.
write_results.
:- set_prolog_flag(unknown, fail).
