PREFIX fhir: <http://hl7.org/fhir/> 
PREFIX fhirvs: <http://hl7.org/fhir/ValueSet/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 

# ShEx Version 2.2
IMPORT <Code.shex>
IMPORT <Group.shex>
IMPORT <Device.shex>
IMPORT <Period.shex>
IMPORT <Patient.shex>
IMPORT <CareTeam.shex>
IMPORT <Location.shex>
IMPORT <Reference.shex>
IMPORT <Identifier.shex>
IMPORT <Organization.shex>
IMPORT <Practitioner.shex>
IMPORT <RelatedPerson.shex>
IMPORT <DomainResource.shex>
IMPORT <CodeableConcept.shex>
IMPORT <PractitionerRole.shex>
IMPORT <CodeableReference.shex>
IMPORT <BiologicallyDerivedProduct.shex>

start=@<DeviceAssociation> AND {fhir:nodeRole [fhir:treeRoot]}

# A record of association of a device with a subject
<DeviceAssociation> EXTENDS @<DomainResource> CLOSED {   
    a [fhir:DeviceAssociation]?;fhir:nodeRole [fhir:treeRoot]?;

    fhir:identifier @<OneOrMore_Identifier>?;  # Instance identifier
    fhir:device @<Reference> AND {fhir:l 
    			@<Device> ? };  # Reference to the device that is 
                                            # being associated 
    fhir:relationship @<OneOrMore_CodeableConcept>?;  # Describes the relationship between 
                                            # the device and subject 
    fhir:status @<Code> AND
    	{fhir:v @fhirvs:deviceassociation-status};  # active | inactive | 
                                            # entered-in-error | unknown 
    fhir:statusReason @<OneOrMore_CodeableConcept>?;  # Administrative or 
                                            # record-management reason for the 
                                            # record status 
    fhir:associationStatus @<CodeableConcept>?;  # State of the device association 
                                            # relationship 
    fhir:subject @<Reference> AND {fhir:l 
    			@<BiologicallyDerivedProduct> OR 
    			@<CareTeam> OR 
    			@<Device> OR 
    			@<Group> OR 
    			@<Location> OR 
    			@<Organization> OR 
    			@<Patient> OR 
    			@<Practitioner> OR 
    			@<PractitionerRole> OR 
    			@<RelatedPerson> ? }?;  # The direct target of the device 
                                            # association 
    fhir:focus @<OneOrMore_Reference_CareTeam_OR_Device_OR_Group_OR_Organization_OR_Patient_OR_Practitioner_OR_PractitionerRole_OR_RelatedPerson>?;  # Context for interpreting the 
                                            # device association 
    fhir:bodyStructure @<OneOrMore_CodeableReference>?;  # Current anatomical location(s) of 
                                            # the device in/on subject 
    fhir:period @<Period>?;                 # Begin and end dates and times for 
                                            # the device association 
}  

#---------------------- Cardinality Types (OneOrMore) -------------------
<OneOrMore_Identifier> CLOSED {
    rdf:first @<Identifier>  ;
    rdf:rest [rdf:nil] OR @<OneOrMore_Identifier> 
}
<OneOrMore_CodeableConcept> CLOSED {
    rdf:first @<CodeableConcept>  ;
    rdf:rest [rdf:nil] OR @<OneOrMore_CodeableConcept> 
}
<OneOrMore_Reference_CareTeam_OR_Device_OR_Group_OR_Organization_OR_Patient_OR_Practitioner_OR_PractitionerRole_OR_RelatedPerson> CLOSED {
    rdf:first @<Reference> AND {fhir:l 
			@<CareTeam> OR 
			@<Device> OR 
			@<Group> OR 
			@<Organization> OR 
			@<Patient> OR 
			@<Practitioner> OR 
			@<PractitionerRole> OR 
			@<RelatedPerson> } ;
    rdf:rest [rdf:nil] OR @<OneOrMore_Reference_CareTeam_OR_Device_OR_Group_OR_Organization_OR_Patient_OR_Practitioner_OR_PractitionerRole_OR_RelatedPerson> 
}
<OneOrMore_CodeableReference> CLOSED {
    rdf:first @<CodeableReference>  ;
    rdf:rest [rdf:nil] OR @<OneOrMore_CodeableReference> 
}

#---------------------- Value Sets ------------------------

# DeviceAssociation Status Codes
fhirvs:deviceassociation-status ["active" "inactive" "entered-in-error" "unknown"]

