Ejemplo n.º 1
0
 private void testInvalid(Model.Resource resource, ErrorType type, String expression)
 {
     try
     {
         var resourceNav = new PocoNavigator(resource);
         resourceNav.Select(expression);
         throw new Exception();
     }
     catch (FormatException)
     {
         if (type != ErrorType.Syntax)
         {
             throw new Exception();
         }
     }
     catch (InvalidCastException)
     {
         if (type != ErrorType.Semantics)
         {
             throw new Exception();
         }
     }
     catch (InvalidOperationException)
     {
         if (type != ErrorType.Semantics)
         {
             throw new Exception();
         }
     }
 }
Ejemplo n.º 2
0
        private void test(Model.Resource resource, String expression, IEnumerable <XElement> expected)
        {
            var tpXml = FhirSerializer.SerializeToXml(resource);
            var npoco = new PocoNavigator(resource);
            //       FhirPathEvaluatorTest.Render(npoco);

            IEnumerable <IElementNavigator> actual = npoco.Select(expression);

            Assert.Equal(expected.Count(), actual.Count());

            expected.Zip(actual, compare).Count();
        }
Ejemplo n.º 3
0
        private static void ExtractExamplesFromResource(Dictionary <string, int> exampleSearchValues, Resource resource, ModelInfo.SearchParamDefinition index, string key)
        {
            var resourceModel = new PocoNavigator(resource);
            var navigator     = new PocoNavigator(resource);

            try
            {
                var results = resourceModel.Select(index.Expression, new EvaluationContext(navigator));
                if (results.Count() > 0)
                {
                    foreach (var t2 in results)
                    {
                        if (t2 != null)
                        {
                            if (t2 is PocoNavigator && (t2 as PocoNavigator).FhirValue != null)
                            {
                                // Validate the type of data returned against the type of search parameter
                                //    Debug.Write(index.Resource + "." + index.Name + ": ");
                                //    Debug.WriteLine((t2 as FhirPath.ModelNavigator).FhirValue.ToString());// + "\r\n";
                                exampleSearchValues[key]++;
                            }
                            else if (t2.Value is Hl7.FhirPath.ConstantValue)
                            {
                                //    Debug.Write(index.Resource + "." + index.Name + ": ");
                                //    Debug.WriteLine((t2.Value as Hl7.FhirPath.ConstantValue).Value);
                                exampleSearchValues[key]++;
                            }
                            else if (t2.Value is bool)
                            {
                                //    Debug.Write(index.Resource + "." + index.Name + ": ");
                                //    Debug.WriteLine((bool)t2.Value);
                                exampleSearchValues[key]++;
                            }
                            else
                            {
                                Debug.Write(index.Resource + "." + index.Name + ": ");
                                Debug.WriteLine(t2.Value);
                                exampleSearchValues[key]++;
                            }
                        }
                    }
                }
            }
            catch (ArgumentException ex)
            {
                Debug.WriteLine("FATAL: Error parsing expression in search index {0}.{1} {2}\r\n\t{3}", index.Resource, index.Name, index.Expression, ex.Message);
            }
        }
Ejemplo n.º 4
0
        public void ProcessExpression()
        {
            //string Expression = $"(AuditEvent.entity.what.value as Reference).Url";
            string Expression = $"AuditEvent.agent.who.where(resolve() is Patient)";

            //string Expression = $"AuditEvent.entity.where(resolve() is 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.agent.who(reference.startsWith('Patient'))";
            Console.Write($"FHIR Path Expression: {Expression}");
            FhirXmlParser FhirXmlParser = new FhirXmlParser();
            Resource      Resource      = FhirXmlParser.Parse <Resource>(ResourceStore.AuditEvent1);
            PocoNavigator Navigator     = new PocoNavigator(Resource);

            try
            {
                IEnumerable <IElementNavigator> ResultList = Navigator.Select(Expression, new EvaluationContext(Navigator));

                bool FoundValue = false;
                foreach (IElementNavigator oElement in ResultList)
                {
                    FoundValue = true;
                    if (oElement is Hl7.Fhir.ElementModel.PocoNavigator Poco && Poco.FhirValue != null)
                    {
                        Console.WriteLine();
                        if (Poco.FhirValue is ResourceReference Ref)
                        {
                            Console.Write($"Found: {Ref.Url.ToString()}");
                        }
                        else
                        {
                            Console.Write($"Found: {Poco.Value.ToString()}");
                        }
                    }
                }
                if (!FoundValue)
                {
                    Console.WriteLine();
                    Console.WriteLine("No Value Found!");
                }
            }
            catch (Exception Exec)
            {
                Console.WriteLine();
                Console.WriteLine($"Error Message: {Exec.Message}");
            }
        }
Ejemplo n.º 5
0
        public void TestFhirPathPolymporphism()
        {
            var patient = new Hl7.Fhir.Model.Patient()
            {
                Active = false
            };

            patient.Meta = new Meta()
            {
                LastUpdated = new DateTimeOffset(2018, 5, 24, 14, 48, 0, TimeSpan.Zero)
            };
            var nav = new PocoNavigator(patient);

            var result = nav.Select("Resource.meta.lastUpdated");

            Assert.IsNotNull(result.FirstOrDefault());
            Assert.AreEqual(PartialDateTime.Parse("2018-05-24T14:48:00+00:00"), result.First().Value);
        }
Ejemplo n.º 6
0
        protected void ParseResponseBody()
        {
            var    tpXml  = this.Body;
            Bundle Bundle = null;

            if (this.Format == FhirApi.FhirFormat.Xml)
            {
                var parser = new Hl7.Fhir.Serialization.FhirXmlParser();
                Bundle = parser.Parse <Bundle>(tpXml);
            }
            else
            {
                var parser = new Hl7.Fhir.Serialization.FhirJsonParser();
                Bundle = parser.Parse <Bundle>(tpXml);
            }

            IElementNavigator PocoBundel = new PocoNavigator(Bundle);

            foreach (var RelatedPerson in PocoBundel.Select(@"Bundle.entry.select(resource as RelatedPerson)"))
            {
                ApiRelatedPerson ApiRelatedPerson = new ApiRelatedPerson(RelatedPerson);
                ApiRelatedPersonList.Add(ApiRelatedPerson);
            }
        }
Ejemplo n.º 7
0
        public void TestPocoPath()
        {
            // Ensure the FHIR extensions are registered
            ElementNavFhirExtensions.PrepareFhirSymbolTableFunctions();
            FhirPathCompiler.DefaultSymbolTable.Add("shortpathname",
                                                    (object f) =>
            {
                if (f is IEnumerable <IElementNavigator> )
                {
                    object[] bits = (f as IEnumerable <IElementNavigator>).Select(i =>
                    {
                        if (i is PocoNavigator)
                        {
                            return((i as PocoNavigator).ShortPath);
                        }
                        return("?");
                    }).ToArray();
                    return(FhirValueList.Create(bits));
                }
                return(FhirValueList.Create(new object[] { "?" }));
            });

            Patient p = new Patient();

            p.Active = true;
            p.ActiveElement.ElementId = "314";
            p.ActiveElement.AddExtension("http://something.org", new FhirBoolean(false));
            p.ActiveElement.AddExtension("http://something.org", new Integer(314));
            p.Telecom = new List <ContactPoint>();
            p.Telecom.Add(new ContactPoint(ContactPoint.ContactPointSystem.Phone, null, "555-phone"));
            p.Telecom[0].Rank = 1;

            foreach (var item in p.Select("descendants().shortpathname()"))
            {
                System.Diagnostics.Trace.WriteLine(item.ToString());
            }
            var patient = new PocoNavigator(p);

            Assert.AreEqual("Patient", patient.Location);

            patient.MoveToFirstChild();
            Assert.AreEqual("Patient.active[0]", patient.Location);
            Assert.AreEqual("Patient.active", patient.ShortPath);

            patient.MoveToFirstChild();
            Assert.AreEqual("Patient.active[0].id[0]", patient.Location);
            Assert.AreEqual("Patient.active.id", patient.ShortPath);

            Assert.IsTrue(patient.MoveToNext());
            Assert.AreEqual("Patient.active[0].extension[0]", patient.Location);
            Assert.AreEqual("Patient.active.extension[0]", patient.ShortPath);

            PocoNavigator v1 = patient.Clone() as PocoNavigator;

            v1.MoveToFirstChild();
            v1.MoveToNext();
            Assert.AreEqual("Patient.active[0].extension[0].value[0]", v1.Location);
            Assert.AreEqual("Patient.active.extension[0].value", v1.ShortPath);
            Assert.IsFalse(v1.MoveToNext());

            // Ensure that the original navigator hasn't changed
            Assert.AreEqual("Patient.active[0].extension[0]", patient.Location);
            Assert.AreEqual("Patient.active.extension[0]", patient.ShortPath);

            PocoNavigator v2 = patient.Clone() as PocoNavigator;

            v2.MoveToNext();
            v2.MoveToFirstChild();
            v2.MoveToNext();
            Assert.AreEqual("Patient.active[0].extension[1].value[0]", v2.Location);
            Assert.AreEqual("Patient.active.extension[1].value", v2.ShortPath);
            Assert.AreEqual("Patient.active.extension('http://something.org').value", v2.CommonPath);

            PocoNavigator v3 = new PocoNavigator(p);

            v3.MoveToFirstChild(); System.Diagnostics.Trace.WriteLine($"{v3.ShortPath} = {v3.FhirValue.ToString()}");
            v3.MoveToNext(); System.Diagnostics.Trace.WriteLine($"{v3.ShortPath} = {v3.FhirValue.ToString()}");
            // v3.MoveToNext(); System.Diagnostics.Trace.WriteLine($"{v3.ShortPath} = {v3.FhirValue.ToString()}");
            // v3.MoveToNext(); System.Diagnostics.Trace.WriteLine($"{v3.ShortPath} = {v3.FhirValue.ToString()}");
            v3.MoveToFirstChild("system"); System.Diagnostics.Trace.WriteLine($"{v3.ShortPath} = {v3.FhirValue.ToString()}");
            Assert.AreEqual("Patient.telecom[0].system[0]", v3.Location);
            Assert.AreEqual("Patient.telecom[0].system", v3.ShortPath);
            Assert.AreEqual("Patient.telecom.where(system='phone').system", v3.CommonPath);

            // Now check navigation bits
            var v4 = new PocoNavigator(p);

            Assert.AreEqual("Patient.telecom.where(system='phone').system",
                            (v4.Select("Patient.telecom.where(system='phone').system").First() as PocoNavigator).CommonPath);
            v4 = new PocoNavigator(p);
            Assert.AreEqual("Patient.telecom[0].system",
                            (v4.Select("Patient.telecom.where(system='phone').system").First() as PocoNavigator).ShortPath);
            v4 = new PocoNavigator(p);
            Assert.AreEqual("Patient.telecom[0].system[0]",
                            (v4.Select("Patient.telecom.where(system='phone').system").First() as PocoNavigator).Location);
            v4 = new PocoNavigator(p);
            Assert.AreEqual("Patient.telecom.where(system='phone').system",
                            (v4.Select("Patient.telecom[0].system").First() as PocoNavigator).CommonPath);
            v4 = new PocoNavigator(p);
            Assert.AreEqual("Patient.telecom[0].system",
                            (v4.Select("Patient.telecom[0].system").First() as PocoNavigator).ShortPath);
        }
Ejemplo n.º 8
0
        public void Index(Resource Resource, PyroSearchParameters DtoSearchParameters = null)
        {
            this.ResourceType = Resource.ResourceType;
            this.FhirID       = Resource.Id;
            string ResourceName = Resource.ResourceType.GetLiteral();
            IList <DtoServiceSearchParameterLight> SearchParametersList = IServiceSearchParameterCache.GetSearchParameterForResource(ResourceName);

            //Filter the list by only the searech parameters provided, do not inex for all
            if (DtoSearchParameters != null)
            {
                SearchParametersList = SearchParametersList.Where(x => DtoSearchParameters.SearchParametersList.Any(d => d.Id == x.Id)).ToList();
            }

            PocoNavigator Navigator = new PocoNavigator(Resource);

            string Resource_ResourceName = FHIRAllTypes.Resource.GetLiteral();

            foreach (DtoServiceSearchParameterLight SearchParameter in SearchParametersList)
            {
                //Todo: Composite searchParameters are not supported as yet, need to do work to read
                // the sub search parameters of the composite directly fro the SearchParameter resources.
                if (SearchParameter.Type != SearchParamType.Composite)
                {
                    bool SetSearchParameterIndex = true;
                    //if ((SearchParameter.Resource == Resource_ResourceName && SearchParameter.Name == "_id") ||
                    //  (SearchParameter.Resource == Resource_ResourceName && SearchParameter.Name == "_lastUpdated"))
                    //{
                    //  SetSearchParameterIndex = false;
                    //}

                    if (SetSearchParameterIndex)
                    {
                        string Expression = SearchParameter.Expression;
                        if (SearchParameter.Resource == Resource_ResourceName)
                        {
                            //If the Expression is one with a parent resource of Resource then swap it for the actual current resource name
                            //For example make 'Resource._tag' be 'Observation._tag' for Observation resources.
                            Expression = Resource.TypeName + SearchParameter.Expression.TrimStart(Resource_ResourceName.ToCharArray());
                        }

                        IEnumerable <IElementNavigator> ResultList = Navigator.Select(Expression, new EvaluationContext(Navigator));
                        foreach (IElementNavigator oElement in ResultList)
                        {
                            if (oElement != null)
                            {
                                switch (SearchParameter.Type)
                                {
                                case SearchParamType.Number:
                                {
                                    this.IndexQuantityList.AddRange(IIndexSetterFactory.CreateNumberSetter().Set(oElement, SearchParameter));
                                    break;
                                }

                                case SearchParamType.Date:
                                {
                                    this.IndexDateTimeList.AddRange(IIndexSetterFactory.CreateDateTimeSetter().Set(oElement, SearchParameter));
                                    break;
                                }

                                case SearchParamType.String:
                                {
                                    this.IndexStringList.AddRange(IIndexSetterFactory.CreateStringSetter().Set(oElement, SearchParameter));
                                    break;
                                }

                                case SearchParamType.Token:
                                {
                                    this.IndexTokenList.AddRange(IIndexSetterFactory.CreateTokenSetter().Set(oElement, SearchParameter));
                                    break;
                                }

                                case SearchParamType.Reference:
                                {
                                    this.IndexReferenceList.AddRange(IIndexSetterFactory.CreateReferenceSetter().Set(oElement, SearchParameter));
                                    break;
                                }

                                case SearchParamType.Composite:
                                {
                                    break;
                                }

                                case SearchParamType.Quantity:
                                {
                                    this.IndexQuantityList.AddRange(IIndexSetterFactory.CreateQuantitySetter().Set(oElement, SearchParameter));
                                    break;
                                }

                                case SearchParamType.Uri:
                                {
                                    this.IndexUriList.AddRange(IIndexSetterFactory.CreateUriSetter().Set(oElement, SearchParameter));
                                    break;
                                }

                                default:
                                    throw new System.ComponentModel.InvalidEnumArgumentException(SearchParameter.Type.ToString(), (int)SearchParameter.Type, typeof(SearchParamType));
                                }
                            }
                        }
                    }
                }
            }
        }