Esempio n. 1
0
 /// <summary>
 /// Takes a given child element (aChildElement) and parent constraint (aParentTemplateConstraint) and steps through the Children of the parent constraint
 /// to determine if any siblings of the child element exist.
 /// </summary>
 /// <param name="aChildElement">Target element that we need to locate siblings for</param>
 /// <param name="aParentTemplateConstraint">Parent Constraint of the aChildElement</param>
 /// <param name="aParentElement">Parent Element for the aChildElement</param>
 /// <param name="aAddedConstraints">Constraints that have already been added to the child collection, we don't need to parse these.</param>
 static private void AddSiblingElements(DocumentTemplateElement aChildElement, IConstraint aParentTemplateConstraint, DocumentTemplateElement aParentElement, Dictionary <DocumentTemplateElement, IConstraint> aConstraintMap)
 {
     //look at parent to get the siblings
     if (aParentTemplateConstraint.Children != null)
     {
         DocumentTemplateElement          parsedElement   = null;
         DocumentTemplateElementAttribute parsedAttribute = null;
         //walk through the siblings
         foreach (var sibling in aParentTemplateConstraint.Children)
         {
             //have we already added this constraint in a previous iteration (e.g. it's on the main path from the leaf to the root)
             if (sibling.IsBranchIdentifier && !aConstraintMap.ContainsValue(sibling))
             {
                 //parse the context
                 var cp = new ContextParser(sibling.Context);
                 cp.Parse(out parsedElement, out parsedAttribute);
                 //is this an element or an attribute?
                 if ((parsedElement != null) && (!string.IsNullOrEmpty(parsedElement.ElementName)))
                 {
                     parsedElement.IsBranchIdentifier = sibling.IsBranchIdentifier;
                     parsedElement.IsBranch           = sibling.IsBranch;
                     //element, let's add it to the parent element's children so it becomes a sibling of aChildElement
                     parsedElement.Value = sibling.Value;
                     aParentElement.AddElement(parsedElement);
                     AddBranchedAttributes(parsedElement, sibling);
                     aConstraintMap.Add(parsedElement, sibling);
                 }
             }
         }
     }
 }
Esempio n. 2
0
 public ContextBuilder(DocumentTemplateElement aElement, string prefix, bool aIsBranch = false, bool aIncludeLeafElement = false)
 {
     _element            = aElement;
     _prefix             = prefix;
     _isBranch           = aIsBranch;
     _includeLeafElement = aIncludeLeafElement;
 }
        public void GenerateSchematronAssertion_NestedElement_WithAttribute_ConformanceSHALL_CardinalityOneToOne()
        {
            var addrElement = new DocumentTemplateElement("addr");
            var useAttr     = new DocumentTemplateElementAttribute("use", "HP");

            addrElement.AddAttribute(useAttr);
            var element             = new DocumentTemplateElement("state");
            var cdaDocumentTemplate = new DocumentTemplate("urn:hl7-org:v3")
                                      .AddElement(new DocumentTemplateElement("recordTarget")
                                                  .AddElement(new DocumentTemplateElement("patientRole")
                                                              .AddElement(addrElement)
                                                              .AddElement(element)));
            //need to have cardinality on both parent and child elements?
            //how do we tell it to generate cda: on context as in below?
            var cardinality       = CardinalityParser.Parse("1..1");
            var builder           = new AssertionLineBuilder(this.tdb, element, this.igType, this.igTypeSchema).WithCardinality(cardinality).ConformsTo(Conformance.SHALL).WithinContext(@"cda:");
            var assertion         = builder.ToString();
            var expectedAssertion = @"count(cda:state)=1";

            Assert.IsTrue(assertion == expectedAssertion,
                          "Assertion string was not correct. Expected '{0}', Actual '{1}'.", expectedAssertion, assertion);
            var contextBuilder  = new ContextBuilder(useAttr, "cda");
            var context         = contextBuilder.GetRelativeContextString();
            var expectedContext = "cda:addr[@use='HP']";

            Assert.IsTrue(context == expectedContext, "Context is not correct. Expected '{0}', Actual '{1}'", expectedContext, context);
        }
Esempio n. 4
0
        private AssertionLineBuilder CreateAssertionLineBuilderForAttribute(DocumentTemplateElement aParentElement, DocumentTemplateElementAttribute aAttribute, ref string aParentContext, ref IConstraint aConstraint)
        {
            AssertionLineBuilder asb = null;

            if ((aParentElement != null) && (aConstraint.Parent.IsBranch))
            {
                asb         = CreateAssertionLineBuilderForElement(aParentElement, aConstraint.Parent, ref aParentContext);
                aConstraint = aConstraint.Parent;
            }
            else
            {
                asb = new AssertionLineBuilder(this.tdb, aAttribute, this.igType, this.igTypeSchema);

                if (aConstraint.Parent != null)
                {
                    Cardinality cardinality = CardinalityParser.Parse(aConstraint.Parent.Cardinality);
                    if (cardinality.Left == 0)
                    {
                        asb.HasOptionalParentContext();
                    }
                }
            }

            return(asb);
        }
        public void GenerateSchematronAssertion_NestedElement_WithAttribute_NULLFlavor_ConformanceSHALL_CardinalityOneToOne()
        {
            var idElement           = new DocumentTemplateElement("id");
            var nullFlavorAttribute = new DocumentTemplateElementAttribute("nullFlavor", "NI");

            idElement.AddAttribute(nullFlavorAttribute);
            var cdaDocumentTemplate = new DocumentTemplate("urn:hl7-org:v3");

            cdaDocumentTemplate.AddElement(new DocumentTemplateElement("component")
                                           .AddElement(new DocumentTemplateElement("section")
                                                       .AddElement(new DocumentTemplateElement("entry")
                                                                   .AddElement(new DocumentTemplateElement("organizer")
                                                                               .AddElement(new DocumentTemplateElement("performer")
                                                                                           .AddElement(new DocumentTemplateElement("assignedEntity")
                                                                                                       .AddElement(idElement)))))));
            var builder = new AssertionLineBuilder(this.tdb, nullFlavorAttribute, this.igType, this.igTypeSchema);

            builder.ConformsTo(ConformanceParser.Parse("SHALL")).WithCardinality(CardinalityParser.Parse("1..1")).WithinContext("cda:");
            var assertion         = builder.ToString();
            var expectedAssertion = @"cda:id[@nullFlavor='NI']";

            Assert.IsTrue(assertion == expectedAssertion,
                          "Assertion string was not correct. Expected '{0}', Actual '{1}'.", expectedAssertion, assertion);
            var contextBuilder  = new ContextBuilder(idElement.ParentElement, "cda");
            var context         = contextBuilder.GetRelativeContextString();
            var expectedContext = "cda:assignedEntity";

            Assert.IsTrue(context == expectedContext, "Context is not correct. Expected '{0}', Actual '{1}'", expectedContext, context);
        }
Esempio n. 6
0
 static public void AddElementValueAndDataType(string aPrefix, DocumentTemplateElement aElement, IConstraint aTemplateConstraint)
 {
     if (!string.IsNullOrEmpty(aTemplateConstraint.Value))
     {
         if (!string.IsNullOrEmpty(aTemplateConstraint.ValueConformance))
         {
             var valueConformanceType = ConformanceParser.Parse(aTemplateConstraint.ValueConformance);
             var conformanceType      = ConformanceParser.Parse(aTemplateConstraint.Conformance);
             if (valueConformanceType == conformanceType)
             {
                 aElement.Value = !string.IsNullOrEmpty(aTemplateConstraint.Value) ? aTemplateConstraint.Value.Trim() : aTemplateConstraint.Value;
             }
         }
         else
         {
             aElement.Value = !string.IsNullOrEmpty(aTemplateConstraint.Value) ? aTemplateConstraint.Value.Trim() : aTemplateConstraint.Value;
         }
     }
     // Add the data type attribute if one is present on the constraint
     if (!string.IsNullOrEmpty(aTemplateConstraint.DataType))
     {
         if (aPrefix == "cda")
         {
             if (aElement.ElementName != "code")
             {
                 DocumentTemplateElementAttribute dataTypeAttr = new DocumentTemplateElementAttribute("xsi:type");
                 dataTypeAttr.SingleValue = aTemplateConstraint.DataType;
                 aElement.AddAttribute(dataTypeAttr);
             }
         }
     }
 }
Esempio n. 7
0
        static public DocumentTemplateElement CreateParentElementForAttribute(IConstraint aConstraint, DocumentTemplateElementAttribute aAttribute)
        {
            DocumentTemplateElement parentElement = null;

            // Is there a parent constraint that should be used as a context
            if (aConstraint.Parent != null && !string.IsNullOrEmpty(aConstraint.Parent.Context) && !aConstraint.Parent.IsBranch)
            {
                if ((aAttribute != null) && (aAttribute.Element == null)) //we have an attribute, but no element attached. the parent constraint would then be the element.
                {
                    parentElement = new DocumentTemplateElement(aConstraint.Parent.Context);
                    if (aConstraint.Parent.IsBranch)
                    {
                        parentElement.AddAttribute(aAttribute);
                    }
                }
                else if ((aAttribute != null) && (aAttribute.Element != null) && (aConstraint.Parent.Context != aAttribute.Element.ElementName)) //we have an attribute, with an element attached, but the element does not match the parent context
                {
                    parentElement = new DocumentTemplateElement(aConstraint.Parent.Context);
                    parentElement.AddElement(aAttribute.Element);
                }
                else if (aAttribute != null && aAttribute.Element != null)
                {
                    parentElement = aAttribute.Element;
                }
            }
            return(parentElement);
        }
        public void GenerateSchematronAssertion_Element_With_Value_And_AttributeOverride_ConformanceSHALL_CardinalityOneToOne()
        {
            //create cda doc
            var cdaDocumentTemplate = new DocumentTemplate("urn:hl7-org:v3");
            //create code element
            var element = new DocumentTemplateElement("statusCode", "Test");

            element.ElementToAttributeOverrideMapping.Add("statusCode", "code");
            element.ElementToAttributeOverrideMapping.Add("code", "code");
            //add element to doc
            cdaDocumentTemplate.AddElement(element);
            //create schematron assertion line builder
            var builder = new AssertionLineBuilder(this.tdb, element, this.igType, this.igTypeSchema);
            //define cardinality and conformance
            var cardinality = CardinalityParser.Parse("1..1");
            var conformance = ConformanceParser.Parse("SHALL");

            //add element (context comes from doc), conformance, cardinality through fluent interface
            builder.ConformsTo(conformance).WithCardinality(cardinality);
            //generate string
            string assertion = builder.ToString();

            //did we generate the correct string?
            Assert.IsTrue(assertion == @"count(cda:statusCode[@code='Test'])=1", "The generated assertion was not correct. Expected 'count(cda:statusCode[@code='Test'])=1', Actual '{0}'.", assertion);
        }
Esempio n. 9
0
 /// <summary>
 /// If the template constraint is a branch then add the appropriate attributes to the element passed in.
 /// </summary>
 /// <param name="aElement">
 /// Element to create the attributes on
 /// </param>
 /// <param name="aTemplateConstraint">
 /// Constraint to evaluate
 /// </param>
 static public void CheckForBranchedAttributes(DocumentTemplateElement aElement, IConstraint aTemplateConstraint)
 {
     if (aTemplateConstraint.IsBranch || aTemplateConstraint.IsBranchIdentifier) //add attributes
     {
         AddBranchedAttributes(aElement, aTemplateConstraint);
     }
 }
Esempio n. 10
0
        public AssertionLineBuilder WithinContext(DocumentTemplateElement aContextElement, ContextWrapper aWrapper = ContextWrapper.Bracket)
        {
            var contextBuilder = new ContextBuilder(aContextElement, this._prefix);

            _context        = contextBuilder.ToString();
            _contextWrapper = aWrapper;
            return(this);
        }
Esempio n. 11
0
        /// <summary>
        /// Builds the full context from the given aTemplateConstraint forward. If the given aTemplateConstraint has a parent context then this will be built also,
        /// using the aPerspectiveConstraint. aPerspectiveConstraint is always the constraint we are requesting the context for. This ensures that we only traverse
        /// the perspective's children 1 time.
        /// </summary>
        /// <param name="aPrefix"></param>
        /// <param name="aTemplateConstraint"></param>
        /// <param name="aPerspectiveConstraint"></param>
        /// <param name="aIgnoreParent">Specifies whether to walk the the parent tree</param>
        /// <returns></returns>
        private string CreateFullBranchedParentContext(TemplateConstraint aTemplateConstraint, TemplateConstraint aPerspectiveConstraint = null, bool aIgnoreParent = false, bool isTarget = false)
        {
            string constraintParentContext = string.Empty;

            if (aTemplateConstraint.Parent != null && !aIgnoreParent)
            {
                constraintParentContext = CreateFullBranchedParentContext(aTemplateConstraint.ParentConstraint, aTemplateConstraint);
            }

            DocumentTemplateElement          element   = null;
            DocumentTemplateElementAttribute attribute = null;
            ContextParser parser = new ContextParser(aTemplateConstraint.Context);

            parser.Parse(out element, out attribute);

            if (attribute != null) //we are only looking for attributes
            {
                attribute.SingleValue = aTemplateConstraint.Value;
            }

            if (element != null)
            {
                element.IsBranch           = aTemplateConstraint.IsBranch;
                element.IsBranchIdentifier = aTemplateConstraint.IsBranchIdentifier;
                ConstraintToDocumentElementHelper.AddElementValueAndDataType(this.prefix, element, aTemplateConstraint);
            }

            ContextBuilder builder      = ContextBuilder.CreateFromElementAndAttribute(element, attribute, this.prefix);
            StringBuilder  childStrings = new StringBuilder();

            foreach (var child in aTemplateConstraint.ChildConstraints)
            {
                if (aPerspectiveConstraint == null || aPerspectiveConstraint.Id != child.Id) //since we call ourselves recursively, this ensures we only go down the path of the original caller once.
                {
                    if (child.IsBranchIdentifier == true)
                    {
                        childStrings.Append(CreateFullBranchedParentContext(child, aIgnoreParent: true));
                    }
                }
            }

            string context = builder.GetFullyQualifiedContextString() + childStrings;

            if (element != null && aTemplateConstraint.Parent != null)
            {
                if (element.IsBranchIdentifier)
                {
                    context = "[" + context + "]";
                }
                else
                {
                    context = "/" + context;
                }
            }

            return(constraintParentContext + context);
        }
Esempio n. 12
0
        public AssertionLineBuilder(IObjectRepository tdb, DocumentTemplateElement aElement, ImplementationGuideType igType, SimpleSchema igTypeSchema, string prefix = null)
            : this(tdb, igType, igTypeSchema, igType.SchemaPrefix)
        {
            _element = aElement;

            if (!string.IsNullOrEmpty(prefix))
            {
                this._prefix = prefix;
            }
        }
        public void TestCDADocumentTemplate_WithValidNamespace_SingleRootElement_RootElementCountIsOne()
        {
            var cdaDocumentTemplate = new DocumentTemplate("urn:hl7-org:v3");
            var element             = new DocumentTemplateElement("code");

            cdaDocumentTemplate.AddElement(element);
            Assert.IsNotNull(cdaDocumentTemplate.ChildElements, "cdaDocumentTemplate.RootElements is null, expected instance.");
            Assert.AreEqual(cdaDocumentTemplate, element.Template, "CDA Document Template was not set on the element properly.");
            Assert.IsTrue(cdaDocumentTemplate.ChildElements.Count == 1, "Root element count failed, expected 1, actual {0}", cdaDocumentTemplate.ChildElements.Count);
        }
Esempio n. 14
0
        private string CreateParentContextForElement(DocumentTemplateElement aParentElement, DocumentTemplateElement aElement, IConstraint aConstraint)
        {
            string parentContext = string.Empty;

            if (aConstraint.Parent != null && !aConstraint.Parent.IsBranch)
            {
                parentContext = ConstraintToDocumentElementMapper.CreateFullParentContext(this.prefix, aConstraint);
            }
            return(parentContext);
        }
Esempio n. 15
0
        /// <summary>
        /// Helper function which creates an AssertionLineBuilder for an element given the constraint. This function will examine the constraint's value and datatype
        /// and add those to the builder if necessary. Also if aGenerateContext == true then it will add the context of the element.
        /// </summary>
        /// <param name="aElement">Element to base the AssertionLineBuilder from</param>
        /// <param name="aTemplateConstraint">TemplateConstraint which has the necessary properties (e.g. Element Value, Data Type) to add to the AssertionLineBuilder</param>
        /// <param name="aGenerateContext">Flag to determine whether a context should be added as part of the AssertionLineBuilder</param>
        /// <returns>A new AssertionLineBuilder for the aElement passed in</returns>
        private AssertionLineBuilder CreateAssertionLineBuilderForElement(DocumentTemplateElement aElement, IConstraint aTemplateConstraint, ref string aParentContext, bool aGenerateContext = true)
        {
            //add the value and data type
            ConstraintToDocumentElementHelper.AddElementValueAndDataType(this.prefix, aElement, aTemplateConstraint);

            //create builders
            var builder = new AssertionLineBuilder(this.tdb, aElement, this.igType, this.igTypeSchema);

            if (aGenerateContext)
            {
                ContextBuilder contextBuilder = null;

                if (aElement.ParentElement != null)                                           //build the context
                {
                    contextBuilder = new ContextBuilder(aElement.ParentElement, this.prefix); //the context will start from the first parent (or root)
                    builder.WithinContext(string.Format("{0}", contextBuilder.GetFullyQualifiedContextString()));
                }
            }

            var containedTemplates = (from tcr in aTemplateConstraint.References
                                      join t in this.allTemplates on tcr.ReferenceIdentifier equals t.Oid
                                      where tcr.ReferenceType == ConstraintReferenceTypes.Template
                                      select new { Identifier = t.Oid, t.PrimaryContextType });

            foreach (var containedTemplate in containedTemplates)
            {
                builder.ContainsTemplate(containedTemplate.Identifier, containedTemplate.PrimaryContextType);

                if (aTemplateConstraint.Parent != null && aTemplateConstraint.Parent.IsBranch)
                {
                    builder.WithinContext(aParentContext, ContextWrapper.Slash); //put the parent context into the element context, this is a special case where we want the full context within the template assertion
                    aParentContext = string.Empty;                               //clear the parent context b/c we have put it within the element's context
                }
            }

            if (aTemplateConstraint.Parent != null)
            {
                Conformance conformance = ConformanceParser.Parse(aTemplateConstraint.Parent.Conformance);
                if (conformance == Conformance.SHOULD)
                {
                    builder.HasOptionalParentContext();
                }
            }

            //TODO: Refactor this out, hardcoding these special cases for QRDA
            if (this.prefix.ToLower().Contains("cda"))
            {
                aElement.ElementToAttributeOverrideMapping.Add("code", "code");
                aElement.ElementToAttributeOverrideMapping.Add("statusCode", "code");
                aElement.ElementToAttributeOverrideMapping.Add("realmCode", "code");
                aElement.ElementToAttributeOverrideMapping.Add("externalDocument", "classCode");
            }

            return(builder);
        }
        /// <summary>
        /// Uses back-tracing algorithm to go backwards through the tree
        /// Helper function which builds the full parent context for a given template constraint. For example, for the template constraint @code with cda:entryRelationship/cda:observation/cda:code[@code]
        /// this function returns the cda:entryRelationship/cda:observation/cda:code.
        /// </summary>
        /// <param name="aElement">current element to start from</param>
        /// <param name="aTemplateConstraint">constraint which will have its parent chain walked to form path</param>
        /// <param name="aIncludeElementInPath">determines whether we start the path with the element passed in (true) or its parent (false)</param>
        /// <returns>full context string</returns>
        public static string CreateFullParentContext(string aPrefix, IConstraint aTemplateConstraint)
        {
            if (aTemplateConstraint == null)
            {
                return(string.Empty);
            }

            DocumentTemplateElement          firstElement    = null;
            DocumentTemplateElement          newElement      = null;
            DocumentTemplateElement          previousElement = null;
            DocumentTemplateElementAttribute newAttribute    = null;
            IConstraint currentConstraint = aTemplateConstraint.Parent;

            while (currentConstraint != null)
            {
                //parse the context to determine whether this is element or attribute
                var contextParser = new ContextParser(currentConstraint.Context);
                contextParser.Parse(out newElement, out newAttribute);
                newElement.Attributes.Clear();
                if (currentConstraint.IsBranch) //if we hit a branch then we stop b/c we are in the branch's context
                {
                    break;
                }
                if (newElement == null)
                {
                    break;  //there is a broken chain, we have null parent
                }
                //add value and data type (if present)
                ConstraintToDocumentElementHelper.AddElementValueAndDataType(aPrefix, newElement, currentConstraint);
                //chain the previous element to the child collection of this new one
                if (previousElement != null)
                {
                    newElement.AddElement(previousElement);
                }
                //get the leaf node
                if (firstElement == null)
                {
                    firstElement = newElement;
                }
                previousElement = newElement;
                //walk the parent chain
                currentConstraint = currentConstraint.Parent;
            }
            if (firstElement == null)
            {
                return(string.Empty);
            }
            else
            {
                var contextBuilder = new ContextBuilder(firstElement, aPrefix);
                return(contextBuilder.GetFullyQualifiedContextString());
            }
        }
Esempio n. 17
0
        public void GenerateContext_SingleElement_Namespace()
        {
            var element             = new DocumentTemplateElement("code");
            var cdaDocumentTemplate = new DocumentTemplate("urn:hl7-org:v3");

            cdaDocumentTemplate.AddElement(element);
            var contextBuilder = new ContextBuilder(element, "ems");
            var context        = contextBuilder.GetRelativeContextString();
            var expected       = "ems:code";

            Assert.IsTrue(context == expected, "Single element did not generate proper context, expected '{0}', actual '{0}'", expected, context);
        }
Esempio n. 18
0
        public void ParseElementAndAttribute()
        {
            string context = "code/@code";
            DocumentTemplateElement          element   = null;
            DocumentTemplateElementAttribute attribute = null;
            var parser = new ContextParser(context);

            parser.Parse(out element, out attribute);
            Assert.IsNotNull(element, "No element was passed back from the parser.");
            Assert.IsTrue(element.ElementName == "code", "Element name was incorrect. Expected 'code', Actual '{0}'", element.ElementName);
            Assert.IsNotNull(attribute, "No attribute was passed back from the parser.");
            Assert.IsTrue(attribute.AttributeName == "code", "Element name was incorrect. Expected 'code', Actual '{0}'", attribute.AttributeName);
        }
        public void TestCDADocumentTemplate_WithValidNamespace_SingleChildElement_SingleChildElement_ChildCountIsOne()
        {
            var cdaDocumentTemplate = new DocumentTemplate("urn:hl7-org:v3");
            var element             = new DocumentTemplateElement("author");

            Assert.IsNotNull(element.ChildElements, "element.ChildElements is null, expected instance");
            element.AddElement(new DocumentTemplateElement("assignedPerson"));
            cdaDocumentTemplate.AddElement(element);
            Assert.IsNotNull(cdaDocumentTemplate.ChildElements, "cdaDocumentTemplate.RootElements is null, expected instance.");
            Assert.IsTrue(cdaDocumentTemplate.ChildElements.Count == 1, "Root element count failed, expected 1, actual {0}", cdaDocumentTemplate.ChildElements.Count);
            Assert.IsTrue(cdaDocumentTemplate.ChildElements[0].ChildElements.Count == 1, "Child element count failed, expected 1, actual {0}", cdaDocumentTemplate.ChildElements[0].ChildElements.Count);
            Assert.AreEqual(cdaDocumentTemplate.ChildElements[0].ChildElements[0].ParentElement, cdaDocumentTemplate.ChildElements[0], "Child element parent was not set properly.");
        }
Esempio n. 20
0
        public static ContextBuilder CreateFromElementAndAttribute(DocumentTemplateElement aElement, DocumentTemplateElementAttribute aAttribute, string aNameSpace)
        {
            if (aAttribute != null)
            {
                return(new ContextBuilder(aAttribute, aNameSpace));
            }
            if (aElement != null)
            {
                return(new ContextBuilder(aElement, aNameSpace));
            }

            throw new ArgumentException("Must provide a DocumentTemplate Element or an Attribute.");
        }
Esempio n. 21
0
        /// <summary>
        /// Checks parent to see if IsBranch attribute is set. If true then adds the sibling elements.
        /// </summary>
        /// <param name="aChildElement">Target element that we need to locate siblings for</param>
        /// <param name="aParentTemplateConstraint">Parent Constraint of the aChildElement</param>
        /// <param name="aParentElement">Parent Element for the aChildElement</param>
        /// <param name="aAddedConstraints">Constraints that have already been added to the child collection, we don't need to parse these.</param>
        static public void CheckForBranchedSiblingElements(DocumentTemplateElement aChildElement, DocumentTemplateElement aParentElement, IConstraint aParentTemplateConstraint, Dictionary <DocumentTemplateElement, IConstraint> aConstraintMap)
        {
            if ((aChildElement == null) ||
                (aParentElement == null) ||
                (aParentTemplateConstraint == null))
            {
                return;
            }

            if (aParentTemplateConstraint.IsBranch || aParentTemplateConstraint.IsBranchIdentifier)
            {
                AddSiblingElements(aChildElement, aParentTemplateConstraint, aParentElement, aConstraintMap);
            }
        }
Esempio n. 22
0
        public void ParseMultipleElements()
        {
            string context = "entry/observation";
            DocumentTemplateElement          element   = null;
            DocumentTemplateElementAttribute attribute = null;
            var parser = new ContextParser(context);

            parser.Parse(out element, out attribute);
            Assert.IsNotNull(element, "No element was passed back from the parser.");
            Assert.IsTrue(element.ElementName == "observation", "Element name was incorrect. Expected 'observation', Actual '{0}'", element.ElementName);

            Assert.IsNotNull(element.ParentElement, "No parent element was passed back from the parser.");
            Assert.IsTrue(element.ParentElement.ElementName == "entry", "Element name was incorrect. Expected 'entry', Actual '{0}'", element.ElementName);

            Assert.IsNull(attribute, "An attribute was passed back from the parser. Exected null.");
        }
Esempio n. 23
0
        public void GenerateContext_SingleChildElement_2Levels()
        {
            var administrativeCodeElement = new DocumentTemplateElement("administrativeGenderCode");

            administrativeCodeElement.AddAttribute(new DocumentTemplateElementAttribute("code", "20", string.Empty, "MMG-GENDER-CODE-OID"));
            var cdaDocumentTemplate = new DocumentTemplate("urn:hl7-org:v3");

            cdaDocumentTemplate.AddElement(new DocumentTemplateElement("recordTarget")
                                           .AddElement(administrativeCodeElement));
            var contextBuilder = new ContextBuilder(administrativeCodeElement, "cda");
            var context        = contextBuilder.GetFullyQualifiedContextString();
            var expected       = "cda:recordTarget/cda:administrativeGenderCode[@code='20']";

            Assert.IsFalse(string.IsNullOrEmpty(context), "Null or empty string returned by context builder");
            Assert.IsTrue(context == expected, "Context string was not correct, expected '{0}', actual '{1}'", expected, context);
        }
Esempio n. 24
0
        private string BuildStringForSiblingElements(DocumentTemplateElement aElement)
        {
            var sb = new StringBuilder();

            if (aElement.ParentElement != null)
            {
                foreach (var sibling in aElement.ParentElement.ChildElements)
                {
                    if (sibling != aElement)
                    {
                        sb.Append(BuildSingleSiblingElementString(sibling));
                    }
                }
            }

            return(sb.ToString());
        }
Esempio n. 25
0
        public void GenerateContext_SingleElementChildElement_2Levels_SingleAttribute()
        {
            var element   = new DocumentTemplateElement("assignedPerson");
            var attribute = new DocumentTemplateElementAttribute("classCode", "ASSIGNED");

            element.AddAttribute(attribute);
            var cdaDocumentTemplate = new DocumentTemplate("urn:hl7-org:v3");

            cdaDocumentTemplate.AddElement(new DocumentTemplateElement("custodian")
                                           .AddElement(element));
            var contextBuilder = new ContextBuilder(attribute, "cda");
            var context        = contextBuilder.GetRelativeContextString();
            var expected       = "cda:assignedPerson[@classCode='ASSIGNED']";

            Assert.IsFalse(string.IsNullOrEmpty(context), "Null or empty string returned by context builder");
            Assert.IsTrue(context == expected, "Context string was not correct, expected '{0}', actual '{1}'", expected, context);
        }
Esempio n. 26
0
        private string BuildSingleSiblingElementString(DocumentTemplateElement aElement)
        {
            var elementAndAttributeString = string.Format("{0}{1}", aElement.ElementName, BuildStringForAttributes(aElement.Attributes));

            if (!string.IsNullOrEmpty(elementAndAttributeString))
            {
                if (elementAndAttributeString.IndexOf(':') < 0)
                {
                    return(string.Format("[{0}:{1}]", _prefix, elementAndAttributeString));
                }
                else if (!string.IsNullOrEmpty(elementAndAttributeString))
                {
                    return(string.Format("[{0}]", elementAndAttributeString));
                }
            }

            return(string.Empty);
        }
Esempio n. 27
0
        public void Parse(out DocumentTemplateElement aContextElement, out DocumentTemplateElementAttribute aAttribute)
        {
            aContextElement = null; //default
            aAttribute      = null; //default

            var parsedContext = _context.Split('/');

            if (parsedContext.Length > 1)   //does the context contain a complex element structure (e.g. code/@code)
            {
                DocumentTemplateElement parentContextElement = null;
                for (int i = 0; i < parsedContext.Length; i++)
                {
                    if (IsAttribute(parsedContext[i]))
                    {
                        aAttribute = new DocumentTemplateElementAttribute(parsedContext[i].Replace("@", ""));
                        aContextElement.AddAttribute(aAttribute);
                    }
                    else
                    {
                        aContextElement = new DocumentTemplateElement(parsedContext[i]);
                        if (parentContextElement != null)
                        {
                            parentContextElement.AddElement(aContextElement);
                        }
                    }
                    parentContextElement = aContextElement;
                }
            }
            else
            {
                if (IsAttribute(_context))
                {
                    aAttribute = new DocumentTemplateElementAttribute(_context.Replace("@", ""));
                }
                else
                {
                    aContextElement = new DocumentTemplateElement(_context);
                }
            }
        }
Esempio n. 28
0
        public void GenerateSchematronAssertion_Element_With_Value_ConformanceSHALL_CardinalityOneToOne()
        {
            //create cda doc
            var cdaDocumentTemplate = new DocumentTemplate("urn:hl7-org:v3");
            //create code element
            var element = new DocumentTemplateElement("code", "Test");

            //add element to doc
            cdaDocumentTemplate.AddElement(element);
            //create schematron assertion line builder
            var builder = new AssertionLineBuilder(element, templateIdentifierXpath, templateVersionIdentifierXpath, "cda");
            //define cardinality and conformance
            var cardinality = CardinalityParser.Parse("1..1");
            var conformance = ConformanceParser.Parse("SHALL");

            //add element (context comes from doc), conformance, cardinality through fluent interface
            builder.ConformsTo(conformance).WithCardinality(cardinality);
            //generate string
            string assertion = builder.ToString();

            //did we generate the correct string?
            Assert.IsTrue(assertion == @"count(cda:code[translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='test'])=1", "The generated assertion was not correct. Expected 'count(cda:code[text()='Test'])=1', Actual '{0}'.", assertion);
        }
Esempio n. 29
0
 /// <summary>
 /// Helper method which steps through the children for a given constraint (aTemplateConstraint) and attaches any attributes to the
 /// given element (aElement)).
 /// </summary>
 /// <param name="aElement">Element to add the attributes to</param>
 /// <param name="aTemplateConstraint">Constraint to walk the children to find attributes</param>
 static private void AddBranchedAttributes(DocumentTemplateElement aElement, IConstraint aTemplateConstraint)
 {
     if (aTemplateConstraint.Children != null)
     {
         DocumentTemplateElement          parsedElement   = null;
         DocumentTemplateElementAttribute parsedAttribute = null;
         foreach (var child in aTemplateConstraint.Children)
         {
             var cp = new ContextParser(child.Context);
             cp.Parse(out parsedElement, out parsedAttribute);
             if (parsedElement != null)
             {
                 parsedElement.IsBranch           = child.IsBranch;
                 parsedElement.IsBranchIdentifier = child.IsBranchIdentifier;
             }
             if (parsedAttribute != null) //we are only looking for attributes
             {
                 parsedAttribute.SingleValue = child.Value;
                 aElement.AddAttribute(parsedAttribute);
             }
         }
     }
 }
        public void GenerateSchematronAssertion_Element_ValueSet_SVS_ConformanceSHALL_CardinalityOneToOne()
        {
            var element = new DocumentTemplateElement("administrativeGenderCode");
            //create schematron assertion line builder, build one at a time (regular interface, see above for fluent interface)
            var builder = new AssertionLineBuilder(this.tdb, element, this.igType, this.igTypeSchema);
            //define cardinality
            var cardinality = CardinalityParser.Parse("1..1");

            //add cardinality for the element
            builder.WithCardinality(cardinality);
            //define conformance
            var conformance = ConformanceParser.Parse("SHALL");

            //add conformance for the element
            builder.ConformsTo(conformance);
            builder.WithinValueSet("4.3.2.1", VocabularyOutputType.SVS_SingleValueSet);
            //generate string
            string assertion = builder.ToString();
            //did we generate the correct string?
            string expected = @"count(cda:administrativeGenderCode[@code=document('4.3.2.1.xml')/svs:RetrieveValueSetResponse/svs:ValueSet[@id='4.3.2.1']/svs:ConceptList/svs:Concept/@code])=1";

            Assert.IsTrue(assertion == expected, "Assertion string was not correct. Expected '{0}', Actual '{1}'.", expected, assertion);
        }