Exemple #1
0
        private static IEnumerable <ITypedElement> runRepeat(Closure ctx, IEnumerable <Invokee> arguments)
        {
            var focus  = arguments.First()(ctx, InvokeeFactory.EmptyArgs);
            var lambda = arguments.Skip(1).First();

            var fullResult = new List <ITypedElement>();
            List <ITypedElement> newNodes = new List <ITypedElement>(focus);

            while (newNodes.Any())
            {
                var current = newNodes;
                newNodes = new List <ITypedElement>();

                foreach (ITypedElement element in current)
                {
                    var newFocus   = FhirValueList.Create(element);
                    var newContext = ctx.Nest(newFocus);
                    newContext.SetThis(newFocus);


                    newNodes.AddRange(lambda(newContext, InvokeeFactory.EmptyArgs));
                }

                fullResult.AddRange(newNodes);
            }

            return(fullResult);
        }
Exemple #2
0
        public void TestItemSelection()
        {
            var values = FhirValueList.Create(1, 2, 3, 4, 5, 6, 7);

            Assert.AreEqual((Int64)1, values.Item(0).Single().Value);
            Assert.AreEqual((Int64)3, values.Item(2).Single().Value);
            Assert.AreEqual((Int64)1, values.First().Value);
            Assert.IsFalse(values.Item(100).Any());
        }
Exemple #3
0
        public void CheckTypeDetermination()
        {
            var values = FhirValueList.Create(1, true, "hi", 4.0m, 4.0f, PartialDateTime.Now());


            Test.IsInstanceOfType(values.Item(0).Single().Value, typeof(Int64));
            Test.IsInstanceOfType(values.Item(1).Single().Value, typeof(Boolean));
            Test.IsInstanceOfType(values.Item(2).Single().Value, typeof(String));
            Test.IsInstanceOfType(values.Item(3).Single().Value, typeof(Decimal));
            Test.IsInstanceOfType(values.Item(4).Single().Value, typeof(Decimal));
            Test.IsInstanceOfType(values.Item(5).Single().Value, typeof(PartialDateTime));
        }
Exemple #4
0
        private static IEnumerable <ITypedElement> runWhere(Closure ctx, IEnumerable <Invokee> arguments)
        {
            var focus  = arguments.First()(ctx, InvokeeFactory.EmptyArgs);
            var lambda = arguments.Skip(1).First();

            foreach (ITypedElement element in focus)
            {
                var newFocus   = FhirValueList.Create(element);
                var newContext = ctx.Nest(newFocus);
                newContext.SetThis(newFocus);

                if (lambda(newContext, InvokeeFactory.EmptyArgs).BooleanEval() == true)
                {
                    yield return(element);
                }
            }
        }
Exemple #5
0
        private static IEnumerable <ITypedElement> runSelect(Closure ctx, IEnumerable <Invokee> arguments)
        {
            var focus  = arguments.First()(ctx, InvokeeFactory.EmptyArgs);
            var lambda = arguments.Skip(1).First();

            foreach (ITypedElement element in focus)
            {
                var newFocus   = FhirValueList.Create(element);
                var newContext = ctx.Nest(newFocus);
                newContext.SetThis(newFocus);

                var result = lambda(newContext, InvokeeFactory.EmptyArgs);
                foreach (var resultElement in result)       // implement SelectMany()
                {
                    yield return(resultElement);
                }
            }
        }
Exemple #6
0
        private static IEnumerable <ITypedElement> runAny(Closure ctx, IEnumerable <Invokee> arguments)
        {
            var focus  = arguments.First()(ctx, InvokeeFactory.EmptyArgs);
            var lambda = arguments.Skip(1).First();

            foreach (ITypedElement element in focus)
            {
                var newFocus   = FhirValueList.Create(element);
                var newContext = ctx.Nest(newFocus);
                newContext.SetThis(newFocus);


                var result = lambda(newContext, InvokeeFactory.EmptyArgs).BooleanEval();

                //if (result == null) return FhirValueList.Empty; -> otherwise this would not be where().exists()
                //Patient.identifier.any(use = 'official') would return {} if ANY identifier has no 'use' element. Unexpected behaviour, I think
                if (result == true)
                {
                    return(FhirValueList.Create(true));
                }
            }

            return(FhirValueList.Create(false));
        }
Exemple #7
0
        private static IEnumerable <ITypedElement> runAll(Closure ctx, IEnumerable <Invokee> arguments)
        {
            var focus  = arguments.First()(ctx, InvokeeFactory.EmptyArgs);
            var lambda = arguments.Skip(1).First();

            foreach (ITypedElement element in focus)
            {
                var newFocus   = FhirValueList.Create(element);
                var newContext = ctx.Nest(newFocus);
                newContext.SetThis(newFocus);

                var result = lambda(newContext, InvokeeFactory.EmptyArgs).BooleanEval();
                if (result == null)
                {
                    return(FhirValueList.Empty);
                }
                if (result == false)
                {
                    return(FhirValueList.Create(false));
                }
            }

            return(FhirValueList.Create(true));
        }
        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);
        }
Exemple #9
0
 private static object any2List(object source)
 {
     return(FhirValueList.Create(source));
 }
        public static void PrepareSqlonfhirSybolTableFunctions()
        {
            if (!_added)
            {
                _added = true;

                // Custom function that returns the name of the property, rather than its value
                Hl7.FhirPath.FhirPathCompiler.DefaultSymbolTable.Add("element_def_path", (object f) =>
                {
                    if (f is IEnumerable <IElementNavigator> g)
                    {
                        object[] bits = g.Select(i => _replace.Replace(i.Location, "")).ToArray();
                        return(FhirValueList.Create(bits));
                    }
                    return(FhirValueList.Create(new object[] { "?" }));
                });
                Hl7.FhirPath.FhirPathCompiler.DefaultSymbolTable.Add("propname", (object f) =>
                {
                    if (f is IEnumerable <IElementNavigator> )
                    {
                        object[] bits = (f as IEnumerable <IElementNavigator>).Select(i => i.Name).ToArray();
                        return(FhirValueList.Create(bits));
                    }
                    return(FhirValueList.Create(new object[] { "?" }));
                });
                Hl7.FhirPath.FhirPathCompiler.DefaultSymbolTable.Add("pathname", (object f) =>
                {
                    if (f is IEnumerable <IElementNavigator> )
                    {
                        object[] bits = (f as IEnumerable <IElementNavigator>).Select(i => i.Location).ToArray();
                        return(FhirValueList.Create(bits));
                    }
                    return(FhirValueList.Create(new object[] { "?" }));
                });

                FhirPathCompiler.DefaultSymbolTable.Add("commonpathname", (object f) =>
                {
                    if (f is IEnumerable <IElementNavigator> )
                    {
                        object[] bits = (f as IEnumerable <IElementNavigator>).Select(i =>
                        {
                            if (i is r4.Hl7.Fhir.ElementModel.PocoNavigator)
                            {
                                return((i as r4.Hl7.Fhir.ElementModel.PocoNavigator).CommonPath);
                            }
                            if (i is stu3.Hl7.Fhir.ElementModel.PocoNavigator)
                            {
                                return((i as stu3.Hl7.Fhir.ElementModel.PocoNavigator).CommonPath);
                            }
                            if (i is dstu2.Hl7.Fhir.ElementModel.PocoNavigator)
                            {
                                return((i as dstu2.Hl7.Fhir.ElementModel.PocoNavigator).CommonPath);
                            }
                            return("?");
                        }).ToArray();
                        return(FhirValueList.Create(bits));
                    }
                    return(FhirValueList.Create(new object[] { "?" }));
                });

                FhirPathCompiler.DefaultSymbolTable.Add("shortpathname", (object f) =>
                {
                    if (f is IEnumerable <IElementNavigator> )
                    {
                        object[] bits = (f as IEnumerable <IElementNavigator>).Select(i =>
                        {
                            if (i is r4.Hl7.Fhir.ElementModel.PocoNavigator)
                            {
                                return((i as r4.Hl7.Fhir.ElementModel.PocoNavigator).ShortPath);
                            }
                            if (i is stu3.Hl7.Fhir.ElementModel.PocoNavigator)
                            {
                                return((i as stu3.Hl7.Fhir.ElementModel.PocoNavigator).ShortPath);
                            }
                            if (i is dstu2.Hl7.Fhir.ElementModel.PocoNavigator)
                            {
                                return((i as dstu2.Hl7.Fhir.ElementModel.PocoNavigator).ShortPath);
                            }
                            return("?");
                        }).ToArray();
                        return(FhirValueList.Create(bits));
                    }
                    return(FhirValueList.Create(new object[] { "?" }));
                });

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