use of org.hl7.fhir.r4.model.StructureMap in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method findMatchingMaps.
private List<StructureMap> findMatchingMaps(String value) {
List<StructureMap> res = new ArrayList<StructureMap>();
if (value.contains("*")) {
for (StructureMap sm : worker.listTransforms()) {
if (urlMatches(value, sm.getUrl())) {
res.add(sm);
}
}
} else {
StructureMap sm = worker.getTransform(value);
if (sm != null)
res.add(sm);
}
Set<String> check = new HashSet<String>();
for (StructureMap sm : res) {
if (check.contains(sm.getUrl()))
throw new Error("duplicate");
else
check.add(sm.getUrl());
}
return res;
}
use of org.hl7.fhir.r4.model.StructureMap in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method analyseRule.
private void analyseRule(String indent, TransformContext context, StructureMap map, VariablesForProfiling vars, StructureMapGroupComponent group, StructureMapGroupRuleComponent rule, StructureMapAnalysis result) throws FHIRException {
log(indent + "Analyse rule : " + rule.getName());
XhtmlNode tr = result.summary.addTag("tr");
XhtmlNode xs = tr.addTag("td");
XhtmlNode xt = tr.addTag("td");
VariablesForProfiling srcVars = vars.copy();
if (rule.getSource().size() != 1)
throw new FHIRException("Rule \"" + rule.getName() + "\": not handled yet");
VariablesForProfiling source = analyseSource(rule.getName(), context, srcVars, rule.getSourceFirstRep(), xs);
TargetWriter tw = new TargetWriter();
for (StructureMapGroupRuleTargetComponent t : rule.getTarget()) {
analyseTarget(rule.getName(), context, source, map, t, rule.getSourceFirstRep().getVariable(), tw, result.profiles, rule.getName());
}
tw.commit(xt);
for (StructureMapGroupRuleComponent childrule : rule.getRule()) {
analyseRule(indent + " ", context, map, source, group, childrule, result);
}
// for (StructureMapGroupRuleDependentComponent dependent : rule.getDependent()) {
// executeDependency(indent+" ", context, map, v, group, dependent); // do we need group here?
// }
}
use of org.hl7.fhir.r4.model.StructureMap in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method runTransform.
private Base runTransform(String ruleId, TransformContext context, StructureMap map, StructureMapGroupComponent group, StructureMapGroupRuleTargetComponent tgt, Variables vars, Base dest, String element, String srcVar, boolean root) throws FHIRException {
try {
switch(tgt.getTransform()) {
case CREATE:
String tn;
if (tgt.getParameter().isEmpty()) {
// we have to work out the type. First, we see if there is a single type for the target. If there is, we use that
String[] types = dest.getTypesForProperty(element.hashCode(), element);
if (types.length == 1 && !"*".equals(types[0]) && !types[0].equals("Resource"))
tn = types[0];
else if (srcVar != null) {
tn = determineTypeFromSourceType(map, group, vars.get(VariableMode.INPUT, srcVar), types);
} else
throw new Error("Cannot determine type implicitly because there is no single input variable");
} else {
tn = getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString());
// ok, now we resolve the type name against the import statements
for (StructureMapStructureComponent uses : map.getStructure()) {
if (uses.getMode() == StructureMapModelMode.TARGET && uses.hasAlias() && tn.equals(uses.getAlias())) {
tn = uses.getUrl();
break;
}
}
}
Base res = services != null ? services.createType(context.getAppInfo(), tn) : ResourceFactory.createResourceOrType(tn);
if (res.isResource() && !res.fhirType().equals("Parameters")) {
// res.setIdBase(tgt.getParameter().size() > 1 ? getParamString(vars, tgt.getParameter().get(0)) : UUID.randomUUID().toString().toLowerCase());
if (services != null)
res = services.createResource(context.getAppInfo(), res, root);
}
if (tgt.hasUserData("profile"))
res.setUserData("profile", tgt.getUserData("profile"));
return res;
case COPY:
return getParam(vars, tgt.getParameter().get(0));
case EVALUATE:
ExpressionNode expr = (ExpressionNode) tgt.getUserData(MAP_EXPRESSION);
if (expr == null) {
expr = fpe.parse(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()));
tgt.setUserData(MAP_WHERE_EXPRESSION, expr);
}
List<Base> v = fpe.evaluate(vars, null, null, tgt.getParameter().size() == 2 ? getParam(vars, tgt.getParameter().get(0)) : new BooleanType(false), expr);
if (v.size() == 0)
return null;
else if (v.size() != 1)
throw new FHIRException("Rule \"" + ruleId + "\": Evaluation of " + expr.toString() + " returned " + v.size() + " objects");
else
return v.get(0);
case TRUNCATE:
String src = getParamString(vars, tgt.getParameter().get(0));
String len = getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString());
if (Utilities.isInteger(len)) {
int l = Integer.parseInt(len);
if (src.length() > l)
src = src.substring(0, l);
}
return new StringType(src);
case ESCAPE:
throw new Error("Rule \"" + ruleId + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
case CAST:
src = getParamString(vars, tgt.getParameter().get(0));
if (tgt.getParameter().size() == 1)
throw new FHIRException("Implicit type parameters on cast not yet supported");
String t = getParamString(vars, tgt.getParameter().get(1));
if (t.equals("string"))
return new StringType(src);
else
throw new FHIRException("cast to " + t + " not yet supported");
case APPEND:
StringBuilder sb = new StringBuilder(getParamString(vars, tgt.getParameter().get(0)));
for (int i = 1; i < tgt.getParameter().size(); i++) sb.append(getParamString(vars, tgt.getParameter().get(i)));
return new StringType(sb.toString());
case TRANSLATE:
return translate(context, map, vars, tgt.getParameter());
case REFERENCE:
Base b = getParam(vars, tgt.getParameter().get(0));
if (b == null)
throw new FHIRException("Rule \"" + ruleId + "\": Unable to find parameter " + ((IdType) tgt.getParameter().get(0).getValue()).asStringValue());
if (!b.isResource())
throw new FHIRException("Rule \"" + ruleId + "\": Transform engine cannot point at an element of type " + b.fhirType());
else {
String id = b.getIdBase();
if (id == null) {
id = UUID.randomUUID().toString().toLowerCase();
b.setIdBase(id);
}
return new Reference().setReference(b.fhirType() + "/" + id);
}
case DATEOP:
throw new Error("Rule \"" + ruleId + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
case UUID:
return new IdType(UUID.randomUUID().toString());
case POINTER:
b = getParam(vars, tgt.getParameter().get(0));
if (b instanceof Resource)
return new UriType("urn:uuid:" + ((Resource) b).getId());
else
throw new FHIRException("Rule \"" + ruleId + "\": Transform engine cannot point at an element of type " + b.fhirType());
case CC:
CodeableConcept cc = new CodeableConcept();
cc.addCoding(buildCoding(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()), getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString())));
return cc;
case C:
Coding c = buildCoding(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()), getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString()));
return c;
default:
throw new Error("Rule \"" + ruleId + "\": Transform Unknown: " + tgt.getTransform().toCode());
}
} catch (Exception e) {
throw new FHIRException("Exception executing transform " + tgt.toString() + " on Rule \"" + ruleId + "\": " + e.getMessage(), e);
}
}
use of org.hl7.fhir.r4.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.addVariable("s");
dep.addVariable("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;
}
use of org.hl7.fhir.r4.model.StructureMap 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("}");
}
Aggregations