use of org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method analyseTarget.
private void analyseTarget(String ruleId, TransformContext context, VariablesForProfiling vars, StructureMap map, StructureMapGroupRuleTargetComponent tgt, String tv, TargetWriter tw, List<StructureDefinition> profiles, String sliceName) throws FHIRException {
VariableForProfiling var = null;
if (tgt.hasContext()) {
var = vars.get(VariableMode.OUTPUT, tgt.getContext());
if (var == null)
throw new FHIRException("Rule \"" + ruleId + "\": target context not known: " + tgt.getContext());
if (!tgt.hasElement())
throw new FHIRException("Rule \"" + ruleId + "\": Not supported yet");
}
TypeDetails type = null;
if (tgt.hasTransform()) {
type = analyseTransform(context, map, tgt, var, vars);
} else {
Property vp = var.getProperty().getBaseProperty().getChild(tgt.getElement(), tgt.getElement());
if (vp == null)
throw new FHIRException("Unknown Property " + tgt.getElement() + " on " + var.getProperty().getPath());
type = new TypeDetails(CollectionStatus.SINGLETON, vp.getType(tgt.getElement()));
}
if (tgt.getTransform() == StructureMapTransform.CREATE) {
String s = getParamString(vars, tgt.getParameter().get(0));
if (worker.getResourceNames().contains(s))
tw.newResource(tgt.getVariable(), s);
} else {
boolean mapsSrc = false;
for (StructureMapGroupRuleTargetParameterComponent p : tgt.getParameter()) {
DataType pr = p.getValue();
if (pr instanceof IdType && ((IdType) pr).asStringValue().equals(tv))
mapsSrc = true;
}
if (mapsSrc) {
if (var == null)
throw new Error("Rule \"" + ruleId + "\": Attempt to assign with no context");
tw.valueAssignment(tgt.getContext(), var.getProperty().getPath() + "." + tgt.getElement() + getTransformSuffix(tgt.getTransform()));
} else if (tgt.hasContext()) {
if (isSignificantElement(var.getProperty(), tgt.getElement())) {
String td = describeTransform(tgt);
if (td != null)
tw.keyAssignment(tgt.getContext(), var.getProperty().getPath() + "." + tgt.getElement() + " = " + td);
}
}
}
DataType fixed = generateFixedValue(tgt);
PropertyWithType prop = updateProfile(var, tgt.getElement(), type, map, profiles, sliceName, fixed, tgt);
if (tgt.hasVariable())
if (tgt.hasElement())
vars.add(VariableMode.OUTPUT, tgt.getVariable(), prop);
else
vars.add(VariableMode.OUTPUT, tgt.getVariable(), prop);
}
use of org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method describeTransformCCorC.
@SuppressWarnings("rawtypes")
private String describeTransformCCorC(StructureMapGroupRuleTargetComponent tgt) throws FHIRException {
if (tgt.getParameter().size() < 2)
return null;
DataType p1 = tgt.getParameter().get(0).getValue();
DataType p2 = tgt.getParameter().get(1).getValue();
if (p1 instanceof IdType || p2 instanceof IdType)
return null;
if (!(p1 instanceof PrimitiveType) || !(p2 instanceof PrimitiveType))
return null;
String uri = ((PrimitiveType) p1).asStringValue();
String code = ((PrimitiveType) p2).asStringValue();
if (Utilities.noString(uri))
throw new FHIRException("Describe Transform, but the uri is blank");
if (Utilities.noString(code))
throw new FHIRException("Describe Transform, but the code is blank");
Coding c = buildCoding(uri, code);
return TerminologyRenderer.describeSystem(c.getSystem()) + "#" + c.getCode() + (c.hasDisplay() ? "(" + c.getDisplay() + ")" : "");
}
use of org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetComponent 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.StructureMapGroupRuleTargetComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method executeRule.
private void executeRule(String indent, TransformContext context, StructureMap map, Variables vars, StructureMapGroupComponent group, StructureMapGroupRuleComponent rule, boolean atRoot) throws FHIRException {
log(indent + "rule : " + rule.getName() + "; vars = " + vars.summary());
Variables srcVars = vars.copy();
if (rule.getSource().size() != 1)
throw new FHIRException("Rule \"" + rule.getName() + "\": not handled yet");
List<Variables> source = processSource(rule.getName(), context, srcVars, rule.getSource().get(0), map.getUrl(), indent);
if (source != null) {
for (Variables v : source) {
for (StructureMapGroupRuleTargetComponent t : rule.getTarget()) {
processTarget(rule.getName(), context, v, map, group, t, rule.getSource().size() == 1 ? rule.getSourceFirstRep().getVariable() : null, atRoot, vars);
}
if (rule.hasRule()) {
for (StructureMapGroupRuleComponent childrule : rule.getRule()) {
executeRule(indent + " ", context, map, v, group, childrule, false);
}
} else if (rule.hasDependent()) {
for (StructureMapGroupRuleDependentComponent dependent : rule.getDependent()) {
executeDependency(indent + " ", context, map, v, group, dependent);
}
} else if (rule.getSource().size() == 1 && rule.getSourceFirstRep().hasVariable() && rule.getTarget().size() == 1 && rule.getTargetFirstRep().hasVariable() && rule.getTargetFirstRep().getTransform() == StructureMapTransform.CREATE && !rule.getTargetFirstRep().hasParameter()) {
// simple inferred, map by type
System.out.println(v.summary());
Base src = v.get(VariableMode.INPUT, rule.getSourceFirstRep().getVariable());
Base tgt = v.get(VariableMode.OUTPUT, rule.getTargetFirstRep().getVariable());
String srcType = src.fhirType();
String tgtType = tgt.fhirType();
ResolvedGroup defGroup = resolveGroupByTypes(map, rule.getName(), group, srcType, tgtType);
Variables vdef = new Variables();
vdef.add(VariableMode.INPUT, defGroup.target.getInput().get(0).getName(), src);
vdef.add(VariableMode.OUTPUT, defGroup.target.getInput().get(1).getName(), tgt);
executeGroup(indent + " ", context, defGroup.targetMap, vdef, defGroup.target, false);
}
}
}
}
use of org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method analyseTarget.
private void analyseTarget(String ruleId, TransformContext context, VariablesForProfiling vars, StructureMap map, StructureMapGroupRuleTargetComponent tgt, String tv, TargetWriter tw, List<StructureDefinition> profiles, String sliceName) throws Exception {
VariableForProfiling var = null;
if (tgt.hasContext()) {
var = vars.get(VariableMode.OUTPUT, tgt.getContext());
if (var == null)
throw new Exception("Rule \"" + ruleId + "\": target context not known: " + tgt.getContext());
if (!tgt.hasElement())
throw new Exception("Rule \"" + ruleId + "\": Not supported yet");
}
TypeDetails type = null;
if (tgt.hasTransform()) {
type = analyseTransform(context, map, tgt, var, vars);
// profiling: dest.setProperty(tgt.getElement().hashCode(), tgt.getElement(), v);
} else {
Property vp = var.property.baseProperty.getChild(tgt.getElement(), tgt.getElement());
if (vp == null)
throw new Exception("Unknown Property " + tgt.getElement() + " on " + var.property.path);
type = new TypeDetails(CollectionStatus.SINGLETON, vp.getType(tgt.getElement()));
}
if (tgt.getTransform() == StructureMapTransform.CREATE) {
String s = getParamString(vars, tgt.getParameter().get(0));
if (worker.getResourceNames().contains(s))
tw.newResource(tgt.getVariable(), s);
} else {
boolean mapsSrc = false;
for (StructureMapGroupRuleTargetParameterComponent p : tgt.getParameter()) {
Type pr = p.getValue();
if (pr instanceof IdType && ((IdType) pr).asStringValue().equals(tv))
mapsSrc = true;
}
if (mapsSrc) {
if (var == null)
throw new Error("Rule \"" + ruleId + "\": Attempt to assign with no context");
tw.valueAssignment(tgt.getContext(), var.property.getPath() + "." + tgt.getElement() + getTransformSuffix(tgt.getTransform()));
} else if (tgt.hasContext()) {
if (isSignificantElement(var.property, tgt.getElement())) {
String td = describeTransform(tgt);
if (td != null)
tw.keyAssignment(tgt.getContext(), var.property.getPath() + "." + tgt.getElement() + " = " + td);
}
}
}
Type fixed = generateFixedValue(tgt);
PropertyWithType prop = updateProfile(var, tgt.getElement(), type, map, profiles, sliceName, fixed, tgt);
if (tgt.hasVariable())
if (tgt.hasElement())
vars.add(VariableMode.OUTPUT, tgt.getVariable(), prop);
else
vars.add(VariableMode.OUTPUT, tgt.getVariable(), prop);
}
Aggregations