Search in sources :

Example 61 with StructureMap

use of org.hl7.fhir.r4b.model.StructureMap in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method parseRule.

private void parseRule(StructureMap map, List<StructureMapGroupRuleComponent> list, FHIRLexer lexer) throws FHIRException {
    StructureMapGroupRuleComponent rule = new StructureMapGroupRuleComponent();
    list.add(rule);
    rule.setName(lexer.takeDottedToken());
    lexer.token(":");
    lexer.token("for");
    boolean done = false;
    while (!done) {
        parseSource(rule, lexer);
        done = !lexer.hasToken(",");
        if (!done)
            lexer.next();
    }
    if (lexer.hasToken("make")) {
        lexer.token("make");
        done = false;
        while (!done) {
            parseTarget(rule, lexer);
            done = !lexer.hasToken(",");
            if (!done)
                lexer.next();
        }
    }
    if (lexer.hasToken("then")) {
        lexer.token("then");
        if (lexer.hasToken("{")) {
            lexer.token("{");
            if (lexer.hasComment()) {
                rule.setDocumentation(lexer.take().substring(2).trim());
            }
            lexer.skipComments();
            while (!lexer.hasToken("}")) {
                if (lexer.done())
                    throw lexer.error("premature termination expecting '}' in nested group");
                parseRule(map, rule.getRule(), lexer);
            }
            lexer.token("}");
        } else {
            done = false;
            while (!done) {
                parseRuleReference(rule, lexer);
                done = !lexer.hasToken(",");
                if (!done)
                    lexer.next();
            }
        }
    } else if (lexer.hasComment()) {
        rule.setDocumentation(lexer.take().substring(2).trim());
    }
    if (isSimpleSyntax(rule)) {
        rule.getSourceFirstRep().setVariable(AUTO_VAR_NAME);
        rule.getTargetFirstRep().setVariable(AUTO_VAR_NAME);
        // with no parameter - e.g. imply what is to be created
        rule.getTargetFirstRep().setTransform(StructureMapTransform.CREATE);
    // no dependencies - imply what is to be done based on types
    }
    lexer.skipComments();
}
Also used : StructureMapGroupRuleComponent(org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupRuleComponent)

Example 62 with StructureMap

use of org.hl7.fhir.r4b.model.StructureMap in project org.hl7.fhir.core by hapifhir.

the class RdfParser method composeStructureMapStructureMapStructureComponent.

protected void composeStructureMapStructureMapStructureComponent(Complex parent, String parentType, String name, StructureMap.StructureMapStructureComponent element, int index) {
    if (element == null)
        return;
    Complex t;
    if (Utilities.noString(parentType))
        t = parent;
    else {
        t = parent.predicate("fhir:" + parentType + '.' + name);
    }
    composeBackboneElement(t, "structure", name, element, index);
    if (element.hasUrlElement())
        composeCanonical(t, "StructureMap", "url", element.getUrlElement(), -1);
    if (element.hasModeElement())
        composeEnum(t, "StructureMap", "mode", element.getModeElement(), -1);
    if (element.hasAliasElement())
        composeString(t, "StructureMap", "alias", element.getAliasElement(), -1);
    if (element.hasDocumentationElement())
        composeString(t, "StructureMap", "documentation", element.getDocumentationElement(), -1);
}
Also used : Complex(org.hl7.fhir.r4.utils.formats.Turtle.Complex)

Example 63 with StructureMap

use of org.hl7.fhir.r4b.model.StructureMap in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilitiesTest method testParseRuleName.

@Test
public void testParseRuleName() throws IOException, FHIRException {
    StructureMapUtilities scu = new StructureMapUtilities(context, this);
    String fileMap = TestingUtilities.loadTestResource("r5", "structure-mapping", "ActivityDefinition.map");
    StructureMap structureMap = scu.parse(fileMap, "ActivityDefinition3To4");
    // StructureMap/ActivityDefinition3to4: StructureMap.group[3].rule[2].name error id value '"expression"' is not valid
    Assertions.assertEquals("expression", structureMap.getGroup().get(2).getRule().get(1).getName());
}
Also used : StructureMap(org.hl7.fhir.r4b.model.StructureMap) StructureMapUtilities(org.hl7.fhir.r4b.utils.structuremap.StructureMapUtilities) Test(org.junit.jupiter.api.Test)

Example 64 with StructureMap

use of org.hl7.fhir.r4b.model.StructureMap in project org.hl7.fhir.core by hapifhir.

the class MappingSheetParser method getStructureMap.

public StructureMap getStructureMap() throws FHIRException {
    StructureMap map = new StructureMap();
    loadMetadata(map);
    if (metadata.containsKey("copyright"))
        map.setCopyright(metadata.get("copyright"));
    StructureMapGroupComponent grp = map.addGroup();
    for (MappingRow row : rows) {
        StructureMapGroupRuleComponent rule = grp.addRule();
        rule.setName(row.getSequence());
        StructureMapGroupRuleSourceComponent src = rule.getSourceFirstRep();
        src.setContext("src");
        src.setElement(row.getIdentifier());
        src.setMin(row.getCardinalityMin());
        src.setMax(row.getCardinalityMax());
        src.setType(row.getDataType());
        src.addExtension(ToolingExtensions.EXT_MAPPING_NAME, new StringType(row.getName()));
        if (row.getCondition() != null) {
            src.setCheck(processCondition(row.getCondition()));
        }
        StructureMapGroupRuleTargetComponent tgt = rule.getTargetFirstRep();
        tgt.setContext("tgt");
        tgt.setElement(row.getAttribute());
        tgt.addExtension(ToolingExtensions.EXT_MAPPING_TGTTYPE, new StringType(row.getType()));
        tgt.addExtension(ToolingExtensions.EXT_MAPPING_TGTCARD, new StringType(row.getMinMax()));
        if (row.getDtMapping() != null) {
            src.setVariable("s");
            tgt.setVariable("t");
            tgt.setTransform(StructureMapTransform.CREATE);
            StructureMapGroupRuleDependentComponent dep = rule.addDependent();
            dep.setName(row.getDtMapping());
            dep.addParameter().setValue(new IdType("s"));
            dep.addParameter().setValue(new IdType("t"));
        } else if (row.getVocabMapping() != null) {
            tgt.setTransform(StructureMapTransform.TRANSLATE);
            tgt.addParameter().setValue(new StringType(row.getVocabMapping()));
            tgt.addParameter().setValue(new IdType("src"));
        } else {
            tgt.setTransform(StructureMapTransform.COPY);
        }
        rule.setDocumentation(row.getComments());
        if (row.getDerived() != null) {
            tgt = rule.addTarget();
            tgt.setContext("tgt");
            tgt.setElement(row.getDerived());
            tgt.setTransform(StructureMapTransform.COPY);
            tgt.addParameter().setValue(new StringType(row.getDerivedMapping()));
        }
    }
    return map;
}
Also used : StructureMap(org.hl7.fhir.r5.model.StructureMap) StructureMapGroupComponent(org.hl7.fhir.r5.model.StructureMap.StructureMapGroupComponent) StringType(org.hl7.fhir.r5.model.StringType) StructureMapGroupRuleDependentComponent(org.hl7.fhir.r5.model.StructureMap.StructureMapGroupRuleDependentComponent) StructureMapGroupRuleComponent(org.hl7.fhir.r5.model.StructureMap.StructureMapGroupRuleComponent) StructureMapGroupRuleSourceComponent(org.hl7.fhir.r5.model.StructureMap.StructureMapGroupRuleSourceComponent) StructureMapGroupRuleTargetComponent(org.hl7.fhir.r5.model.StructureMap.StructureMapGroupRuleTargetComponent) IdType(org.hl7.fhir.r5.model.IdType)

Example 65 with StructureMap

use of org.hl7.fhir.r4b.model.StructureMap in project org.hl7.fhir.core by hapifhir.

the class R3R4ConversionTests method test.

@SuppressWarnings("deprecation")
@ParameterizedTest(name = "{index}: id {0}")
@MethodSource("data")
public void test(String name, byte[] content) throws Exception {
    checkLoad();
    StructureMapUtilities smu4 = new StructureMapUtilities(contextR4, this);
    StructureMapUtilities smu3 = new StructureMapUtilities(contextR3, this);
    String tn = null;
    workingid = null;
    byte[] cnt = content;
    Exception executionError = null;
    List<ValidationMessage> r4validationErrors = new ArrayList<ValidationMessage>();
    String roundTripError = null;
    try {
        extras = new ArrayList<Resource>();
        // load the example (r3)
        org.hl7.fhir.r4.elementmodel.Element r3 = new org.hl7.fhir.r4.elementmodel.XmlParser(contextR3).parse(new ByteArrayInputStream(content));
        tn = r3.fhirType();
        workingid = r3.getChildValue("id");
        if (SAVING) {
            ByteArrayOutputStream bso = new ByteArrayOutputStream();
            new org.hl7.fhir.r4.elementmodel.JsonParser(contextR3).compose(r3, bso, OutputStyle.PRETTY, null);
            cnt = bso.toByteArray();
            Utilities.createDirectory(Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "test-output"));
            TextFile.bytesToFile(cnt, Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "test-output", tn + "-" + workingid + ".input.json"));
        }
        String mapFile = Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "R3toR4", r3.fhirType() + ".map");
        if (new File(mapFile).exists()) {
            StructureMap sm = smu4.parse(TextFile.fileToString(mapFile), mapFile);
            tn = smu4.getTargetType(sm).getType();
            // convert from r3 to r4
            Resource r4 = ResourceFactory.createResource(tn);
            smu4.transform(contextR4, r3, sm, r4);
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            new org.hl7.fhir.r4.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(bs, r4);
            if (SAVING) {
                TextFile.bytesToFile(bs.toByteArray(), Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "test-output", tn + "-" + workingid + ".r4.json"));
                for (Resource r : extras) {
                    bs = new ByteArrayOutputStream();
                    new org.hl7.fhir.r4.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(bs, r);
                    TextFile.bytesToFile(bs.toByteArray(), Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "test-output", r.fhirType() + "-" + r.getId() + ".r4.json"));
                }
            }
            // validate against R4
            IResourceValidator validator = contextR4.newValidator();
            validator.setNoTerminologyChecks(true);
            validator.setFetcher(this);
            validator.validate(null, r4validationErrors, r4);
            // load the R4 to R3 map
            mapFile = Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "R4toR3", getMapFor(r4.fhirType(), r3.fhirType()) + ".map");
            sm = smu3.parse(TextFile.fileToString(mapFile), mapFile);
            // convert to R3
            StructureDefinition sd = smu3.getTargetType(sm);
            org.hl7.fhir.r4.elementmodel.Element ro3 = Manager.build(contextR3, sd);
            smu3.transform(contextR3, r4, sm, ro3);
            // compare the XML
            bs = new ByteArrayOutputStream();
            new org.hl7.fhir.r4.elementmodel.JsonParser(contextR3).compose(ro3, bs, OutputStyle.PRETTY, null);
            if (SAVING)
                TextFile.bytesToFile(bs.toByteArray(), Utilities.path(TestingUtilities.home(), "implementations", "r3maps", "test-output", tn + "-" + workingid + ".output.json"));
            // check(errors, tn, workingid);
            roundTripError = TestingUtilities.checkJsonSrcIsSame(new String(cnt), new String(bs.toByteArray()), filter != null);
            if (roundTripError != null && roundTripError.equals(rules.getStringProperty(tn + "/" + workingid, "roundtrip")))
                roundTripError = null;
        } else {
            if (loadErrors.containsKey(r3.fhirType() + ".map")) {
                executionError = loadErrors.get(r3.fhirType() + ".map");
            }
        }
    } catch (Exception e) {
        executionError = e;
    }
    if (tn != null && workingid != null)
        updateOutcomes(tn, workingid, executionError, r4validationErrors, roundTripError);
    if (executionError != null)
        throw executionError;
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) IResourceValidator(org.hl7.fhir.r4.utils.validation.IResourceValidator) ArrayList(java.util.ArrayList) Resource(org.hl7.fhir.r4.model.Resource) MetadataResource(org.hl7.fhir.r4.model.MetadataResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StructureMapUtilities(org.hl7.fhir.r4.utils.StructureMapUtilities) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureMap(org.hl7.fhir.r4.model.StructureMap) StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) ByteArrayInputStream(java.io.ByteArrayInputStream) Element(org.hl7.fhir.r4.elementmodel.Element) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)69 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)23 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)17 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)16 StructureMap (org.hl7.fhir.r4b.model.StructureMap)13 StructureMap (org.hl7.fhir.r5.model.StructureMap)13 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 StructureMap (org.hl7.fhir.r4.model.StructureMap)11 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)11 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)10 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)9 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)9 StructureMap (org.hl7.fhir.dstu3.model.StructureMap)8 File (java.io.File)7 UriType (org.hl7.fhir.r4.model.UriType)7 Test (org.junit.jupiter.api.Test)7 Base (org.hl7.fhir.dstu3.model.Base)6 TextFile (org.hl7.fhir.utilities.TextFile)6 FileOutputStream (java.io.FileOutputStream)5