use of org.hl7.fhir.r4b.model.StructureMap in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method getTargetType.
public StructureDefinition getTargetType(StructureMap map) throws FHIRException {
boolean found = false;
StructureDefinition res = null;
for (StructureMapStructureComponent uses : map.getStructure()) {
if (uses.getMode() == StructureMapModelMode.TARGET) {
if (found)
throw new FHIRException("Multiple targets found in map " + map.getUrl());
found = true;
res = worker.fetchResource(StructureDefinition.class, uses.getUrl());
if (res == null)
throw new FHIRException("Unable to find " + uses.getUrl() + " referenced from map " + map.getUrl());
}
}
if (res == null)
throw new FHIRException("No targets found in map " + map.getUrl());
return res;
}
use of org.hl7.fhir.r4b.model.StructureMap in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method executeDependency.
private void executeDependency(String indent, TransformContext context, StructureMap map, Variables vin, StructureMapGroupComponent group, StructureMapGroupRuleDependentComponent dependent) throws FHIRException {
ResolvedGroup rg = resolveGroupReference(map, group, dependent.getName());
if (rg.target.getInput().size() != dependent.getVariable().size()) {
throw new FHIRException("Rule '" + dependent.getName() + "' has " + Integer.toString(rg.target.getInput().size()) + " but the invocation has " + Integer.toString(dependent.getVariable().size()) + " variables");
}
Variables v = new Variables();
for (int i = 0; i < rg.target.getInput().size(); i++) {
StructureMapGroupInputComponent input = rg.target.getInput().get(i);
StringType rdp = dependent.getVariable().get(i);
String var = rdp.asStringValue();
VariableMode mode = input.getMode() == StructureMapInputMode.SOURCE ? VariableMode.INPUT : VariableMode.OUTPUT;
Base vv = vin.get(mode, var);
if (// * once source, always source. but target can be treated as source at user convenient
vv == null && mode == VariableMode.INPUT)
vv = vin.get(VariableMode.OUTPUT, var);
if (vv == null)
throw new FHIRException("Rule '" + dependent.getName() + "' " + mode.toString() + " variable '" + input.getName() + "' named as '" + var + "' has no value (vars = " + vin.summary() + ")");
v.add(mode, input.getName(), vv);
}
executeGroup(indent + " ", context, rg.targetMap, v, rg.target, false);
}
use of org.hl7.fhir.r4b.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.r4b.model.StructureMap in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method processTarget.
private void processTarget(String ruleId, TransformContext context, Variables vars, StructureMap map, StructureMapGroupComponent group, StructureMapGroupRuleTargetComponent tgt, String srcVar, boolean atRoot, Variables sharedVars) throws FHIRException {
Base dest = null;
if (tgt.hasContext()) {
dest = vars.get(VariableMode.OUTPUT, tgt.getContext());
if (dest == null)
throw new FHIRException("Rule \"" + ruleId + "\": target context not known: " + tgt.getContext());
if (!tgt.hasElement())
throw new FHIRException("Rule \"" + ruleId + "\": Not supported yet");
}
Base v = null;
if (tgt.hasTransform()) {
v = runTransform(ruleId, context, map, group, tgt, vars, dest, tgt.getElement(), srcVar, atRoot);
if (v != null && dest != null)
// reset v because some implementations may have to rewrite v when setting the value
v = dest.setProperty(tgt.getElement().hashCode(), tgt.getElement(), v);
} else if (dest != null) {
if (tgt.hasListMode(StructureMapTargetListMode.SHARE)) {
v = sharedVars.get(VariableMode.SHARED, tgt.getListRuleId());
if (v == null) {
v = dest.makeProperty(tgt.getElement().hashCode(), tgt.getElement());
sharedVars.add(VariableMode.SHARED, tgt.getListRuleId(), v);
}
} else {
v = dest.makeProperty(tgt.getElement().hashCode(), tgt.getElement());
}
}
if (tgt.hasVariable() && v != null)
vars.add(VariableMode.OUTPUT, tgt.getVariable(), v);
}
use of org.hl7.fhir.r4b.model.StructureMap in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method resolveGroupReference.
private ResolvedGroup resolveGroupReference(StructureMap map, StructureMapGroupComponent source, String name) throws FHIRException {
String kn = "ref^" + name;
if (source.hasUserData(kn))
return (ResolvedGroup) source.getUserData(kn);
ResolvedGroup res = new ResolvedGroup();
res.targetMap = null;
res.target = null;
for (StructureMapGroupComponent grp : map.getGroup()) {
if (grp.getName().equals(name)) {
if (res.targetMap == null) {
res.targetMap = map;
res.target = grp;
} else
throw new FHIRException("Multiple possible matches for rule '" + name + "'");
}
}
if (res.targetMap != null) {
source.setUserData(kn, res);
return res;
}
for (UriType imp : map.getImport()) {
List<StructureMap> impMapList = findMatchingMaps(imp.getValue());
if (impMapList.size() == 0)
throw new FHIRException("Unable to find map(s) for " + imp.getValue());
for (StructureMap impMap : impMapList) {
if (!impMap.getUrl().equals(map.getUrl())) {
for (StructureMapGroupComponent grp : impMap.getGroup()) {
if (grp.getName().equals(name)) {
if (res.targetMap == null) {
res.targetMap = impMap;
res.target = grp;
} else
throw new FHIRException("Multiple possible matches for rule group '" + name + "' in " + res.targetMap.getUrl() + "#" + res.target.getName() + " and " + impMap.getUrl() + "#" + grp.getName());
}
}
}
}
}
if (res.target == null)
throw new FHIRException("No matches found for rule '" + name + "'. Reference found in " + map.getUrl());
source.setUserData(kn, res);
return res;
}
Aggregations