use of org.hl7.fhir.dstu2.model.ConceptMap.TargetElementComponent in project org.hl7.fhir.core by hapifhir.
the class NarrativeGenerator method generate.
public void generate(ConceptMap cm) {
XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
x.addTag("h2").addText(cm.getName() + " (" + cm.getUrl() + ")");
XhtmlNode p = x.addTag("p");
p.addText("Mapping from ");
AddVsRef(((Reference) cm.getSource()).getReference(), p);
p.addText(" to ");
AddVsRef(((Reference) cm.getTarget()).getReference(), p);
p = x.addTag("p");
if (cm.getExperimental())
p.addText(Utilities.capitalize(cm.getStatus().toString()) + " (not intended for production usage). ");
else
p.addText(Utilities.capitalize(cm.getStatus().toString()) + ". ");
p.addText("Published on " + cm.getDateElement().toHumanDisplay() + " by " + cm.getPublisher());
if (!cm.getContact().isEmpty()) {
p.addText(" (");
boolean firsti = true;
for (ConceptMapContactComponent ci : cm.getContact()) {
if (firsti)
firsti = false;
else
p.addText(", ");
if (ci.hasName())
p.addText(ci.getName() + ": ");
boolean first = true;
for (ContactPoint c : ci.getTelecom()) {
if (first)
first = false;
else
p.addText(", ");
addTelecom(p, c);
}
p.addText("; ");
}
p.addText(")");
}
p.addText(". ");
p.addText(cm.getCopyright());
if (!Utilities.noString(cm.getDescription()))
x.addTag("p").addText(cm.getDescription());
x.addTag("br");
if (!cm.getElement().isEmpty()) {
SourceElementComponent cc = cm.getElement().get(0);
String src = cc.getCodeSystem();
boolean comments = false;
boolean ok = cc.getTarget().size() == 1;
Map<String, HashSet<String>> sources = new HashMap<String, HashSet<String>>();
sources.put("code", new HashSet<String>());
Map<String, HashSet<String>> targets = new HashMap<String, HashSet<String>>();
targets.put("code", new HashSet<String>());
if (ok) {
String dst = cc.getTarget().get(0).getCodeSystem();
for (SourceElementComponent ccl : cm.getElement()) {
ok = ok && src.equals(ccl.getCodeSystem()) && ccl.getTarget().size() == 1 && dst.equals(ccl.getTarget().get(0).getCodeSystem()) && ccl.getTarget().get(0).getDependsOn().isEmpty() && ccl.getTarget().get(0).getProduct().isEmpty();
if (ccl.hasCodeSystem())
sources.get("code").add(ccl.getCodeSystem());
for (TargetElementComponent ccm : ccl.getTarget()) {
comments = comments || !Utilities.noString(ccm.getComments());
for (OtherElementComponent d : ccm.getDependsOn()) {
if (!sources.containsKey(d.getElement()))
sources.put(d.getElement(), new HashSet<String>());
sources.get(d.getElement()).add(d.getCodeSystem());
}
if (ccm.hasCodeSystem())
targets.get("code").add(ccm.getCodeSystem());
for (OtherElementComponent d : ccm.getProduct()) {
if (!targets.containsKey(d.getElement()))
targets.put(d.getElement(), new HashSet<String>());
targets.get(d.getElement()).add(d.getCodeSystem());
}
}
}
}
String display;
if (ok) {
// simple
XhtmlNode tbl = x.addTag("table").setAttribute("class", "grid");
XhtmlNode tr = tbl.addTag("tr");
tr.addTag("td").addTag("b").addText("Source Code");
tr.addTag("td").addTag("b").addText("Equivalence");
tr.addTag("td").addTag("b").addText("Destination Code");
if (comments)
tr.addTag("td").addTag("b").addText("Comments");
for (SourceElementComponent ccl : cm.getElement()) {
tr = tbl.addTag("tr");
XhtmlNode td = tr.addTag("td");
td.addText(ccl.getCode());
display = getDisplayForConcept(ccl.getCodeSystem(), ccl.getCode());
if (display != null)
td.addText(" (" + display + ")");
TargetElementComponent ccm = ccl.getTarget().get(0);
tr.addTag("td").addText(!ccm.hasEquivalence() ? "" : ccm.getEquivalence().toCode());
td = tr.addTag("td");
td.addText(ccm.getCode());
display = getDisplayForConcept(ccm.getCodeSystem(), ccm.getCode());
if (display != null)
td.addText(" (" + display + ")");
if (comments)
tr.addTag("td").addText(ccm.getComments());
}
} else {
XhtmlNode tbl = x.addTag("table").setAttribute("class", "grid");
XhtmlNode tr = tbl.addTag("tr");
XhtmlNode td;
tr.addTag("td").setAttribute("colspan", Integer.toString(sources.size())).addTag("b").addText("Source Concept");
tr.addTag("td").addTag("b").addText("Equivalence");
tr.addTag("td").setAttribute("colspan", Integer.toString(targets.size())).addTag("b").addText("Destination Concept");
if (comments)
tr.addTag("td").addTag("b").addText("Comments");
tr = tbl.addTag("tr");
if (sources.get("code").size() == 1)
tr.addTag("td").addTag("b").addText("Code " + sources.get("code").toString() + "");
else
tr.addTag("td").addTag("b").addText("Code");
for (String s : sources.keySet()) {
if (!s.equals("code")) {
if (sources.get(s).size() == 1)
tr.addTag("td").addTag("b").addText(getDescForConcept(s) + " " + sources.get(s).toString());
else
tr.addTag("td").addTag("b").addText(getDescForConcept(s));
}
}
tr.addTag("td");
if (targets.get("code").size() == 1)
tr.addTag("td").addTag("b").addText("Code " + targets.get("code").toString());
else
tr.addTag("td").addTag("b").addText("Code");
for (String s : targets.keySet()) {
if (!s.equals("code")) {
if (targets.get(s).size() == 1)
tr.addTag("td").addTag("b").addText(getDescForConcept(s) + " " + targets.get(s).toString() + "");
else
tr.addTag("td").addTag("b").addText(getDescForConcept(s));
}
}
if (comments)
tr.addTag("td");
for (SourceElementComponent ccl : cm.getElement()) {
tr = tbl.addTag("tr");
td = tr.addTag("td");
if (sources.get("code").size() == 1)
td.addText(ccl.getCode());
else
td.addText(ccl.getCodeSystem() + " / " + ccl.getCode());
display = getDisplayForConcept(ccl.getCodeSystem(), ccl.getCode());
if (display != null)
td.addText(" (" + display + ")");
TargetElementComponent ccm = ccl.getTarget().get(0);
for (String s : sources.keySet()) {
if (!s.equals("code")) {
td = tr.addTag("td");
td.addText(getCode(ccm.getDependsOn(), s, sources.get(s).size() != 1));
display = getDisplay(ccm.getDependsOn(), s);
if (display != null)
td.addText(" (" + display + ")");
}
}
tr.addTag("td").addText(ccm.getEquivalence().toString());
td = tr.addTag("td");
if (targets.get("code").size() == 1)
td.addText(ccm.getCode());
else
td.addText(ccm.getCodeSystem() + " / " + ccm.getCode());
display = getDisplayForConcept(ccm.getCodeSystem(), ccm.getCode());
if (display != null)
td.addText(" (" + display + ")");
for (String s : targets.keySet()) {
if (!s.equals("code")) {
td = tr.addTag("td");
td.addText(getCode(ccm.getProduct(), s, targets.get(s).size() != 1));
display = getDisplay(ccm.getProduct(), s);
if (display != null)
td.addText(" (" + display + ")");
}
}
if (comments)
tr.addTag("td").addText(ccm.getComments());
}
}
}
inject(cm, x, NarrativeStatus.GENERATED);
}
use of org.hl7.fhir.dstu2.model.ConceptMap.TargetElementComponent 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.getRelationshipElement().asStringValue(), t.getCode(), t.getDisplay());
}
}
}
use of org.hl7.fhir.dstu2.model.ConceptMap.TargetElementComponent in project org.hl7.fhir.core by hapifhir.
the class ConceptMapEngine method translateByJustCode.
private Coding translateByJustCode(ConceptMap cm, String code) throws FHIRException {
SourceElementComponent ct = null;
ConceptMapGroupComponent cg = null;
for (ConceptMapGroupComponent g : cm.getGroup()) {
for (SourceElementComponent e : g.getElement()) {
if (code.equals(e.getCode())) {
if (e != null)
throw new FHIRException("Unable to process translate " + code + " because multiple candidate matches were found in concept map " + cm.getUrl());
ct = e;
cg = g;
}
}
}
if (ct == null)
return null;
TargetElementComponent tt = null;
for (TargetElementComponent t : ct.getTarget()) {
if (!t.hasDependsOn() && !t.hasProduct() && isOkRelationship(t.getRelationship())) {
if (tt != null)
throw new FHIRException("Unable to process translate " + code + " because multiple targets were found in concept map " + cm.getUrl());
tt = t;
}
}
if (tt == null)
return null;
CanonicalPair cp = new CanonicalPair(cg.getTarget());
return new Coding().setSystem(cp.getUrl()).setVersion(cp.getVersion()).setCode(tt.getCode()).setDisplay(tt.getDisplay());
}
use of org.hl7.fhir.dstu2.model.ConceptMap.TargetElementComponent in project org.hl7.fhir.core by hapifhir.
the class NarrativeGenerator method addExpansionRowToTable.
private void addExpansionRowToTable(XhtmlNode t, ValueSetExpansionContainsComponent c, int i, boolean doSystem, boolean doDefinition, Map<ConceptMap, String> mymaps, CodeSystem allCS) {
XhtmlNode tr = t.addTag("tr");
XhtmlNode td = tr.addTag("td");
String tgt = makeAnchor(c.getSystem(), c.getCode());
td.addTag("a").setAttribute("name", tgt).addText(" ");
String s = Utilities.padLeft("", '.', i * 2);
td.addText(s);
Resource e = context.fetchCodeSystem(c.getSystem());
if (e == null)
td.addText(c.getCode());
else {
XhtmlNode a = td.addTag("a");
a.addText(c.getCode());
a.setAttribute("href", prefix + getCsRef(e) + "#" + Utilities.nmtokenize(c.getCode()));
}
if (doSystem) {
td = tr.addTag("td");
td.addText(c.getSystem());
}
td = tr.addTag("td");
if (c.hasDisplayElement())
td.addText(c.getDisplay());
if (doDefinition) {
CodeSystem cs = allCS;
if (cs == null)
cs = context.fetchCodeSystem(c.getSystem());
td = tr.addTag("td");
if (cs != null)
td.addText(CodeSystemUtilities.getCodeDefinition(cs, c.getCode()));
}
for (ConceptMap m : mymaps.keySet()) {
td = tr.addTag("td");
List<TargetElementComponent> mappings = findMappingsForCode(c.getCode(), m);
boolean first = true;
for (TargetElementComponent mapping : mappings) {
if (!first)
td.addTag("br");
first = false;
XhtmlNode span = td.addTag("span");
span.setAttribute("title", mapping.getEquivalence().toString());
span.addText(getCharForEquivalence(mapping));
XhtmlNode a = td.addTag("a");
a.setAttribute("href", prefix + mymaps.get(m) + "#" + mapping.getCode());
a.addText(mapping.getCode());
if (!Utilities.noString(mapping.getComments()))
td.addTag("i").addText("(" + mapping.getComments() + ")");
}
}
for (ValueSetExpansionContainsComponent cc : c.getContains()) {
addExpansionRowToTable(t, cc, i + 1, doSystem, doDefinition, mymaps, allCS);
}
}
use of org.hl7.fhir.dstu2.model.ConceptMap.TargetElementComponent 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();
ConceptMapRelationship rel = readRelationship(lexer);
String tgts = 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.setRelationship(rel);
lexer.token(":");
tgt.setCode(lexer.take());
if (tgt.getCode().startsWith("\""))
tgt.setCode(lexer.processConstant(tgt.getCode()));
tgt.setComment(lexer.getFirstComment());
}
lexer.token("}");
}
Aggregations