use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule 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) 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());
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);
}
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(1), tgt.toString()));
tgt.setUserData(MAP_WHERE_EXPRESSION, expr);
}
List<Base> v = fpe.evaluate(vars, 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 " + Integer.toString(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:
throw new Error("Rule \"" + ruleId + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
case APPEND:
throw new Error("Rule \"" + ruleId + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
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.utilities.xml.SchematronWriter.Rule 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) throws FHIRException {
log(indent + "rule : " + rule.getName());
if (rule.getName().contains("CarePlan.participant-unlink"))
System.out.println("debug");
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));
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);
}
if (rule.hasRule()) {
for (StructureMapGroupRuleComponent childrule : rule.getRule()) {
executeRule(indent + " ", context, map, v, group, childrule);
}
} 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
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);
}
}
}
}
use of org.hl7.fhir.utilities.xml.SchematronWriter.Rule 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) 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);
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)
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.utilities.xml.SchematronWriter.Rule 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 Exception {
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 Exception("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.utilities.xml.SchematronWriter.Rule in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method parseRule.
private void parseRule(StructureMap map, List<StructureMapGroupRuleComponent> list, FHIRLexer lexer) throws FHIRException {
StructureMapGroupRuleComponent rule = new StructureMapGroupRuleComponent();
list.add(rule);
rule.setName(lexer.takeDottedToken());
lexer.token(":");
lexer.token("for");
boolean done = false;
while (!done) {
parseSource(rule, lexer);
done = !lexer.hasToken(",");
if (!done)
lexer.next();
}
if (lexer.hasToken("make")) {
lexer.token("make");
done = false;
while (!done) {
parseTarget(rule, lexer);
done = !lexer.hasToken(",");
if (!done)
lexer.next();
}
}
if (lexer.hasToken("then")) {
lexer.token("then");
if (lexer.hasToken("{")) {
lexer.token("{");
if (lexer.hasComment()) {
rule.setDocumentation(lexer.take().substring(2).trim());
}
lexer.skipComments();
while (!lexer.hasToken("}")) {
if (lexer.done())
throw lexer.error("premature termination expecting '}' in nested group");
parseRule(map, rule.getRule(), lexer);
}
lexer.token("}");
} else {
done = false;
while (!done) {
parseRuleReference(rule, lexer);
done = !lexer.hasToken(",");
if (!done)
lexer.next();
}
}
} else if (lexer.hasComment()) {
rule.setDocumentation(lexer.take().substring(2).trim());
}
if (isSimpleSyntax(rule)) {
rule.getSourceFirstRep().setVariable(AUTO_VAR_NAME);
rule.getTargetFirstRep().setVariable(AUTO_VAR_NAME);
// with no parameter - e.g. imply what is to be created
rule.getTargetFirstRep().setTransform(StructureMapTransform.CREATE);
// no dependencies - imply what is to be done based on types
}
lexer.skipComments();
}
Aggregations