/// <summary>
        /// Parse the documentation and add a prefix
        /// </summary>
        internal static MohawkCollege.EHR.gpmr.COR.Documentation ParseAddPrefix(string prefix, Documentation annot)
        {
            MohawkCollege.EHR.gpmr.COR.Documentation documentation = new MohawkCollege.EHR.gpmr.COR.Documentation();
            Dictionary<String, StringCollection> parameters = MifCompiler.hostContext.Data["CommandParameters"] as Dictionary<String, StringCollection>;

            if (annot == null) return documentation;

            // Append documentation
            #region Definition
            if (annot.Definition != null && annot.Definition.Count > 0)
            {
                documentation.Definition = new List<string>();
                if (!String.IsNullOrEmpty(prefix))
                    documentation.Definition.Add(String.Format("<em>{0}</em>", prefix));
                annot.Definition.Sort(ComplexAnnotation.Comparator);
                foreach (ComplexAnnotation ca in annot.Definition)
                    foreach (ComplexMarkupWithLanguage cmwl in ca.Text)
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List<XmlElement>().ToArray())
                                documentation.Definition.Add(CleanHtml(xel).OuterXml); // Clean mif doc data from docs
                            if (cmwl.MarkupText != null) documentation.Definition.Add(cmwl.MarkupText);
                        }
            }
            #endregion
            #region Description
            if (annot.Description != null && annot.Description.Count > 0)
            {
                documentation.Description = new List<string>();
                if (!String.IsNullOrEmpty(prefix))
                    documentation.Description.Add(String.Format("<em>{0}</em>", prefix));

                annot.Description.Sort(ComplexAnnotation.Comparator);
                foreach (ComplexAnnotation ca in annot.Description)
                    foreach (ComplexMarkupWithLanguage cmwl in ca.Text)
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List<XmlElement>().ToArray())
                                documentation.Description.Add(CleanHtml(xel).OuterXml);
                            if (cmwl.MarkupText != null) documentation.Description.Add(cmwl.MarkupText);
                        }
            }
            #endregion
            #region Rationale
            if (annot.Rationale != null && annot.Rationale.Count > 0)
            {
                documentation.Rationale = new List<string>();
                if (!String.IsNullOrEmpty(prefix))
                    documentation.Rationale.Add(String.Format("<em>{0}</em>", prefix));
                annot.Rationale.Sort(ComplexAnnotation.Comparator);
                foreach (ComplexAnnotation ca in annot.Rationale)
                    foreach (ComplexMarkupWithLanguage cmwl in ca.Text)
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List<XmlElement>().ToArray())
                                documentation.Rationale.Add(CleanHtml(xel).OuterXml);
                            if (cmwl.MarkupText != null) documentation.Rationale.Add(cmwl.MarkupText);
                        }
            }
            #endregion
            #region Walkthrough
            if (annot.Walkthrough != null && annot.Walkthrough.Count > 0)
            {
                documentation.Walkthrough = new List<string>();
                if (!String.IsNullOrEmpty(prefix))
                    documentation.Walkthrough.Add(String.Format("<em>{0}</em>", prefix)); 
                annot.Walkthrough.Sort(ComplexAnnotation.Comparator);
                foreach (ComplexAnnotation ca in annot.Walkthrough)
                    foreach (ComplexMarkupWithLanguage cmwl in ca.Text)
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List<XmlElement>().ToArray())
                                documentation.Walkthrough.Add(CleanHtml(xel).OuterXml);
                            if (cmwl.MarkupText != null) documentation.Walkthrough.Add(cmwl.MarkupText);
                        }
            }
            #endregion
            #region Usage
            if (annot.Usage != null && annot.Usage.Count > 0)
            {
                documentation.Usage = new List<string>();
                if (!String.IsNullOrEmpty(prefix))
                    documentation.Usage.Add(String.Format("<em>{0}</em>", prefix)); 
                annot.Usage.Sort(ComplexAnnotation.Comparator);
                foreach (ComplexAnnotation ca in annot.Usage)
                    foreach (ComplexMarkupWithLanguage cmwl in ca.Text)
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List<XmlElement>().ToArray())
                                documentation.Usage.Add(CleanHtml(xel).OuterXml);
                            if (cmwl.MarkupText != null) documentation.Usage.Add(cmwl.MarkupText);
                        }
            }
            #endregion
            #region Appendicies
            if (annot.Appendix != null && annot.Appendix.Count > 0)
            {
                documentation.Appendix = new List<MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation>();
                foreach (AppendixAnnotation aa in annot.Appendix)
                {
                    MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation td = new MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation();
                    td.Name = aa.Name;
                    td.Title = aa.Title;
                    td.Text = new List<string>();
                    if (!String.IsNullOrEmpty(prefix))
                        td.Text.Add(String.Format("<em>{0}</em>", prefix)); 
                    foreach (ComplexMarkupWithLanguage cmwl in aa.Text)
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List<XmlElement>().ToArray())
                                td.Text.Add(CleanHtml(xel).OuterXml);
                            if (cmwl.MarkupText != null) td.Text.Add(cmwl.MarkupText);
                        }
                    documentation.Appendix.Add(td);
                }
            }
            #endregion
            #region Other
            if (annot.OtherAnnotation != null && annot.OtherAnnotation.Count > 0)
            {
                documentation.Other = new List<MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation>();
                foreach (AppendixAnnotation aa in annot.OtherAnnotation)
                {
                    MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation td = new MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation();
                    td.Name = aa.Name;
                    td.Title = aa.Title ?? aa.Type;
                    td.Text = new List<string>();
                    if (!String.IsNullOrEmpty(prefix))
                        td.Text.Add(String.Format("<em>{0}</em>", prefix)); 
                    foreach (ComplexMarkupWithLanguage cmwl in aa.Text)
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List<XmlElement>().ToArray())
                                td.Text.Add(CleanHtml(xel).OuterXml);
                            if (cmwl.MarkupText != null) td.Text.Add(cmwl.MarkupText);
                        }

                    if (aa.Data != null)
                        foreach (XmlNode nd in aa.Data)
                            td.Text.Add(CleanHtml(nd).OuterXml);

                    // Add if not ignored
                    if (parameters.ContainsKey("omit-other-annotation") &&
                        parameters["omit-other-annotation"] != null &&
                        parameters["omit-other-annotation"].Contains(td.Title))
                        continue;
                    documentation.Other.Add(td);
                }
            }

            #endregion
            return documentation;
        }
        public void Compile()
        {

            // Already processing?
            if (interactionModel == null || processingStack.Contains(interactionModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT)))
                return;

            // Add to the processing stack
            processingStack.Add(interactionModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT));

            // Otput the name of the package.
            System.Diagnostics.Trace.WriteLine(string.Format("Compiling interaction model package '{0}'...", interactionModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT)), "debug");

            // Check if the package has already been "compiled"
            if (ClassRepository.ContainsKey(interactionModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT)))
                return; // Already compiled

            // Process the interaction
            MohawkCollege.EHR.gpmr.COR.Interaction interaction = new MohawkCollege.EHR.gpmr.COR.Interaction();
            interaction.Name = interactionModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT);
            //interaction.Realm = interactionModel.PackageLocation.Realm;

            // Process business names
            foreach (BusinessName bn in interactionModel.BusinessName ?? new List<BusinessName>())
                if (bn.Language == MifCompiler.Language || bn.Language == null)
                    interaction.BusinessName = bn.Name;

            // Process documentation
            if(interactionModel.Annotations != null)
                interaction.Documentation = DocumentationParser.Parse(interactionModel.Annotations.Documentation);

            // Set the derivation from pointer
            interaction.DerivedFrom = interactionModel;

            // Trigger event
            interaction.TriggerEvent = interactionModel.InvokingTriggerEvent.ToString(MifCompiler.NAME_FORMAT);

            // Types
            TypeReference tr = new TypeReference();

            // Has the entry class been created yet?
            if (!ClassRepository.ContainsKey(interactionModel.ArgumentMessage.ToString(MifCompiler.NAME_FORMAT)))
                // Process
                PackageParser.Parse(interactionModel.ArgumentMessage.ToString(MifCompiler.NAME_FORMAT), repository, ClassRepository);

            var entry = (ClassRepository[interactionModel.ArgumentMessage.ToString(MifCompiler.NAME_FORMAT)] as MohawkCollege.EHR.gpmr.COR.SubSystem);

            // Could we even find the model?
            if (entry == null)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Could not find the argument message '{0}', interaction '{1}' can't be processed",
                    interactionModel.ArgumentMessage.ToString(MifCompiler.NAME_FORMAT), interaction.Name), "error");
                return;
            }
            else if (entry.EntryPoint.Count == 0)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Argument message '{0}' must have an entry point, interaction '{1}' can't be processed",
                    entry.Name, interaction.Name), "error");
                return;
            }
            else if (entry.EntryPoint.Count != 1)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Ambiguous entry point for argument message '{0}', interaction '{1}' can't be processed", entry.Name, interaction.Name), "error");
                return;
            }

            // Set the entry class
            tr = entry.EntryPoint[0].CreateTypeReference();
            tr.MemberOf = ClassRepository; // Set member of property
            ProcessTypeParameters(interactionModel.ArgumentMessage.ParameterModel, tr, interaction);
            interaction.MessageType = tr;

            #region Response types
            if (interactionModel.ReceiverResponsibilities != null)
            {
                // Create the array
                interaction.Responses = new List<MohawkCollege.EHR.gpmr.COR.Interaction>();

                //  Iterate through
                foreach (ReceiverResponsibility rr in interactionModel.ReceiverResponsibilities)
                {
                    if (rr.InvokeInteraction == null)
                    {
                        System.Diagnostics.Trace.WriteLine("Invoking interaction on receiver responsibility is missing", "warn");
                        continue;
                    }

                    // Does the receiver responsibility exist in the class repository
                    if (!ClassRepository.ContainsKey(rr.InvokeInteraction.ToString(MifCompiler.NAME_FORMAT)))
                    {
                        InteractionCompiler icc = new InteractionCompiler();
                        icc.PackageRepository = repository;
                        icc.Package = repository.Find(o => o.PackageLocation.ToString(MifCompiler.NAME_FORMAT) == rr.InvokeInteraction.ToString(MifCompiler.NAME_FORMAT));
                        icc.ClassRepository = ClassRepository;
                        icc.Compile();
                    }

                    MohawkCollege.EHR.gpmr.COR.Feature foundFeature = null;
                    if (ClassRepository.TryGetValue(rr.InvokeInteraction.ToString(MifCompiler.NAME_FORMAT), out foundFeature))
                    {

                        // Reason element for documentation
                        if (rr.Reason != null && (rr.Reason.Language == MifCompiler.Language || rr.Reason.Language == null)
                            && (rr.Reason.MarkupElements != null || rr.Reason.MarkupText != null))
                        {
                            MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation td= new MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation()
                            {
                                Title = "Reason", Name = "Reason", Text = new List<string>()
                            };
                            if (rr.Reason.MarkupText != null)
                                td.Text.Add(rr.Reason.MarkupText);
                            if(rr.Reason.MarkupElements != null)
                                foreach(XmlElement xe in rr.Reason.MarkupElements)
                                    td.Text.Add(xe.OuterXml.Replace(" xmlns:html=\"http://www.w3.org/1999/xhtml\"", "").Replace("html:", ""));

                            // Append the documentation
                            if (interaction.Documentation == null)
                                interaction.Documentation = new MohawkCollege.EHR.gpmr.COR.Documentation();
                            if (interaction.Documentation.Other == null)
                                interaction.Documentation.Other = new List<MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation>();
                            interaction.Documentation.Other.Add(td);
                        }
                        interaction.Responses.Add(foundFeature as MohawkCollege.EHR.gpmr.COR.Interaction);
                    }
                    else
                        System.Diagnostics.Trace.WriteLine(String.Format("Can't find response interaction '{0}'...", rr.InvokeInteraction.ToString(MifCompiler.NAME_FORMAT)), "warn");


                }
            }
            #endregion

            // Fire the complete method
            interaction.FireParsed();
        }
        public void Compile()
        {
            
            System.Diagnostics.Trace.WriteLine(string.Format("Processing global vocabulary model '{0}'...", vocabularyModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT)), "debug");

            // JF: This is safe to ignore here, this is handled by each of the individual binding
            //     realms
            // Bind all contexts so that property parsing will perform normally
            //foreach (ContextBinding cb in vocabularyModel.ContextBinding)
            //    if (cb.BindingRealmName == MifCompiler.BindingRealm && cb.BindingPriority == 0)
            //        PropertyParser.defaultCodingStrengths.Add(cb.ConceptDomain, cb.CodingStrength == CodingStrengthKind.CNE ? Property.CodingStrengthKind.CodedNoExceptions : Property.CodingStrengthKind.CodedWithExceptions);

            // Try to compile all concept domains from the model
            #region ConceptDomains
            foreach (MohawkCollege.EHR.HL7v3.MIF.MIF20.Vocabulary.ConceptDomain cd in vocabularyModel.ConceptDomain)
            {
                // Process
                if (ClassRepository.Find(o => o.Name == cd.Name && o is MohawkCollege.EHR.gpmr.COR.ConceptDomain) == null)
                {
                    EnumerationParser.Parse(cd, vocabularyModel, repository, ClassRepository);

                    // Specialize domains now and get them out of the way
                    foreach (ConceptDomainRef cdr in cd.SpecializesDomain)
                    {
                        // Has the specialized domain been processed?
                        if (!ClassRepository.ContainsKey(cdr.Name))
                        {
                            // Find the model that contains this concept domain
                            GlobalVocabularyModel gvm = repository.Find(delegate(PackageArtifact p)
                            {
                                if (!(p is GlobalVocabularyModel)) return false; // Don't search anything but vocab

                                // Try to find something in this model
                                GlobalVocabularyModel gvc = p as GlobalVocabularyModel;

                                return (gvc.CodeSystem.Find(o => o.Name == cdr.Name) as object ??
                                gvc.ValueSet.Find(o => o.Name == cdr.Name) as object) != null;
                            }) as GlobalVocabularyModel;

                            // Was a global vocab model found
                            if (gvm != null)
                            {
                                // Sometimes there is no concept domain, but a code system only
                                MohawkCollege.EHR.HL7v3.MIF.MIF20.Vocabulary.CodeSystem cs = gvm.CodeSystem.Find(o => o.Name == cdr.Name);
                                MohawkCollege.EHR.HL7v3.MIF.MIF20.Vocabulary.ValueSet vs = gvm.ValueSet.Find(o => o.Name == cdr.Name);
                                if (cs != null) // We found a code system, built it out
                                {
                                    Enumeration enumeration = new MohawkCollege.EHR.gpmr.COR.CodeSystem();
                                    enumeration.Name = cs.Name;
                                    EnumerationParser.Parse(cs, enumeration, ClassRepository, vocabularyModel);
                                    enumeration.FireParsed();
                                }
                                else if (vs != null)
                                {
                                    Enumeration enumeration = new MohawkCollege.EHR.gpmr.COR.ValueSet();
                                    enumeration.Name = vs.Name;
                                    EnumerationParser.Parse(vs, enumeration, ClassRepository, vocabularyModel);
                                    enumeration.FireParsed();
                                }
                            }
                        }

                        // Lookup the specialized domain in COR format
                        if (ClassRepository.ContainsKey(cdr.Name) && ClassRepository.ContainsKey(cd.Name))
                        {
                            Enumeration specializor = ClassRepository[cd.Name] as Enumeration; // One who specializes
                            Enumeration specializee = ClassRepository[cdr.Name] as Enumeration; // One who is specialized

                            // Add properties
                            specializee.Literals.AddRange(specializor.Literals);
                        }
                    }
                }
                else
                    Trace.WriteLine(String.Format("Concept domain '{0}' is already represented, skipping...", cd.Name), "warn");

            }
            #endregion

            #region Code Systems

            // Any un-parsed code-systems
            foreach (MohawkCollege.EHR.HL7v3.MIF.MIF20.Vocabulary.CodeSystem cs in vocabularyModel.CodeSystem)
            {
                if (ClassRepository.Find(o=>o.Name == cs.Name && o is MohawkCollege.EHR.gpmr.COR.CodeSystem) == null)
                {
                    Enumeration enumeration = new MohawkCollege.EHR.gpmr.COR.CodeSystem();
                    enumeration.Name = cs.Name;
                    EnumerationParser.Parse(cs, enumeration, ClassRepository, vocabularyModel);
                    enumeration.FireParsed();
                }
                else
                    Trace.WriteLine(String.Format("Code system '{0}' is already represented, skipping...", cs.Name), "warn");
            }

            #endregion

            #region Value Sets

            // Any un-parsed value-sets
            foreach (MohawkCollege.EHR.HL7v3.MIF.MIF20.Vocabulary.ValueSet vs in vocabularyModel.ValueSet)
            {

                string realm = vocabularyModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT),
                    name = vs.Name;

                // Contains the key
                if (ClassRepository.Find(o => o.Name == vs.Name && o is MohawkCollege.EHR.gpmr.COR.ValueSet) == null)
                {
                    Enumeration enumeration = new MohawkCollege.EHR.gpmr.COR.ValueSet();
                    enumeration.Name = name;
                    EnumerationParser.Parse(vs, enumeration, ClassRepository, vocabularyModel);
                    enumeration.FireParsed();
                }
                else
                    Trace.WriteLine(String.Format("Value set '{0}' is already represented, skipping...", vs.Name), "warn");

            }
            #endregion

            #region Default Coding Strengths

            foreach (var strength in vocabularyModel.ContextBinding)
            {
                string id = strength.ValueSet;
                var valueSet = ClassRepository.Find(o => o is MohawkCollege.EHR.gpmr.COR.ValueSet && (o as MohawkCollege.EHR.gpmr.COR.ValueSet).Id == id);
                if (valueSet != null && !PropertyParser.defaultCodingStrengths.ContainsKey(valueSet.Name))
                    PropertyParser.defaultCodingStrengths.Add(valueSet.Name, strength.CodingStrength == CodingStrengthKind.CNE ? Property.CodingStrengthKind.CodedNoExtensions : Property.CodingStrengthKind.CodedWithExtensions);
                else if (valueSet == null)
                    Trace.WriteLine(String.Format("Value set '{0}' was not parsed, therefore context binding cannot be placed on it", id));
                else if (PropertyParser.defaultCodingStrengths[valueSet.Name] != (strength.CodingStrength == CodingStrengthKind.CNE ? Property.CodingStrengthKind.CodedNoExtensions : Property.CodingStrengthKind.CodedWithExtensions))
                    System.Diagnostics.Trace.WriteLine(String.Format("Binding strength for '{0}' is declared twice and both instances don't match!", strength.ConceptDomain), "warn");

                var conceptDomain = EnumerationParser.GetOrParseConceptDomainByName(strength.ConceptDomain, ClassRepository, PackageRepository as PackageRepository);
                // Context binding
                if (valueSet is MohawkCollege.EHR.gpmr.COR.ValueSet)
                    (valueSet as MohawkCollege.EHR.gpmr.COR.ValueSet).ContextBinding = conceptDomain;
                if (conceptDomain != null && valueSet != null)
                {
                    if (conceptDomain.ContextBinding == null) conceptDomain.ContextBinding = new List<Enumeration>();
                    // Add the binding realm
                    (valueSet as Enumeration).OwnerRealm = strength.BindingRealmName;
                    conceptDomain.ContextBinding.Add(valueSet as Enumeration);
                }
            }

            #endregion

            #region Supplements

            // Code systems supplement:
            // Apparently in v3 it is possible to add codes to a code system
            // after it has been defined
            foreach (var css in vocabularyModel.CodeSystemSupplement)
            {

                // First, find the code system to supplement
                var cs = EnumerationParser.GetOrParseCodeSystem(css.CodeSystemId, this.ClassRepository, this.vocabularyModel.MemberOfRepository);
                if (cs == null && MifCompiler.hostContext.Mode == Pipeline.OperationModeType.Quirks)
                {
                    // Try to find a value set 
                    Trace.WriteLine(String.Format("Could not supplement code system '{0}' as it was not defined in the repository", css.CodeSystemId), "quirks");
                    continue;
                }
                else if (css == null)// not in quirks
                    throw new InvalidOperationException(String.Format("Could not find target code system '{0}' of supplement '{1}', aborting", css.CodeSystemId, css.SupplementId));

                string docTitle = "Supplement";
                if (css.Header != null && css.Header.ResponsibleGroup != null && css.Header.ResponsibleGroup.Count > 0)
                    docTitle = String.Format("{0} Supplement:",css.Header.ResponsibleGroup[0].GroupName ?? css.Header.ResponsibleGroup[0].OrganizationName);
                
                // Create documentation
                var doc = new MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation()
                {
                    Name = "supplement",
                    Title = docTitle    
                };

                // Add text
                if (css.Annotations != null)
                {
                    var docContent = DocumentationParser.ParseAddPrefix(doc.Title, css.Annotations.Documentation);
                    if (cs.Documentation == null)
                        cs.Documentation = new COR.Documentation();
                    cs.Documentation.Merge(docContent);
                }

                // Now supplement the appropriate codes
                if(css.CodeSystemVersionSupplement.Count == 0)
                {
                    Trace.WriteLine(String.Format("Supplement '{0}' does not contain a any supplements", css.SupplementId), "warn");
                    continue;
                }
                else if (css.CodeSystemVersionSupplement.Count > 1) // More than one
                {
                    if (MifCompiler.hostContext.Mode == Pipeline.OperationModeType.Quirks)
                        Trace.WriteLine(String.Format("Supplement '{0}' contains more than one version to supplement. Since GPMR does not version enumeration structures only the first supplement ('{1}') will be used",
                            css.SupplementId, css.CodeSystemVersionSupplement[0].AppliesToReleaseDate), "quirks");
                    else
                    {
                        Trace.WriteLine(String.Format("Supplement '{0}' contains more than one version to supplement. GPMR does not version its enumeration releases and cannot apply this supplement!",
                            css.SupplementId, css.CodeSystemVersionSupplement[0].AppliesToReleaseDate), "error");
                        continue;
                    }
                }
                foreach (var concept in css.CodeSystemVersionSupplement[0].ConceptSupplement)
                {
                    // Attempt to find the ev
                    var ev = cs.GetEnumeratedLiterals().Find(o => o.Name == concept.Code);
                    if (ev == null)
                    {
                        Trace.WriteLine(String.Format("Could not find code '{0}' in '{1}' to which the supplemented concept applies", concept.Code, cs.Id), "error");
                        continue;
                    }
                    
                    // Supplement annotations?
                    if (concept.Annotations != null && concept.Annotations.Documentation != null)
                    {
                        var csDocumentation = DocumentationParser.ParseAddPrefix(doc.Name, concept.Annotations.Documentation);
                        // Append the supplement notes as comments
                        if (ev.Documentation == null)
                            ev.Documentation = new COR.Documentation();
                        ev.Documentation.Merge(csDocumentation);
                    }
                }
                
            }

            // These structures seem to needlessly complex the MIF ...
            // Why can't this data be added directly to the 
            // exported MIF? Meh, I guess we have to process it :@
            // This is equivalent in C++ to doing this:
            // Include STDIO but add some data to cout and 
            // change the name of X to Y ... stupid
            if(vocabularyModel.DependsOnVocabModel != null)
            {
                foreach (var dep in vocabularyModel.DependsOnVocabModel)
                {
                    List<Enumeration> alreadySupplemented = new List<Enumeration>();

                    foreach (var supp in dep.SupplementArtifact)
                        try
                        {

                            // First we have to find the object that is being supplemented
                            string artifactName = supp.SupplementedObject.Params.Find(o => o.Name == "artifactName").Value;
                            Type artifactType = supp.SupplementedObject.Params.Exists(o => o.Name == "subArtifact" && o.Value == "VS") ? typeof(MohawkCollege.EHR.gpmr.COR.ValueSet) : typeof(MohawkCollege.EHR.gpmr.COR.ConceptDomain);
                            if (artifactType == null)
                                throw new InvalidOperationException(String.Format("Cannot supplement object of type '{0}'", supp.SupplementedObject.Name));
                            Enumeration supplementObject = artifactType == typeof(MohawkCollege.EHR.gpmr.COR.ConceptDomain) ? EnumerationParser.GetOrParseConceptDomainByName(artifactName, ClassRepository, (PackageRepository)PackageRepository) : (Enumeration)EnumerationParser.GetOrParseValueSetByName(artifactName, ClassRepository, (PackageRepository)PackageRepository);

                            // HACK: Does the trick though
                            if (alreadySupplemented.Contains(supplementObject))
                            {
                                if(supplementObject != null)
                                    Trace.WriteLine(String.Format("Object '{0}' has already been supplemented, skipping", supplementObject.Name), "warn");
                                continue;
                            }
                            alreadySupplemented.Add(supplementObject);

                            // Supplemented object?
                            //if(supplementObject == null && MifCompiler.hostContext.Mode == Pipeline.OperationModeType.Quirks)
                            //{
                            //    supplementObject = ClassRepository.Find(o=>o.Name == artifactName) as Enumeration;
                            //    if (supplementObject != null)
                            //        Trace.WriteLine(String.Format("Could not find '{0}' of type '{1}' but found '{2}' named '{0}', supplementing that object instead",
                            //            artifactName, artifactType.Name, supplementObject.EnumerationType), "quirks");
                            //    else
                            //        Trace.WriteLine(String.Format("Could not find '{0}' of type '{1}', looked for any vocabulary structure named '{0}' but couldn't find any, will cause an error",
                            //            artifactType, artifactType.Name), "quirks");
                            //}
                            if (supplementObject == null)
                            {
                                Trace.WriteLine(String.Format("Cannot supplement object '{0}' as it was not found in the repository", artifactName), "warn");
                                continue;

                            }

                            Trace.WriteLine(String.Format("Adding supplements to '{0}'...", artifactName), "debug");
                            // Supplement the business name...
                            foreach (var bn in supp.BusinessName)
                                if (bn.Language == MifCompiler.Language || bn.Language == null)
                                    supplementObject.BusinessName = bn.Name;

                            // Supplement annotations
                            if (supp.Annotations != null)
                            {
                                if (supplementObject.Documentation == null)
                                    supplementObject.Documentation = new COR.Documentation();
                                supplementObject.Documentation.Merge(DocumentationParser.Parse(supp.Annotations.Documentation));
                            }
                        }
                        catch (Exception e)
                        {
                            if (MifCompiler.hostContext.Mode == Pipeline.OperationModeType.Quirks)
                                Trace.WriteLine(String.Format("Won't supplement object due to error : '{0}'.", e.Message), "quirks");
                            else
                                throw e;
                        }
                }

            }


            #endregion

        }
Beispiel #4
0
        public void Compile()
        {
            System.Diagnostics.Trace.WriteLine(string.Format("Processing global vocabulary model '{0}'...", vocabularyModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT)), "debug");

            // JF: This is safe to ignore here, this is handled by each of the individual binding
            //     realms
            // Bind all contexts so that property parsing will perform normally
            //foreach (ContextBinding cb in vocabularyModel.ContextBinding)
            //    if (cb.BindingRealmName == MifCompiler.BindingRealm && cb.BindingPriority == 0)
            //        PropertyParser.defaultCodingStrengths.Add(cb.ConceptDomain, cb.CodingStrength == CodingStrengthKind.CNE ? Property.CodingStrengthKind.CodedNoExceptions : Property.CodingStrengthKind.CodedWithExceptions);

            // Try to compile all concept domains from the model
            #region ConceptDomains
            foreach (MohawkCollege.EHR.HL7v3.MIF.MIF20.Vocabulary.ConceptDomain cd in vocabularyModel.ConceptDomain)
            {
                // Process
                if (ClassRepository.Find(o => o.Name == cd.Name && o is MohawkCollege.EHR.gpmr.COR.ConceptDomain) == null)
                {
                    EnumerationParser.Parse(cd, vocabularyModel, repository, ClassRepository);

                    // Specialize domains now and get them out of the way
                    foreach (ConceptDomainRef cdr in cd.SpecializesDomain)
                    {
                        // Has the specialized domain been processed?
                        if (!ClassRepository.ContainsKey(cdr.Name))
                        {
                            // Find the model that contains this concept domain
                            GlobalVocabularyModel gvm = repository.Find(delegate(PackageArtifact p)
                            {
                                if (!(p is GlobalVocabularyModel))
                                {
                                    return(false);                               // Don't search anything but vocab
                                }
                                // Try to find something in this model
                                GlobalVocabularyModel gvc = p as GlobalVocabularyModel;

                                return((gvc.CodeSystem.Find(o => o.Name == cdr.Name) as object ??
                                        gvc.ValueSet.Find(o => o.Name == cdr.Name) as object) != null);
                            }) as GlobalVocabularyModel;

                            // Was a global vocab model found
                            if (gvm != null)
                            {
                                // Sometimes there is no concept domain, but a code system only
                                MohawkCollege.EHR.HL7v3.MIF.MIF20.Vocabulary.CodeSystem cs = gvm.CodeSystem.Find(o => o.Name == cdr.Name);
                                MohawkCollege.EHR.HL7v3.MIF.MIF20.Vocabulary.ValueSet   vs = gvm.ValueSet.Find(o => o.Name == cdr.Name);
                                if (cs != null) // We found a code system, built it out
                                {
                                    Enumeration enumeration = new MohawkCollege.EHR.gpmr.COR.CodeSystem();
                                    enumeration.Name = cs.Name;
                                    EnumerationParser.Parse(cs, enumeration, ClassRepository, vocabularyModel);
                                    enumeration.FireParsed();
                                }
                                else if (vs != null)
                                {
                                    Enumeration enumeration = new MohawkCollege.EHR.gpmr.COR.ValueSet();
                                    enumeration.Name = vs.Name;
                                    EnumerationParser.Parse(vs, enumeration, ClassRepository, vocabularyModel);
                                    enumeration.FireParsed();
                                }
                            }
                        }

                        // Lookup the specialized domain in COR format
                        if (ClassRepository.ContainsKey(cdr.Name) && ClassRepository.ContainsKey(cd.Name))
                        {
                            Enumeration specializor = ClassRepository[cd.Name] as Enumeration;  // One who specializes
                            Enumeration specializee = ClassRepository[cdr.Name] as Enumeration; // One who is specialized

                            // Add properties
                            specializee.Literals.AddRange(specializor.Literals);
                        }
                    }
                }
                else
                {
                    Trace.WriteLine(String.Format("Concept domain '{0}' is already represented, skipping...", cd.Name), "warn");
                }
            }
            #endregion

            #region Code Systems

            // Any un-parsed code-systems
            foreach (MohawkCollege.EHR.HL7v3.MIF.MIF20.Vocabulary.CodeSystem cs in vocabularyModel.CodeSystem)
            {
                if (ClassRepository.Find(o => o.Name == cs.Name && o is MohawkCollege.EHR.gpmr.COR.CodeSystem) == null)
                {
                    Enumeration enumeration = new MohawkCollege.EHR.gpmr.COR.CodeSystem();
                    enumeration.Name = cs.Name;
                    EnumerationParser.Parse(cs, enumeration, ClassRepository, vocabularyModel);
                    enumeration.FireParsed();
                }
                else
                {
                    Trace.WriteLine(String.Format("Code system '{0}' is already represented, skipping...", cs.Name), "warn");
                }
            }

            #endregion

            #region Value Sets

            // Any un-parsed value-sets
            foreach (MohawkCollege.EHR.HL7v3.MIF.MIF20.Vocabulary.ValueSet vs in vocabularyModel.ValueSet)
            {
                string realm = vocabularyModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT),
                                                                           name = vs.Name;

                // Contains the key
                if (ClassRepository.Find(o => o.Name == vs.Name && o is MohawkCollege.EHR.gpmr.COR.ValueSet) == null)
                {
                    Enumeration enumeration = new MohawkCollege.EHR.gpmr.COR.ValueSet();
                    enumeration.Name = name;
                    EnumerationParser.Parse(vs, enumeration, ClassRepository, vocabularyModel);
                    enumeration.FireParsed();
                }
                else
                {
                    Trace.WriteLine(String.Format("Value set '{0}' is already represented, skipping...", vs.Name), "warn");
                }
            }
            #endregion

            #region Default Coding Strengths

            foreach (var strength in vocabularyModel.ContextBinding)
            {
                string id       = strength.ValueSet;
                var    valueSet = ClassRepository.Find(o => o is MohawkCollege.EHR.gpmr.COR.ValueSet && (o as MohawkCollege.EHR.gpmr.COR.ValueSet).Id == id);
                if (valueSet != null && !PropertyParser.defaultCodingStrengths.ContainsKey(valueSet.Name))
                {
                    PropertyParser.defaultCodingStrengths.Add(valueSet.Name, strength.CodingStrength == CodingStrengthKind.CNE ? Property.CodingStrengthKind.CodedNoExtensions : Property.CodingStrengthKind.CodedWithExtensions);
                }
                else if (valueSet == null)
                {
                    Trace.WriteLine(String.Format("Value set '{0}' was not parsed, therefore context binding cannot be placed on it", id));
                }
                else if (PropertyParser.defaultCodingStrengths[valueSet.Name] != (strength.CodingStrength == CodingStrengthKind.CNE ? Property.CodingStrengthKind.CodedNoExtensions : Property.CodingStrengthKind.CodedWithExtensions))
                {
                    System.Diagnostics.Trace.WriteLine(String.Format("Binding strength for '{0}' is declared twice and both instances don't match!", strength.ConceptDomain), "warn");
                }

                var conceptDomain = EnumerationParser.GetOrParseConceptDomainByName(strength.ConceptDomain, ClassRepository, PackageRepository as PackageRepository);
                // Context binding
                if (valueSet is MohawkCollege.EHR.gpmr.COR.ValueSet)
                {
                    (valueSet as MohawkCollege.EHR.gpmr.COR.ValueSet).ContextBinding = conceptDomain;
                }
                if (conceptDomain != null && valueSet != null)
                {
                    if (conceptDomain.ContextBinding == null)
                    {
                        conceptDomain.ContextBinding = new List <Enumeration>();
                    }
                    // Add the binding realm
                    (valueSet as Enumeration).OwnerRealm = strength.BindingRealmName;
                    conceptDomain.ContextBinding.Add(valueSet as Enumeration);
                }
            }

            #endregion

            #region Supplements

            // Code systems supplement:
            // Apparently in v3 it is possible to add codes to a code system
            // after it has been defined
            foreach (var css in vocabularyModel.CodeSystemSupplement)
            {
                // First, find the code system to supplement
                var cs = EnumerationParser.GetOrParseCodeSystem(css.CodeSystemId, this.ClassRepository, this.vocabularyModel.MemberOfRepository);
                if (cs == null && MifCompiler.hostContext.Mode == Pipeline.OperationModeType.Quirks)
                {
                    // Try to find a value set
                    Trace.WriteLine(String.Format("Could not supplement code system '{0}' as it was not defined in the repository", css.CodeSystemId), "quirks");
                    continue;
                }
                else if (css == null)// not in quirks
                {
                    throw new InvalidOperationException(String.Format("Could not find target code system '{0}' of supplement '{1}', aborting", css.CodeSystemId, css.SupplementId));
                }

                string docTitle = "Supplement";
                if (css.Header != null && css.Header.ResponsibleGroup != null && css.Header.ResponsibleGroup.Count > 0)
                {
                    docTitle = String.Format("{0} Supplement:", css.Header.ResponsibleGroup[0].GroupName ?? css.Header.ResponsibleGroup[0].OrganizationName);
                }

                // Create documentation
                var doc = new MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation()
                {
                    Name  = "supplement",
                    Title = docTitle
                };

                // Add text
                if (css.Annotations != null)
                {
                    var docContent = DocumentationParser.ParseAddPrefix(doc.Title, css.Annotations.Documentation);
                    if (cs.Documentation == null)
                    {
                        cs.Documentation = new COR.Documentation();
                    }
                    cs.Documentation.Merge(docContent);
                }

                // Now supplement the appropriate codes
                if (css.CodeSystemVersionSupplement.Count == 0)
                {
                    Trace.WriteLine(String.Format("Supplement '{0}' does not contain a any supplements", css.SupplementId), "warn");
                    continue;
                }
                else if (css.CodeSystemVersionSupplement.Count > 1) // More than one
                {
                    if (MifCompiler.hostContext.Mode == Pipeline.OperationModeType.Quirks)
                    {
                        Trace.WriteLine(String.Format("Supplement '{0}' contains more than one version to supplement. Since GPMR does not version enumeration structures only the first supplement ('{1}') will be used",
                                                      css.SupplementId, css.CodeSystemVersionSupplement[0].AppliesToReleaseDate), "quirks");
                    }
                    else
                    {
                        Trace.WriteLine(String.Format("Supplement '{0}' contains more than one version to supplement. GPMR does not version its enumeration releases and cannot apply this supplement!",
                                                      css.SupplementId, css.CodeSystemVersionSupplement[0].AppliesToReleaseDate), "error");
                        continue;
                    }
                }
                foreach (var concept in css.CodeSystemVersionSupplement[0].ConceptSupplement)
                {
                    // Attempt to find the ev
                    var ev = cs.GetEnumeratedLiterals().Find(o => o.Name == concept.Code);
                    if (ev == null)
                    {
                        Trace.WriteLine(String.Format("Could not find code '{0}' in '{1}' to which the supplemented concept applies", concept.Code, cs.Id), "error");
                        continue;
                    }

                    // Supplement annotations?
                    if (concept.Annotations != null && concept.Annotations.Documentation != null)
                    {
                        var csDocumentation = DocumentationParser.ParseAddPrefix(doc.Name, concept.Annotations.Documentation);
                        // Append the supplement notes as comments
                        if (ev.Documentation == null)
                        {
                            ev.Documentation = new COR.Documentation();
                        }
                        ev.Documentation.Merge(csDocumentation);
                    }
                }
            }

            // These structures seem to needlessly complex the MIF ...
            // Why can't this data be added directly to the
            // exported MIF? Meh, I guess we have to process it :@
            // This is equivalent in C++ to doing this:
            // Include STDIO but add some data to cout and
            // change the name of X to Y ... stupid
            if (vocabularyModel.DependsOnVocabModel != null)
            {
                foreach (var dep in vocabularyModel.DependsOnVocabModel)
                {
                    List <Enumeration> alreadySupplemented = new List <Enumeration>();

                    foreach (var supp in dep.SupplementArtifact)
                    {
                        try
                        {
                            // First we have to find the object that is being supplemented
                            string artifactName = supp.SupplementedObject.Params.Find(o => o.Name == "artifactName").Value;
                            Type   artifactType = supp.SupplementedObject.Params.Exists(o => o.Name == "subArtifact" && o.Value == "VS") ? typeof(MohawkCollege.EHR.gpmr.COR.ValueSet) : typeof(MohawkCollege.EHR.gpmr.COR.ConceptDomain);
                            if (artifactType == null)
                            {
                                throw new InvalidOperationException(String.Format("Cannot supplement object of type '{0}'", supp.SupplementedObject.Name));
                            }
                            Enumeration supplementObject = artifactType == typeof(MohawkCollege.EHR.gpmr.COR.ConceptDomain) ? EnumerationParser.GetOrParseConceptDomainByName(artifactName, ClassRepository, (PackageRepository)PackageRepository) : (Enumeration)EnumerationParser.GetOrParseValueSetByName(artifactName, ClassRepository, (PackageRepository)PackageRepository);

                            // HACK: Does the trick though
                            if (alreadySupplemented.Contains(supplementObject))
                            {
                                if (supplementObject != null)
                                {
                                    Trace.WriteLine(String.Format("Object '{0}' has already been supplemented, skipping", supplementObject.Name), "warn");
                                }
                                continue;
                            }
                            alreadySupplemented.Add(supplementObject);

                            // Supplemented object?
                            //if(supplementObject == null && MifCompiler.hostContext.Mode == Pipeline.OperationModeType.Quirks)
                            //{
                            //    supplementObject = ClassRepository.Find(o=>o.Name == artifactName) as Enumeration;
                            //    if (supplementObject != null)
                            //        Trace.WriteLine(String.Format("Could not find '{0}' of type '{1}' but found '{2}' named '{0}', supplementing that object instead",
                            //            artifactName, artifactType.Name, supplementObject.EnumerationType), "quirks");
                            //    else
                            //        Trace.WriteLine(String.Format("Could not find '{0}' of type '{1}', looked for any vocabulary structure named '{0}' but couldn't find any, will cause an error",
                            //            artifactType, artifactType.Name), "quirks");
                            //}
                            if (supplementObject == null)
                            {
                                Trace.WriteLine(String.Format("Cannot supplement object '{0}' as it was not found in the repository", artifactName), "warn");
                                continue;
                            }

                            Trace.WriteLine(String.Format("Adding supplements to '{0}'...", artifactName), "debug");
                            // Supplement the business name...
                            foreach (var bn in supp.BusinessName)
                            {
                                if (bn.Language == MifCompiler.Language || bn.Language == null)
                                {
                                    supplementObject.BusinessName = bn.Name;
                                }
                            }

                            // Supplement annotations
                            if (supp.Annotations != null)
                            {
                                if (supplementObject.Documentation == null)
                                {
                                    supplementObject.Documentation = new COR.Documentation();
                                }
                                supplementObject.Documentation.Merge(DocumentationParser.Parse(supp.Annotations.Documentation));
                            }
                        }
                        catch (Exception e)
                        {
                            if (MifCompiler.hostContext.Mode == Pipeline.OperationModeType.Quirks)
                            {
                                Trace.WriteLine(String.Format("Won't supplement object due to error : '{0}'.", e.Message), "quirks");
                            }
                            else
                            {
                                throw e;
                            }
                        }
                    }
                }
            }


            #endregion
        }
Beispiel #5
0
        /// <summary>
        /// Parse the documentation and add a prefix
        /// </summary>
        internal static MohawkCollege.EHR.gpmr.COR.Documentation ParseAddPrefix(string prefix, Documentation annot)
        {
            MohawkCollege.EHR.gpmr.COR.Documentation documentation = new MohawkCollege.EHR.gpmr.COR.Documentation();
            Dictionary <String, StringCollection>    parameters    = MifCompiler.hostContext.Data["CommandParameters"] as Dictionary <String, StringCollection>;

            if (annot == null)
            {
                return(documentation);
            }

            // Append documentation
            #region Definition
            if (annot.Definition != null && annot.Definition.Count > 0)
            {
                documentation.Definition = new List <string>();
                if (!String.IsNullOrEmpty(prefix))
                {
                    documentation.Definition.Add(String.Format("<em>{0}</em>", prefix));
                }
                annot.Definition.Sort(ComplexAnnotation.Comparator);
                foreach (ComplexAnnotation ca in annot.Definition)
                {
                    foreach (ComplexMarkupWithLanguage cmwl in ca.Text)
                    {
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List <XmlElement>().ToArray())
                            {
                                documentation.Definition.Add(CleanHtml(xel).OuterXml); // Clean mif doc data from docs
                            }
                            if (cmwl.MarkupText != null)
                            {
                                documentation.Definition.Add(cmwl.MarkupText);
                            }
                        }
                    }
                }
            }
            #endregion
            #region Description
            if (annot.Description != null && annot.Description.Count > 0)
            {
                documentation.Description = new List <string>();
                if (!String.IsNullOrEmpty(prefix))
                {
                    documentation.Description.Add(String.Format("<em>{0}</em>", prefix));
                }

                annot.Description.Sort(ComplexAnnotation.Comparator);
                foreach (ComplexAnnotation ca in annot.Description)
                {
                    foreach (ComplexMarkupWithLanguage cmwl in ca.Text)
                    {
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List <XmlElement>().ToArray())
                            {
                                documentation.Description.Add(CleanHtml(xel).OuterXml);
                            }
                            if (cmwl.MarkupText != null)
                            {
                                documentation.Description.Add(cmwl.MarkupText);
                            }
                        }
                    }
                }
            }
            #endregion
            #region Rationale
            if (annot.Rationale != null && annot.Rationale.Count > 0)
            {
                documentation.Rationale = new List <string>();
                if (!String.IsNullOrEmpty(prefix))
                {
                    documentation.Rationale.Add(String.Format("<em>{0}</em>", prefix));
                }
                annot.Rationale.Sort(ComplexAnnotation.Comparator);
                foreach (ComplexAnnotation ca in annot.Rationale)
                {
                    foreach (ComplexMarkupWithLanguage cmwl in ca.Text)
                    {
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List <XmlElement>().ToArray())
                            {
                                documentation.Rationale.Add(CleanHtml(xel).OuterXml);
                            }
                            if (cmwl.MarkupText != null)
                            {
                                documentation.Rationale.Add(cmwl.MarkupText);
                            }
                        }
                    }
                }
            }
            #endregion
            #region Walkthrough
            if (annot.Walkthrough != null && annot.Walkthrough.Count > 0)
            {
                documentation.Walkthrough = new List <string>();
                if (!String.IsNullOrEmpty(prefix))
                {
                    documentation.Walkthrough.Add(String.Format("<em>{0}</em>", prefix));
                }
                annot.Walkthrough.Sort(ComplexAnnotation.Comparator);
                foreach (ComplexAnnotation ca in annot.Walkthrough)
                {
                    foreach (ComplexMarkupWithLanguage cmwl in ca.Text)
                    {
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List <XmlElement>().ToArray())
                            {
                                documentation.Walkthrough.Add(CleanHtml(xel).OuterXml);
                            }
                            if (cmwl.MarkupText != null)
                            {
                                documentation.Walkthrough.Add(cmwl.MarkupText);
                            }
                        }
                    }
                }
            }
            #endregion
            #region Usage
            if (annot.Usage != null && annot.Usage.Count > 0)
            {
                documentation.Usage = new List <string>();
                if (!String.IsNullOrEmpty(prefix))
                {
                    documentation.Usage.Add(String.Format("<em>{0}</em>", prefix));
                }
                annot.Usage.Sort(ComplexAnnotation.Comparator);
                foreach (ComplexAnnotation ca in annot.Usage)
                {
                    foreach (ComplexMarkupWithLanguage cmwl in ca.Text)
                    {
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List <XmlElement>().ToArray())
                            {
                                documentation.Usage.Add(CleanHtml(xel).OuterXml);
                            }
                            if (cmwl.MarkupText != null)
                            {
                                documentation.Usage.Add(cmwl.MarkupText);
                            }
                        }
                    }
                }
            }
            #endregion
            #region Appendicies
            if (annot.Appendix != null && annot.Appendix.Count > 0)
            {
                documentation.Appendix = new List <MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation>();
                foreach (AppendixAnnotation aa in annot.Appendix)
                {
                    MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation td = new MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation();
                    td.Name  = aa.Name;
                    td.Title = aa.Title;
                    td.Text  = new List <string>();
                    if (!String.IsNullOrEmpty(prefix))
                    {
                        td.Text.Add(String.Format("<em>{0}</em>", prefix));
                    }
                    foreach (ComplexMarkupWithLanguage cmwl in aa.Text)
                    {
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List <XmlElement>().ToArray())
                            {
                                td.Text.Add(CleanHtml(xel).OuterXml);
                            }
                            if (cmwl.MarkupText != null)
                            {
                                td.Text.Add(cmwl.MarkupText);
                            }
                        }
                    }
                    documentation.Appendix.Add(td);
                }
            }
            #endregion
            #region Other
            if (annot.OtherAnnotation != null && annot.OtherAnnotation.Count > 0)
            {
                documentation.Other = new List <MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation>();
                foreach (AppendixAnnotation aa in annot.OtherAnnotation)
                {
                    MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation td = new MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation();
                    td.Name  = aa.Name;
                    td.Title = aa.Title ?? aa.Type;
                    td.Text  = new List <string>();
                    if (!String.IsNullOrEmpty(prefix))
                    {
                        td.Text.Add(String.Format("<em>{0}</em>", prefix));
                    }
                    foreach (ComplexMarkupWithLanguage cmwl in aa.Text)
                    {
                        if (cmwl.Language == MifCompiler.Language || cmwl.Language == null)
                        {
                            foreach (XmlElement xel in cmwl.MarkupElements ?? new List <XmlElement>().ToArray())
                            {
                                td.Text.Add(CleanHtml(xel).OuterXml);
                            }
                            if (cmwl.MarkupText != null)
                            {
                                td.Text.Add(cmwl.MarkupText);
                            }
                        }
                    }

                    if (aa.Data != null)
                    {
                        foreach (XmlNode nd in aa.Data)
                        {
                            td.Text.Add(CleanHtml(nd).OuterXml);
                        }
                    }

                    // Add if not ignored
                    if (parameters.ContainsKey("omit-other-annotation") &&
                        parameters["omit-other-annotation"] != null &&
                        parameters["omit-other-annotation"].Contains(td.Title))
                    {
                        continue;
                    }
                    documentation.Other.Add(td);
                }
            }

            #endregion
            return(documentation);
        }
Beispiel #6
0
        public void Compile()
        {
            // Already processing?
            if (interactionModel == null || processingStack.Contains(interactionModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT)))
            {
                return;
            }

            // Add to the processing stack
            processingStack.Add(interactionModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT));

            // Otput the name of the package.
            System.Diagnostics.Trace.WriteLine(string.Format("Compiling interaction model package '{0}'...", interactionModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT)), "debug");

            // Check if the package has already been "compiled"
            if (ClassRepository.ContainsKey(interactionModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT)))
            {
                return; // Already compiled
            }
            // Process the interaction
            MohawkCollege.EHR.gpmr.COR.Interaction interaction = new MohawkCollege.EHR.gpmr.COR.Interaction();
            interaction.Name = interactionModel.PackageLocation.ToString(MifCompiler.NAME_FORMAT);
            //interaction.Realm = interactionModel.PackageLocation.Realm;

            // Process business names
            foreach (BusinessName bn in interactionModel.BusinessName ?? new List <BusinessName>())
            {
                if (bn.Language == MifCompiler.Language || bn.Language == null)
                {
                    interaction.BusinessName = bn.Name;
                }
            }

            // Process documentation
            if (interactionModel.Annotations != null)
            {
                interaction.Documentation = DocumentationParser.Parse(interactionModel.Annotations.Documentation);
            }

            // Set the derivation from pointer
            interaction.DerivedFrom = interactionModel;

            // Trigger event
            interaction.TriggerEvent = interactionModel.InvokingTriggerEvent.ToString(MifCompiler.NAME_FORMAT);

            // Types
            TypeReference tr = new TypeReference();

            // Has the entry class been created yet?
            if (!ClassRepository.ContainsKey(interactionModel.ArgumentMessage.ToString(MifCompiler.NAME_FORMAT)))
            {
                // Process
                PackageParser.Parse(interactionModel.ArgumentMessage.ToString(MifCompiler.NAME_FORMAT), repository, ClassRepository);
            }

            var entry = (ClassRepository[interactionModel.ArgumentMessage.ToString(MifCompiler.NAME_FORMAT)] as MohawkCollege.EHR.gpmr.COR.SubSystem);

            // Could we even find the model?
            if (entry == null)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Could not find the argument message '{0}', interaction '{1}' can't be processed",
                                                                 interactionModel.ArgumentMessage.ToString(MifCompiler.NAME_FORMAT), interaction.Name), "error");
                return;
            }
            else if (entry.EntryPoint.Count == 0)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Argument message '{0}' must have an entry point, interaction '{1}' can't be processed",
                                                                 entry.Name, interaction.Name), "error");
                return;
            }
            else if (entry.EntryPoint.Count != 1)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Ambiguous entry point for argument message '{0}', interaction '{1}' can't be processed", entry.Name, interaction.Name), "error");
                return;
            }

            // Set the entry class
            tr          = entry.EntryPoint[0].CreateTypeReference();
            tr.MemberOf = ClassRepository; // Set member of property
            ProcessTypeParameters(interactionModel.ArgumentMessage.ParameterModel, tr, interaction);
            interaction.MessageType = tr;

            #region Response types
            if (interactionModel.ReceiverResponsibilities != null)
            {
                // Create the array
                interaction.Responses = new List <MohawkCollege.EHR.gpmr.COR.Interaction>();

                //  Iterate through
                foreach (ReceiverResponsibility rr in interactionModel.ReceiverResponsibilities)
                {
                    if (rr.InvokeInteraction == null)
                    {
                        System.Diagnostics.Trace.WriteLine("Invoking interaction on receiver responsibility is missing", "warn");
                        continue;
                    }

                    // Does the receiver responsibility exist in the class repository
                    if (!ClassRepository.ContainsKey(rr.InvokeInteraction.ToString(MifCompiler.NAME_FORMAT)))
                    {
                        InteractionCompiler icc = new InteractionCompiler();
                        icc.PackageRepository = repository;
                        icc.Package           = repository.Find(o => o.PackageLocation.ToString(MifCompiler.NAME_FORMAT) == rr.InvokeInteraction.ToString(MifCompiler.NAME_FORMAT));
                        icc.ClassRepository   = ClassRepository;
                        icc.Compile();
                    }

                    MohawkCollege.EHR.gpmr.COR.Feature foundFeature = null;
                    if (ClassRepository.TryGetValue(rr.InvokeInteraction.ToString(MifCompiler.NAME_FORMAT), out foundFeature))
                    {
                        // Reason element for documentation
                        if (rr.Reason != null && (rr.Reason.Language == MifCompiler.Language || rr.Reason.Language == null) &&
                            (rr.Reason.MarkupElements != null || rr.Reason.MarkupText != null))
                        {
                            MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation td = new MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation()
                            {
                                Title = "Reason", Name = "Reason", Text = new List <string>()
                            };
                            if (rr.Reason.MarkupText != null)
                            {
                                td.Text.Add(rr.Reason.MarkupText);
                            }
                            if (rr.Reason.MarkupElements != null)
                            {
                                foreach (XmlElement xe in rr.Reason.MarkupElements)
                                {
                                    td.Text.Add(xe.OuterXml.Replace(" xmlns:html=\"http://www.w3.org/1999/xhtml\"", "").Replace("html:", ""));
                                }
                            }

                            // Append the documentation
                            if (interaction.Documentation == null)
                            {
                                interaction.Documentation = new MohawkCollege.EHR.gpmr.COR.Documentation();
                            }
                            if (interaction.Documentation.Other == null)
                            {
                                interaction.Documentation.Other = new List <MohawkCollege.EHR.gpmr.COR.Documentation.TitledDocumentation>();
                            }
                            interaction.Documentation.Other.Add(td);
                        }
                        interaction.Responses.Add(foundFeature as MohawkCollege.EHR.gpmr.COR.Interaction);
                    }
                    else
                    {
                        System.Diagnostics.Trace.WriteLine(String.Format("Can't find response interaction '{0}'...", rr.InvokeInteraction.ToString(MifCompiler.NAME_FORMAT)), "warn");
                    }
                }
            }
            #endregion

            // Fire the complete method
            interaction.FireParsed();
        }