public SequenceElement(DicomTag sequenceTag, Dictionary <DicomTag, object> dataset, SequenceElement parentIfAny = null)
 {
     SequenceTag   = sequenceTag;
     Dataset       = dataset;
     Parent        = parentIfAny;
     ArraySiblings = new List <SequenceElement>();
 }
Beispiel #2
0
        public SequenceElement[] GetSubset(SequenceElement location)
        {
            if (location.Dataset.ContainsKey(_tag))
            {
                return(ToSequenceElementArray((Dictionary <DicomTag, object>[])location.Dataset[_tag], location));
            }

            return(new SequenceElement[0]);
        }
Beispiel #3
0
        public bool IsMatch(SequenceElement element, DicomTag currentTag)
        {
            //match is '.' so only care if the current value matches
            if (IsCurrentNodeMatch)
            {
                return(IsMatch(element.Dataset[currentTag]));
            }

            //match all elements in the current array of the sequence e.g. [2]
            var toMatchIn = new List <SequenceElement> {
                element
            };

            foreach (string relativeOperator in _relativeOperators)
            {
                //[..] - match array siblings
                if (relativeOperator == "[..]")
                {
                    toMatchIn = toMatchIn.SelectMany(s => s.ArraySiblings).Distinct().ToList();
                }

                //.. - match containing parent of the current Sequence (where not null)
                if (relativeOperator == "..")
                {
                    toMatchIn = toMatchIn.Select(s => s.Parent).Where(p => p != null).Distinct().ToList();
                }

                //distinct it
                toMatchIn = toMatchIn.Distinct().ToList();
            }

            List <object> finalObjects = new List <object>();

            foreach (var navigation in _navigations)
            {
                var newSets = new List <SequenceElement>();

                if (navigation.IsLast)
                {
                    foreach (SequenceElement sequenceElement in toMatchIn)
                    {
                        finalObjects.Add(navigation.GetTags(sequenceElement, null));
                    }
                }
                else
                {
                    foreach (SequenceElement sequenceElement in toMatchIn)
                    {
                        newSets.AddRange(navigation.GetSubset(sequenceElement));
                    }
                }

                toMatchIn = newSets;
            }

            return(finalObjects.Any(IsMatch));
        }
Beispiel #4
0
        /// <summary>
        /// Returns tag (which must be non Sequence tags) value contained in the current sequence (<see cref="SequenceElement"/>).  Optionally only match those that pass the conditional
        /// </summary>
        /// <param name="sequenceElement"></param>
        /// <param name="conditional"></param>
        /// <returns></returns>
        public object GetTags(SequenceElement sequenceElement, TagRelativeConditional conditional)
        {
            if (sequenceElement.Dataset.ContainsKey(_tag))
            {
                if (conditional == null || conditional.IsMatch(sequenceElement, _tag))
                {
                    return(sequenceElement.Dataset[_tag]);
                }
            }

            return(null);
        }
        private IEnumerable <object> GetValues(SequenceElement element, int i)
        {
            List <object> toReturn = new List <object>();

            if (_navigations[i].IsLast)
            {
                var o = _navigations[i].GetTags(element, _conditional);

                if (o is Array a)
                {
                    //if we have a conditional which should be applied to the array elements
                    if (_conditionalMatchesArrayElementsOfMultiplicity)
                    {
                        a = a.Cast <object>().Where(IsMatch).ToArray();
                    }

                    //this last branch matches nothing
                    if (a.Length == 0)
                    {
                        return(new object[0]);
                    }

                    if (a.Length == 1)
                    {
                        toReturn.Add(a.GetValue(0));
                    }
                    else
                    if (!ConcatenateMultiplicity)
                    {
                        throw new TagNavigationException("Found " + a.Length + " multiplicity in leaf tag (ConcatenateMultiplicity is off - append a '&' to turn it on)");
                    }
                    else
                    {
                        toReturn.Add(string.Join(ConcatenateMultiplicitySplitter, a.Cast <object>().Select(s => s.ToString())));
                    }
                }
                else
                if (IsMatch(o))
                {
                    toReturn.Add(o);
                }
            }
            else
            {
                foreach (SequenceElement subSequence in _navigations[i].GetSubset(element))
                {
                    toReturn.AddRange(GetValues(subSequence, i + 1));
                }
            }

            return(toReturn.ToArray());
        }
Beispiel #6
0
        private SequenceElement[] ToSequenceElementArray(IEnumerable <Dictionary <DicomTag, object> > getCSharpValue, SequenceElement location)
        {
            if (getCSharpValue == null)
            {
                return(new SequenceElement[0]);
            }

            var toReturn = getCSharpValue.Select(s => new SequenceElement(_tag, s, location)).ToArray();

            //tell the SequenceElement about all the other elements (including itself) which appear side by side as array siblings in the sequence
            foreach (SequenceElement element in toReturn)
            {
                element.ArraySiblings.AddRange(toReturn);
            }

            return(toReturn);
        }