# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/SubscriptionTopic5to4B'
/// name = 'SubscriptionTopic5to4B'
/// title = 'FML Conversion for SubscriptionTopic: R5 to R4B'
/// status = 'draft'

conceptmap "CriteriaNotExistsBehavior" {
  prefix s = "http://hl7.org/fhir/subscriptiontopic-cr-behavior"
  prefix t = "http://hl7.org/fhir/4.3/subscriptiontopic-cr-behavior"

  s:"test-passes" - t:"test-passes"
  s:"test-fails" - t:"test-fails"
}

conceptmap "PublicationStatus" {
  prefix s = "http://hl7.org/fhir/publication-status"
  prefix t = "http://hl7.org/fhir/4.3/publication-status"

  s:draft - t:draft
  s:active - t:active
  s:retired - t:retired
  s:unknown - t:unknown
}

conceptmap "SearchComparator" {
  prefix s = "http://hl7.org/fhir/search-comparator"
  prefix t = "http://hl7.org/fhir/4.3/search-comparator"

  s:eq - t:eq
  s:ne - t:ne
  s:gt - t:gt
  s:lt - t:lt
  s:ge - t:ge
  s:le - t:le
  s:sa - t:sa
  s:eb - t:eb
  s:ap - t:ap
}

conceptmap "SearchModifierCode" {
  prefix s = "http://hl7.org/fhir/search-modifier-code"
  prefix t = "http://hl7.org/fhir/4.3/search-modifier-code"

  s:missing - t:missing
  s:exact - t:exact
  s:contains - t:contains
  s:not - t:not
  s:text - t:text
  s:in - t:in
  s:"not-in" - t:"not-in"
  s:below - t:below
  s:above - t:above
  s:type - t:type
  s:identifier - t:identifier
}

uses "http://hl7.org/fhir/StructureDefinition/SubscriptionTopic" alias SubscriptionTopicR5 as source
uses "http://hl7.org/fhir/4.3/StructureDefinition/SubscriptionTopic" alias SubscriptionTopicR4B as target

imports "http://hl7.org/fhir/StructureMap/*5to4B"

group SubscriptionTopic(source src : SubscriptionTopicR5, target tgt : SubscriptionTopicR4B) extends DomainResource <<type+>> {
  src.url -> tgt.url;
  src.identifier -> tgt.identifier;
  src.version -> tgt.version;
  src.title -> tgt.title; // src.versionAlgorithm : string -> tgt.versionAlgorithm;
  src.derivedFrom -> tgt.derivedFrom;
  src.status as v -> tgt.status = translate(v, '#PublicationStatus', 'code');
  src.experimental -> tgt.experimental;
  src.date -> tgt.date;
  src.publisher -> tgt.publisher;
  src.contact -> tgt.contact;
  src.description -> tgt.description;
  src.useContext -> tgt.useContext;
  src.jurisdiction -> tgt.jurisdiction;
  src.purpose -> tgt.purpose;
  src.copyright -> tgt.copyright;
  src.approvalDate -> tgt.approvalDate; // src.copyrightLabel -> tgt.copyrightLabel;
  src.lastReviewDate -> tgt.lastReviewDate;
  src.effectivePeriod -> tgt.effectivePeriod;
  src.resourceTrigger as s -> tgt.resourceTrigger as t then SubscriptionTopicResourceTrigger(s, t);
  src.eventTrigger as s -> tgt.eventTrigger as t then SubscriptionTopicEventTrigger(s, t);
  src.notificationShape as s -> tgt.notificationShape as t then SubscriptionTopicNotificationShape(s, t); // src.canFilterBy as s -> tgt.canFilterBy as t then SubscriptionTopicCanFilterBy(s,t);
}

group SubscriptionTopicResourceTrigger(source src, target tgt) extends BackboneElement {
  src.description -> tgt.description;
  src.resource -> tgt.resource;
  src.supportedInteraction -> tgt.supportedInteraction;
  src.queryCriteria as s -> tgt.queryCriteria as t then SubscriptionTopicResourceTriggerQueryCriteria(s, t);
  src.fhirPathCriteria -> tgt.fhirPathCriteria;
}

group SubscriptionTopicResourceTriggerQueryCriteria(source src, target tgt) extends BackboneElement {
  src.previous -> tgt.previous;
  src.resultForCreate as v -> tgt.resultForCreate = translate(v, '#CriteriaNotExistsBehavior', 'code');
  src.current -> tgt.current;
  src.resultForDelete as v -> tgt.resultForDelete = translate(v, '#CriteriaNotExistsBehavior', 'code');
  src.requireBoth -> tgt.requireBoth;
}

group SubscriptionTopicEventTrigger(source src, target tgt) extends BackboneElement {
  src.description -> tgt.description;
  src.event -> tgt.event;
  src.resource -> tgt.resource;
}

group SubscriptionTopicNotificationShape(source src, target tgt) extends BackboneElement {
  src.resource -> tgt.resource;
  src.include -> tgt.include;
  src.revInclude -> tgt.revInclude;
}

# entry[].resource.title
FML Conversion for SubscriptionTopic: R5 to R4B

# entry[].resource.group[].rule[].documentation
src.canFilterBy as s -> tgt.canFilterBy as t then SubscriptionTopicCanFilterBy(s,t);

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/BiologicallyDerivedProduct4to5'
/// name = 'BiologicallyDerivedProduct4to5'
/// title = 'FML Conversion for BiologicallyDerivedProduct: R4 to R5'
/// status = 'active'

uses "http://hl7.org/fhir/4.0/StructureDefinition/BiologicallyDerivedProduct" alias BiologicallyDerivedProductR4 as source
uses "http://hl7.org/fhir/StructureDefinition/BiologicallyDerivedProduct" alias BiologicallyDerivedProductR5 as target

imports "http://hl7.org/fhir/StructureMap/*4to5"

group BiologicallyDerivedProduct(source src : BiologicallyDerivedProductR4, target tgt : BiologicallyDerivedProductR5) extends DomainResource <<type+>> {
  src.productCategory as s where productCategory = 'organ' ->  tgt.productCategory = create('Coding') as tc,  tc.system = 'http://hl7.org/fhir/product-category',  tc.code = 'organ',  tc.display = 'Organ';
  src.productCategory as s where productCategory = 'tissue' ->  tgt.productCategory = create('Coding') as tc,  tc.system = 'http://hl7.org/fhir/product-category',  tc.code = 'tissue',  tc.display = 'Tissue';
  src.productCategory as s where productCategory = 'fluid' ->  tgt.productCategory = create('Coding') as tc,  tc.system = 'http://hl7.org/fhir/product-category',  tc.code = 'fluid',  tc.display = 'Fluid';
  src.productCategory as s where productCategory = 'cells' ->  tgt.productCategory = create('Coding') as tc,  tc.system = 'http://hl7.org/fhir/product-category',  tc.code = 'cells',  tc.display = 'Cells';
  src.productCategory as s where productCategory = 'biologicalAgent' ->  tgt.productCategory = create('Coding') as tc,  tc.system = 'http://hl7.org/fhir/product-category',  tc.code = 'biologicalAgent',  tc.display = 'BiologicalAgent';
  src.productCode -> tgt.productCode;
  src.parent -> tgt.parent;
  src.request -> tgt.request;
  src.identifier -> tgt.identifier;
  src.collection as s -> tgt.collection as t then BiologicallyDerivedProductCollection(s, t); // src.biologicalSourceEvent -> tgt.biologicalSourceEvent;
}

group BiologicallyDerivedProductCollection(source src, target tgt) extends BackboneElement {
  src.collector -> tgt.collector;
  src.source -> tgt.source;
  src.collected : dateTime -> tgt.collected;
  src.collected : Period -> tgt.collected;
}

# entry[].resource.title
FML Conversion for BiologicallyDerivedProduct: R4 to R5

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/MessageDefinition4Bto5'
/// name = 'MessageDefinition4Bto5'
/// title = 'FML Conversion for MessageDefinition: R4B to R5'
/// status = 'active'

conceptmap "MessageSignificanceCategory" {
  prefix s = "http://hl7.org/fhir/4.3/message-significance-category"
  prefix t = "http://hl7.org/fhir/message-significance-category"

  s:consequence - t:consequence
  s:currency - t:currency
  s:notification - t:notification
}

conceptmap "PublicationStatus" {
  prefix s = "http://hl7.org/fhir/4.3/publication-status"
  prefix t = "http://hl7.org/fhir/publication-status"

  s:draft - t:draft
  s:active - t:active
  s:retired - t:retired
  s:unknown - t:unknown
}

conceptmap "messageheader-response-request" {
  prefix s = "http://hl7.org/fhir/4.3/messageheader-response-request"
  prefix t = "http://hl7.org/fhir/messageheader-response-request"

  s:always - t:always
  s:"on-error" - t:"on-error"
  s:never - t:never
  s:"on-success" - t:"on-success"
}

uses "http://hl7.org/fhir/4.3/StructureDefinition/MessageDefinition" alias MessageDefinitionR4B as source
uses "http://hl7.org/fhir/StructureDefinition/MessageDefinition" alias MessageDefinitionR5 as target

imports "http://hl7.org/fhir/StructureMap/*4Bto5"

group MessageDefinition(source src : MessageDefinitionR4B, target tgt : MessageDefinitionR5) extends DomainResource <<type+>> {
  src.url -> tgt.url;
  src.identifier -> tgt.identifier;
  src.version -> tgt.version;
  src.name -> tgt.name; // src.versionAlgorithm : string -> tgt.versionAlgorithm;
  src.title -> tgt.title;
  src.replaces -> tgt.replaces;
  src.status as v -> tgt.status = translate(v, '#PublicationStatus', 'code');
  src.experimental -> tgt.experimental;
  src.date -> tgt.date;
  src.publisher -> tgt.publisher;
  src.contact -> tgt.contact;
  src.description -> tgt.description;
  src.useContext -> tgt.useContext;
  src.jurisdiction -> tgt.jurisdiction;
  src.purpose -> tgt.purpose;
  src.copyright -> tgt.copyright;
  src.base -> tgt.base; // src.copyrightLabel -> tgt.copyrightLabel;
  src.parent -> tgt.parent;
  src.event : Coding -> tgt.event;
  src.event : uri -> tgt.event;
  src.category as v -> tgt.category = translate(v, '#MessageSignificanceCategory', 'code');
  src.focus as s -> tgt.focus as t then MessageDefinitionFocus(s, t);
  src.responseRequired as v -> tgt.responseRequired = translate(v, '#messageheader-response-request', 'code');
  src.allowedResponse as s -> tgt.allowedResponse as t then MessageDefinitionAllowedResponse(s, t);
  src.graph -> tgt.graph;
}

group MessageDefinitionFocus(source src, target tgt) extends BackboneElement {
  src.code -> tgt.code;
  src.profile -> tgt.profile;
  src.min -> tgt.min;
  src.max -> tgt.max;
}

group MessageDefinitionAllowedResponse(source src, target tgt) extends BackboneElement {
  src.message -> tgt.message;
  src.situation -> tgt.situation;
}

# entry[].resource.title
FML Conversion for MessageDefinition: R4B to R5

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/QuestionnaireResponse4to5'
/// name = 'QuestionnaireResponse4to5'
/// title = 'FML Conversion for QuestionnaireResponse: R4 to R5'
/// status = 'active'

conceptmap "QuestionnaireResponseStatus" {
  prefix s = "http://hl7.org/fhir/4.0/questionnaire-answers-status"
  prefix t = "http://hl7.org/fhir/questionnaire-answers-status"

  s:"in-progress" - t:"in-progress"
  s:completed - t:completed
  s:amended - t:amended
  s:"entered-in-error" - t:"entered-in-error"
  s:stopped - t:stopped
}

uses "http://hl7.org/fhir/4.0/StructureDefinition/QuestionnaireResponse" alias QuestionnaireResponseR4 as source
uses "http://hl7.org/fhir/StructureDefinition/QuestionnaireResponse" alias QuestionnaireResponseR5 as target

imports "http://hl7.org/fhir/StructureMap/*4to5"

group QuestionnaireResponse(source src : QuestionnaireResponseR4, target tgt : QuestionnaireResponseR5) extends DomainResource <<type+>> {
  src.identifier -> tgt.identifier;
  src.basedOn -> tgt.basedOn;
  src.partOf -> tgt.partOf;
  src.questionnaire -> tgt.questionnaire;
  src.status as v -> tgt.status = translate(v, '#QuestionnaireResponseStatus', 'code');
  src.subject -> tgt.subject;
  src.encounter -> tgt.encounter;
  src.authored -> tgt.authored;
  src.author -> tgt.author;
  src.source -> tgt.source;
  src.item as s -> tgt.item as t then QuestionnaireResponseItem(s, t);
}

group QuestionnaireResponseItem(source src, target tgt) extends BackboneElement {
  src.linkId -> tgt.linkId;
  src.definition -> tgt.definition;
  src.text -> tgt.text;
  src.answer as s -> tgt.answer as t then QuestionnaireResponseItemAnswer(s, t);
}

group QuestionnaireResponseItemAnswer(source src, target tgt) extends BackboneElement {
  src.value : boolean -> tgt.value;
  src.value : decimal -> tgt.value;
  src.value : integer -> tgt.value;
  src.value : date -> tgt.value;
  src.value : dateTime -> tgt.value;
  src.value : time -> tgt.value;
  src.value : string -> tgt.value;
  src.value : uri -> tgt.value;
  src.value : Attachment -> tgt.value;
  src.value : Coding -> tgt.value;
  src.value : Quantity -> tgt.value;
  src.value : Reference -> tgt.value;
}

# entry[].resource.title
FML Conversion for QuestionnaireResponse: R4 to R5

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/ClinicalImpression4Bto5'
/// name = 'ClinicalImpression4Bto5'
/// title = 'FML Conversion for ClinicalImpression: R4B to R5'
/// status = 'draft'

conceptmap "ClinicalImpressionStatus" {
  prefix s = "http://hl7.org/fhir/4.3/event-status"
  prefix t = "http://hl7.org/fhir/event-status"

  s:preparation - t:preparation
  s:"in-progress" - t:"in-progress"
  s:"not-done" - t:"not-done"
  s:"on-hold" - t:"on-hold"
  s:stopped - t:stopped
  s:completed - t:completed
  s:"entered-in-error" - t:"entered-in-error"
  s:unknown - t:unknown
}

uses "http://hl7.org/fhir/4.3/StructureDefinition/ClinicalImpression" alias ClinicalImpressionR4B as source
uses "http://hl7.org/fhir/StructureDefinition/ClinicalImpression" alias ClinicalImpressionR5 as target

imports "http://hl7.org/fhir/StructureMap/*4Bto5"

group ClinicalImpression(source src : ClinicalImpressionR4B, target tgt : ClinicalImpressionR5) extends DomainResource <<type+>> {
  src.identifier -> tgt.identifier;
  src.status as v -> tgt.status = translate(v, '#ClinicalImpressionStatus', 'code');
  src.statusReason -> tgt.statusReason;
  src.description -> tgt.description;
  src.subject -> tgt.subject;
  src.encounter -> tgt.encounter;
  src.effective : dateTime -> tgt.effective;
  src.effective : Period -> tgt.effective;
  src.date -> tgt.date;
  src.assessor -> tgt.performer;
  src.previous -> tgt.previous;
  src.problem -> tgt.problem;
  src.protocol -> tgt.protocol; // src.changePattern -> tgt.changePattern;
  src.summary -> tgt.summary;
  src.finding as s -> tgt.finding as t then ClinicalImpressionFinding(s, t);
  src.prognosisCodeableConcept -> tgt.prognosisCodeableConcept;
  src.prognosisReference -> tgt.prognosisReference;
  src.supportingInfo -> tgt.supportingInfo;
  src.note -> tgt.note;
}

group ClinicalImpressionFinding(source src, target tgt) extends BackboneElement {
  src.itemCodeableConcept -> tgt.item;
  src.itemReference -> tgt.item;
  src.basis -> tgt.basis;
}

# entry[].resource.title
FML Conversion for ClinicalImpression: R4B to R5

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/SupplyRequest4to5'
/// name = 'SupplyRequest4to5'
/// title = 'FML Conversion for SupplyRequest: R4 to R5'
/// status = 'active'

conceptmap "RequestPriority" {
  prefix s = "http://hl7.org/fhir/4.0/request-priority"
  prefix t = "http://hl7.org/fhir/request-priority"

  s:routine - t:routine
  s:urgent - t:urgent
  s:asap - t:asap
  s:stat - t:stat
}

conceptmap "SupplyRequestStatus" {
  prefix s = "http://hl7.org/fhir/4.0/supplyrequest-status"
  prefix t = "http://hl7.org/fhir/supplyrequest-status"

  s:draft - t:draft
  s:active - t:active
  s:suspended - t:suspended
  s:cancelled - t:cancelled
  s:completed - t:completed
  s:"entered-in-error" - t:"entered-in-error"
  s:unknown - t:unknown
}

uses "http://hl7.org/fhir/4.0/StructureDefinition/SupplyRequest" alias SupplyRequestR4 as source
uses "http://hl7.org/fhir/StructureDefinition/SupplyRequest" alias SupplyRequestR5 as target

imports "http://hl7.org/fhir/StructureMap/*4to5"

group SupplyRequest(source src : SupplyRequestR4, target tgt : SupplyRequestR5) extends DomainResource <<type+>> {
  src.identifier -> tgt.identifier;
  src.status as v -> tgt.status = translate(v, '#SupplyRequestStatus', 'code');
  src.category -> tgt.category; // src.basedOn -> tgt.basedOn;
  src.priority as v -> tgt.priority = translate(v, '#RequestPriority', 'code');
  src.item : CodeableConcept -> tgt.item; // src.deliverFor -> tgt.deliverFor;
  src.item : Reference -> tgt.item;
  src.quantity -> tgt.quantity;
  src.parameter as s -> tgt.parameter as t then SupplyRequestParameter(s, t);
  src.occurrence : dateTime -> tgt.occurrence;
  src.occurrence : Period -> tgt.occurrence;
  src.occurrence : Timing -> tgt.occurrence;
  src.authoredOn -> tgt.authoredOn;
  src.requester -> tgt.requester;
  src.supplier -> tgt.supplier;
  src.reasonCode -> tgt.reason;
  src.reasonReference -> tgt.reason;
  src.deliverFrom -> tgt.deliverFrom;
  src.deliverTo -> tgt.deliverTo;
}

group SupplyRequestParameter(source src, target tgt) extends BackboneElement {
  src.code -> tgt.code;
  src.value : CodeableConcept -> tgt.value;
  src.value : Quantity -> tgt.value;
  src.value : Range -> tgt.value;
  src.value : boolean -> tgt.value;
}

# entry[].resource.title
FML Conversion for SupplyRequest: R4 to R5

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/HumanName4Bto5'
/// name = 'HumanName4Bto5'
/// title = 'FML Conversion for HumanName: R4B to R5'
/// status = 'active'

conceptmap "NameUse" {
  prefix s = "http://hl7.org/fhir/4.3/name-use"
  prefix t = "http://hl7.org/fhir/name-use"

  s:usual - t:usual
  s:official - t:official
  s:temp - t:temp
  s:nickname - t:nickname
  s:anonymous - t:anonymous
  s:old - t:old
  s:maiden - t:maiden
}

uses "http://hl7.org/fhir/4.3/StructureDefinition/HumanName" alias HumanNameR4B as source
uses "http://hl7.org/fhir/StructureDefinition/HumanName" alias HumanNameR5 as target

imports "http://hl7.org/fhir/StructureMap/*4Bto5"

group HumanName(source src : HumanNameR4B, target tgt : HumanNameR5) extends Element <<type+>> {
  src.use as v -> tgt.use = translate(v, '#NameUse', 'code');
  src.text -> tgt.text;
  src.family -> tgt.family;
  src.given -> tgt.given;
  src.prefix -> tgt.prefix;
  src.suffix -> tgt.suffix;
  src.period -> tgt.period;
}

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/Parameters4to5'
/// name = 'Parameters4to5'
/// title = 'FML Conversion for Parameters: R4 to R5'
/// status = 'active'

uses "http://hl7.org/fhir/4.0/StructureDefinition/Parameters" alias ParametersR4 as source
uses "http://hl7.org/fhir/StructureDefinition/Parameters" alias ParametersR5 as target

imports "http://hl7.org/fhir/StructureMap/*4to5"

group Parameters(source src : ParametersR4, target tgt : ParametersR5) extends Resource <<type+>> {
  src.parameter as s -> tgt.parameter as t then ParametersParameter(s, t);
}

group ParametersParameter(source src, target tgt) extends BackboneElement {
  src.name -> tgt.name;
  src.value : base64Binary -> tgt.value;
  src.value : boolean -> tgt.value;
  src.value : canonical -> tgt.value;
  src.value : code -> tgt.value;
  src.value : date -> tgt.value;
  src.value : dateTime -> tgt.value;
  src.value : decimal -> tgt.value;
  src.value : id -> tgt.value;
  src.value : instant -> tgt.value;
  src.value : integer -> tgt.value;
  src.value : markdown -> tgt.value; // src.value : integer64 -> tgt.value;
  src.value : oid -> tgt.value;
  src.value : positiveInt -> tgt.value;
  src.value : string -> tgt.value;
  src.value : time -> tgt.value;
  src.value : unsignedInt -> tgt.value;
  src.value : uri -> tgt.value;
  src.value : url -> tgt.value;
  src.value : uuid -> tgt.value;
  src.value : Address -> tgt.value;
  src.value : Age -> tgt.value;
  src.value : Annotation -> tgt.value;
  src.value : Attachment -> tgt.value;
  src.value : CodeableConcept -> tgt.value;
  src.value : Coding -> tgt.value; // src.value : CodeableReference -> tgt.value;
  src.value : ContactPoint -> tgt.value;
  src.value : Count -> tgt.value;
  src.value : Distance -> tgt.value;
  src.value : Duration -> tgt.value;
  src.value : HumanName -> tgt.value;
  src.value : Identifier -> tgt.value;
  src.value : Money -> tgt.value;
  src.value : Period -> tgt.value;
  src.value : Quantity -> tgt.value;
  src.value : Range -> tgt.value;
  src.value : Ratio -> tgt.value;
  src.value : Reference -> tgt.value; // src.value : RatioRange -> tgt.value;
  src.value : SampledData -> tgt.value;
  src.value : Signature -> tgt.value;
  src.value : Timing -> tgt.value;
  src.value : ContactDetail -> tgt.value;
  src.value : DataRequirement -> tgt.value;
  src.value : Expression -> tgt.value;
  src.value : ParameterDefinition -> tgt.value;
  src.value : RelatedArtifact -> tgt.value;
  src.value : TriggerDefinition -> tgt.value;
  src.value : UsageContext -> tgt.value;
  src.value : Dosage -> tgt.value; // src.value : Availability -> tgt.value;
  src.value : Meta -> tgt.value;
  src.resource : Resource -> tgt.resource;
}

# entry[].resource.group[].rule[].documentation
src.value : CodeableReference -> tgt.value;

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/ImmunizationEvaluation5to4'
/// name = 'ImmunizationEvaluation5to4'
/// title = 'FML Conversion for ImmunizationEvaluation: R5 to R4'
/// status = 'active'

uses "http://hl7.org/fhir/StructureDefinition/ImmunizationEvaluation" alias ImmunizationEvaluationR5 as source
uses "http://hl7.org/fhir/4.0/StructureDefinition/ImmunizationEvaluation" alias ImmunizationEvaluationR4 as target

imports "http://hl7.org/fhir/StructureMap/*5to4"

group ImmunizationEvaluation(source src : ImmunizationEvaluationR5, target tgt : ImmunizationEvaluationR4) extends DomainResource <<type+>> {
  src.identifier -> tgt.identifier;
  src.status -> tgt.status;
  src.patient -> tgt.patient;
  src.date -> tgt.date;
  src.authority -> tgt.authority;
  src.targetDisease -> tgt.targetDisease;
  src.immunizationEvent -> tgt.immunizationEvent;
  src.doseStatus -> tgt.doseStatus;
  src.doseStatusReason -> tgt.doseStatusReason;
  src.description -> tgt.description;
  src.series -> tgt.series;
  src.doseNumber -> tgt.doseNumber;
  src.seriesDoses -> tgt.seriesDoses;
}

# entry[].resource.title
FML Conversion for ImmunizationEvaluation: R5 to R4

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/Identifier5to4B'
/// name = 'Identifier5to4B'
/// title = 'FML Conversion for Identifier: R5 to R4B'
/// status = 'active'

conceptmap "IdentifierUse" {
  prefix s = "http://hl7.org/fhir/identifier-use"
  prefix t = "http://hl7.org/fhir/4.3/identifier-use"

  s:usual - t:usual
  s:official - t:official
  s:temp - t:temp
  s:secondary - t:secondary
  s:old - t:old
}

uses "http://hl7.org/fhir/StructureDefinition/Identifier" alias IdentifierR5 as source
uses "http://hl7.org/fhir/4.3/StructureDefinition/Identifier" alias IdentifierR4B as target

imports "http://hl7.org/fhir/StructureMap/*5to4B"

group Identifier(source src : IdentifierR5, target tgt : IdentifierR4B) extends Element <<type+>> {
  src.use as v -> tgt.use = translate(v, '#IdentifierUse', 'code');
  src.type -> tgt.type;
  src.system -> tgt.system;
  src.value -> tgt.value;
  src.period -> tgt.period;
  src.assigner -> tgt.assigner;
}

# entry[].resource.title
FML Conversion for Identifier: R5 to R4B

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/Duration4Bto5'
/// name = 'Duration4Bto5'
/// title = 'FML Conversion for Duration: R4B to R5'
/// status = 'active'

uses "http://hl7.org/fhir/4.3/StructureDefinition/Duration" alias DurationR4B as source
uses "http://hl7.org/fhir/StructureDefinition/Duration" alias DurationR5 as target

imports "http://hl7.org/fhir/StructureMap/*4Bto5"

group Duration(source src : DurationR4B, target tgt : DurationR5) extends Quantity <<type+>> {
}

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/SubstancePolymer4to5'
/// name = 'SubstancePolymer4to5'
/// title = 'FML Conversion for SubstancePolymer: R4 to R5'
/// status = 'active'

uses "http://hl7.org/fhir/4.0/StructureDefinition/SubstancePolymer" alias SubstancePolymerR4 as source
uses "http://hl7.org/fhir/StructureDefinition/SubstancePolymer" alias SubstancePolymerR5 as target

imports "http://hl7.org/fhir/StructureMap/*4to5"

group SubstancePolymer(source src : SubstancePolymerR4, target tgt : SubstancePolymerR5) extends DomainResource <<type+>> {
  src.class -> tgt.class; // src.identifier -> tgt.identifier;
  src.geometry -> tgt.geometry;
  src.copolymerConnectivity -> tgt.copolymerConnectivity;
  src.modification -> tgt.modification;
  src.monomerSet as s -> tgt.monomerSet as t then SubstancePolymerMonomerSet(s, t);
  src.repeat as s -> tgt.repeat as t then SubstancePolymerRepeat(s, t);
}

group SubstancePolymerMonomerSet(source src, target tgt) extends BackboneElement {
  src.ratioType -> tgt.ratioType;
  src.startingMaterial as s -> tgt.startingMaterial as t then SubstancePolymerMonomerSetStartingMaterial(s, t);
}

group SubstancePolymerMonomerSetStartingMaterial(source src, target tgt) extends BackboneElement {
  src.material -> tgt.code;
  src.type -> tgt.category;
  src.isDefining -> tgt.isDefining;
  src.amount as s ->  tgt.amount as t,  t = s.amount;
}

group SubstancePolymerRepeat(source src, target tgt) extends BackboneElement {
  src.averageMolecularFormula -> tgt.averageMolecularFormula;
  src.repeatUnitAmountType -> tgt.repeatUnitAmountType;
  src.repeatUnit as s -> tgt.repeatUnit as t then SubstancePolymerRepeatRepeatUnit(s, t);
}

group SubstancePolymerRepeatRepeatUnit(source src, target tgt) extends BackboneElement {
  src.repeatUnit -> tgt.unit;
  src.orientationOfPolymerisation -> tgt.orientation;
  src.degreeOfPolymerisation as s -> tgt.degreeOfPolymerisation as t then SubstancePolymerRepeatRepeatUnitDegreeOfPolymerisation(s, t); // src.amount -> tgt.amount;
  src.structuralRepresentation as s -> tgt.structuralRepresentation as t then SubstancePolymerRepeatRepeatUnitStructuralRepresentation(s, t);
}

group SubstancePolymerRepeatRepeatUnitDegreeOfPolymerisation(source src, target tgt) extends BackboneElement {
  src.degree -> tgt.type;
  src.amount as s -> tgt then {
    s.amount : Quantity as v -> tgt.average = v.value;
    s.referenceRange as sr -> tgt then {
      sr.lowLimit as vl -> tgt.low = vl.value;
      sr.highLimit as vh -> tgt.high = vh.value;
    };
  };
}

group SubstancePolymerRepeatRepeatUnitStructuralRepresentation(source src, target tgt) extends BackboneElement {
  src.type -> tgt.type;
  src.representation -> tgt.representation;
  src.attachment -> tgt.attachment; // src.format -> tgt.format;
}

# entry[].resource.title
FML Conversion for SubstancePolymer: R4 to R5

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/Encounter5to4B'
/// name = 'Encounter5to4B'
/// title = 'FML Conversion for Encounter: R5 to R4B'
/// status = 'active'

conceptmap "EncounterLocationStatus" {
  prefix s = "http://hl7.org/fhir/encounter-location-status"
  prefix t = "http://hl7.org/fhir/4.3/encounter-location-status"

  s:planned - t:planned
  s:active - t:active
  s:reserved - t:reserved
  s:completed - t:completed
}

conceptmap "EncounterStatus" {
  prefix s = "http://hl7.org/fhir/encounter-status"
  prefix t = "http://hl7.org/fhir/4.3/encounter-status"

  s:planned - t:planned
  s:"in-progress" - t:triaged
  s:"on-hold" - t:onleave
  s:discharged - t:finished
  s:completed - t:finished
  s:cancelled - t:cancelled
  s:discontinued - t:cancelled
  s:"entered-in-error" - t:"entered-in-error"
  s:unknown - t:unknown
}

uses "http://hl7.org/fhir/StructureDefinition/Encounter" alias EncounterR5 as source
uses "http://hl7.org/fhir/4.3/StructureDefinition/Encounter" alias EncounterR4B as target

imports "http://hl7.org/fhir/StructureMap/*5to4B"

group Encounter(source src : EncounterR5, target tgt : EncounterR4B) extends DomainResource <<type+>> {
  src.identifier -> tgt.identifier;
  src.status as v -> tgt.status = translate(v, '#EncounterStatus', 'code');
  src.class as s -> tgt then {
    s.coding as sc -> tgt.class as t then Coding(sc, t);
  }; // src.class as s -> tgt.class = create('Coding') as tc, tc.system = (s.system), tc.code = (s.code), tc.display = s.display;
  src.priority -> tgt.priority;
  src.type -> tgt.type;
  src.serviceType -> tgt.serviceType;
  src.subject -> tgt.subject;
  src.episodeOfCare -> tgt.episodeOfCare; // src.subjectStatus -> tgt.subjectStatus;
  src.basedOn -> tgt.basedOn;
  src.partOf -> tgt.partOf; // src.careTeam -> tgt.careTeam;
  src.serviceProvider -> tgt.serviceProvider;
  src.participant as s -> tgt.participant as t then EncounterParticipant(s, t);
  src.appointment -> tgt.appointment;
  src.actualPeriod -> tgt.period; // src.virtualService -> tgt.virtualService;
  src.length -> tgt.length; // src.plannedStartDate -> tgt.plannedStartDate;
  src.diagnosis as s -> tgt.diagnosis as t then EncounterDiagnosis(s, t); // src.reason as s -> tgt.reason as t then EncounterReason(s,t);
  src.account -> tgt.account;
  src.admission as s -> tgt.hospitalization as t then EncounterAdmission(s, t); // src.dietPreference -> tgt.dietPreference;
  src.location as s -> tgt.location as t then EncounterLocation(s, t);
}

group EncounterParticipant(source src, target tgt) extends BackboneElement {
  src.type -> tgt.type;
  src.period -> tgt.period;
  src.actor -> tgt.individual;
}

// group EncounterReason(source src, target tgt) extends BackboneElement {
// src.use -> tgt.use;
// src.value -> tgt.value;
// }
group EncounterDiagnosis(source src, target tgt) extends BackboneElement {
  src.condition -> tgt.condition;
  src.use -> tgt.use;
}

group EncounterAdmission(source src, target tgt) extends BackboneElement {
  src.preAdmissionIdentifier -> tgt.preAdmissionIdentifier;
  src.origin -> tgt.origin;
  src.admitSource -> tgt.admitSource;
  src.reAdmission -> tgt.reAdmission;
  src.destination -> tgt.destination;
  src.dischargeDisposition -> tgt.dischargeDisposition;
}

group EncounterLocation(source src, target tgt) extends BackboneElement {
  src.location -> tgt.location;
  src.status as v -> tgt.status = translate(v, '#EncounterLocationStatus', 'code');
  src.form -> tgt.physicalType;
  src.period -> tgt.period;
}

# entry[].resource.group[].rule[].documentation
src.class as s -> tgt.class = create('Coding') as tc, tc.system = (s.system), tc.code = (s.code), tc.display = s.display;

# entry[].resource.group[].rule[].documentation
src.reason as s -> tgt.reason as t then EncounterReason(s,t);

# entry[].resource.group[].documentation
group EncounterReason(source src, target tgt) extends BackboneElement {
src.use -> tgt.use;
src.value -> tgt.value;
}

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/Quantity5to4'
/// name = 'Quantity5to4'
/// title = 'FML Conversion for Quantity: R5 to R4'
/// status = 'active'

conceptmap "QuantityComparator" {
  prefix s = "http://hl7.org/fhir/quantity-comparator"
  prefix t = "http://hl7.org/fhir/4.0/quantity-comparator"

  s:"<" - t:"<"
  s:"<=" - t:"<="
  s:">=" - t:">="
  s:">" - t:">"
  s:ad - t:"<="
}

uses "http://hl7.org/fhir/StructureDefinition/Quantity" alias QuantityR5 as source
uses "http://hl7.org/fhir/4.0/StructureDefinition/Quantity" alias QuantityR4 as target

imports "http://hl7.org/fhir/StructureMap/*5to4"

group Quantity(source src : QuantityR5, target tgt : QuantityR4) extends Element <<type+>> {
  src.value -> tgt.value;
  src.comparator as v -> tgt.comparator = translate(v, '#QuantityComparator', 'code');
  src.unit -> tgt.unit;
  src.system -> tgt.system;
  src.code -> tgt.code;
}

# entry[].resource.contained[].group[].element[].target[].comment
can't round trip this without an extension

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/Communication5to4B'
/// name = 'Communication5to4B'
/// title = 'FML Conversion for Communication: R5 to R4B'
/// status = 'active'

conceptmap "CommunicationPriority" {
  prefix s = "http://hl7.org/fhir/request-priority"
  prefix t = "http://hl7.org/fhir/4.3/request-priority"

  s:routine - t:routine
  s:urgent - t:urgent
  s:asap - t:asap
  s:stat - t:stat
}

conceptmap "CommunicationStatus" {
  prefix s = "http://hl7.org/fhir/event-status"
  prefix t = "http://hl7.org/fhir/4.3/event-status"

  s:preparation - t:preparation
  s:"in-progress" - t:"in-progress"
  s:"not-done" - t:"not-done"
  s:"on-hold" - t:"on-hold"
  s:stopped - t:stopped
  s:completed - t:completed
  s:"entered-in-error" - t:"entered-in-error"
  s:unknown - t:unknown
}

uses "http://hl7.org/fhir/StructureDefinition/Communication" alias CommunicationR5 as source
uses "http://hl7.org/fhir/4.3/StructureDefinition/Communication" alias CommunicationR4B as target

imports "http://hl7.org/fhir/StructureMap/*5to4B"

group Communication(source src : CommunicationR5, target tgt : CommunicationR4B) extends DomainResource <<type+>> {
  src.identifier -> tgt.identifier;
  src.instantiatesCanonical -> tgt.instantiatesCanonical;
  src.instantiatesUri -> tgt.instantiatesUri;
  src.basedOn -> tgt.basedOn;
  src.partOf -> tgt.partOf;
  src.inResponseTo -> tgt.inResponseTo;
  src.status as v -> tgt.status = translate(v, '#CommunicationStatus', 'code');
  src.statusReason -> tgt.statusReason;
  src.category -> tgt.category;
  src.priority as v -> tgt.priority = translate(v, '#CommunicationPriority', 'code');
  src.medium -> tgt.medium;
  src.subject -> tgt.subject;
  src.topic -> tgt.topic;
  src.about -> tgt.about;
  src.encounter -> tgt.encounter;
  src.sent -> tgt.sent;
  src.received -> tgt.received;
  src.recipient -> tgt.recipient;
  src.sender -> tgt.sender;
  src.reason -> tgt.reasonCode;
  src.reason -> tgt.reasonReference;
  src.payload as s -> tgt.payload as t then CommunicationPayload(s, t);
  src.note -> tgt.note;
}

group CommunicationPayload(source src, target tgt) extends BackboneElement {
  src.content : Attachment -> tgt.content;
  src.content : Reference -> tgt.content;
  src.content : CodeableConcept -> tgt.content;
}

# entry[].resource.title
FML Conversion for Communication: R5 to R4B

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/Endpoint4to5'
/// name = 'Endpoint4to5'
/// title = 'FML Conversion for Endpoint: R4 to R5'
/// status = 'active'

conceptmap "EndpointStatus" {
  prefix s = "http://hl7.org/fhir/4.0/endpoint-status"
  prefix t = "http://hl7.org/fhir/endpoint-status"

  s:active - t:active
  s:suspended - t:suspended
  s:error - t:error
  s:off - t:off
  s:"entered-in-error" - t:"entered-in-error"
}

uses "http://hl7.org/fhir/4.0/StructureDefinition/Endpoint" alias EndpointR4 as source
uses "http://hl7.org/fhir/StructureDefinition/Endpoint" alias EndpointR5 as target

imports "http://hl7.org/fhir/StructureMap/*4to5"

group Endpoint(source src : EndpointR4, target tgt : EndpointR5) extends DomainResource <<type+>> {
  src.identifier -> tgt.identifier;
  src.status as v -> tgt.status = translate(v, '#EndpointStatus', 'code');
  src.connectionType -> tgt.connectionType;
  src.name -> tgt.name;
  src.managingOrganization -> tgt.managingOrganization; // src.description -> tgt.description;
  src.contact -> tgt.contact;
  src.period -> tgt.period;
  src.payloadType as s ->  tgt.payload as t,  t.type = s;
  src.payloadMimeType as s ->  tgt.payload as t,  t.mimeType = s;
  src.address -> tgt.address;
  src.header -> tgt.header;
}

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/PlanDefinition4Bto5'
/// name = 'PlanDefinition4Bto5'
/// title = 'FML Conversion for PlanDefinition: R4B to R5'
/// status = 'active'

conceptmap "ActionCardinalityBehavior" {
  prefix s = "http://hl7.org/fhir/4.3/action-cardinality-behavior"
  prefix t = "http://hl7.org/fhir/action-cardinality-behavior"

  s:single - t:single
  s:multiple - t:multiple
}

conceptmap "ActionConditionKind" {
  prefix s = "http://hl7.org/fhir/4.3/action-condition-kind"
  prefix t = "http://hl7.org/fhir/action-condition-kind"

  s:applicability - t:applicability
  s:start - t:start
  s:stop - t:stop
}

conceptmap "ActionGroupingBehavior" {
  prefix s = "http://hl7.org/fhir/4.3/action-grouping-behavior"
  prefix t = "http://hl7.org/fhir/action-grouping-behavior"

  s:"visual-group" - t:"visual-group"
  s:"logical-group" - t:"logical-group"
  s:"sentence-group" - t:"sentence-group"
}

conceptmap "ActionPrecheckBehavior" {
  prefix s = "http://hl7.org/fhir/4.3/action-precheck-behavior"
  prefix t = "http://hl7.org/fhir/action-precheck-behavior"

  s:yes - t:yes
  s:no - t:no
}

conceptmap "ActionRelationshipType" {
  prefix s = "http://hl7.org/fhir/4.3/action-relationship-type"
  prefix t = "http://hl7.org/fhir/action-relationship-type"

  s:before - t:before
  s:"before-start" - t:"before-start"
  s:"before-end" - t:"before-end"
  s:concurrent - t:concurrent
  s:"concurrent-with-start" - t:"concurrent-with-start"
  s:"concurrent-with-end" - t:"concurrent-with-end"
  s:after - t:after
  s:"after-start" - t:"after-start"
  s:"after-end" - t:"after-end"
}

conceptmap "ActionRequiredBehavior" {
  prefix s = "http://hl7.org/fhir/4.3/action-required-behavior"
  prefix t = "http://hl7.org/fhir/action-required-behavior"

  s:must - t:must
  s:could - t:could
  s:"must-unless-documented" - t:"must-unless-documented"
}

conceptmap "ActionSelectionBehavior" {
  prefix s = "http://hl7.org/fhir/4.3/action-selection-behavior"
  prefix t = "http://hl7.org/fhir/action-selection-behavior"

  s:any - t:any
  s:all - t:all
  s:"all-or-none" - t:"all-or-none"
  s:"exactly-one" - t:"exactly-one"
  s:"at-most-one" - t:"at-most-one"
  s:"one-or-more" - t:"one-or-more"
}

conceptmap "ActivityParticipantType" {
  prefix s = "http://hl7.org/fhir/4.3/action-participant-type"
  prefix t = "http://hl7.org/fhir/action-participant-type"

  s:device - t:device
  s:patient - t:patient
  s:practitioner - t:practitioner
  s:"related-person" - t:relatedperson
}

conceptmap "PublicationStatus" {
  prefix s = "http://hl7.org/fhir/4.3/publication-status"
  prefix t = "http://hl7.org/fhir/publication-status"

  s:draft - t:draft
  s:active - t:active
  s:retired - t:retired
  s:unknown - t:unknown
}

conceptmap "RequestPriority" {
  prefix s = "http://hl7.org/fhir/4.3/request-priority"
  prefix t = "http://hl7.org/fhir/request-priority"

  s:routine - t:routine
  s:urgent - t:urgent
  s:asap - t:asap
  s:stat - t:stat
}

uses "http://hl7.org/fhir/4.3/StructureDefinition/PlanDefinition" alias PlanDefinitionR4B as source
uses "http://hl7.org/fhir/StructureDefinition/PlanDefinition" alias PlanDefinitionR5 as target

imports "http://hl7.org/fhir/StructureMap/*4Bto5"

group PlanDefinition(source src : PlanDefinitionR4B, target tgt : PlanDefinitionR5) extends DomainResource <<type+>> {
  src.url -> tgt.url;
  src.identifier -> tgt.identifier;
  src.version -> tgt.version;
  src.name -> tgt.name; // src.versionAlgorithm : string -> tgt.versionAlgorithm;
  src.title -> tgt.title;
  src.subtitle -> tgt.subtitle;
  src.type -> tgt.type;
  src.status as v -> tgt.status = translate(v, '#PublicationStatus', 'code');
  src.experimental -> tgt.experimental;
  src.subject as s where code = 'Patient' ->  tgt.subject = create('CodeableConcept') as tc,  tc.coding = create('Coding') as tcc,  tcc.system = 'http://hl7.org/fhir/fhir-types',  tcc.code = 'Patient',  tcc.display = 'Patient';
  src.subject as s where code = 'Practitioner' ->  tgt.subject = create('CodeableConcept') as tc,  tc.coding = create('Coding') as tcc,  tcc.system = 'http://hl7.org/fhir/fhir-types',  tcc.code = 'Patient',  tcc.display = 'Practitioner';
  src.subject as s where code = 'Organization' ->  tgt.subject = create('CodeableConcept') as tc,  tc.coding = create('Coding') as tcc,  tcc.system = 'http://hl7.org/fhir/fhir-types',  tcc.code = 'Patient',  tcc.display = 'Organization';
  src.subject as s where code = 'Location' ->  tgt.subject = create('CodeableConcept') as tc,  tc.coding = create('Coding') as tcc,  tcc.system = 'http://hl7.org/fhir/fhir-types',  tcc.code = 'Patient',  tcc.display = 'Location';
  src.subject as s where code = 'Device' ->  tgt.subject = create('CodeableConcept') as tc,  tc.coding = create('Coding') as tcc,  tcc.system = 'http://hl7.org/fhir/fhir-types',  tcc.code = 'Patient',  tcc.display = 'Device';
  src.subject : Reference -> tgt.subject;
  src.subject : canonical -> tgt.subject;
  src.date -> tgt.date;
  src.publisher -> tgt.publisher;
  src.contact -> tgt.contact;
  src.description -> tgt.description;
  src.useContext -> tgt.useContext;
  src.jurisdiction -> tgt.jurisdiction;
  src.purpose -> tgt.purpose;
  src.usage -> tgt.usage;
  src.copyright -> tgt.copyright;
  src.approvalDate -> tgt.approvalDate; // src.copyrightLabel -> tgt.copyrightLabel;
  src.lastReviewDate -> tgt.lastReviewDate;
  src.effectivePeriod -> tgt.effectivePeriod;
  src.topic -> tgt.topic;
  src.author -> tgt.author;
  src.editor -> tgt.editor;
  src.reviewer -> tgt.reviewer;
  src.endorser -> tgt.endorser;
  src.relatedArtifact -> tgt.relatedArtifact;
  src.library -> tgt.library;
  src.goal as s -> tgt.goal as t then PlanDefinitionGoal(s, t);
  src.action as s -> tgt.action as t then PlanDefinitionAction(s, t); // src.actor as s -> tgt.actor as t then PlanDefinitionActor(s,t);
}

group PlanDefinitionGoal(source src, target tgt) extends BackboneElement {
  src.category -> tgt.category;
  src.description -> tgt.description;
  src.priority -> tgt.priority;
  src.start -> tgt.start;
  src.addresses -> tgt.addresses;
  src.documentation -> tgt.documentation;
  src.target as s -> tgt.target as t then PlanDefinitionGoalTarget(s, t);
}

group PlanDefinitionGoalTarget(source src, target tgt) extends BackboneElement {
  src.measure -> tgt.measure;
  src.detail : Quantity -> tgt.detail;
  src.detail : Range -> tgt.detail;
  src.detail : CodeableConcept -> tgt.detail;
  src.due -> tgt.due; // src.detail : string -> tgt.detail;
}

// group PlanDefinitionActor(source src, target tgt) extends BackboneElement {
// src.title -> tgt.title;
// src.description -> tgt.description;
// src.option as s -> tgt.option as t then PlanDefinitionActorOption(s,t);
// }
// group PlanDefinitionActorOption(source src, target tgt) extends BackboneElement {
// src.type as v -> tgt.type = translate(v, '#ActivityParticipantType', 'code');
// src.typeCanonical -> tgt.typeCanonical;
// src.typeReference -> tgt.typeReference;
// src.role -> tgt.role;
// }
group PlanDefinitionAction(source src, target tgt) extends BackboneElement {
  src.id -> tgt.linkId;
  src.prefix -> tgt.prefix;
  src.title -> tgt.title;
  src.description -> tgt.description;
  src.textEquivalent -> tgt.textEquivalent;
  src.priority as v -> tgt.priority = translate(v, '#RequestPriority', 'code');
  src.code -> tgt.code;
  src.reason -> tgt.reason;
  src.documentation -> tgt.documentation;
  src.goalId -> tgt.goalId;
  src.subject as s where code = 'Patient' ->  tgt.subject = create('CodeableConcept') as tc,  tc.coding = create('Coding') as tcc,  tcc.system = 'http://hl7.org/fhir/fhir-types',  tcc.code = 'Patient',  tcc.display = 'Patient';
  src.subject as s where code = 'Practitioner' ->  tgt.subject = create('CodeableConcept') as tc,  tc.coding = create('Coding') as tcc,  tcc.system = 'http://hl7.org/fhir/fhir-types',  tcc.code = 'Patient',  tcc.display = 'Practitioner';
  src.subject as s where code = 'Organization' ->  tgt.subject = create('CodeableConcept') as tc,  tc.coding = create('Coding') as tcc,  tcc.system = 'http://hl7.org/fhir/fhir-types',  tcc.code = 'Patient',  tcc.display = 'Organization';
  src.subject as s where code = 'Location' ->  tgt.subject = create('CodeableConcept') as tc,  tc.coding = create('Coding') as tcc,  tcc.system = 'http://hl7.org/fhir/fhir-types',  tcc.code = 'Patient',  tcc.display = 'Location';
  src.subject as s where code = 'Device' ->  tgt.subject = create('CodeableConcept') as tc,  tc.coding = create('Coding') as tcc,  tcc.system = 'http://hl7.org/fhir/fhir-types',  tcc.code = 'Patient',  tcc.display = 'Device';
  src.subject : Reference -> tgt.subject;
  src.subject : canonical -> tgt.subject;
  src.trigger -> tgt.trigger;
  src.condition as s -> tgt.condition as t then PlanDefinitionActionCondition(s, t);
  src.input as s ->  tgt.input as t,  t.requirement = s;
  src.output as s ->  tgt.output as t,  t.requirement = s;
  src.relatedAction as s -> tgt.relatedAction as t then PlanDefinitionActionRelatedAction(s, t);
  src.timing : Age -> tgt.timing;
  src.timing : Duration -> tgt.timing;
  src.timing : Range -> tgt.timing;
  src.timing : Timing -> tgt.timing;
  src.participant as s -> tgt.participant as t then PlanDefinitionActionParticipant(s, t); // src.location -> tgt.location;
  src.type -> tgt.type;
  src.groupingBehavior as v -> tgt.groupingBehavior = translate(v, '#ActionGroupingBehavior', 'code');
  src.selectionBehavior as v -> tgt.selectionBehavior = translate(v, '#ActionSelectionBehavior', 'code');
  src.requiredBehavior as v -> tgt.requiredBehavior = translate(v, '#ActionRequiredBehavior', 'code');
  src.precheckBehavior as v -> tgt.precheckBehavior = translate(v, '#ActionPrecheckBehavior', 'code');
  src.cardinalityBehavior as v -> tgt.cardinalityBehavior = translate(v, '#ActionCardinalityBehavior', 'code');
  src.definition : canonical -> tgt.definition;
  src.definition : uri -> tgt.definition;
  src.transform -> tgt.transform;
  src.dynamicValue as s -> tgt.dynamicValue as t then PlanDefinitionActionDynamicValue(s, t);
}

group PlanDefinitionActionCondition(source src, target tgt) extends BackboneElement {
  src.kind as v -> tgt.kind = translate(v, '#ActionConditionKind', 'code');
  src.expression -> tgt.expression;
}

group PlanDefinitionActionRelatedAction(source src, target tgt) extends BackboneElement {
  src.actionId -> tgt.targetId;
  src.relationship as v -> tgt.relationship = translate(v, '#ActionRelationshipType', 'code');
  src.relationship as v -> tgt.endRelationship = translate(v, '#ActionRelationshipType', 'code');
  src.offset : Duration -> tgt.offset;
  src.offset : Range -> tgt.offset;
}

group PlanDefinitionActionParticipant(source src, target tgt) extends BackboneElement {
  src.type as v -> tgt.type = translate(v, '#ActivityParticipantType', 'code'); // src.actorId -> tgt.actorId;
  src.role -> tgt.role; // src.typeCanonical -> tgt.typeCanonical;
}

group PlanDefinitionActionDynamicValue(source src, target tgt) extends BackboneElement {
  src.path -> tgt.path;
  src.expression -> tgt.expression;
}

# entry[].resource.title
FML Conversion for PlanDefinition: R4B to R5

# entry[].resource.group[].rule[].documentation
src.actor as s -> tgt.actor as t then PlanDefinitionActor(s,t);

# entry[].resource.group[].documentation
group PlanDefinitionActor(source src, target tgt) extends BackboneElement {
src.title -> tgt.title;
src.description -> tgt.description;
src.option as s -> tgt.option as t then PlanDefinitionActorOption(s,t);
}
group PlanDefinitionActorOption(source src, target tgt) extends BackboneElement {
src.type as v -> tgt.type = translate(v, '#ActivityParticipantType', 'code');
src.typeCanonical -> tgt.typeCanonical;
src.typeReference -> tgt.typeReference;
src.role -> tgt.role;
}

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/RequestOrchestration4Bto5'
/// name = 'RequestOrchestration4Bto5'
/// title = 'FML Conversion for RequestOrchestration: R4B to R5'
/// status = 'active'

conceptmap "ActionCardinalityBehavior" {
  prefix s = "http://hl7.org/fhir/4.3/action-cardinality-behavior"
  prefix t = "http://hl7.org/fhir/action-cardinality-behavior"

  s:single - t:single
  s:multiple - t:multiple
}

conceptmap "ActionConditionKind" {
  prefix s = "http://hl7.org/fhir/4.3/action-condition-kind"
  prefix t = "http://hl7.org/fhir/action-condition-kind"

  s:applicability - t:applicability
  s:start - t:start
  s:stop - t:stop
}

conceptmap "ActionGroupingBehavior" {
  prefix s = "http://hl7.org/fhir/4.3/action-grouping-behavior"
  prefix t = "http://hl7.org/fhir/action-grouping-behavior"

  s:"visual-group" - t:"visual-group"
  s:"logical-group" - t:"logical-group"
  s:"sentence-group" - t:"sentence-group"
}

conceptmap "ActionPrecheckBehavior" {
  prefix s = "http://hl7.org/fhir/4.3/action-precheck-behavior"
  prefix t = "http://hl7.org/fhir/action-precheck-behavior"

  s:yes - t:yes
  s:no - t:no
}

conceptmap "ActionRelationshipType" {
  prefix s = "http://hl7.org/fhir/4.3/action-relationship-type"
  prefix t = "http://hl7.org/fhir/action-relationship-type"

  s:before - t:before
  s:"before-start" - t:"before-start"
  s:"before-end" - t:"before-end"
  s:concurrent - t:concurrent
  s:"concurrent-with-start" - t:"concurrent-with-start"
  s:"concurrent-with-end" - t:"concurrent-with-end"
  s:after - t:after
  s:"after-start" - t:"after-start"
  s:"after-end" - t:"after-end"
}

conceptmap "ActionRequiredBehavior" {
  prefix s = "http://hl7.org/fhir/4.3/action-required-behavior"
  prefix t = "http://hl7.org/fhir/action-required-behavior"

  s:must - t:must
  s:could - t:could
  s:"must-unless-documented" - t:"must-unless-documented"
}

conceptmap "ActionSelectionBehavior" {
  prefix s = "http://hl7.org/fhir/4.3/action-selection-behavior"
  prefix t = "http://hl7.org/fhir/action-selection-behavior"

  s:any - t:any
  s:all - t:all
  s:"all-or-none" - t:"all-or-none"
  s:"exactly-one" - t:"exactly-one"
  s:"at-most-one" - t:"at-most-one"
  s:"one-or-more" - t:"one-or-more"
}

conceptmap "RequestIntent" {
  prefix s = "http://hl7.org/fhir/4.3/request-intent"
  prefix t = "http://hl7.org/fhir/request-intent"

  s:proposal - t:proposal
  s:plan - t:plan
  s:directive - t:directive
  s:order - t:order
  s:"original-order" - t:"original-order"
  s:"reflex-order" - t:"reflex-order"
  s:"filler-order" - t:"filler-order"
  s:"instance-order" - t:"instance-order"
  s:option - t:option
}

conceptmap "RequestPriority" {
  prefix s = "http://hl7.org/fhir/4.3/request-priority"
  prefix t = "http://hl7.org/fhir/request-priority"

  s:routine - t:routine
  s:urgent - t:urgent
  s:asap - t:asap
  s:stat - t:stat
}

conceptmap "RequestStatus" {
  prefix s = "http://hl7.org/fhir/4.3/request-status"
  prefix t = "http://hl7.org/fhir/request-status"

  s:draft - t:draft
  s:active - t:active
  s:"on-hold" - t:"on-hold"
  s:revoked - t:revoked
  s:completed - t:completed
  s:"entered-in-error" - t:"entered-in-error"
  s:unknown - t:unknown
}

uses "http://hl7.org/fhir/4.3/StructureDefinition/RequestGroup" alias RequestGroupR4B as source
uses "http://hl7.org/fhir/StructureDefinition/RequestOrchestration" alias RequestOrchestrationR5 as target

imports "http://hl7.org/fhir/StructureMap/*4Bto5"

group RequestOrchestration(source src : RequestGroupR4B, target tgt : RequestOrchestrationR5) extends DomainResource <<type+>> {
  src.identifier -> tgt.identifier;
  src.instantiatesCanonical -> tgt.instantiatesCanonical;
  src.instantiatesUri -> tgt.instantiatesUri;
  src.basedOn -> tgt.basedOn;
  src.replaces -> tgt.replaces;
  src.groupIdentifier -> tgt.groupIdentifier;
  src.status as v -> tgt.status = translate(v, '#RequestStatus', 'code');
  src.intent as v -> tgt.intent = translate(v, '#RequestIntent', 'code');
  src.priority as v -> tgt.priority = translate(v, '#RequestPriority', 'code');
  src.code -> tgt.code;
  src.subject -> tgt.subject;
  src.encounter -> tgt.encounter;
  src.authoredOn -> tgt.authoredOn;
  src.author -> tgt.author;
  src.reasonCode : CodeableConcept -> tgt.reason;
  src.reasonReference : Reference -> tgt.reason;
  src.note -> tgt.note; // src.goal -> tgt.goal;
  src.action as s -> tgt.action as t then RequestOrchestrationAction(s, t);
}

group RequestOrchestrationAction(source src, target tgt) extends BackboneElement {
  src.prefix -> tgt.prefix; // src.linkId -> tgt.linkId;
  src.title -> tgt.title;
  src.description -> tgt.description;
  src.textEquivalent -> tgt.textEquivalent;
  src.priority as v -> tgt.priority = translate(v, '#RequestPriority', 'code');
  src.code -> tgt.code;
  src.documentation -> tgt.documentation;
  src.condition as s -> tgt.condition as t then RequestOrchestrationActionCondition(s, t); // src.goal -> tgt.goal;
  src.relatedAction as s -> tgt.relatedAction as t then RequestOrchestrationActionRelatedAction(s, t); // src.input as s -> tgt.input as t then RequestOrchestrationActionInput(s,t);
  src.timing : dateTime -> tgt.timing;
  src.timing : Age -> tgt.timing;
  src.timing : Period -> tgt.timing;
  src.timing : Duration -> tgt.timing;
  src.timing : Range -> tgt.timing;
  src.timing : Timing -> tgt.timing;
  src.participant : Reference as s ->  tgt.participant as t,  t.actor = s; // src.location -> tgt.location;
  src.type -> tgt.type;
  src.groupingBehavior as v -> tgt.groupingBehavior = translate(v, '#ActionGroupingBehavior', 'code');
  src.selectionBehavior as v -> tgt.selectionBehavior = translate(v, '#ActionSelectionBehavior', 'code');
  src.requiredBehavior as v -> tgt.requiredBehavior = translate(v, '#ActionRequiredBehavior', 'code');
  src.precheckBehavior as v -> tgt.precheckBehavior = translate(v, '#ActionPrecheckBehavior', 'code');
  src.cardinalityBehavior as v -> tgt.cardinalityBehavior = translate(v, '#ActionCardinalityBehavior', 'code');
  src.resource -> tgt.resource;
}

group RequestOrchestrationActionCondition(source src, target tgt) extends BackboneElement {
  src.kind as v -> tgt.kind = translate(v, '#ActionConditionKind', 'code');
  src.expression -> tgt.expression;
}

// group RequestOrchestrationActionInput(source src, target tgt) extends BackboneElement {
// src.title -> tgt.title;
// src.requirement -> tgt.requirement;
// src.relatedData -> tgt.relatedData;
// }
// group RequestOrchestrationActionOutput(source src, target tgt) extends BackboneElement {
// src.title -> tgt.title;
// src.requirement -> tgt.requirement;
// src.relatedData -> tgt.relatedData;
// }
group RequestOrchestrationActionRelatedAction(source src, target tgt) extends BackboneElement {
  src.actionId -> tgt.targetId;
  src.relationship as v -> tgt.relationship = translate(v, '#ActionRelationshipType', 'code');
  src.offset : Duration -> tgt.offset; // src.endRelationship as v -> tgt.endRelationship = translate(v, '#ActionRelationshipType', 'code');
  src.offset : Range -> tgt.offset;
}

# entry[].resource.title
FML Conversion for RequestOrchestration: R4B to R5

# entry[].resource.group[].rule[].documentation
src.input as s -> tgt.input as t then RequestOrchestrationActionInput(s,t);

# entry[].resource.group[].documentation
group RequestOrchestrationActionInput(source src, target tgt) extends BackboneElement {
src.title -> tgt.title;
src.requirement -> tgt.requirement;
src.relatedData -> tgt.relatedData;
}
group RequestOrchestrationActionOutput(source src, target tgt) extends BackboneElement {
src.title -> tgt.title;
src.requirement -> tgt.requirement;
src.relatedData -> tgt.relatedData;
}

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/UsageContext4to5'
/// name = 'UsageContext4to5'
/// title = 'FML Conversion for UsageContext: R4 to R5'
/// status = 'active'

uses "http://hl7.org/fhir/4.0/StructureDefinition/UsageContext" alias UsageContextR4 as source
uses "http://hl7.org/fhir/StructureDefinition/UsageContext" alias UsageContextR5 as target

imports "http://hl7.org/fhir/StructureMap/*4to5"

group UsageContext(source src : UsageContextR4, target tgt : UsageContextR5) extends Element <<type+>> {
  src.code -> tgt.code;
  src.value : CodeableConcept -> tgt.value;
  src.value : Quantity -> tgt.value;
  src.value : Range -> tgt.value;
  src.value : Reference -> tgt.value;
}

# entry[].resource.title
FML Conversion for UsageContext: R4 to R5

# entry[].resource.text.div
/// url = 'http://hl7.org/fhir/StructureMap/Appointment5to4'
/// name = 'Appointment5to4'
/// title = 'FML Conversion for Appointment: R5 to R4'
/// status = 'active'

conceptmap "AppointmentStatus" {
  prefix s = "http://hl7.org/fhir/appointmentstatus"
  prefix t = "http://hl7.org/fhir/4.0/appointmentstatus"

  s:proposed - t:proposed
  s:pending - t:pending
  s:booked - t:booked
  s:arrived - t:arrived
  s:fulfilled - t:fulfilled
  s:cancelled - t:cancelled
  s:noshow - t:noshow
  s:"entered-in-error" - t:"entered-in-error"
  s:"checked-in" - t:"checked-in"
  s:waitlist - t:waitlist
}

conceptmap "ParticipationStatus" {
  prefix s = "http://hl7.org/fhir/participationstatus"
  prefix t = "http://hl7.org/fhir/4.0/participationstatus"

  s:accepted - t:accepted
  s:declined - t:declined
  s:tentative - t:tentative
  s:"needs-action" - t:"needs-action"
}

uses "http://hl7.org/fhir/StructureDefinition/Appointment" alias AppointmentR5 as source
uses "http://hl7.org/fhir/4.0/StructureDefinition/Appointment" alias AppointmentR4 as target

imports "http://hl7.org/fhir/StructureMap/*5to4"

group Appointment(source src : AppointmentR5, target tgt : AppointmentR4) extends DomainResource <<type+>> {
  src.identifier -> tgt.identifier;
  src.status as v -> tgt.status = translate(v, '#AppointmentStatus', 'code');
  src.cancellationReason -> tgt.cancelationReason;
  src.serviceCategory -> tgt.serviceCategory; // src.class -> tgt.class;
  src.serviceType -> tgt.serviceType;
  src.specialty -> tgt.specialty;
  src.appointmentType -> tgt.appointmentType;
  src.reason -> tgt.reasonCode;
  src.description -> tgt.description; // src.priority -> tgt.priority; // CodeableConcept.text -> unsignedInt ??
  src.supportingInformation -> tgt.supportingInformation; // src.replaces -> tgt.replaces;
  src.start -> tgt.start; // src.previousAppointment -> tgt.previousAppointment;
  src.end -> tgt.end;
  src.minutesDuration -> tgt.minutesDuration;
  src.requestedPeriod -> tgt.requestedPeriod;
  src.slot -> tgt.slot;
  src.created -> tgt.created; // src.account -> tgt.account;
  src.note -> tgt.comment; // src.cancellationDate -> tgt.cancellationDate;
  src.patientInstruction -> tgt.patientInstruction;
  src.basedOn -> tgt.basedOn;
  src.participant as s -> tgt.participant as t then AppointmentParticipant(s, t); // src.subject -> tgt.subject;
}

group AppointmentParticipant(source src, target tgt) extends BackboneElement {
  src.type -> tgt.type;
  src.period -> tgt.period;
  src.actor -> tgt.actor;
  src.required as s where required = 'true' ->  tgt.required as t,  t.value = 'required';
  src.required as s where required = 'false' ->  tgt.required as t,  t.value = 'optional';
  src.status as v -> tgt.status = translate(v, '#ParticipationStatus', 'code');
}

# entry[].resource.title
FML Conversion for Appointment: R5 to R4

# entry[].resource.group[].rule[].documentation
src.priority -> tgt.priority; // CodeableConcept.text -> unsignedInt ??