Search in sources :

Example 11 with IMP

use of org.hl7.fhir.r4.model.codesystems.V3ActCode.IMP in project pathling by aehrc.

the class MemberOfFunctionTest method memberOfCoding.

@Test
void memberOfCoding() {
    final Coding coding1 = new Coding(MY_VALUE_SET_URL, "AMB", "ambulatory");
    final Coding coding2 = new Coding(MY_VALUE_SET_URL, "EMER", null);
    final Coding coding3 = new Coding(MY_VALUE_SET_URL, "IMP", "inpatient encounter");
    final Coding coding4 = new Coding(MY_VALUE_SET_URL, "IMP", null);
    final Coding coding5 = new Coding(MY_VALUE_SET_URL, "ACUTE", "inpatient acute");
    final Optional<ElementDefinition> optionalDefinition = FhirHelpers.getChildOfResource(fhirContext, "Encounter", "class");
    assertTrue(optionalDefinition.isPresent());
    final ElementDefinition definition = optionalDefinition.get();
    final Dataset<Row> inputDataset = new DatasetBuilder(spark).withIdColumn().withEidColumn().withStructTypeColumns(codingStructType()).withRow("encounter-1", makeEid(1), rowFromCoding(coding1)).withRow("encounter-1", makeEid(0), rowFromCoding(coding5)).withRow("encounter-2", makeEid(0), rowFromCoding(coding2)).withRow("encounter-3", makeEid(0), rowFromCoding(coding3)).withRow("encounter-4", makeEid(0), rowFromCoding(coding4)).withRow("encounter-5", makeEid(0), rowFromCoding(coding5)).withRow("encounter-6", null, null).buildWithStructValue();
    final CodingPath inputExpression = (CodingPath) new ElementPathBuilder(spark).dataset(inputDataset).idAndEidAndValueColumns().expression("Encounter.class").singular(false).definition(definition).buildDefined();
    final StringLiteralPath argumentExpression = StringLiteralPath.fromString("'" + MY_VALUE_SET_URL + "'", inputExpression);
    // Setup mocks
    when(terminologyService.intersect(any(), any())).thenReturn(setOfSimpleFrom(coding2, coding5));
    // Prepare the inputs to the function.
    final ParserContext parserContext = new ParserContextBuilder(spark, fhirContext).idColumn(inputExpression.getIdColumn()).terminologyClientFactory(terminologyServiceFactory).build();
    final NamedFunctionInput memberOfInput = new NamedFunctionInput(parserContext, inputExpression, Collections.singletonList(argumentExpression));
    // Invoke the function.
    final FhirPath result = new MemberOfFunction().invoke(memberOfInput);
    // The outcome is somehow random with regard to the sequence passed to MemberOfMapperAnswerer.
    final Dataset<Row> expectedResult = new DatasetBuilder(spark).withIdColumn().withEidColumn().withColumn(DataTypes.BooleanType).withRow("encounter-1", makeEid(0), true).withRow("encounter-1", makeEid(1), false).withRow("encounter-2", makeEid(0), true).withRow("encounter-3", makeEid(0), false).withRow("encounter-4", makeEid(0), false).withRow("encounter-5", makeEid(0), true).withRow("encounter-6", null, null).build();
    // Check the result.
    assertThat(result).hasExpression("Encounter.class.memberOf('" + MY_VALUE_SET_URL + "')").isElementPath(BooleanPath.class).hasFhirType(FHIRDefinedType.BOOLEAN).isNotSingular().selectOrderedResultWithEid().hasRows(expectedResult);
    verify(terminologyService).intersect(eq(MY_VALUE_SET_URL), eq(setOfSimpleFrom(coding1, coding2, coding3, coding4, coding5)));
    verifyNoMoreInteractions(terminologyService);
}
Also used : CodingPath(au.csiro.pathling.fhirpath.element.CodingPath) StringLiteralPath(au.csiro.pathling.fhirpath.literal.StringLiteralPath) FhirPath(au.csiro.pathling.fhirpath.FhirPath) ParserContextBuilder(au.csiro.pathling.test.builders.ParserContextBuilder) NamedFunctionInput(au.csiro.pathling.fhirpath.function.NamedFunctionInput) Coding(org.hl7.fhir.r4.model.Coding) SparkHelpers.rowFromCoding(au.csiro.pathling.test.helpers.SparkHelpers.rowFromCoding) ElementDefinition(au.csiro.pathling.fhirpath.element.ElementDefinition) Row(org.apache.spark.sql.Row) DatasetBuilder(au.csiro.pathling.test.builders.DatasetBuilder) ParserContext(au.csiro.pathling.fhirpath.parser.ParserContext) ElementPathBuilder(au.csiro.pathling.test.builders.ElementPathBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 12 with IMP

use of org.hl7.fhir.r4.model.codesystems.V3ActCode.IMP in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method determineTypeFromSourceType.

private String determineTypeFromSourceType(StructureMap map, StructureMapGroupComponent source, Base base, String[] types) throws FHIRException {
    String type = base.fhirType();
    String kn = "type^" + type;
    if (source.hasUserData(kn))
        return source.getUserString(kn);
    ResolvedGroup res = new ResolvedGroup();
    res.targetMap = null;
    res.target = null;
    for (StructureMapGroupComponent grp : map.getGroup()) {
        if (matchesByType(map, grp, type)) {
            if (res.targetMap == null) {
                res.targetMap = map;
                res.target = grp;
            } else
                throw new FHIRException("Multiple possible matches looking for default rule for '" + type + "'");
        }
    }
    if (res.targetMap != null) {
        String result = getActualType(res.targetMap, res.target.getInput().get(1).getType());
        source.setUserData(kn, result);
        return result;
    }
    for (UriType imp : map.getImport()) {
        List<StructureMap> impMapList = findMatchingMaps(imp.getValue());
        if (impMapList.size() == 0)
            throw new FHIRException("Unable to find map(s) for " + imp.getValue());
        for (StructureMap impMap : impMapList) {
            if (!impMap.getUrl().equals(map.getUrl())) {
                for (StructureMapGroupComponent grp : impMap.getGroup()) {
                    if (matchesByType(impMap, grp, type)) {
                        if (res.targetMap == null) {
                            res.targetMap = impMap;
                            res.target = grp;
                        } else
                            throw new FHIRException("Multiple possible matches for default rule for '" + type + "' in " + res.targetMap.getUrl() + " (" + res.target.getName() + ") and " + impMap.getUrl() + " (" + grp.getName() + ")");
                    }
                }
            }
        }
    }
    if (res.target == null)
        throw new FHIRException("No matches found for default rule for '" + type + "' from " + map.getUrl());
    // should be .getType, but R2...
    String result = getActualType(res.targetMap, res.target.getInput().get(1).getType());
    source.setUserData(kn, result);
    return result;
}
Also used : StructureMap(org.hl7.fhir.r5.model.StructureMap) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 13 with IMP

use of org.hl7.fhir.r4.model.codesystems.V3ActCode.IMP in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method resolveGroupReference.

private ResolvedGroup resolveGroupReference(StructureMap map, StructureMapGroupComponent source, String name) throws FHIRException {
    String kn = "ref^" + name;
    if (source.hasUserData(kn))
        return (ResolvedGroup) source.getUserData(kn);
    ResolvedGroup res = new ResolvedGroup();
    res.targetMap = null;
    res.target = null;
    for (StructureMapGroupComponent grp : map.getGroup()) {
        if (grp.getName().equals(name)) {
            if (res.targetMap == null) {
                res.targetMap = map;
                res.target = grp;
            } else
                throw new FHIRException("Multiple possible matches for rule '" + name + "'");
        }
    }
    if (res.targetMap != null) {
        source.setUserData(kn, res);
        return res;
    }
    for (UriType imp : map.getImport()) {
        List<StructureMap> impMapList = findMatchingMaps(imp.getValue());
        if (impMapList.size() == 0)
            throw new FHIRException("Unable to find map(s) for " + imp.getValue());
        for (StructureMap impMap : impMapList) {
            if (!impMap.getUrl().equals(map.getUrl())) {
                for (StructureMapGroupComponent grp : impMap.getGroup()) {
                    if (grp.getName().equals(name)) {
                        if (res.targetMap == null) {
                            res.targetMap = impMap;
                            res.target = grp;
                        } else
                            throw new FHIRException("Multiple possible matches for rule '" + name + "' in " + res.targetMap.getUrl() + " and " + impMap.getUrl());
                    }
                }
            }
        }
    }
    if (res.target == null)
        throw new FHIRException("No matches found for rule '" + name + "'. Reference found in " + map.getUrl());
    source.setUserData(kn, res);
    return res;
}
Also used : StructureMapGroupComponent(org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupComponent) StructureMap(org.hl7.fhir.dstu3.model.StructureMap) FHIRException(org.hl7.fhir.exceptions.FHIRException) UriType(org.hl7.fhir.dstu3.model.UriType)

Example 14 with IMP

use of org.hl7.fhir.r4.model.codesystems.V3ActCode.IMP in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method resolveGroupByTypes.

private ResolvedGroup resolveGroupByTypes(StructureMap map, String ruleid, StructureMapGroupComponent source, String srcType, String tgtType) throws FHIRException {
    String kn = "types^" + srcType + ":" + tgtType;
    if (source.hasUserData(kn))
        return (ResolvedGroup) source.getUserData(kn);
    ResolvedGroup res = new ResolvedGroup();
    res.targetMap = null;
    res.target = null;
    for (StructureMapGroupComponent grp : map.getGroup()) {
        if (matchesByType(map, grp, srcType, tgtType)) {
            if (res.targetMap == null) {
                res.targetMap = map;
                res.target = grp;
            } else
                throw new FHIRException("Multiple possible matches looking for rule for '" + srcType + "/" + tgtType + "', from rule '" + ruleid + "'");
        }
    }
    if (res.targetMap != null) {
        source.setUserData(kn, res);
        return res;
    }
    for (UriType imp : map.getImport()) {
        List<StructureMap> impMapList = findMatchingMaps(imp.getValue());
        if (impMapList.size() == 0)
            throw new FHIRException("Unable to find map(s) for " + imp.getValue());
        for (StructureMap impMap : impMapList) {
            if (!impMap.getUrl().equals(map.getUrl())) {
                for (StructureMapGroupComponent grp : impMap.getGroup()) {
                    if (matchesByType(impMap, grp, srcType, tgtType)) {
                        if (res.targetMap == null) {
                            res.targetMap = impMap;
                            res.target = grp;
                        } else
                            throw new FHIRException("Multiple possible matches for rule for '" + srcType + "/" + tgtType + "' in " + res.targetMap.getUrl() + " and " + impMap.getUrl() + ", from rule '" + ruleid + "'");
                    }
                }
            }
        }
    }
    if (res.target == null)
        throw new FHIRException("No matches found for rule for '" + srcType + "/" + tgtType + "' from " + map.getUrl() + ", from rule '" + ruleid + "'");
    source.setUserData(kn, res);
    return res;
}
Also used : StructureMapGroupComponent(org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupComponent) StructureMap(org.hl7.fhir.dstu3.model.StructureMap) FHIRException(org.hl7.fhir.exceptions.FHIRException) UriType(org.hl7.fhir.dstu3.model.UriType)

Example 15 with IMP

use of org.hl7.fhir.r4.model.codesystems.V3ActCode.IMP in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method determineTypeFromSourceType.

private String determineTypeFromSourceType(StructureMap map, StructureMapGroupComponent source, Base base, String[] types) throws FHIRException {
    String type = base.fhirType();
    String kn = "type^" + type;
    if (source.hasUserData(kn))
        return source.getUserString(kn);
    ResolvedGroup res = new ResolvedGroup();
    res.targetMap = null;
    res.target = null;
    for (StructureMapGroupComponent grp : map.getGroup()) {
        if (matchesByType(map, grp, type)) {
            if (res.targetMap == null) {
                res.targetMap = map;
                res.target = grp;
            } else
                throw new FHIRException("Multiple possible matches looking for default rule for '" + type + "'");
        }
    }
    if (res.targetMap != null) {
        String result = getActualType(res.targetMap, res.target.getInput().get(1).getType());
        source.setUserData(kn, result);
        return result;
    }
    for (UriType imp : map.getImport()) {
        List<StructureMap> impMapList = findMatchingMaps(imp.getValue());
        if (impMapList.size() == 0)
            throw new FHIRException("Unable to find map(s) for " + imp.getValue());
        for (StructureMap impMap : impMapList) {
            if (!impMap.getUrl().equals(map.getUrl())) {
                for (StructureMapGroupComponent grp : impMap.getGroup()) {
                    if (matchesByType(impMap, grp, type)) {
                        if (res.targetMap == null) {
                            res.targetMap = impMap;
                            res.target = grp;
                        } else
                            throw new FHIRException("Multiple possible matches for default rule for '" + type + "' in " + res.targetMap.getUrl() + " (" + res.target.getName() + ") and " + impMap.getUrl() + " (" + grp.getName() + ")");
                    }
                }
            }
        }
    }
    if (res.target == null)
        throw new FHIRException("No matches found for default rule for '" + type + "' from " + map.getUrl());
    // should be .getType, but R2...
    String result = getActualType(res.targetMap, res.target.getInput().get(1).getType());
    source.setUserData(kn, result);
    return result;
}
Also used : StructureMapGroupComponent(org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupComponent) StructureMap(org.hl7.fhir.dstu3.model.StructureMap) FHIRException(org.hl7.fhir.exceptions.FHIRException) UriType(org.hl7.fhir.dstu3.model.UriType)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)13 ArrayList (java.util.ArrayList)4 UriType (org.hl7.fhir.r4.model.UriType)4 UriType (org.hl7.fhir.dstu2016may.model.UriType)3 StructureMap (org.hl7.fhir.dstu3.model.StructureMap)3 StructureMapGroupComponent (org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupComponent)3 UriType (org.hl7.fhir.dstu3.model.UriType)3 StructureMap (org.hl7.fhir.r4.model.StructureMap)3 StructureMapGroupComponent (org.hl7.fhir.r4.model.StructureMap.StructureMapGroupComponent)3 StructureMap (org.hl7.fhir.r4b.model.StructureMap)3 StructureMap (org.hl7.fhir.r5.model.StructureMap)3 UriType (org.hl7.fhir.dstu2.model.UriType)2 ConceptSetComponent (org.hl7.fhir.dstu2.model.ValueSet.ConceptSetComponent)2 ConceptSetComponent (org.hl7.fhir.dstu2016may.model.ValueSet.ConceptSetComponent)2 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)2 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)2 FhirPath (au.csiro.pathling.fhirpath.FhirPath)1 CodingPath (au.csiro.pathling.fhirpath.element.CodingPath)1 ElementDefinition (au.csiro.pathling.fhirpath.element.ElementDefinition)1 NamedFunctionInput (au.csiro.pathling.fhirpath.function.NamedFunctionInput)1