use of org.hl7.fhir.dstu2.model.ConceptMap.TargetElementComponent in project org.hl7.fhir.core by hapifhir.
the class NarrativeGenerator method generate.
public boolean generate(ResourceContext rcontext, ConceptMap cm) throws FHIRFormatError, DefinitionException, IOException {
XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
x.h2().addText(cm.getName() + " (" + cm.getUrl() + ")");
XhtmlNode p = x.para();
if (cm.hasSource() || cm.hasTarget())
p.tx("Mapping from ");
if (!cm.hasSource())
p.tx("(unspecified)");
else
AddVsRef(rcontext, cm.getSource() instanceof Reference ? ((Reference) cm.getSource()).getReference() : ((UriType) cm.getSource()).asStringValue(), p);
p.tx(" to ");
if (!cm.hasTarget())
p.tx("(unspecified)");
else
AddVsRef(rcontext, cm.getTarget() instanceof Reference ? ((Reference) cm.getTarget()).getReference() : ((UriType) cm.getTarget()).asStringValue(), p);
p = x.para();
if (cm.getExperimental())
p.addText(Utilities.capitalize(cm.getStatus().toString()) + " (not intended for production usage). ");
else
p.addText(Utilities.capitalize(cm.getStatus().toString()) + ". ");
p.tx("Published on " + (cm.hasDate() ? cm.getDateElement().toHumanDisplay() : "??") + " by " + cm.getPublisher());
if (!cm.getContact().isEmpty()) {
p.tx(" (");
boolean firsti = true;
for (ContactDetail ci : cm.getContact()) {
if (firsti)
firsti = false;
else
p.tx(", ");
if (ci.hasName())
p.addText(ci.getName() + ": ");
boolean first = true;
for (ContactPoint c : ci.getTelecom()) {
if (first)
first = false;
else
p.tx(", ");
addTelecom(p, c);
}
}
p.tx(")");
}
p.tx(". ");
p.addText(cm.getCopyright());
if (!Utilities.noString(cm.getDescription()))
addMarkdown(x, cm.getDescription());
x.br();
for (ConceptMapGroupComponent grp : cm.getGroup()) {
String src = grp.getSource();
boolean comment = false;
boolean ok = true;
Map<String, HashSet<String>> sources = new HashMap<String, HashSet<String>>();
Map<String, HashSet<String>> targets = new HashMap<String, HashSet<String>>();
sources.put("code", new HashSet<String>());
targets.put("code", new HashSet<String>());
SourceElementComponent cc = grp.getElement().get(0);
String dst = grp.getTarget();
sources.get("code").add(grp.getSource());
targets.get("code").add(grp.getTarget());
for (SourceElementComponent ccl : grp.getElement()) {
ok = ok && ccl.getTarget().size() == 1 && ccl.getTarget().get(0).getDependsOn().isEmpty() && ccl.getTarget().get(0).getProduct().isEmpty();
for (TargetElementComponent ccm : ccl.getTarget()) {
comment = comment || !Utilities.noString(ccm.getComment());
for (OtherElementComponent d : ccm.getDependsOn()) {
if (!sources.containsKey(d.getProperty()))
sources.put(d.getProperty(), new HashSet<String>());
sources.get(d.getProperty()).add(d.getSystem());
}
for (OtherElementComponent d : ccm.getProduct()) {
if (!targets.containsKey(d.getProperty()))
targets.put(d.getProperty(), new HashSet<String>());
targets.get(d.getProperty()).add(d.getSystem());
}
}
}
String display;
if (ok) {
// simple
XhtmlNode tbl = x.table("grid");
XhtmlNode tr = tbl.tr();
tr.td().b().tx("Source Code");
tr.td().b().tx("Equivalence");
tr.td().b().tx("Destination Code");
if (comment)
tr.td().b().tx("Comment");
for (SourceElementComponent ccl : grp.getElement()) {
tr = tbl.tr();
XhtmlNode td = tr.td();
td.addText(ccl.getCode());
display = getDisplayForConcept(grp.getSource(), ccl.getCode());
if (display != null)
td.tx(" (" + display + ")");
TargetElementComponent ccm = ccl.getTarget().get(0);
tr.td().addText(!ccm.hasEquivalence() ? "" : ccm.getEquivalence().toCode());
td = tr.td();
td.addText(ccm.getCode());
display = getDisplayForConcept(grp.getTarget(), ccm.getCode());
if (display != null)
td.tx(" (" + display + ")");
if (comment)
tr.td().addText(ccm.getComment());
}
} else {
XhtmlNode tbl = x.table("grid");
XhtmlNode tr = tbl.tr();
XhtmlNode td;
tr.td().colspan(Integer.toString(sources.size())).b().tx("Source Concept");
tr.td().b().tx("Equivalence");
tr.td().colspan(Integer.toString(targets.size())).b().tx("Destination Concept");
if (comment)
tr.td().b().tx("Comment");
tr = tbl.tr();
if (sources.get("code").size() == 1)
tr.td().b().tx("Code " + sources.get("code").toString() + "");
else
tr.td().b().tx("Code");
for (String s : sources.keySet()) {
if (!s.equals("code")) {
if (sources.get(s).size() == 1)
tr.td().b().addText(getDescForConcept(s) + " " + sources.get(s).toString());
else
tr.td().b().addText(getDescForConcept(s));
}
}
tr.td();
if (targets.get("code").size() == 1)
tr.td().b().tx("Code " + targets.get("code").toString());
else
tr.td().b().tx("Code");
for (String s : targets.keySet()) {
if (!s.equals("code")) {
if (targets.get(s).size() == 1)
tr.td().b().addText(getDescForConcept(s) + " " + targets.get(s).toString() + "");
else
tr.td().b().addText(getDescForConcept(s));
}
}
if (comment)
tr.td();
for (SourceElementComponent ccl : grp.getElement()) {
tr = tbl.tr();
td = tr.td();
if (sources.get("code").size() == 1)
td.addText(ccl.getCode());
else
td.addText(grp.getSource() + " / " + ccl.getCode());
display = getDisplayForConcept(grp.getSource(), ccl.getCode());
if (display != null)
td.tx(" (" + display + ")");
TargetElementComponent ccm = ccl.getTarget().get(0);
for (String s : sources.keySet()) {
if (!s.equals("code")) {
td = tr.td();
td.addText(getCode(ccm.getDependsOn(), s, sources.get(s).size() != 1));
display = getDisplay(ccm.getDependsOn(), s);
if (display != null)
td.tx(" (" + display + ")");
}
}
if (!ccm.hasEquivalence())
tr.td().tx(":" + "(" + ConceptMapEquivalence.EQUIVALENT.toCode() + ")");
else
tr.td().tx(":" + ccm.getEquivalence().toCode());
td = tr.td();
if (targets.get("code").size() == 1)
td.addText(ccm.getCode());
else
td.addText(grp.getTarget() + " / " + ccm.getCode());
display = getDisplayForConcept(grp.getTarget(), ccm.getCode());
if (display != null)
td.tx(" (" + display + ")");
for (String s : targets.keySet()) {
if (!s.equals("code")) {
td = tr.td();
td.addText(getCode(ccm.getProduct(), s, targets.get(s).size() != 1));
display = getDisplay(ccm.getProduct(), s);
if (display != null)
td.tx(" (" + display + ")");
}
}
if (comment)
tr.td().addText(ccm.getComment());
}
}
}
inject(cm, x, NarrativeStatus.GENERATED);
return true;
}
use of org.hl7.fhir.dstu2.model.ConceptMap.TargetElementComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method translate.
public Base translate(TransformContext context, StructureMap map, Base source, String conceptMapUrl, String fieldToReturn) throws FHIRException {
Coding src = new Coding();
if (source.isPrimitive()) {
src.setCode(source.primitiveValue());
} else if ("Coding".equals(source.fhirType())) {
Base[] b = source.getProperty("system".hashCode(), "system", true);
if (b.length == 1)
src.setSystem(b[0].primitiveValue());
b = source.getProperty("code".hashCode(), "code", true);
if (b.length == 1)
src.setCode(b[0].primitiveValue());
} else if ("CE".equals(source.fhirType())) {
Base[] b = source.getProperty("codeSystem".hashCode(), "codeSystem", true);
if (b.length == 1)
src.setSystem(b[0].primitiveValue());
b = source.getProperty("code".hashCode(), "code", true);
if (b.length == 1)
src.setCode(b[0].primitiveValue());
} else
throw new FHIRException("Unable to translate source " + source.fhirType());
String su = conceptMapUrl;
if (conceptMapUrl.equals("http://hl7.org/fhir/ConceptMap/special-oid2uri")) {
String uri = worker.oid2Uri(src.getCode());
if (uri == null)
uri = "urn:oid:" + src.getCode();
if ("uri".equals(fieldToReturn))
return new UriType(uri);
else
throw new FHIRException("Error in return code");
} else {
ConceptMap cmap = null;
if (conceptMapUrl.startsWith("#")) {
for (Resource r : map.getContained()) {
if (r instanceof ConceptMap && ((ConceptMap) r).getId().equals(conceptMapUrl.substring(1))) {
cmap = (ConceptMap) r;
su = map.getUrl() + "#" + conceptMapUrl;
}
}
if (cmap == null)
throw new FHIRException("Unable to translate - cannot find map " + conceptMapUrl);
} else {
if (conceptMapUrl.contains("#")) {
String[] p = conceptMapUrl.split("\\#");
StructureMap mapU = worker.fetchResource(StructureMap.class, p[0]);
for (Resource r : mapU.getContained()) {
if (r instanceof ConceptMap && ((ConceptMap) r).getId().equals(p[1])) {
cmap = (ConceptMap) r;
su = conceptMapUrl;
}
}
}
if (cmap == null)
cmap = worker.fetchResource(ConceptMap.class, conceptMapUrl);
}
Coding outcome = null;
boolean done = false;
String message = null;
if (cmap == null) {
if (services == null)
message = "No map found for " + conceptMapUrl;
else {
outcome = services.translate(context.appInfo, src, conceptMapUrl);
done = true;
}
} else {
List<SourceElementComponentWrapper> list = new ArrayList<SourceElementComponentWrapper>();
for (ConceptMapGroupComponent g : cmap.getGroup()) {
for (SourceElementComponent e : g.getElement()) {
if (!src.hasSystem() && src.getCode().equals(e.getCode()))
list.add(new SourceElementComponentWrapper(g, e));
else if (src.hasSystem() && src.getSystem().equals(g.getSource()) && src.getCode().equals(e.getCode()))
list.add(new SourceElementComponentWrapper(g, e));
}
}
if (list.size() == 0)
done = true;
else if (list.get(0).comp.getTarget().size() == 0)
message = "Concept map " + su + " found no translation for " + src.getCode();
else {
for (TargetElementComponent tgt : list.get(0).comp.getTarget()) {
if (tgt.getEquivalence() == null || EnumSet.of(ConceptMapEquivalence.EQUAL, ConceptMapEquivalence.RELATEDTO, ConceptMapEquivalence.EQUIVALENT, ConceptMapEquivalence.WIDER).contains(tgt.getEquivalence())) {
if (done) {
message = "Concept map " + su + " found multiple matches for " + src.getCode();
done = false;
} else {
done = true;
outcome = new Coding().setCode(tgt.getCode()).setSystem(list.get(0).group.getTarget());
}
} else if (tgt.getEquivalence() == ConceptMapEquivalence.UNMATCHED) {
done = true;
}
}
if (!done)
message = "Concept map " + su + " found no usable translation for " + src.getCode();
}
}
if (!done)
throw new FHIRException(message);
if (outcome == null)
return null;
if ("code".equals(fieldToReturn))
return new CodeType(outcome.getCode());
else
return outcome;
}
}
use of org.hl7.fhir.dstu2.model.ConceptMap.TargetElementComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method translate.
public Base translate(TransformContext context, StructureMap map, Base source, String conceptMapUrl, String fieldToReturn) throws FHIRException {
Coding src = new Coding();
if (source.isPrimitive()) {
src.setCode(source.primitiveValue());
} else if ("Coding".equals(source.fhirType())) {
Base[] b = source.getProperty("system".hashCode(), "system", true);
if (b.length == 1)
src.setSystem(b[0].primitiveValue());
b = source.getProperty("code".hashCode(), "code", true);
if (b.length == 1)
src.setCode(b[0].primitiveValue());
} else if ("CE".equals(source.fhirType())) {
Base[] b = source.getProperty("codeSystem".hashCode(), "codeSystem", true);
if (b.length == 1)
src.setSystem(b[0].primitiveValue());
b = source.getProperty("code".hashCode(), "code", true);
if (b.length == 1)
src.setCode(b[0].primitiveValue());
} else
throw new FHIRException("Unable to translate source " + source.fhirType());
String su = conceptMapUrl;
if (conceptMapUrl.equals("http://hl7.org/fhir/ConceptMap/special-oid2uri")) {
String uri = worker.oid2Uri(src.getCode());
if (uri == null)
uri = "urn:oid:" + src.getCode();
if ("uri".equals(fieldToReturn))
return new UriType(uri);
else
throw new FHIRException("Error in return code");
} else {
ConceptMap cmap = null;
if (conceptMapUrl.startsWith("#")) {
for (Resource r : map.getContained()) {
if (r instanceof ConceptMap && r.getId().equals(conceptMapUrl.substring(1))) {
cmap = (ConceptMap) r;
su = map.getUrl() + "#" + conceptMapUrl;
}
}
if (cmap == null)
throw new FHIRException("Unable to translate - cannot find map " + conceptMapUrl);
} else {
if (conceptMapUrl.contains("#")) {
String[] p = conceptMapUrl.split("\\#");
StructureMap mapU = worker.fetchResource(StructureMap.class, p[0]);
for (Resource r : mapU.getContained()) {
if (r instanceof ConceptMap && r.getId().equals(p[1])) {
cmap = (ConceptMap) r;
su = conceptMapUrl;
}
}
}
if (cmap == null)
cmap = worker.fetchResource(ConceptMap.class, conceptMapUrl);
}
Coding outcome = null;
boolean done = false;
String message = null;
if (cmap == null) {
if (services == null)
message = "No map found for " + conceptMapUrl;
else {
outcome = services.translate(context.getAppInfo(), src, conceptMapUrl);
done = true;
}
} else {
List<SourceElementComponentWrapper> list = new ArrayList<SourceElementComponentWrapper>();
for (ConceptMapGroupComponent g : cmap.getGroup()) {
for (SourceElementComponent e : g.getElement()) {
if (!src.hasSystem() && src.getCode().equals(e.getCode()))
list.add(new SourceElementComponentWrapper(g, e));
else if (src.hasSystem() && src.getSystem().equals(g.getSource()) && src.getCode().equals(e.getCode()))
list.add(new SourceElementComponentWrapper(g, e));
}
}
if (list.size() == 0)
done = true;
else if (list.get(0).getComp().getTarget().size() == 0)
message = "Concept map " + su + " found no translation for " + src.getCode();
else {
for (TargetElementComponent tgt : list.get(0).getComp().getTarget()) {
if (tgt.getRelationship() == null || EnumSet.of(ConceptMapRelationship.RELATEDTO, ConceptMapRelationship.EQUIVALENT, ConceptMapRelationship.SOURCEISNARROWERTHANTARGET).contains(tgt.getRelationship())) {
if (done) {
message = "Concept map " + su + " found multiple matches for " + src.getCode();
done = false;
} else {
done = true;
outcome = new Coding().setCode(tgt.getCode()).setSystem(list.get(0).getGroup().getTarget());
}
}
}
if (!done)
message = "Concept map " + su + " found no usable translation for " + src.getCode();
}
}
if (!done)
throw new FHIRException(message);
if (outcome == null)
return null;
if ("code".equals(fieldToReturn))
return new CodeType(outcome.getCode());
else
return outcome;
}
}
use of org.hl7.fhir.dstu2.model.ConceptMap.TargetElementComponent in project pathling by aehrc.
the class ConceptMapBuilder method build.
@Nonnull
public ConceptMap build() {
final Map<Pair<VersionedSystem, VersionedSystem>, List<ConceptMapEntry>> mappingsBySystem = entries.stream().collect(Collectors.groupingBy(ConceptMapBuilder::getVersionedSystems));
final ConceptMap result = new ConceptMap();
result.setStatus(PublicationStatus.ACTIVE);
mappingsBySystem.forEach((srcAndTarget, systemMappings) -> {
final ConceptMapGroupComponent group = result.addGroup();
group.setSource(srcAndTarget.getLeft().getSystem());
group.setSourceVersion(srcAndTarget.getLeft().getVersion());
group.setTarget(srcAndTarget.getRight().getSystem());
group.setTargetVersion(srcAndTarget.getRight().getVersion());
systemMappings.forEach(m -> {
final SourceElementComponent sourceElement = group.addElement();
sourceElement.setCode(m.getSource().getCode());
final TargetElementComponent targetElement = sourceElement.addTarget();
targetElement.setCode(m.getTarget().getCode());
targetElement.setEquivalence(m.getEquivalence());
});
});
return result;
}
use of org.hl7.fhir.dstu2.model.ConceptMap.TargetElementComponent in project bunsen by cerner.
the class ConceptMaps method expandMappingsIterator.
private static Iterator<Mapping> expandMappingsIterator(ConceptMap map) {
List<Mapping> mappings = new ArrayList<>();
for (ConceptMapGroupComponent group : map.getGroup()) {
for (SourceElementComponent element : group.getElement()) {
for (TargetElementComponent target : element.getTarget()) {
Mapping mapping = new Mapping();
mapping.setConceptMapUri(map.getUrl());
mapping.setConceptMapVersion(map.getVersion());
try {
String sourceValue = map.getSource() instanceof UriType ? map.getSourceUriType().getValue() : map.getSourceReference().getReference();
mapping.setSourceValueSet(sourceValue);
String targetValue = map.getTarget() instanceof UriType ? map.getTargetUriType().getValue() : map.getTargetReference().getReference();
mapping.setTargetValueSet(targetValue);
} catch (FHIRException fhirException) {
// an exception.
throw new RuntimeException(fhirException);
}
mapping.setSourceSystem(group.getSource());
mapping.setSourceValue(element.getCode());
mapping.setTargetSystem(group.getTarget());
mapping.setTargetValue(target.getCode());
if (target.getEquivalence() != null) {
mapping.setEquivalence(target.getEquivalence().toCode());
}
mappings.add(mapping);
}
}
}
return mappings.iterator();
}
Aggregations