PREFIX dt: <http://hl7.org/owl/datatypes/uv#>
PREFIX hl7: <http://hl7.org/owl/metadata#>
PREFIX rim: <http://hl7.org/owl/rim#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX err: <tag:eric@w3.org/2013/err>
PREFIX my: <http://my.example/#>
PREFIX fhir: <http://hl7/org/fhir/>

%GenR{ %}

start=<DOC>
<DOC> {
  a (rim:Act),
  rim:Act.classCode {
    hl7:coding {
      dt:CDCoding.code ("DOCCLIN")? ,
      dt:CDCoding.codeSystem ("2.16.840.1.113883.5.6")
    }
  } ,
  rim:Act.id {
    dt:COLL.item {
      dt:II.extension LITERAL
        %GenR{ Docname = iri(concat("urn:uuid:", o)) %}
    }
  } ,
  rim:Act.effectiveTime xsd:date , # %GenR{ DATE = o %}
  ^rim:Participation.act @<AR-target>
}

<AR-target> {
  rim:Participation.typeCode {
    hl7:coding {
      dt:CDCoding.code ("RCT")
    }
  } ,
  rim:Participation.act @<DOC> ,
  rim:Participation.role  @<Role-target>
}

<Role-target> {
  rim:Role.classCode { hl7:coding { dt:CDCoding.code ("PAT") } } ,
  rim:Role.player @<Player-target>
}

<Player-target> {
  a (rim:Person) 
          %GenR{ Patient = s . Patient <a> fhir:Patient %},
  rim:Entity.name {
    a (dt:COLL_EN)
          %GenR{ Name = bnode("name") . Patient fhir:name Name %},
    dt:COLL.item {
      err:person-name-family LITERAL
          %GenR{ Name fhir:family o %},
      err:person-name-given LITERAL
          %GenR{ Name fhir:given o %}
    }
  },
  rim:LivingSubject.administrativeGenderCode {
    hl7:coding {
      dt:CDCoding.codeSystem ("2.16.840.1.113883.5.1")
          %GenR{ GenderCoding fhir:system o %},
      dt:CDCoding.code ("M" "F")
          %GenR{ GenderCoding fhir:code o %}
    }     %GenR{ GenderCoding = bnode("genderCoding") .
                 Gender fhir:coding GenderCoding %}
  }       %GenR{ Gender = bnode("gender") .
                 Patient fhir:gender Gender %}
}
