Skip to content

Commit

Permalink
Extract more specific types in OnlyRules with target profiles (#229)
Browse files Browse the repository at this point in the history
Reference, CodeableReference, and canonical types can all be specified
with target profiles. When extracting an OnlyRule, set the corresponding
boolean value on the extracted type along with the target profiles. This
takes advantage of the newly available isCodeableReference flag on
SUSHI's OnlyRuleType.
  • Loading branch information
mint-thompson committed Jul 17, 2023
1 parent 4f1c7c0 commit 67472b7
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/extractor/OnlyRuleExtractor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fshrules } from 'fsh-sushi';
import { ProcessableElementDefinition } from '../processor';
import { ExportableOnlyRule } from '../exportable';
import { getPath } from '../utils';
Expand All @@ -9,9 +10,17 @@ export class OnlyRuleExtractor {
}
const onlyRule = new ExportableOnlyRule(getPath(input));
input.type.forEach((t, i) => {
if (['Reference', 'CodeableReference'].includes(t.code) && t.targetProfile) {
if (['Reference', 'CodeableReference', 'canonical'].includes(t.code) && t.targetProfile) {
const targeting: Partial<fshrules.OnlyRuleType> = {};
if (t.code === 'Reference') {
targeting.isReference = true;
} else if (t.code === 'CodeableReference') {
targeting.isCodeableReference = true;
} else {
targeting.isCanonical = true;
}
t.targetProfile.forEach((tp, tpi) => {
onlyRule.types.push({ type: tp, isReference: true });
onlyRule.types.push(Object.assign({ type: tp }, targeting));
input.processedPaths.push(`type[${i}].targetProfile[${tpi}]`);
});
} else if (t.profile) {
Expand Down
100 changes: 98 additions & 2 deletions test/extractor/OnlyRuleExtractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ describe('OnlyRuleExtractor', () => {
const onlyRule = OnlyRuleExtractor.process(element);
const expectedRule = new ExportableOnlyRule('activity.performedActivity');
expectedRule.types = [
{ type: 'http://hl7.org/fhir/StructureDefinition/Observation', isReference: true },
{ type: 'http://hl7.org/fhir/StructureDefinition/MolecularSequence', isReference: true }
{ type: 'http://hl7.org/fhir/StructureDefinition/Observation', isCodeableReference: true },
{
type: 'http://hl7.org/fhir/StructureDefinition/MolecularSequence',
isCodeableReference: true
}
];
expect(onlyRule).toEqual<ExportableOnlyRule>(expectedRule);
expect(element.processedPaths).toEqual([
Expand All @@ -107,6 +110,99 @@ describe('OnlyRuleExtractor', () => {
expect(element.processedPaths).toEqual(['type[0].code', 'type[1].profile[0]', 'type[1].code']);
});

it('should extract an only rule with a subset of a canonical type', () => {
const element = ProcessableElementDefinition.fromJSON(
sdWithCodeableReference.differential.element[4]
);
const onlyRule = OnlyRuleExtractor.process(element);
const expectedRule = new ExportableOnlyRule('extension[toast].value[x]');
expectedRule.types = [
{ type: 'http://hl7.org/fhir/StructureDefinition/Observation', isCanonical: true },
{
type: 'http://hl7.org/fhir/StructureDefinition/Patient',
isCanonical: true
}
];
expect(onlyRule).toEqual<ExportableOnlyRule>(expectedRule);
expect(element.processedPaths).toEqual([
'type[0].targetProfile[0]',
'type[0].targetProfile[1]',
'type[0].code'
]);
});

it('should extract an only rule on an element with both Reference and CodeableReference types', () => {
const element = ProcessableElementDefinition.fromJSON(
sdWithCodeableReference.differential.element[2]
);
const onlyRule = OnlyRuleExtractor.process(element);
const expectedRule = new ExportableOnlyRule('extension[bar].value[x]');
expectedRule.types = [
{ type: 'http://hl7.org/fhir/StructureDefinition/Practitioner', isReference: true },
{ type: 'http://hl7.org/fhir/StructureDefinition/Patient', isReference: true },
{ type: 'http://hl7.org/fhir/StructureDefinition/Observation', isCodeableReference: true },
{
type: 'http://hl7.org/fhir/StructureDefinition/DiagnosticReport',
isCodeableReference: true
}
];
expect(onlyRule).toEqual<ExportableOnlyRule>(expectedRule);
expect(element.processedPaths).toEqual([
'type[0].targetProfile[0]',
'type[0].targetProfile[1]',
'type[0].code',
'type[1].targetProfile[0]',
'type[1].targetProfile[1]',
'type[1].code'
]);
});

it('should extract an only rule on an element with CodeableReference and non-Reference types', () => {
const element = ProcessableElementDefinition.fromJSON(
sdWithCodeableReference.differential.element[3]
);
const onlyRule = OnlyRuleExtractor.process(element);
const expectedRule = new ExportableOnlyRule('extension[cookie].value[x]');
expectedRule.types = [
{ type: 'string' },
{ type: 'http://hl7.org/fhir/StructureDefinition/Observation', isCodeableReference: true },
{
type: 'http://hl7.org/fhir/StructureDefinition/DiagnosticReport',
isCodeableReference: true
}
];
expect(onlyRule).toEqual<ExportableOnlyRule>(expectedRule);
expect(element.processedPaths).toEqual([
'type[0].code',
'type[1].targetProfile[0]',
'type[1].targetProfile[1]',
'type[1].code'
]);
});

it('should extract an only rule with canonical and non-canonical types', () => {
const element = ProcessableElementDefinition.fromJSON(
sdWithCodeableReference.differential.element[5]
);
const onlyRule = OnlyRuleExtractor.process(element);
const expectedRule = new ExportableOnlyRule('extension[blank].value[x]');
expectedRule.types = [
{ type: 'http://hl7.org/fhir/StructureDefinition/Patient', isCanonical: true },
{
type: 'http://hl7.org/fhir/StructureDefinition/Observation',
isCanonical: true
},
{ type: 'Annotation' }
];
expect(onlyRule).toEqual<ExportableOnlyRule>(expectedRule);
expect(element.processedPaths).toEqual([
'type[0].targetProfile[0]',
'type[0].targetProfile[1]',
'type[0].code',
'type[1].code'
]);
});

it('should return null when the element has no type info', () => {
const element = ProcessableElementDefinition.fromJSON(looseSD.differential.element[4]);
const onlyRule = OnlyRuleExtractor.process(element);
Expand Down
65 changes: 65 additions & 0 deletions test/extractor/fixtures/only-profile-with-codeablereference.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,71 @@
"versioning": "either"
}
]
},
{
"id": "CarePlan.extension:bar.value[x]",
"path": "CarePlan.extension:bar.value[x]",
"type": [
{
"code": "Reference",
"targetProfile": [
"http://hl7.org/fhir/StructureDefinition/Practitioner",
"http://hl7.org/fhir/StructureDefinition/Patient"
]
},
{
"code": "CodeableReference",
"targetProfile": [
"http://hl7.org/fhir/StructureDefinition/Observation",
"http://hl7.org/fhir/StructureDefinition/DiagnosticReport"
]
}
]
},
{
"id": "CarePlan.extension:cookie.value[x]",
"path": "CarePlan.extension:cookie.value[x]",
"type": [
{
"code": "string"
},
{
"code": "CodeableReference",
"targetProfile": [
"http://hl7.org/fhir/StructureDefinition/Observation",
"http://hl7.org/fhir/StructureDefinition/DiagnosticReport"
]
}
]
},
{
"id": "CarePlan.extension:toast.value[x]",
"path": "CarePlan.extension:toast.value[x]",
"type": [
{
"code": "canonical",
"targetProfile": [
"http://hl7.org/fhir/StructureDefinition/Observation",
"http://hl7.org/fhir/StructureDefinition/Patient"
]
}
]
},
{
"id": "CarePlan.extension:blank.value[x]",
"path": "CarePlan.extension:blank.value[x]",
"type": [
{
"code": "canonical",
"targetProfile": [
"http://hl7.org/fhir/StructureDefinition/Patient",
"http://hl7.org/fhir/StructureDefinition/Observation"
]
},
{
"code": "Annotation"
}
]
}
]
}
Expand Down

0 comments on commit 67472b7

Please sign in to comment.