use of org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent 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;
}
use of org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method analyseSource.
private VariablesForProfiling analyseSource(String ruleId, TransformContext context, VariablesForProfiling vars, StructureMapGroupRuleSourceComponent src, XhtmlNode td) throws FHIRException {
VariableForProfiling var = vars.get(VariableMode.INPUT, src.getContext());
if (var == null)
throw new FHIRException("Rule \"" + ruleId + "\": Unknown input variable " + src.getContext());
PropertyWithType prop = var.getProperty();
boolean optional = false;
boolean repeating = false;
if (src.hasCondition()) {
optional = true;
}
if (src.hasElement()) {
Property element = prop.getBaseProperty().getChild(prop.types.getType(), src.getElement());
if (element == null)
throw new FHIRException("Rule \"" + ruleId + "\": Unknown element name " + src.getElement());
if (element.getDefinition().getMin() == 0)
optional = true;
if (element.getDefinition().getMax().equals("*"))
repeating = true;
VariablesForProfiling result = vars.copy(optional, repeating);
TypeDetails type = new TypeDetails(CollectionStatus.SINGLETON);
for (TypeRefComponent tr : element.getDefinition().getType()) {
if (!tr.hasCode())
throw new Error("Rule \"" + ruleId + "\": Element has no type");
ProfiledType pt = new ProfiledType(tr.getWorkingCode());
if (tr.hasProfile())
pt.addProfiles(tr.getProfile());
if (element.getDefinition().hasBinding())
pt.addBinding(element.getDefinition().getBinding());
type.addType(pt);
}
td.addText(prop.getPath() + "." + src.getElement());
if (src.hasVariable())
result.add(VariableMode.INPUT, src.getVariable(), new PropertyWithType(prop.getPath() + "." + src.getElement(), element, null, type));
return result;
} else {
// ditto!
td.addText(prop.getPath());
return vars.copy(optional, repeating);
}
}
use of org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method processSource.
private List<Variables> processSource(String ruleId, TransformContext context, Variables vars, StructureMapGroupRuleSourceComponent src, String pathForErrors, String indent) throws FHIRException {
List<Base> items;
if (src.getContext().equals("@search")) {
ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_SEARCH_EXPRESSION);
if (expr == null) {
expr = fpe.parse(src.getElement());
src.setUserData(MAP_SEARCH_EXPRESSION, expr);
}
// string is a holder of nothing to ensure that variables are processed correctly
String search = fpe.evaluateToString(vars, null, null, new StringType(), expr);
items = services.performSearch(context.getAppInfo(), search);
} else {
items = new ArrayList<Base>();
Base b = vars.get(VariableMode.INPUT, src.getContext());
if (b == null)
throw new FHIRException("Unknown input variable " + src.getContext() + " in " + pathForErrors + " rule " + ruleId + " (vars = " + vars.summary() + ")");
if (!src.hasElement())
items.add(b);
else {
getChildrenByName(b, src.getElement(), items);
if (items.size() == 0 && src.hasDefaultValue())
items.add(src.getDefaultValueElement());
}
}
if (src.hasType()) {
List<Base> remove = new ArrayList<Base>();
for (Base item : items) {
if (item != null && !isType(item, src.getType())) {
remove.add(item);
}
}
items.removeAll(remove);
}
if (src.hasCondition()) {
ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_EXPRESSION);
if (expr == null) {
expr = fpe.parse(src.getCondition());
// fpe.check(context.appInfo, ??, ??, expr)
src.setUserData(MAP_WHERE_EXPRESSION, expr);
}
List<Base> remove = new ArrayList<Base>();
for (Base item : items) {
if (!fpe.evaluateToBoolean(vars, null, null, item, expr)) {
log(indent + " condition [" + src.getCondition() + "] for " + item.toString() + " : false");
remove.add(item);
} else
log(indent + " condition [" + src.getCondition() + "] for " + item.toString() + " : true");
}
items.removeAll(remove);
}
if (src.hasCheck()) {
ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_CHECK);
if (expr == null) {
expr = fpe.parse(src.getCheck());
// fpe.check(context.appInfo, ??, ??, expr)
src.setUserData(MAP_WHERE_CHECK, expr);
}
List<Base> remove = new ArrayList<Base>();
for (Base item : items) {
if (!fpe.evaluateToBoolean(vars, null, null, item, expr))
throw new FHIRException("Rule \"" + ruleId + "\": Check condition failed");
}
}
if (src.hasLogMessage()) {
ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_LOG);
if (expr == null) {
expr = fpe.parse(src.getLogMessage());
// fpe.check(context.appInfo, ??, ??, expr)
src.setUserData(MAP_WHERE_LOG, expr);
}
CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
for (Base item : items) b.appendIfNotNull(fpe.evaluateToString(vars, null, null, item, expr));
if (b.length() > 0)
services.log(b.toString());
}
if (src.hasListMode() && !items.isEmpty()) {
switch(src.getListMode()) {
case FIRST:
Base bt = items.get(0);
items.clear();
items.add(bt);
break;
case NOTFIRST:
if (items.size() > 0)
items.remove(0);
break;
case LAST:
bt = items.get(items.size() - 1);
items.clear();
items.add(bt);
break;
case NOTLAST:
if (items.size() > 0)
items.remove(items.size() - 1);
break;
case ONLYONE:
if (items.size() > 1)
throw new FHIRException("Rule \"" + ruleId + "\": Check condition failed: the collection has more than one item");
break;
case NULL:
}
}
List<Variables> result = new ArrayList<Variables>();
for (Base r : items) {
Variables v = vars.copy();
if (src.hasVariable())
v.add(VariableMode.INPUT, src.getVariable(), r);
result.add(v);
}
return result;
}
use of org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method sourceToString.
public static String sourceToString(StructureMapGroupRuleSourceComponent r) {
StringBuilder b = new StringBuilder();
renderSource(b, r, false);
return b.toString();
}
use of org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method analyseSource.
private VariablesForProfiling analyseSource(String ruleId, TransformContext context, VariablesForProfiling vars, StructureMapGroupRuleSourceComponent src, XhtmlNode td) throws FHIRException {
VariableForProfiling var = vars.get(VariableMode.INPUT, src.getContext());
if (var == null)
throw new FHIRException("Rule \"" + ruleId + "\": Unknown input variable " + src.getContext());
PropertyWithType prop = var.getProperty();
boolean optional = false;
boolean repeating = false;
if (src.hasCondition()) {
optional = true;
}
if (src.hasElement()) {
Property element = prop.getBaseProperty().getChild(prop.getTypes().getType(), src.getElement());
if (element == null)
throw new FHIRException("Rule \"" + ruleId + "\": Unknown element name " + src.getElement());
if (element.getDefinition().getMin() == 0)
optional = true;
if (element.getDefinition().getMax().equals("*"))
repeating = true;
VariablesForProfiling result = vars.copy(optional, repeating);
TypeDetails type = new TypeDetails(CollectionStatus.SINGLETON);
for (TypeRefComponent tr : element.getDefinition().getType()) {
if (!tr.hasCode())
throw new Error("Rule \"" + ruleId + "\": Element has no type");
ProfiledType pt = new ProfiledType(tr.getWorkingCode());
if (tr.hasProfile())
pt.addProfiles(tr.getProfile());
if (element.getDefinition().hasBinding())
pt.addBinding(element.getDefinition().getBinding());
type.addType(pt);
}
td.addText(prop.getPath() + "." + src.getElement());
if (src.hasVariable())
result.add(VariableMode.INPUT, src.getVariable(), new PropertyWithType(prop.getPath() + "." + src.getElement(), element, null, type));
return result;
} else {
// ditto!
td.addText(prop.getPath());
return vars.copy(optional, repeating);
}
}
Aggregations