use of org.hl7.fhir.r4.model.Expression in project org.hl7.fhir.core by hapifhir.
the class GraphQLEngine method processValues.
private void processValues(Resource context, Selection sel, Property prop, ObjectValue target, List<Base> values, boolean extensionMode, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
boolean il = false;
Argument arg = null;
ExpressionNode expression = null;
if (sel.getField().hasDirective("slice")) {
Directive dir = sel.getField().directive("slice");
String s = ((StringValue) dir.getArguments().get(0).getValues().get(0)).getValue();
if (s.equals("$index"))
expression = magicExpression;
else
expression = fpe.parse(s);
}
if (// special: instruction to drop this node...
sel.getField().hasDirective("flatten"))
il = prop.isList() && !sel.getField().hasDirective("first");
else if (sel.getField().hasDirective("first")) {
if (expression != null)
throw new FHIRException("You cannot mix @slice and @first");
arg = target.addField(sel.getField().getAlias() + suffix, listStatus(sel.getField(), inheritedList));
} else if (expression == null)
arg = target.addField(sel.getField().getAlias() + suffix, listStatus(sel.getField(), prop.isList() || inheritedList));
int index = 0;
for (Base value : values) {
String ss = "";
if (expression != null) {
if (expression == magicExpression)
ss = suffix + '.' + Integer.toString(index);
else
ss = suffix + '.' + fpe.evaluateToString(null, null, null, value, expression);
if (!sel.getField().hasDirective("flatten"))
arg = target.addField(sel.getField().getAlias() + suffix, listStatus(sel.getField(), prop.isList() || inheritedList));
}
if (value.isPrimitive() && !extensionMode) {
if (!sel.getField().getSelectionSet().isEmpty())
throw new EGraphQLException("Encountered a selection set on a scalar field type");
processPrimitive(arg, value);
} else {
if (sel.getField().getSelectionSet().isEmpty())
throw new EGraphQLException("No Fields selected on a complex object");
if (arg == null)
processObject(context, value, target, sel.getField().getSelectionSet(), il, ss);
else {
ObjectValue n = new ObjectValue();
arg.addValue(n);
processObject(context, value, n, sel.getField().getSelectionSet(), il, ss);
}
}
if (sel.getField().hasDirective("first"))
return;
index++;
}
}
use of org.hl7.fhir.r4.model.Expression in project org.hl7.fhir.core by hapifhir.
the class GraphDefinitionEngine method processLinkPath.
private void processLinkPath(String focusPath, Resource focus, GraphDefinitionLinkComponent link, int depth) {
String path = focusPath + " -> " + link.getPath();
check(link.hasPath(), "Path is needed at " + path);
check(!link.hasSliceName(), "SliceName is not yet supported at " + path);
ExpressionNode node;
if (link.getPathElement().hasUserData(TAG_NAME)) {
node = (ExpressionNode) link.getPathElement().getUserData(TAG_NAME);
} else {
node = engine.parse(link.getPath());
link.getPathElement().setUserData(TAG_NAME, node);
}
List<Base> matches = engine.evaluate(null, focus, focus, focus, node);
check(!validating || matches.size() >= (link.hasMin() ? link.getMin() : 0), "Link at path " + path + " requires at least " + link.getMin() + " matches, but only found " + matches.size());
check(!validating || matches.size() <= (link.hasMax() ? Integer.parseInt(link.getMax()) : Integer.MAX_VALUE), "Link at path " + path + " requires at most " + link.getMax() + " matches, but found " + matches.size());
for (Base sel : matches) {
// todo: should a URL be ok?
check(sel.fhirType().equals("Reference"), "Selected node from an expression must be a Reference");
ReferenceResolution res = services.lookup(appInfo, focus, (Reference) sel);
if (res != null) {
// todo
check(res.getTargetContext() != focus, "how to handle contained resources is not yet resolved");
for (GraphDefinitionLinkTargetComponent tl : link.getTarget()) {
if (tl.getType().equals(res.getTarget().fhirType())) {
Resource r = (Resource) res.getTarget();
if (!isInBundle(r)) {
addToBundle(r);
for (GraphDefinitionLinkComponent l : graphDefinition.getLink()) {
processLink(focus.fhirType(), r, l, depth + 1);
}
}
}
}
}
}
}
use of org.hl7.fhir.r4.model.Expression in project org.hl7.fhir.core by hapifhir.
the class Utilities method fhirPathToXPath.
/**
* Only handles simple FHIRPath expressions of the type produced by the validator
*
* @param path
* @return
*/
public static String fhirPathToXPath(String path) {
String[] p = path.split("\\.");
CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(".");
int i = 0;
while (i < p.length) {
String s = p[i];
if (s.contains("[")) {
String si = s.substring(s.indexOf("[") + 1, s.length() - 1);
if (!Utilities.isInteger(si))
throw new FHIRException("The FHIRPath expression '" + path + "' is not valid");
s = s.substring(0, s.indexOf("[")) + "[" + Integer.toString(Integer.parseInt(si) + 1) + "]";
}
if (i < p.length - 1 && p[i + 1].startsWith(".ofType(")) {
i++;
s = s + capitalize(p[i].substring(8, p.length - 1));
}
b.append(s);
i++;
}
return b.toString();
}
use of org.hl7.fhir.r4.model.Expression in project org.hl7.fhir.core by hapifhir.
the class InstanceValidator method buildIdentifierExpression.
private void buildIdentifierExpression(ElementDefinition ed, StringBuilder expression, String discriminator, Identifier ii) throws DefinitionException {
if (ii.hasExtension())
throw new DefinitionException(context.formatMessage(I18nConstants.UNSUPPORTED_IDENTIFIER_PATTERN__EXTENSIONS_ARE_NOT_ALLOWED__FOR_DISCRIMINATOR_FOR_SLICE_, discriminator, ed.getId()));
boolean first = true;
expression.append(discriminator + ".where(");
if (ii.hasSystem()) {
first = false;
expression.append("system = '" + ii.getSystem() + "'");
}
if (ii.hasValue()) {
if (first)
first = false;
else
expression.append(" and ");
expression.append("value = '" + ii.getValue() + "'");
}
if (ii.hasUse()) {
if (first)
first = false;
else
expression.append(" and ");
expression.append("use = '" + ii.getUse() + "'");
}
if (ii.hasType()) {
if (first)
first = false;
else
expression.append(" and ");
buildCodeableConceptExpression(ed, expression, TYPE, ii.getType());
}
if (first) {
throw new DefinitionException(context.formatMessage(I18nConstants.UNSUPPORTED_IDENTIFIER_PATTERN_NO_PROPERTY_NOT_SUPPORTED_FOR_DISCRIMINATOR_FOR_SLICE, discriminator, ed.getId(), ii.fhirType()));
}
expression.append(").exists()");
}
use of org.hl7.fhir.r4.model.Expression in project org.hl7.fhir.core by hapifhir.
the class InstanceValidator method buildCodingExpression.
private void buildCodingExpression(ElementDefinition ed, StringBuilder expression, String discriminator, Coding c) throws DefinitionException {
if (c.hasExtension())
throw new DefinitionException(context.formatMessage(I18nConstants.UNSUPPORTED_CODEABLECONCEPT_PATTERN__EXTENSIONS_ARE_NOT_ALLOWED__FOR_DISCRIMINATOR_FOR_SLICE_, discriminator, ed.getId()));
expression.append(discriminator + ".where(");
boolean first = true;
if (c.hasSystem()) {
first = false;
expression.append("system = '" + c.getSystem() + "'");
}
if (c.hasVersion()) {
if (first)
first = false;
else
expression.append(" and ");
expression.append("version = '" + c.getVersion() + "'");
}
if (c.hasCode()) {
if (first)
first = false;
else
expression.append(" and ");
expression.append("code = '" + c.getCode() + "'");
}
if (c.hasDisplay()) {
if (first)
first = false;
else
expression.append(" and ");
expression.append("display = '" + c.getDisplay() + "'");
}
if (first) {
throw new DefinitionException(context.formatMessage(I18nConstants.UNSUPPORTED_IDENTIFIER_PATTERN_NO_PROPERTY_NOT_SUPPORTED_FOR_DISCRIMINATOR_FOR_SLICE, discriminator, ed.getId(), c.fhirType()));
}
expression.append(").exists()");
}
Aggregations