Search in sources :

Example 26 with FHIRPathEngine

use of org.hl7.fhir.r4b.utils.FHIRPathEngine in project org.hl7.fhir.core by hapifhir.

the class EnableWhenEvaluator method isQuestionEnabled.

/**
 * the stack contains a set of QR items that represent the tree of the QR being validated, each tagged with the definition of the item from the Q for the QR being validated
 * <p>
 * the itembeing validated is in the context of the stack. For root items, the stack is empty.
 * <p>
 * The context Questionnaire and QuestionnaireResponse are always available
 */
public boolean isQuestionEnabled(ValidatorHostContext hostContext, QuestionnaireItemComponent qitem, QStack qstack, FHIRPathEngine engine) {
    if (hasExpressionExtension(qitem)) {
        String expr = getExpression(qitem);
        ExpressionNode node = engine.parse(expr);
        return engine.evaluateToBoolean(hostContext, qstack.a, qstack.a, qstack.a, node);
    }
    if (!qitem.hasEnableWhen()) {
        return true;
    }
    List<EnableWhenResult> evaluationResults = new ArrayList<>();
    for (QuestionnaireItemEnableWhenComponent enableCondition : qitem.getEnableWhen()) {
        evaluationResults.add(evaluateCondition(enableCondition, qitem, qstack));
    }
    return checkConditionResults(evaluationResults, qitem);
}
Also used : ExpressionNode(org.hl7.fhir.r5.model.ExpressionNode) ArrayList(java.util.ArrayList) QuestionnaireItemEnableWhenComponent(org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemEnableWhenComponent)

Example 27 with FHIRPathEngine

use of org.hl7.fhir.r4b.utils.FHIRPathEngine in project org.hl7.fhir.core by hapifhir.

the class ProfileValidator method validate.

public List<ValidationMessage> validate(StructureDefinition profile, boolean forBuild) {
    List<ValidationMessage> errors = new ArrayList<ValidationMessage>();
    // must have a FHIR version- GF#3160
    warning(errors, IssueType.BUSINESSRULE, profile.getUrl(), profile.hasFhirVersion(), "Profiles SHOULD state the FHIR Version on which they are based");
    warning(errors, IssueType.BUSINESSRULE, profile.getUrl(), profile.hasVersion(), "Profiles SHOULD state their own version");
    // extensions must be defined
    for (ElementDefinition ec : profile.getDifferential().getElement()) checkExtensions(profile, errors, "differential", ec);
    rule(errors, IssueType.STRUCTURE, profile.getId(), profile.hasSnapshot(), "missing Snapshot at " + profile.getName() + "." + profile.getName());
    for (ElementDefinition ec : profile.getSnapshot().getElement()) checkExtensions(profile, errors, "snapshot", ec);
    if (rule(errors, IssueType.STRUCTURE, profile.getId(), profile.hasSnapshot(), "A snapshot is required")) {
        Hashtable<String, ElementDefinition> snapshotElements = new Hashtable<String, ElementDefinition>();
        for (ElementDefinition ed : profile.getSnapshot().getElement()) {
            snapshotElements.put(ed.getId(), ed);
            checkExtensions(profile, errors, "snapshot", ed);
            for (ElementDefinitionConstraintComponent inv : ed.getConstraint()) {
                if (forBuild) {
                    if (!inExemptList(inv.getKey())) {
                        if (rule(errors, IssueType.BUSINESSRULE, profile.getId() + "::" + ed.getPath() + "::" + inv.getKey(), inv.hasExpression(), "The invariant has no FHIR Path expression (" + inv.getXpath() + ")")) {
                            try {
                                // , inv.hasXpath() && inv.getXpath().startsWith("@value")
                                new FHIRPathEngine(context).check(null, profile.getType(), ed.getPath(), inv.getExpression());
                            } catch (Exception e) {
                            // rule(errors, IssueType.STRUCTURE, profile.getId()+"::"+ed.getPath()+"::"+inv.getId(), exprExt != null, e.getMessage());
                            }
                        }
                    }
                }
            }
        }
        if (snapshotElements != null) {
            for (ElementDefinition diffElement : profile.getDifferential().getElement()) {
                if (diffElement == null)
                    throw new Error("Diff Element is null - this is not an expected thing");
                ElementDefinition snapElement = snapshotElements.get(diffElement.getId());
                if (snapElement != null) {
                    // Happens with profiles in the main build - should be able to fix once snapshot generation is fixed - Lloyd
                    warning(errors, IssueType.BUSINESSRULE, diffElement.getId(), !checkMustSupport || snapElement.hasMustSupport(), "Elements included in the differential should declare mustSupport");
                    if (checkAggregation) {
                        for (TypeRefComponent type : snapElement.getType()) {
                            if ("http://hl7.org/fhir/Reference".equals(type.getWorkingCode()) || "http://hl7.org/fhir/canonical".equals(type.getWorkingCode())) {
                                warning(errors, IssueType.BUSINESSRULE, diffElement.getId(), type.hasAggregation(), "Elements with type Reference or canonical should declare aggregation");
                            }
                        }
                    }
                }
            }
        }
    }
    return errors;
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) FHIRPathEngine(org.hl7.fhir.r5.utils.FHIRPathEngine) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) ElementDefinitionConstraintComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionConstraintComponent)

Example 28 with FHIRPathEngine

use of org.hl7.fhir.r4b.utils.FHIRPathEngine 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.r5.model.Bundle.BundleEntryComponent) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource)

Example 29 with FHIRPathEngine

use of org.hl7.fhir.r4b.utils.FHIRPathEngine in project org.hl7.fhir.core by hapifhir.

the class GraphQLEngine method execute.

@Override
public void execute() throws EGraphEngine, EGraphQLException, FHIRException {
    if (graphQL == null)
        throw new EGraphEngine("Unable to process graphql - graphql document missing");
    fpe = new FHIRPathEngine(this.context);
    magicExpression = new ExpressionNode(0);
    output = new GraphQLResponse();
    Operation op = null;
    // todo: initial conditions
    if (!Utilities.noString(graphQL.getOperationName())) {
        op = graphQL.getDocument().operation(graphQL.getOperationName());
        if (op == null)
            throw new EGraphEngine("Unable to find operation \"" + graphQL.getOperationName() + "\"");
    } else if ((graphQL.getDocument().getOperations().size() == 1))
        op = graphQL.getDocument().getOperations().get(0);
    else
        throw new EGraphQLException("No operation name provided, so expected to find a single operation");
    if (op.getOperationType() == OperationType.qglotMutation)
        throw new EGraphQLException("Mutation operations are not supported (yet)");
    checkNoDirectives(op.getDirectives());
    processVariables(op);
    if (focus == null)
        processSearch(output, op.getSelectionSet(), false, "");
    else
        processObject(focus, focus, output, op.getSelectionSet(), false, "");
}
Also used : EGraphEngine(org.hl7.fhir.utilities.graphql.EGraphEngine) GraphQLResponse(org.hl7.fhir.utilities.graphql.GraphQLResponse) Operation(org.hl7.fhir.utilities.graphql.Operation) EGraphQLException(org.hl7.fhir.utilities.graphql.EGraphQLException)

Example 30 with FHIRPathEngine

use of org.hl7.fhir.r4b.utils.FHIRPathEngine in project org.hl7.fhir.core by hapifhir.

the class SnapShotGenerationTests method test.

@SuppressWarnings("deprecation")
@ParameterizedTest(name = "{index}: file {0}")
@MethodSource("data")
public void test(String name, TestScriptTestComponent test, SnapShotGenerationTestsContext context) throws IOException, FHIRException {
    if (TestingUtilities.context == null)
        TestingUtilities.context = SimpleWorkerContext.fromPack(Utilities.path(TestingUtilities.home(), "publish", "definitions.xml.zip"));
    if (fp == null)
        fp = new FHIRPathEngine(TestingUtilities.context);
    fp.setHostServices(context);
    resolveFixtures(context);
    SetupActionOperationComponent op = test.getActionFirstRep().getOperation();
    StructureDefinition source = (StructureDefinition) context.fetchFixture(op.getSourceId());
    StructureDefinition base = getSD(source.getBaseDefinition(), context);
    StructureDefinition output = source.copy();
    ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context, null, null);
    pu.setIds(source, false);
    pu.generateSnapshot(base, output, source.getUrl(), source.getName());
    context.fixtures.put(op.getResponseId(), output);
    context.snapshots.put(output.getUrl(), output);
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(Utilities.path("[tmp]"), op.getResponseId() + ".xml")), output);
    // ok, now the asserts:
    for (int i = 1; i < test.getAction().size(); i++) {
        SetupActionAssertComponent a = test.getAction().get(i).getAssert();
        Assertions.assertTrue(fp.evaluateToBoolean(source, source, a.getExpression()), a.getLabel() + ": " + a.getDescription());
    }
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) StructureDefinition(org.hl7.fhir.dstu3.model.StructureDefinition) FHIRPathEngine(org.hl7.fhir.dstu3.utils.FHIRPathEngine) ProfileUtilities(org.hl7.fhir.dstu3.conformance.ProfileUtilities) FileOutputStream(java.io.FileOutputStream) SetupActionOperationComponent(org.hl7.fhir.dstu3.model.TestScript.SetupActionOperationComponent) SetupActionAssertComponent(org.hl7.fhir.dstu3.model.TestScript.SetupActionAssertComponent) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

ArrayList (java.util.ArrayList)9 FHIRPathEngine (org.hl7.fhir.r5.utils.FHIRPathEngine)9 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)8 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)7 InstanceValidator (org.hl7.fhir.validation.instance.InstanceValidator)6 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)5 BeforeAll (org.junit.jupiter.api.BeforeAll)5 ExpressionNode (org.hl7.fhir.dstu2016may.model.ExpressionNode)4 FHIRPathEngine (org.hl7.fhir.dstu2016may.utils.FHIRPathEngine)4 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)4 IOException (java.io.IOException)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)3 ExpressionNode (org.hl7.fhir.r4b.model.ExpressionNode)3 FileNotFoundException (java.io.FileNotFoundException)2 Base (org.hl7.fhir.dstu2016may.model.Base)2 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)2 DomainResource (org.hl7.fhir.r4b.model.DomainResource)2 Resource (org.hl7.fhir.r4b.model.Resource)2 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)2 FHIRPathEngine (org.hl7.fhir.r4b.utils.FHIRPathEngine)2