use of org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method renderRule.
private static void renderRule(StringBuilder b, StructureMapGroupRuleComponent r, int indent) {
for (int i = 0; i < indent; i++) b.append(' ');
boolean canBeAbbreviated = checkisSimple(r);
boolean first = true;
for (StructureMapGroupRuleSourceComponent rs : r.getSource()) {
if (first)
first = false;
else
b.append(", ");
renderSource(b, rs, canBeAbbreviated);
}
if (r.getTarget().size() > 1) {
b.append(" -> ");
first = true;
for (StructureMapGroupRuleTargetComponent rt : r.getTarget()) {
if (first)
first = false;
else
b.append(", ");
if (RENDER_MULTIPLE_TARGETS_ONELINE)
b.append(' ');
else {
b.append("\r\n");
for (int i = 0; i < indent + 4; i++) b.append(' ');
}
renderTarget(b, rt, false);
}
} else if (r.hasTarget()) {
b.append(" -> ");
renderTarget(b, r.getTarget().get(0), canBeAbbreviated);
}
if (r.hasRule()) {
b.append(" then {\r\n");
renderDoco(b, r.getDocumentation());
for (StructureMapGroupRuleComponent ir : r.getRule()) {
renderRule(b, ir, indent + 2);
}
for (int i = 0; i < indent; i++) b.append(' ');
b.append("}");
} else {
if (r.hasDependent()) {
b.append(" then ");
first = true;
for (StructureMapGroupRuleDependentComponent rd : r.getDependent()) {
if (first)
first = false;
else
b.append(", ");
b.append(rd.getName());
b.append("(");
boolean ifirst = true;
for (StringType rdp : rd.getVariable()) {
if (ifirst)
ifirst = false;
else
b.append(", ");
b.append(rdp.asStringValue());
}
b.append(")");
}
}
}
if (r.hasName()) {
String n = ntail(r.getName());
if (!n.startsWith("\""))
n = "\"" + n + "\"";
if (!matchesName(n, r.getSource())) {
b.append(" ");
b.append(n);
}
}
b.append(";");
renderDoco(b, r.getDocumentation());
b.append("\r\n");
}
use of org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method parseTarget.
private void parseTarget(StructureMapGroupRuleComponent rule, FHIRLexer lexer) throws FHIRException {
StructureMapGroupRuleTargetComponent target = rule.addTarget();
String start = lexer.take();
if (lexer.hasToken(".")) {
target.setContext(start);
target.setContextType(StructureMapContextType.VARIABLE);
start = null;
lexer.token(".");
target.setElement(lexer.take());
}
String name;
boolean isConstant = false;
if (lexer.hasToken("=")) {
if (start != null)
target.setContext(start);
lexer.token("=");
isConstant = lexer.isConstant();
name = lexer.take();
} else
name = start;
if ("(".equals(name)) {
// inline fluentpath expression
target.setTransform(StructureMapTransform.EVALUATE);
ExpressionNode node = fpe.parse(lexer);
target.setUserData(MAP_EXPRESSION, node);
target.addParameter().setValue(new StringType(node.toString()));
lexer.token(")");
} else if (lexer.hasToken("(")) {
target.setTransform(StructureMapTransform.fromCode(name));
lexer.token("(");
if (target.getTransform() == StructureMapTransform.EVALUATE) {
parseParameter(target, lexer);
lexer.token(",");
ExpressionNode node = fpe.parse(lexer);
target.setUserData(MAP_EXPRESSION, node);
target.addParameter().setValue(new StringType(node.toString()));
} else {
while (!lexer.hasToken(")")) {
parseParameter(target, lexer);
if (!lexer.hasToken(")"))
lexer.token(",");
}
}
lexer.token(")");
} else if (name != null) {
target.setTransform(StructureMapTransform.COPY);
if (!isConstant) {
String id = name;
while (lexer.hasToken(".")) {
id = id + lexer.take() + lexer.take();
}
target.addParameter().setValue(new IdType(id));
} else
target.addParameter().setValue(readConstant(name, lexer));
}
if (lexer.hasToken("as")) {
lexer.take();
target.setVariable(lexer.take());
}
while (Utilities.existsInList(lexer.getCurrent(), "first", "last", "share", "collate")) {
if (lexer.getCurrent().equals("share")) {
target.addListMode(StructureMapTargetListMode.SHARE);
lexer.next();
target.setListRuleId(lexer.take());
} else {
if (lexer.getCurrent().equals("first"))
target.addListMode(StructureMapTargetListMode.FIRST);
else
target.addListMode(StructureMapTargetListMode.LAST);
lexer.next();
}
}
}
use of org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method ruleToString.
public static String ruleToString(StructureMapGroupRuleComponent r) {
StringBuilder b = new StringBuilder();
renderRule(b, r, 0);
return b.toString();
}
use of org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleComponent 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 Exception {
log(indent + "rule : " + rule.getName());
Variables srcVars = vars.copy();
if (rule.getSource().size() != 1)
throw new Exception("not handled yet");
List<Variables> source = analyseSource(context, srcVars, rule.getSource().get(0));
if (source != null) {
for (Variables v : source) {
for (StructureMapGroupRuleTargetComponent t : rule.getTarget()) {
processTarget(context, v, map, t);
}
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);
}
}
}
}
}
use of org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method renderRule.
private void renderRule(StringBuilder b, StructureMapGroupRuleComponent r, int indent) throws FHIRException {
for (int i = 0; i < indent; i++) b.append(' ');
b.append(r.getName());
b.append(": for ");
boolean first = true;
for (StructureMapGroupRuleSourceComponent rs : r.getSource()) {
if (first)
first = false;
else
b.append(", ");
renderSource(b, rs);
}
if (r.getTarget().size() > 1) {
b.append(" make ");
first = true;
for (StructureMapGroupRuleTargetComponent rt : r.getTarget()) {
if (first)
first = false;
else
b.append(", ");
b.append("\r\n");
for (int i = 0; i < indent + 4; i++) b.append(' ');
renderTarget(b, rt);
}
} else if (r.hasTarget()) {
b.append(" make ");
renderTarget(b, r.getTarget().get(0));
}
if (r.hasRule()) {
b.append(" then {");
renderDoco(b, r.getDocumentation());
for (int i = 0; i < indent; i++) b.append(' ');
b.append("}\r\n");
} else {
if (r.hasDependent()) {
first = true;
for (StructureMapGroupRuleDependentComponent rd : r.getDependent()) {
if (first)
first = false;
else
b.append(", ");
b.append(rd.getName());
b.append("(");
boolean ifirst = true;
for (StringType rdp : rd.getVariable()) {
if (ifirst)
ifirst = false;
else
b.append(", ");
b.append(rd.getVariable());
}
}
}
renderDoco(b, r.getDocumentation());
b.append("\r\n");
}
}
Aggregations