@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. 
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. 
@prefix owl: <http://www.w3.org/2002/07/owl#>. 
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>. 
@prefix math: <http://www.w3.org/2000/10/swap/math#>. 
@prefix rpo: <http://www.agfa.com/w3c/euler/rpo-rules#>.
@prefix list: <http://www.w3.org/2000/10/swap/list#>.
@prefix atype: <http://wopeg.he.agfa.be/rules/ArthetypeRIM#>.

@prefix rim: <http://wopeg.he.agfa.be/rules/RIMV3OWL#>.
@prefix acpo: <http://wopeg.he.agfa.be/rules/ACPOntologyRIM1#>.

@prefix : <http://wopeg.he.agfa.be/rules/ACPOntologyRIM1#>.


#########################################################
# rules to generate clinical pathway for a patient
#########################################################

#matching pre-conditions specified in "all of" 
#a step will only be recommended when all of its pre-condition are met
{?Y acpo:preCondition ?P.
 ?P acpo:allOf ?L.
 ?X acpo:inAllStateOf ?L.
}
 =>
{?X rim:participateInAct ?Y}.


#matching pre-conditions specified in "some of"
#a step will be recommended when one of its pre-condition is met
{?Y acpo:preCondition ?P.
 ?P acpo:someOf ?L.
 ?X acpo:inSomeStateOf ?L}
 =>
{?X rim:participateInAct ?Y}.

 
# if a step is replaced by a variance, the variance is also a step in pathway

{?X rim:participateInAct ?Z.
 ?V rim:replaces ?Z}
 =>
 {?X acpo:hasVariance (?V ?Z);
     rim:participateInAct ?V}.  
 
 #when an act is cancelled, aborted, held, or suspended, this act is recorded as a variant of a pathway

{?S a atype:Patient;
    rim:participateInAct ?X.
 ?X rim:actStatusCd ?Y.
 acpo:UnsuccessfulStatus owl:oneOf ?L.
 ?L list:member ?Y.}
 =>
 {?S acpo:hasVariance (rim:nullValue ?X)}.
    

##########################################################################
#rules to generate all steps for plan
##########################################################################

# if any one possible post condition of act X matches one of the pre-conditions of act Y
# act Y is a sequel of act X

{?X acpo:preCondition ?P.
 ?P acpo:allOrSomeItem ?V.
 ?Y acpo:postCondition ?Q.
 ?Q acpo:allOrSomeItem ?V}
 =>
 {?X rim:actRelationshipSequel ?Y}.

{?P rdfs:subPropertyOf rim:actRelationshipSequel.
 ?S ?P ?O}
 =>
 {?S rim:actRelationshipSequel ?O}.


#match every element in :hasExpectedOutcome list with postcondition list 
#regardless of someOf or allOf in the postcondition list

{?X acpo:hasExpectedState ?U.
 ?U acpo:allOrSomeItem ?I.
 ?Z acpo:postCondition ?V.
 ?V acpo:allOrSomeItem ?I}
 =>
 {?X acpo:hasPossiblePlan ?Z}.

#if patient participte in an act, he/she also participate in the act's antecendent and postcendent tasks
{?Y rim:actRelationshipSequel ?Z.
 ?X acpo:hasPossiblePlan ?Z}
 =>
 {?X acpo:hasPossiblePlan ?Y}.

{?Y rim:actRelationshipSequel ?Z.
 ?X acpo:hasPossiblePlan ?Y}
 =>
 {?X acpo:hasPossiblePlan ?Z}.

 
##################################################
#:nextStep and rim:actRelationshipSequel inverse property of each other
###################################################
{?Z rim:actRelationshipSequel ?V.
 ?X acpo:hasPossiblePlan ?Z}
 =>
 {?V acpo:connect ?Z}.

  
#rules to infer patient state



#operational state - patient in said to be in a "operational state of an act" if the act status is one of "new, 
#"completed", "active"

#{?S a atype:Patient;
#    rim:participateInAct ?X.
# ?X rim:actStatusCd ?Y.
# (rim:New rim:Active) list:member ?Y.}
# =>
# {?X a acpo:OperationalState. 
# ?S acpo:inPatientState ?X}.

#{?S a atype:Patient;
#    rim:participateInAct ?X.
# ?X rim:actStatusCd rim:Completed.}
# =>
# {?X a acpo:OperationalState. 
# ?S acpo:inPatientState ?X}.
 
#missed the distinguishing an new/active act vs. a completed act. 



#supporting properties

acpo:allOf  a  rdf:Property;
	rdfs:range rdf:List;
	rdfs:comment """ this property is used to state a list of pre or post conditions.
	The match of these conditions is true only when every item is found in the list""".
acpo:someOf a  rdf:Property;
	rdfs:range rdf:List;
	rdfs:comment """ this property is used to state a list of pre or post conditions. 
	The match of these conditions is true when one or more items are found in the list""". 

{?L rdf:first ?A; a rdf:List. ?X acpo:inPatientState ?A. ?L rdf:rest rdf:nil} => {?X acpo:inAllStateOf ?L}.
{?L rdf:first ?A; a rdf:List. ?L rdf:rest ?R. ?X acpo:inPatientState ?A. ?X acpo:inAllStateOf ?R} => {?X acpo:inAllStateOf ?L}.
{?L rdf:first ?A; a rdf:List. ?X acpo:inPatientState ?A} => {?X acpo:inSomeStateOf ?L}.
{?L rdf:rest ?R; a rdf:List. ?X acpo:inPatientState ?A. ?X acpo:inSomeStateOf ?R} => {?X acpo:inSomeStateOf ?L}.

{?L rdf:rest ?X; a rdf:List} => {?X a rdf:List}.
{?M acpo:allOf ?X} => {?X a rdf:List}.
{?M acpo:someOf ?X} => {?X a rdf:List}.

{?X acpo:someOf ?L. ?L list:member ?Y} => {?X acpo:allOrSomeItem ?Y}.
{?X acpo:allOf ?L. ?L list:member ?Y} => {?X acpo:allOrSomeItem ?Y}.

{?L rdf:first ?I; a rdf:List} => {?L list:member ?I}.
{?L rdf:rest ?R; a rdf:List. ?R list:member ?I} => {?L list:member ?I}.
