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

            foreach (ITypedElement element in focus)
            {
                var newFocus   = ElementNode.CreateList(element);
                var newContext = ctx.Nest(newFocus);
                newContext.SetThis(newFocus);
                newContext.SetIndex(ElementNode.CreateList(index));
                index++;

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

                //if (result == null) return ElementNode.EmptyList; -> 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(ElementNode.CreateList(true));
                }
            }

            return(ElementNode.CreateList(false));
        }
        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   = ElementNode.CreateList(element);
                    var newContext = ctx.Nest(newFocus);
                    newContext.SetThis(newFocus);


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

                fullResult.AddRange(newNodes);
            }

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

            foreach (ITypedElement element in focus)
            {
                var newFocus   = ElementNode.CreateList(element);
                var newContext = ctx.Nest(newFocus);
                newContext.SetThis(newFocus);
                newContext.SetIndex(ElementNode.CreateList(index));
                index++;

                var result = lambda(newContext, InvokeeFactory.EmptyArgs).BooleanEval();
                if (result == null)
                {
                    return(ElementNode.EmptyList);
                }
                if (result == false)
                {
                    return(ElementNode.CreateList(false));
                }
            }

            return(ElementNode.CreateList(true));
        }
        public void TestExclude()
        {
            var left  = ElementNode.CreateList(1, 3, 3, 5, 6);
            var right = ElementNode.CreateList(5, 6);

            CollectionAssert.AreEqual(ElementNode.CreateList(1, 3, 3).ToList(),
                                      left.Exclude(right).ToList());
        }
        public void TestIntersect()
        {
            var left  = ElementNode.CreateList(1, 3, 3, 5, 6);
            var right = ElementNode.CreateList(3, 5, 5, 6, 8);

            CollectionAssert.AreEqual(ElementNode.CreateList(3, 5, 6).ToList(),
                                      left.Intersect(right).ToList());
        }
Exemple #6
0
        public void TestItemSelection()
        {
            var values = ElementNode.CreateList(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 #7
0
        public void CheckTypeDetermination()
        {
            var values = ElementNode.CreateList(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(bool));
            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 #8
0
        public void ConvertToString()
        {
            var inputs = ElementNode.CreateList("hoi", 4L, 3.4m, true, false, PartialTime.Parse("15:47:00+01:00"),
                                                PartialDateTime.Parse("2019-01-11T15:47:00+01:00"));
            var vals = new[] { "hoi", "4", "3.4", "true", "false", "15:47:00+01:00", "2019-01-11T15:47:00+01:00" };

            inputs.Zip(vals, (i, v) => (i, v))
            .ToList()
            .ForEach(c => Assert.AreEqual(c.v, c.i.ToString()));
            inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToString()));
        }
Exemple #9
0
        public void ConvertToInteger()
        {
            var inputs = ElementNode.CreateList(1L, "2", -4L, "-5", "+4", true, false);
            var vals   = new[] { 1L, 2L, -4L, -5L, 4L, 1L, 0L };

            inputs.Zip(vals, (i, v) => (i, v))
            .ToList()
            .ForEach(c => Assert.AreEqual(c.i.ToInteger(), c.v));
            inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToInteger()));

            var wrong = ElementNode.CreateList("2.4", "++6", "2,6", "no", "false", DateTimeOffset.Now).ToList();

            wrong.ForEach(c => Assert.IsNull(c.ToInteger()));
            wrong.ForEach(c => Assert.IsFalse(c.ConvertsToInteger()));
        }
Exemple #10
0
        public void ConvertToDecimal()
        {
            var inputs = ElementNode.CreateList(1L, 2m, "2", "3.14", -4.4m, true, false);
            var vals   = new[] { 1m, 2m, 2m, 3.14m, -4.4m, 1m, 0m };

            inputs.Zip(vals, (i, v) => (i, v))
            .ToList()
            .ForEach(c => Assert.AreEqual(c.i.ToDecimal(), c.v));
            inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToDecimal()));

            var wrong = ElementNode.CreateList("hi", "++6", "2,6", "no", "false", DateTimeOffset.Now).ToList();

            wrong.ForEach(c => Assert.IsNull(c.ToDecimal()));
            wrong.ForEach(c => Assert.IsFalse(c.ConvertsToDecimal()));
        }
Exemple #11
0
        public void TestStringJoin()
        {
            var dummy  = ElementNode.CreateList("This ", "is ", "one ", "sentence", ".");
            var result = dummy.FpJoin(string.Empty);

            Assert.IsNotNull(result);
            Assert.AreEqual("This is one sentence.", result);

            dummy  = ElementNode.CreateList();
            result = dummy.FpJoin(string.Empty);
            Assert.AreEqual(string.Empty, result);

            dummy  = ElementNode.CreateList("This", "is", "a", "separated", "sentence.");
            result = dummy.FpJoin(";");
            Assert.IsNotNull(result);
            Assert.AreEqual("This;is;a;separated;sentence.", result);
        }
        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   = ElementNode.CreateList(element);
                var newContext = ctx.Nest(newFocus);
                newContext.SetThis(newFocus);

                if (lambda(newContext, InvokeeFactory.EmptyArgs).BooleanEval() == true)
                {
                    yield return(element);
                }
            }
        }
Exemple #13
0
        public void ConvertToBoolean()
        {
            var areTrue = ElementNode.CreateList(true, "TruE", "Yes", "y", "t", "1", "1.0", 1L, 1m, 1.0m).ToList();

            areTrue.ForEach(o => Assert.IsTrue(o.ToBoolean().Value));
            areTrue.ForEach(o => Assert.IsTrue(o.ConvertsToBoolean()));

            var areFalse = ElementNode.CreateList(false, "fAlse", "nO", "N", "f", "0", "0.0", 0L, 0m, 0.0m).ToList();

            areFalse.ForEach(o => Assert.IsFalse(o.ToBoolean().Value));
            areFalse.ForEach(o => Assert.IsTrue(o.ConvertsToBoolean()));

            var wrong = ElementNode.CreateList("truex", "falsx", "not", "tr", -1L, 2L, 2.0m, 1.1m).ToList();

            wrong.ForEach(o => Assert.IsNull(o.ToBoolean()));
            wrong.ForEach(o => Assert.IsFalse(o.ConvertsToBoolean()));
        }
Exemple #14
0
        public void ConvertToDateTime()
        {
            var now    = PartialDateTime.Parse("2019-01-11T15:47:00+01:00");
            var inputs = ElementNode.CreateList(new DateTimeOffset(2019, 1, 11, 15, 47, 00, new TimeSpan(1, 0, 0)),
                                                "2019-01", "2019-01-11T15:47:00+01:00");
            var vals = new[] { now, PartialDateTime.Parse("2019-01"), now };

            inputs.Zip(vals, (i, v) => (i, v))
            .ToList()
            .ForEach(c => Assert.AreEqual(c.i.ToDateTime(), c.v));
            inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToDateTime()));

            var wrong = ElementNode.CreateList("hi", 2.6m, false, PartialTime.Parse("16:05:49")).ToList();

            wrong.ForEach(c => Assert.IsNull(c.ToDateTime()));
            wrong.ForEach(c => Assert.IsFalse(c.ConvertsToDateTime()));
        }
        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   = ElementNode.CreateList(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 #16
0
        public void ConvertToQuantity()
        {
            var inputs = ElementNode.CreateList(75L, 75.6m, "30 'wk'", false, true,
                                                new Quantity(80.0m, "kg"));
            var vals = new[] { new Quantity(75m, "1"), new Quantity(75.6m, "1"),
                               new Quantity(30m, "wk"), new Quantity(0.0, "1"),
                               new Quantity(1.0, "1"), new Quantity(80m, "kg") };

            inputs.Zip(vals, (i, v) => (i, v))
            .ToList()
            .ForEach(c => Assert.AreEqual(c.v, c.i.ToQuantity()));
            inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToQuantity()));

            var wrong = ElementNode.CreateList("hi", "++6", "2,6", "no", "false",
                                               DateTimeOffset.Now).ToList();

            wrong.ForEach(c => Assert.IsNull(c.ToQuantity()));
            wrong.ForEach(c => Assert.IsFalse(c.ConvertsToQuantity()));
        }
Exemple #17
0
        public static Closure Root(ITypedElement root, EvaluationContext ctx = null)
        {
            var newContext = new Closure()
            {
                EvaluationContext = ctx ?? EvaluationContext.CreateDefault()
            };

            var input = new[] { root };

            newContext.SetThis(input);
            newContext.SetThat(input);
            newContext.SetIndex(ElementNode.CreateList(0));
            newContext.SetOriginalContext(input);
            if (ctx.Resource != null)
            {
                newContext.SetResource(new[] { ctx.Resource });
            }
            if (ctx.RootResource != null)
            {
                newContext.SetRootResource(new[] { ctx.RootResource });
            }

            return(newContext);
        }
Exemple #18
0
        public void TestStringJoinError()
        {
            var dummy = ElementNode.CreateList("This", "is", "sentence", "with", 1, "number.");

            dummy.FpJoin(string.Empty);
        }
Exemple #19
0
        public void TestPocoPath()
        {
            // Ensure the FHIR extensions are registered
            ElementNavFhirExtensions.PrepareFhirSymbolTableFunctions();
            FhirPathCompiler.DefaultSymbolTable.Add("shortpathname",
                                                    (object f) =>
            {
                if (f is IEnumerable <ITypedElement> )
                {
                    object[] bits = (f as IEnumerable <ITypedElement>).Select(i =>
                    {
                        return(i is PocoElementNode ? (i as PocoElementNode).ShortPath : "?");
                    }).ToArray();
                    return(ElementNode.CreateList(bits));
                }
                return(ElementNode.CreateList("?"));
            });

            Patient p = new Patient
            {
                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>
            {
                new ContactPoint(ContactPoint.ContactPointSystem.Phone, null, "555-phone")
            };
            p.Telecom[0].Rank = 1;

            Assert.IsTrue(new FhirString("Patient.active").IsExactly(p.Select("descendants().shortpathname()").FirstOrDefault()));

            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
            Assert.AreEqual("Patient.telecom[0].system",
                            (p.ToTypedElement().Select("Patient.telecom.where(system='phone').system").First() as PocoElementNode).ShortPath);
            var v4 = new PocoNavigator(p);

            Assert.AreEqual("Patient.telecom[0].system[0]",
                            (v4.Select("Patient.telecom.where(system='phone').system").First()).Location);
        }
 private static object FhirValueListCreate(object[] values)
 {
     return(ElementNode.CreateList(values));
 }
 private static IEnumerable <ITypedElement> any2List(object source) => ElementNode.CreateList(source);
 private static object any2List(object source)
 {
     return(ElementNode.CreateList(source));
 }