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) 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, new StringType(), expr);
items = services.performSearch(context.appInfo, 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());
if (!src.hasElement())
items.add(b);
else {
getChildrenByName(b, src.getElement(), items);
if (items.size() == 0 && src.hasDefaultValue())
items.add(src.getDefaultValue());
}
}
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, item, expr))
remove.add(item);
}
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, item, expr))
throw new FHIRException("Rule \"" + ruleId + "\": Check condition failed");
}
}
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 renderRule.
private static void renderRule(StringBuilder b, StructureMapGroupRuleComponent r, int indent) {
for (int i = 0; i < indent; i++) b.append(' ');
b.append(r.getName());
b.append(" : for ");
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(" make ");
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(" make ");
renderTarget(b, r.getTarget().get(0), canBeAbbreviated);
}
if (!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("}\r\n");
} 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(")");
}
}
}
}
renderDoco(b, r.getDocumentation());
b.append("\r\n");
}
use of org.hl7.fhir.r4b.model.StructureMap.StructureMapGroupRuleSourceComponent in project org.hl7.fhir.core by hapifhir.
the class StructureMapUtilities method parseSource.
private void parseSource(StructureMapGroupRuleComponent rule, FHIRLexer lexer) throws FHIRException {
StructureMapGroupRuleSourceComponent source = rule.addSource();
source.setContext(lexer.take());
if (source.getContext().equals("search") && lexer.hasToken("(")) {
source.setContext("@search");
lexer.take();
ExpressionNode node = fpe.parse(lexer);
source.setUserData(MAP_SEARCH_EXPRESSION, node);
source.setElement(node.toString());
lexer.token(")");
} else if (lexer.hasToken(".")) {
lexer.token(".");
source.setElement(lexer.take());
}
if (lexer.hasToken(":")) {
// type and cardinality
lexer.token(":");
source.setType(lexer.takeDottedToken());
if (!lexer.hasToken("as", "first", "last", "not_first", "not_last", "only_one", "default")) {
source.setMin(lexer.takeInt());
lexer.token("..");
source.setMax(lexer.take());
}
}
if (lexer.hasToken("default")) {
lexer.token("default");
source.setDefaultValue(new StringType(lexer.readConstant("default value")));
}
if (Utilities.existsInList(lexer.getCurrent(), "first", "last", "not_first", "not_last", "only_one"))
source.setListMode(StructureMapSourceListMode.fromCode(lexer.take()));
if (lexer.hasToken("as")) {
lexer.take();
source.setVariable(lexer.take());
}
if (lexer.hasToken("where")) {
lexer.take();
ExpressionNode node = fpe.parse(lexer);
source.setUserData(MAP_WHERE_EXPRESSION, node);
source.setCondition(node.toString());
}
if (lexer.hasToken("check")) {
lexer.take();
ExpressionNode node = fpe.parse(lexer);
source.setUserData(MAP_WHERE_CHECK, node);
source.setCheck(node.toString());
}
}
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 parseSource.
private void parseSource(StructureMapGroupRuleComponent rule, FHIRLexer lexer) throws FHIRException {
StructureMapGroupRuleSourceComponent source = rule.addSource();
source.setContext(lexer.take());
if (source.getContext().equals("search") && lexer.hasToken("(")) {
source.setContext("@search");
lexer.take();
ExpressionNode node = fpe.parse(lexer);
source.setUserData(MAP_SEARCH_EXPRESSION, node);
source.setElement(node.toString());
lexer.token(")");
} else if (lexer.hasToken(".")) {
lexer.token(".");
source.setElement(lexer.take());
}
if (lexer.hasToken(":")) {
// type and cardinality
lexer.token(":");
source.setType(lexer.takeDottedToken());
if (!lexer.hasToken("as", "first", "last", "not_first", "not_last", "only_one", "default")) {
source.setMin(lexer.takeInt());
lexer.token("..");
source.setMax(lexer.take());
}
}
if (lexer.hasToken("default")) {
lexer.token("default");
source.setDefaultValue(new StringType(lexer.readConstant("default value")));
}
if (Utilities.existsInList(lexer.getCurrent(), "first", "last", "not_first", "not_last", "only_one"))
source.setListMode(StructureMapSourceListMode.fromCode(lexer.take()));
if (lexer.hasToken("as")) {
lexer.take();
source.setVariable(lexer.take());
}
if (lexer.hasToken("where")) {
lexer.take();
ExpressionNode node = fpe.parse(lexer);
source.setUserData(MAP_WHERE_EXPRESSION, node);
source.setCondition(node.toString());
}
if (lexer.hasToken("check")) {
lexer.take();
ExpressionNode node = fpe.parse(lexer);
source.setUserData(MAP_WHERE_CHECK, node);
source.setCheck(node.toString());
}
if (lexer.hasToken("log")) {
lexer.take();
ExpressionNode node = fpe.parse(lexer);
source.setUserData(MAP_WHERE_CHECK, node);
source.setLogMessage(node.toString());
}
}
Aggregations