use of org.hl7.fhir.r5.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);
}
use of org.hl7.fhir.r5.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;
}
use of org.hl7.fhir.r5.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;
}
use of org.hl7.fhir.r5.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, "");
}
use of org.hl7.fhir.r5.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());
}
}
Aggregations