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 void Process() { StringBuilder sb = new StringBuilder(); sb.AppendLine($"{"Resource".PadRight(27, ' ')}, {"Name".PadRight(20, ' ')}, {"Expression".PadRight(30, ' ')}, {"Error".PadRight(40, ' ')}"); sb.AppendLine($"-".PadRight(100, '-')); FhirSpecExamplesTool = new FhirSpecExamplesTool(); FHIRTools.R4.Common.SearchParameterTools SearchParameterTools = new Common.SearchParameterTools(); var SearchParamneterDefList = SearchParameterTools.GetSearchParameterDefinitionList(); FHIRTools.R4.Common.FhirPathTools.FhirPathProcessor FhirPathProcessor = new Common.FhirPathTools.FhirPathProcessor(); foreach (var SearchParam in SearchParamneterDefList) { if (!string.IsNullOrWhiteSpace(SearchParam.Expression)) { var ParseExpressionOutCome = FhirPathProcessor.ParseExpression(SearchParam.Expression); if (!ParseExpressionOutCome.PasreOk) { Console.WriteLine($"SearchParam for Resource {SearchParam.Base.ToString()} with Name {SearchParam.Name} has error. "); Console.WriteLine($"Expression was: {SearchParam.Expression}"); Console.WriteLine($"Error was: {ParseExpressionOutCome.ErrorMessage}"); } else { foreach (var BaseResourceType in SearchParam.Base) { var ExampleList = FhirSpecExamplesTool.GetResourceByResourceType(BaseResourceType.Value); foreach (var Res in ExampleList) { var CompiledExpressionOutrCome = FhirPathProcessor.GetCompiledExpression(Res, ParseExpressionOutCome); if (!CompiledExpressionOutrCome.Ok) { if (string.IsNullOrWhiteSpace(CompiledExpressionOutrCome.ErrorMessage)) { sb.AppendLine($"{Res.ResourceType.GetLiteral().PadRight(27, ' ')}, {SearchParam.Name.PadRight(20, ' ')}, {CompiledExpressionOutrCome.ParseExpressionOutCome.ExpressionString.PadRight(30, ' ')}"); } else { sb.AppendLine($"{Res.ResourceType.GetLiteral().PadRight(27, ' ')}, {SearchParam.Name.PadRight(20, ' ')}, {CompiledExpressionOutrCome.ParseExpressionOutCome.ExpressionString.PadRight(30, ' ')}, {CompiledExpressionOutrCome.ErrorMessage.PadRight(40, ' ')}"); } Console.WriteLine($"Resource {Res.ResourceType.GetLiteral()} with Search Name {SearchParam.Name} has error. "); Console.WriteLine($"Expression was: {CompiledExpressionOutrCome.ParseExpressionOutCome.ExpressionString}"); Console.WriteLine($"Error was: {CompiledExpressionOutrCome.ErrorMessage}"); } } } } } } File.WriteAllText(FilePath, sb.ToString()); }