public static IEnumerable <Base> Select(this Base input, string expression, FhirEvaluationContext ctx = null)
        {
            var inputNav = input.ToTypedElement();
            var result   = inputNav.Select(expression, ctx ?? FhirEvaluationContext.CreateDefault());

            return(result.ToFhirValues());
        }
Exemple #2
0
        public void ProcessExpression()
        {
            //string Expression = $"(AuditEvent.entity.what.value as Reference).Url";
            string Expression = $"AuditEvent.entity.what.where(resolve() is Patient)";

            //string Expression = $"AuditEvent.entity.what.reference.startsWith('Patient')";
            //string Expression = $"AuditEvent.entity.what.where(reference.startsWith('Patient/') or reference.contains('/Patient/')) | AuditEvent.agent.who.where(reference.startsWith('Patient/') or reference.contains('/Patient/'))";
            //string Expression = $"AuditEvent.entity.what.where(reference.startsWith('Patient'))";
            Console.Write($"FHIR Path Expression: {Expression}");
            FhirXmlParser FhirXmlParser = new FhirXmlParser();
            Resource      Resource      = FhirXmlParser.Parse <Resource>(ResourceStore.FhirResource1);
            //Hl7.Fhir.ElementModel.PocoNavigator Navigator = new Hl7.Fhir.ElementModel.PocoNavigator(Resource);
            var TypedElement = Resource.ToTypedElement();

            try
            {
                FHIRTools.R4.Common.FhirPathTools.FhirPathProcessor FhirPathProcessor = new Common.FhirPathTools.FhirPathProcessor();
                var CompileEx = FhirPathProcessor.ParseExpression(Expression);


                Hl7.FhirPath.FhirPathCompiler.DefaultSymbolTable.AddFhirExtensions();

                var oFhirEvaluationContext = new Hl7.Fhir.FhirPath.FhirEvaluationContext(TypedElement);


                //The resolve() function then also needs to be provided an external resolver delegate that performs the resolve
                //that delegate can be set as below. Here I am providing my own implementation 'IPyroFhirPathResolve.Resolver'
                oFhirEvaluationContext.ElementResolver = FHIRTools.R4.Common.FhirPathTools.CustomFhirPathResolver.Resolver;
                IEnumerable <ITypedElement> ResultList = TypedElement.Select(Expression, oFhirEvaluationContext);
                //IEnumerable<IElementNavigator> ResultList = Navigator.Select(Expression, oFhirEvaluationContext);



                foreach (ITypedElement oElement in ResultList)
                {
                    if (oElement is IFhirValueProvider FhirValueProvider)
                    {
                        if (FhirValueProvider.FhirValue is ResourceReference Ref)
                        {
                            Console.WriteLine($"Found: {Ref.Url.ToString()}");
                        }
                        else
                        {
                            Console.WriteLine($"TYpe was: {FhirValueProvider.FhirValue.GetType().ToString()}");
                        }
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("No Value Found!");
                    }
                }
            }
            catch (Exception Exec)
            {
                Console.WriteLine();
                Console.WriteLine($"Error Message: {Exec.Message}");
            }
        }
        public CompiledExpressionOutCome GetCompiledExpression(Resource Resource, ParseExpressionOutCome ParseExpressionOutCome)
        {
            CompiledExpressionOutCome CompiledExpressionOutCome = new CompiledExpressionOutCome();

            CompiledExpressionOutCome.ParseExpressionOutCome = ParseExpressionOutCome;
            Hl7.FhirPath.FhirPathCompiler.DefaultSymbolTable.AddFhirExtensions();
            FhirPathCompiler Compiler = new FhirPathCompiler();

            var PocoNavigator = Resource.ToTypedElement();

            //var PocoNavigator = new Hl7.Fhir.ElementModel.PocoNavigator(Resource);
            try
            {
                CompiledExpressionOutCome.CompiledExpression = Compiler.Compile(ParseExpressionOutCome.ExpressionString);
            }
            catch (Exception ex)
            {
                CompiledExpressionOutCome.Ok           = false;
                CompiledExpressionOutCome.ErrorMessage = ex.Message;
                return(CompiledExpressionOutCome);
            }
            if (CompiledExpressionOutCome.CompiledExpression != null)
            {
                try
                {
                    var oFhirEvaluationContext = new Hl7.Fhir.FhirPath.FhirEvaluationContext(PocoNavigator);
                    oFhirEvaluationContext.ElementResolver = CustomFhirPathResolver.Resolver;
                    CompiledExpressionOutCome.Ok           = CompiledExpressionOutCome.CompiledExpression.Predicate(PocoNavigator, oFhirEvaluationContext);
                }
                catch (System.Exception Exec)
                {
                    CompiledExpressionOutCome.Ok           = false;
                    CompiledExpressionOutCome.ErrorMessage = Exec.Message;
                    return(CompiledExpressionOutCome);
                }
            }
            return(CompiledExpressionOutCome);
        }
Exemple #4
0
        public IEnumerable <ITypedElement>?Select(IFhirResourceR4 fhirResource, string Expression)
        {
            ITypedElement TypedElement = fhirResource.R4.ToTypedElement();

            FhirPathCompiler.DefaultSymbolTable.AddFhirExtensions();
            //var oFhirEvaluationContext = new Hl7.Fhir.FhirPath.FhirEvaluationContext(TypedElement);
            var oFhirEvaluationContext = new Hl7.Fhir.FhirPath.FhirEvaluationContext(fhirResource.R4);

            //The resolve() function then also needs to be provided an external resolver delegate that performs the resolve
            //that delegate can be set as below. Here I am providing my own implementation 'IPyroFhirPathResolve.Resolver'
            oFhirEvaluationContext.ElementResolver = IFhirPathResolve.Resolver;



            try
            {
                return(TypedElement.Select(Expression, oFhirEvaluationContext));
            }
            catch (Exception Exec)
            {
                throw new Bug.Common.Exceptions.FhirFatalException(System.Net.HttpStatusCode.InternalServerError, $"Unable to evaluate the SearchParameter FhirPath expression of: {Expression} for FHIR {FhirVersion.R4.GetCode()}. See inner exception for more info.", Exec);
            }
        }
        public static bool IsBoolean(this Base input, string expression, bool value, FhirEvaluationContext ctx = null)
        {
            var inputNav = input.ToTypedElement();

            return(inputNav.IsBoolean(expression, value, ctx ?? FhirEvaluationContext.CreateDefault()));
        }
        public static bool Predicate(this Base input, string expression, FhirEvaluationContext ctx = null)
        {
            var inputNav = input.ToTypedElement();

            return(inputNav.Predicate(expression, ctx ?? FhirEvaluationContext.CreateDefault()));
        }
        public static object Scalar(this Base input, string expression, FhirEvaluationContext ctx = null)
        {
            var inputNav = input.ToTypedElement();

            return(inputNav.Scalar(expression, ctx ?? FhirEvaluationContext.CreateDefault()));
        }
        public static bool IsBoolean(this Base input, string expression, bool value, FhirEvaluationContext ctx = null)
        {
            var inputNav = createNav(input);

            return(inputNav.IsBoolean(expression, value, ctx ?? FhirEvaluationContext.Default));
        }
        public static bool Predicate(this Base input, string expression, FhirEvaluationContext ctx = null)
        {
            var inputNav = input.ToNavigator();

            return(inputNav.Predicate(expression, ctx ?? FhirEvaluationContext.Default));
        }
        public static object Scalar(this Base input, string expression, FhirEvaluationContext ctx = null)
        {
            var inputNav = input.ToNavigator();

            return(inputNav.Scalar(expression, ctx ?? FhirEvaluationContext.Default));
        }