All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hl7.fhir.r4.hapi.fluentpath.FhirPathR4 Maven / Gradle / Ivy

There is a newer version: 7.0.2-r11
Show newest version
package org.hl7.fhir.r4.hapi.fluentpath;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.support.IValidationSupport;
import ca.uhn.fhir.fhirpath.FhirPathExecutionException;
import ca.uhn.fhir.fhirpath.IFhirPath;
import ca.uhn.fhir.fhirpath.IFhirPathEvaluationContext;
import ca.uhn.fhir.i18n.Msg;
import jakarta.annotation.Nonnull;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.exceptions.PathEngineException;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.r4.hapi.ctx.HapiWorkerContext;
import org.hl7.fhir.r4.model.Base;
import org.hl7.fhir.r4.model.ExpressionNode;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.TypeDetails;
import org.hl7.fhir.r4.model.ValueSet;
import org.hl7.fhir.r4.utils.FHIRPathEngine;
import org.hl7.fhir.r4.utils.FHIRPathUtilityClasses.FunctionDetails;

import java.util.List;
import java.util.Optional;

public class FhirPathR4 implements IFhirPath {

	private final FHIRPathEngine myEngine;

	public FhirPathR4(FhirContext theCtx) {
		IValidationSupport validationSupport = theCtx.getValidationSupport();
		myEngine = new FHIRPathEngine(new HapiWorkerContext(theCtx, validationSupport));
		// These changes are to make the FP evaluation non-strict
		myEngine.setDoNotEnforceAsCaseSensitive(true);
		myEngine.setDoNotEnforceAsSingletonRule(true);
	}

	@SuppressWarnings("unchecked")
	@Override
	public  List evaluate(IBase theInput, String thePath, Class theReturnType) {
		ExpressionNode parsed;
		try {
			parsed = myEngine.parse(thePath);
		} catch (FHIRException e) {
			throw new FhirPathExecutionException(Msg.code(2409) + e);
		}
		return (List) evaluate(theInput, parsed, theReturnType);
	}

	@SuppressWarnings("unchecked")
	@Override
	public  List evaluate(
			IBase theInput, IParsedExpression theParsedExpression, Class theReturnType) {
		ExpressionNode expressionNode = ((ParsedExpression) theParsedExpression).myParsedExpression;
		return (List) evaluate(theInput, expressionNode, theReturnType);
	}

	@Nonnull
	private  List evaluate(
			IBase theInput, ExpressionNode expressionNode, Class theReturnType) {
		List result;
		try {
			result = myEngine.evaluate((Base) theInput, expressionNode);
		} catch (FHIRException e) {
			throw new FhirPathExecutionException(Msg.code(255) + e.getMessage(), e);
		}

		for (IBase next : result) {
			if (!theReturnType.isAssignableFrom(next.getClass())) {
				throw new FhirPathExecutionException(Msg.code(256) + "FhirPath expression returned unexpected type "
						+ next.getClass().getSimpleName() + " - Expected " + theReturnType.getName());
			}
		}
		return result;
	}

	@Override
	public  Optional evaluateFirst(IBase theInput, String thePath, Class theReturnType) {
		return evaluate(theInput, thePath, theReturnType).stream().findFirst();
	}

	@Override
	public  Optional evaluateFirst(
			IBase theInput, IParsedExpression theParsedExpression, Class theReturnType) {
		return evaluate(theInput, theParsedExpression, theReturnType).stream().findFirst();
	}

	@Override
	public IParsedExpression parse(String theExpression) {
		return new ParsedExpression(myEngine.parse(theExpression));
	}

	@Override
	public void setEvaluationContext(@Nonnull IFhirPathEvaluationContext theEvaluationContext) {
		myEngine.setHostServices(new FHIRPathEngine.IEvaluationContext() {

			@Override
			public List resolveConstant(Object appContext, String name, boolean beforeContext)
					throws PathEngineException {
				return null;
			}

			@Override
			public TypeDetails resolveConstantType(Object appContext, String name) throws PathEngineException {
				return null;
			}

			@Override
			public boolean log(String argument, List focus) {
				return false;
			}

			@Override
			public FunctionDetails resolveFunction(String functionName) {
				return null;
			}

			@Override
			public TypeDetails checkFunction(Object appContext, String functionName, List parameters)
					throws PathEngineException {
				return null;
			}

			@Override
			public List executeFunction(
					Object appContext, List focus, String functionName, List> parameters) {
				return null;
			}

			@Override
			public Base resolveReference(Object appContext, String theUrl, Base theRefContext) throws FHIRException {
				return (Base) theEvaluationContext.resolveReference(new IdType(theUrl), theRefContext);
			}

			@Override
			public boolean conformsToProfile(Object appContext, Base item, String url) throws FHIRException {
				return false;
			}

			@Override
			public ValueSet resolveValueSet(Object appContext, String url) {
				return null;
			}
		});
	}

	private static class ParsedExpression implements IParsedExpression {

		private final ExpressionNode myParsedExpression;

		public ParsedExpression(ExpressionNode theParsedExpression) {
			myParsedExpression = theParsedExpression;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy