use of org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method parseConceptMap.
private void parseConceptMap(StructureMap result, FHIRLexer lexer) throws FHIRLexerException {
lexer.token("conceptmap");
ConceptMap map = new ConceptMap();
String id = lexer.readConstant("map id");
if (id.startsWith("#")) {
throw lexer.error("Concept Map identifier must start with #");
}
map.setId(id);
// todo: how to add this to the text format
map.setStatus(PublicationStatus.DRAFT);
result.getContained().add(map);
lexer.token("{");
// lexer.token("source");
// map.setSource(new UriType(lexer.readConstant("source")));
// lexer.token("target");
// map.setSource(new UriType(lexer.readConstant("target")));
Map<String, String> prefixes = new HashMap<String, String>();
while (lexer.hasToken("prefix")) {
lexer.token("prefix");
String n = lexer.take();
lexer.token("=");
String v = lexer.readConstant("prefix url");
prefixes.put(n, v);
}
while (lexer.hasToken("unmapped")) {
lexer.token("unmapped");
lexer.token("for");
String n = readPrefix(prefixes, lexer);
ConceptMapGroupComponent g = getGroup(map, n, null);
lexer.token("=");
String v = lexer.take();
if (v.equals("provided")) {
g.getUnmapped().setMode(ConceptMapGroupUnmappedMode.PROVIDED);
} else {
throw lexer.error("Only unmapped mode PROVIDED is supported at this time");
}
}
while (!lexer.hasToken("}")) {
String srcs = readPrefix(prefixes, lexer);
lexer.token(":");
String sc = lexer.getCurrent().startsWith("\"") ? lexer.readConstant("code") : lexer.take();
ConceptMapEquivalence eq = readEquivalence(lexer);
String tgts = (eq != ConceptMapEquivalence.UNMATCHED) ? readPrefix(prefixes, lexer) : "";
ConceptMapGroupComponent g = getGroup(map, srcs, tgts);
SourceElementComponent e = g.addElement();
e.setCode(sc);
if (e.getCode().startsWith("\"")) {
e.setCode(lexer.processConstant(e.getCode()));
}
TargetElementComponent tgt = e.addTarget();
tgt.setEquivalence(eq);
if (tgt.getEquivalence() != ConceptMapEquivalence.UNMATCHED) {
lexer.token(":");
tgt.setCode(lexer.take());
if (tgt.getCode().startsWith("\"")) {
tgt.setCode(lexer.processConstant(tgt.getCode()));
}
}
tgt.setComment(lexer.getFirstComment());
}
lexer.token("}");
}
use of org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method produceConceptMap.
private static void produceConceptMap(StringBuilder b, ConceptMap cm) {
b.append("conceptmap \"");
b.append(cm.getId());
b.append("\" {\r\n");
Map<String, String> prefixesSrc = new HashMap<String, String>();
Map<String, String> prefixesTgt = new HashMap<String, String>();
char prefix = 's';
for (ConceptMapGroupComponent cg : cm.getGroup()) {
if (!prefixesSrc.containsKey(cg.getSource())) {
prefixesSrc.put(cg.getSource(), String.valueOf(prefix));
b.append(" prefix ");
b.append(prefix);
b.append(" = \"");
b.append(cg.getSource());
b.append("\"\r\n");
prefix++;
}
if (!prefixesTgt.containsKey(cg.getTarget())) {
prefixesTgt.put(cg.getTarget(), String.valueOf(prefix));
b.append(" prefix ");
b.append(prefix);
b.append(" = \"");
b.append(cg.getTarget());
b.append("\"\r\n");
prefix++;
}
}
b.append("\r\n");
for (ConceptMapGroupComponent cg : cm.getGroup()) {
if (cg.hasUnmapped()) {
b.append(" unmapped for ");
b.append(prefixesSrc.get(cg.getSource()));
b.append(" = ");
b.append(cg.getUnmapped().getMode().toCode());
b.append("\r\n");
}
}
for (ConceptMapGroupComponent cg : cm.getGroup()) {
for (SourceElementComponent ce : cg.getElement()) {
b.append(" ");
b.append(prefixesSrc.get(cg.getSource()));
b.append(":");
if (Utilities.isToken(ce.getCode())) {
b.append(ce.getCode());
} else {
b.append("\"");
b.append(ce.getCode());
b.append("\"");
}
b.append(" ");
b.append(getChar(ce.getTargetFirstRep().getEquivalence()));
b.append(" ");
b.append(prefixesTgt.get(cg.getTarget()));
b.append(":");
if (Utilities.isToken(ce.getTargetFirstRep().getCode())) {
b.append(ce.getTargetFirstRep().getCode());
} else {
b.append("\"");
b.append(ce.getTargetFirstRep().getCode());
b.append("\"");
}
b.append("\r\n");
}
}
b.append("}\r\n\r\n");
}
use of org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method produceConceptMap.
private static void produceConceptMap(StringBuilder b, ConceptMap cm) {
b.append("conceptmap \"");
b.append(cm.getId());
b.append("\" {\r\n");
Map<String, String> prefixesSrc = new HashMap<String, String>();
Map<String, String> prefixesTgt = new HashMap<String, String>();
char prefix = 's';
for (ConceptMapGroupComponent cg : cm.getGroup()) {
if (!prefixesSrc.containsKey(cg.getSource())) {
prefixesSrc.put(cg.getSource(), String.valueOf(prefix));
b.append(" prefix ");
b.append(prefix);
b.append(" = \"");
b.append(cg.getSource());
b.append("\"\r\n");
prefix++;
}
if (!prefixesTgt.containsKey(cg.getTarget())) {
prefixesTgt.put(cg.getTarget(), String.valueOf(prefix));
b.append(" prefix ");
b.append(prefix);
b.append(" = \"");
b.append(cg.getTarget());
b.append("\"\r\n");
prefix++;
}
}
b.append("\r\n");
for (ConceptMapGroupComponent cg : cm.getGroup()) {
if (cg.hasUnmapped()) {
b.append(" unmapped for ");
b.append(prefix);
b.append(" = ");
b.append(cg.getUnmapped().getMode());
b.append("\r\n");
}
}
for (ConceptMapGroupComponent cg : cm.getGroup()) {
for (SourceElementComponent ce : cg.getElement()) {
b.append(" ");
b.append(prefixesSrc.get(cg.getSource()));
b.append(":");
b.append(ce.getCode());
b.append(" ");
b.append(getChar(ce.getTargetFirstRep().getEquivalence()));
b.append(" ");
b.append(prefixesTgt.get(cg.getTarget()));
b.append(":");
b.append(ce.getTargetFirstRep().getCode());
b.append("\r\n");
}
}
b.append("}\r\n\r\n");
}
use of org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent in project org.hl7.fhir.core by hapifhir.
the class ConceptMapEngineTest method codingTranslate.
@Test
@DisplayName("Coding is translated according to ConceptMap")
void codingTranslate() throws IOException {
final ConceptMap.SourceElementComponent sourceElementComponent = getSourceElementComponent();
final ConceptMapEngine conceptMapEngine = getConceptMapEngine(Arrays.asList(sourceElementComponent));
Coding coding = new Coding(null, SOURCE_CODE_STRING, "Body Weight");
Coding actual = conceptMapEngine.translate(coding, CONCEPT_MAP_URL);
assertEquals(TARGET_CODE_STRING, actual.getCode());
}
use of org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent in project org.hl7.fhir.core by hapifhir.
the class ConceptMapSpreadsheetGenerator method renderGroup.
private void renderGroup(ConceptMapGroupComponent grp, int i) {
Sheet sheet = makeSheet("Mapping Table " + Integer.toString(i));
addHeaders(sheet, "Source", "Display", "Relationship", "Target", "Display");
addRow(sheet, grp.getSource(), "", "", grp.getTarget(), "");
for (SourceElementComponent s : grp.getElement()) {
for (TargetElementComponent t : s.getTarget()) {
addRow(sheet, s.getCode(), s.getDisplay(), t.getEquivalenceElement().asStringValue(), t.getCode(), t.getDisplay());
}
}
}
Aggregations