Search in sources :

Example 26 with Argument

use of org.hl7.fhir.utilities.graphql.Argument in project org.hl7.fhir.core by hapifhir.

the class GraphQLEngine method resolveValues.

private List<Value> resolveValues(Argument arg, int max, String vars) throws EGraphQLException {
    List<Value> result = new ArrayList<Value>();
    for (Value v : arg.getValues()) {
        if (!(v instanceof VariableValue))
            result.add(v);
        else {
            if (vars.contains(":" + v.toString() + ":"))
                throw new EGraphQLException("Recursive reference to variable " + v.toString());
            Argument a = workingVariables.get(v.toString());
            if (a == null)
                throw new EGraphQLException("No value found for variable \"" + v.toString() + "\" in \"" + arg.getName() + "\"");
            List<Value> vl = resolveValues(a, -1, vars + ":" + v.toString() + ":");
            result.addAll(vl);
        }
    }
    if ((max != -1 && result.size() > max))
        throw new EGraphQLException("Only " + Integer.toString(max) + " values are allowed for \"" + arg.getName() + "\", but " + Integer.toString(result.size()) + " enoucntered");
    return result;
}
Also used : Argument(org.hl7.fhir.utilities.graphql.Argument) VariableValue(org.hl7.fhir.utilities.graphql.VariableValue) StringValue(org.hl7.fhir.utilities.graphql.StringValue) ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) VariableValue(org.hl7.fhir.utilities.graphql.VariableValue) Value(org.hl7.fhir.utilities.graphql.Value) NameValue(org.hl7.fhir.utilities.graphql.NameValue) NumberValue(org.hl7.fhir.utilities.graphql.NumberValue) ArrayList(java.util.ArrayList) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Example 27 with Argument

use of org.hl7.fhir.utilities.graphql.Argument in project org.hl7.fhir.core by hapifhir.

the class GraphQLEngine method filterResources.

private List<Resource> filterResources(Argument fhirpath, Bundle bnd) throws EGraphQLException, FHIRException {
    List<Resource> result = new ArrayList<Resource>();
    if (bnd.getEntry().size() > 0) {
        if ((fhirpath == null))
            for (BundleEntryComponent be : bnd.getEntry()) result.add(be.getResource());
        else {
            FHIRPathEngine fpe = new FHIRPathEngine(context);
            ExpressionNode node = fpe.parse(getSingleValue(fhirpath));
            for (BundleEntryComponent be : bnd.getEntry()) if (fpe.evaluateToBoolean(null, be.getResource(), be.getResource(), node))
                result.add(be.getResource());
        }
    }
    return result;
}
Also used : BundleEntryComponent(org.hl7.fhir.r4b.model.Bundle.BundleEntryComponent) ExpressionNode(org.hl7.fhir.r4b.model.ExpressionNode) Resource(org.hl7.fhir.r4b.model.Resource) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) DomainResource(org.hl7.fhir.r4b.model.DomainResource) ArrayList(java.util.ArrayList)

Example 28 with Argument

use of org.hl7.fhir.utilities.graphql.Argument in project org.hl7.fhir.core by hapifhir.

the class GraphQLEngine method processPrimitive.

private void processPrimitive(Argument arg, Base value) {
    String s = value.fhirType();
    if (s.equals("integer") || s.equals("decimal") || s.equals("unsignedInt") || s.equals("positiveInt"))
        arg.addValue(new NumberValue(value.primitiveValue()));
    else if (s.equals("boolean"))
        arg.addValue(new NameValue(value.primitiveValue()));
    else
        arg.addValue(new StringValue(value.primitiveValue()));
}
Also used : NameValue(org.hl7.fhir.utilities.graphql.NameValue) NumberValue(org.hl7.fhir.utilities.graphql.NumberValue) StringValue(org.hl7.fhir.utilities.graphql.StringValue)

Example 29 with Argument

use of org.hl7.fhir.utilities.graphql.Argument in project org.hl7.fhir.core by hapifhir.

the class GraphQLEngine method processCanonicalReference.

private void processCanonicalReference(Resource context, Base source, Field field, ObjectValue target, boolean inheritedList, String suffix) throws EGraphQLException, FHIRException {
    if (!(source instanceof CanonicalType))
        throw new EGraphQLException("Not done yet");
    if (services == null)
        throw new EGraphQLException("Resource Referencing services not provided");
    Reference ref = new Reference(source.primitiveValue());
    ReferenceResolution res = services.lookup(appInfo, context, ref);
    if (res != null) {
        if (targetTypeOk(field.getArguments(), res.getTarget())) {
            Argument arg = target.addField(field.getAlias() + suffix, listStatus(field, inheritedList));
            ObjectValue obj = new ObjectValue();
            arg.addValue(obj);
            processObject((Resource) res.getTargetContext(), (Base) res.getTarget(), obj, field.getSelectionSet(), inheritedList, suffix);
        }
    } else if (!hasArgument(field.getArguments(), "optional", "true"))
        throw new EGraphQLException("Unable to resolve reference to " + ref.getReference());
}
Also used : ObjectValue(org.hl7.fhir.utilities.graphql.ObjectValue) Argument(org.hl7.fhir.utilities.graphql.Argument) Reference(org.hl7.fhir.r4b.model.Reference) ReferenceResolution(org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution) CanonicalType(org.hl7.fhir.r4b.model.CanonicalType) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Example 30 with Argument

use of org.hl7.fhir.utilities.graphql.Argument in project pathling by aehrc.

the class ReverseResolveFunction method invoke.

@Nonnull
@Override
public FhirPath invoke(@Nonnull final NamedFunctionInput input) {
    checkUserInput(input.getInput() instanceof ResourcePath, "Input to " + NAME + " function must be a resource: " + input.getInput().getExpression());
    final ResourcePath inputPath = (ResourcePath) input.getInput();
    final String expression = NamedFunction.expressionFromInput(input, NAME);
    checkUserInput(input.getArguments().size() == 1, "reverseResolve function accepts a single argument: " + expression);
    final FhirPath argument = input.getArguments().get(0);
    checkUserInput(argument instanceof ReferencePath, "Argument to reverseResolve function must be a Reference: " + argument.getExpression());
    final ReferencePath referencePath = (ReferencePath) argument;
    // Check that the input type is one of the possible types specified by the argument.
    final Set<ResourceType> argumentTypes = ((ReferencePath) argument).getResourceTypes();
    final ResourceType inputType = inputPath.getResourceType();
    checkUserInput(argumentTypes.contains(inputType), "Reference in argument to reverseResolve does not support input resource type: " + expression);
    // Do a left outer join from the input to the argument dataset using the reference field in the
    // argument.
    final Column joinCondition = referencePath.getResourceEquality(inputPath);
    final Dataset<Row> dataset = join(referencePath.getDataset(), inputPath.getDataset(), joinCondition, JoinType.RIGHT_OUTER);
    // Check the argument for information about the current resource that it originated from - if it
    // is not present, reverse reference resolution will not be possible.
    final NonLiteralPath nonLiteralArgument = (NonLiteralPath) argument;
    checkUserInput(nonLiteralArgument.getCurrentResource().isPresent(), "Argument to reverseResolve must be an element that is navigable from a " + "target resource type: " + expression);
    final ResourcePath currentResource = nonLiteralArgument.getCurrentResource().get();
    final Optional<Column> thisColumn = inputPath.getThisColumn();
    // TODO: Consider removing in the future once we separate ordering from element ID.
    // Create an synthetic element ID column for reverse resolved resources.
    final Column currentResourceValue = currentResource.getValueColumn();
    final WindowSpec windowSpec = Window.partitionBy(inputPath.getIdColumn(), inputPath.getOrderingColumn()).orderBy(currentResourceValue);
    // row_number() is 1-based, and we use 0-based indexes - thus (minus(1)).
    final Column currentResourceIndex = when(currentResourceValue.isNull(), lit(null)).otherwise(row_number().over(windowSpec).minus(lit(1)));
    // We need to add the synthetic EID column to the parser context so that it can be used within
    // joins in certain situations, e.g. extract.
    final Column syntheticEid = inputPath.expandEid(currentResourceIndex);
    final DatasetWithColumn datasetWithEid = QueryHelpers.createColumn(dataset, syntheticEid);
    input.getContext().getNodeIdColumns().putIfAbsent(expression, datasetWithEid.getColumn());
    final ResourcePath result = currentResource.copy(expression, datasetWithEid.getDataset(), inputPath.getIdColumn(), Optional.of(syntheticEid), currentResource.getValueColumn(), false, thisColumn);
    result.setCurrentResource(currentResource);
    return result;
}
Also used : FhirPath(au.csiro.pathling.fhirpath.FhirPath) ResourceType(org.hl7.fhir.r4.model.Enumerations.ResourceType) ResourcePath(au.csiro.pathling.fhirpath.ResourcePath) DatasetWithColumn(au.csiro.pathling.QueryHelpers.DatasetWithColumn) Column(org.apache.spark.sql.Column) DatasetWithColumn(au.csiro.pathling.QueryHelpers.DatasetWithColumn) ReferencePath(au.csiro.pathling.fhirpath.element.ReferencePath) Row(org.apache.spark.sql.Row) NonLiteralPath(au.csiro.pathling.fhirpath.NonLiteralPath) WindowSpec(org.apache.spark.sql.expressions.WindowSpec) Nonnull(javax.annotation.Nonnull)

Aggregations

Argument (org.hl7.fhir.utilities.graphql.Argument)24 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)22 ArrayList (java.util.ArrayList)21 EGraphQLException (org.hl7.fhir.utilities.graphql.EGraphQLException)20 ObjectValue (org.hl7.fhir.utilities.graphql.ObjectValue)20 StringValue (org.hl7.fhir.utilities.graphql.StringValue)16 FHIRException (org.hl7.fhir.exceptions.FHIRException)8 ReferenceResolution (org.hl7.fhir.utilities.graphql.IGraphQLStorageServices.ReferenceResolution)8 IOException (java.io.IOException)7 List (java.util.List)6 Resource (org.hl7.fhir.r4b.model.Resource)6 NameValue (org.hl7.fhir.utilities.graphql.NameValue)6 NumberValue (org.hl7.fhir.utilities.graphql.NumberValue)6 DomainResource (org.hl7.fhir.r4b.model.DomainResource)5 Nonnull (javax.annotation.Nonnull)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 Row (org.apache.spark.sql.Row)4 SAXException (org.xml.sax.SAXException)4 FhirPath (au.csiro.pathling.fhirpath.FhirPath)3 NonLiteralPath (au.csiro.pathling.fhirpath.NonLiteralPath)3