/// <summary> /// Render literals /// </summary> private void RenderLiterals(StringWriter sw, Enumeration enu, List <string> rendered, List <String> mnemonics, List <Enumeration.EnumerationValue> literals) { // Literals foreach (Enumeration.EnumerationValue ev in literals) { string bn = Util.Util.PascalCase(ev.BusinessName); string rendName = Util.Util.PascalCase(bn ?? ev.Name) ?? "__Unknown"; // Already rendered, so warn and skip if (rendered.Contains(rendName) || mnemonics.Contains(ev.Name)) { System.Diagnostics.Trace.WriteLine(String.Format("Enumeration value {0} already rendered, skipping", ev.BusinessName), "warn"); } else if (!ev.Annotations.Exists(o => o is SuppressBrowseAnnotation)) { sw.Write(DocumentationRenderer.Render(ev.Documentation, 2)); if (DocumentationRenderer.Render(ev.Documentation, 2).Length == 0) // Documentation correction { sw.WriteLine("\t\t/// <summary>{0}</summary>", (ev.BusinessName ?? ev.Name).Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\r", "").Replace("\n", "")); } sw.WriteLine("\t\t[Enumeration(Value = \"{0}\", SupplierDomain = \"{1}\")]", ev.Name, ev.CodeSystem ?? enu.ContentOid); // Annotations? if (ev.Annotations != null && ev.Annotations.Find(o => o is SuppressBrowseAnnotation) != null) { sw.WriteLine("\t\t[EditorBrowsable(EditorBrowsableState.Never)]\r\n\t\t[Browsable(false)]"); } // Business name if (!String.IsNullOrEmpty(ev.BusinessName)) { sw.WriteLine("\t\t[Description(\"{0}\")]", ev.BusinessName.Replace("\r", "").Replace("\n", "")); } if (rendered.Find(o => o.Equals(rendName)) != null) // .NET enumeration field will be the same, so render something different { sw.Write("\t\t{0}", Util.Util.PascalCase(rendName + "_" + ev.Name) ?? "__Unknown"); } else { sw.Write("\t\t{0}", rendName); } sw.WriteLine(","); // Another literal follows sw.Write("\r\n"); // Newline rendered.Add(rendName); // Add to rendered list to keep track mnemonics.Add(ev.Name); } if (ev.RelatedCodes != null) { RenderLiterals(sw, enu, rendered, mnemonics, ev.RelatedCodes); } } }
public void Render(string OwnerNS, string apiNs, Feature f, System.IO.TextWriter tw) { this.generatedFactoryMethods = new List <string>(); // Get a strongly typed reference to the Interaction interaction = f.Clone() as Interaction; interaction.MemberOf = f.MemberOf; StringWriter sw = new StringWriter(); #region Usings // Validate Usings string[] usings = new string[] { "Attributes", "Interfaces", "DataTypes", "DataTypes.Primitives" }; // API usings foreach (string s in usings) { sw.WriteLine("using {1}.{0};", s, apiNs); } // Owner Usings foreach (String s in GatherUsings(interaction.MessageType)) { sw.WriteLine("using {0}.{1};", OwnerNS, s); } if (f.MemberOf.Find(o => o is Enumeration) != null) { sw.WriteLine("using {0}.Vocabulary;", OwnerNS); } #endregion // HACK: If there is no description on the interaction then we'll // HACK: add the business name as a description // TODO: Remove this and come up with a better solution if (interaction.Documentation == null || interaction.Documentation.Description == null) { interaction.Documentation = new Documentation() { Description = new List <string>() { interaction.BusinessName } } } ; // Determine if the interaction is an IInteraction bool isIInteraction = true; string[] members = { "creationtime", "versioncode", "interactionid", "processingmodecode" }; foreach (var m in members) { isIInteraction &= interaction.MessageType.Class.Content.Exists(o => o.Name.ToLower() == m); } sw.WriteLine("namespace {0}.Interactions {{\r\n", OwnerNS); var vLength = sw.ToString().Length; sw.Write(DocumentationRenderer.Render(interaction.Documentation, 1)); if (sw.ToString().Length == vLength) { sw.WriteLine("\t/// <summary>{0}</summary>", interaction.BusinessName != null ? interaction.BusinessName.Replace("\n", "").Replace("\r", "") : interaction.Name); } sw.WriteLine("\t[Structure(Name = \"{0}\", StructureType = StructureAttribute.StructureAttributeType.Interaction)]", interaction.Name); sw.WriteLine("\t[Interaction(TriggerEvent = \"{0}\")]", interaction.TriggerEvent); sw.WriteLine("\t#if !WINDOWS_PHONE"); sw.WriteLine("\t[Serializable]"); sw.WriteLine("\t#endif"); sw.WriteLine("\t[System.CodeDom.Compiler.GeneratedCode(\"gpmr\",\"{0}\")]", Assembly.GetEntryAssembly().GetName().Version.ToString()); foreach (Interaction response in interaction.Responses) { sw.WriteLine("\t[InteractionResponse(Name = \"{0}\", TriggerEvent = \"{1}\")]", response.Name, response.TriggerEvent); } sw.WriteLine("\t[System.ComponentModel.Description(\"{0}\")]", interaction.BusinessName != null ? interaction.BusinessName.Replace("\n", "").Replace("\r", "") : interaction.Name); sw.WriteLine("\tpublic class {0} : {1}{2} {{", interaction.Name, CreateInteractionDatatype(interaction.MessageType), isIInteraction ? ", MARC.Everest.Interfaces.IInteraction" : ""); #region Constants // Get the trigger event sw.WriteLine("\t\t/// <summary> Gets the default trigger event to be used for this interaction </summary>"); sw.WriteLine("\t\tpublic static CV<String> GetTriggerEvent() {{ return new CV<String>(\"{0}\", \"{1}\"); }}", interaction.TriggerEvent, triggerEventOid); // Get the interaction id sw.WriteLine("\t\t/// <summary> Gets the interaction ID of this interaction </summary>"); sw.WriteLine("\t\tpublic static II GetInteractionId() {{ return new II(new OID(\"{1}\"), \"{0}\"); }}", interaction.Name, interactionIdOid); // Get the profile id if (!String.IsNullOrEmpty(profileId)) { sw.WriteLine("\t\t/// <summary> Gets the profile id of this interaction </summary>"); sw.WriteLine("\t\tpublic static LIST<II> GetProfileId() {{ return new LIST<II>() {{ new II (new OID(\"{0}\"), \"{1}\") }}; }}", profileIdOid, profileId); } #endregion #region Constructors sw.WriteLine("\t\t/// <summary> Creates a new, empty instance of {0} </summary>", interaction.Name); sw.WriteLine("\t\tpublic {0}() : base() {{ }}\r\n", interaction.Name); //string ctor_parameters = ""; //string ctor_body = ""; //foreach (ClassContent cc in interaction.MessageType.Class.Content) //{ // if (cc.Conformance == ClassContent.ConformanceKind.Mandatory) // Mandatory ctor // { // if (cc is Property && ((cc as Property).FixedValue == null || (cc as Property).PropertyType == Property.PropertyTypes.TraversableAssociation)) // { // Property p = cc as Property; // TypeReference tr = Datatypes.MapDatatype(p.Type); // // Documentation // if (p.Documentation != null && p.Documentation.Definition != null && p.Documentation.Definition.Count > 0) // sw.WriteLine("\t\t/// <param name=\"{0}\">{1}</param>", p.Name, p.Documentation.Definition[0]); // // Now the signature // ctor_parameters += string.Format("{0} {1},", ClassRenderer.CreateDatatypeRef(tr, p), p.Name); // ctor_body += string.Format("\t\t\tthis.{0} = {1};\r\n", Util.Util.PascalCase(p.Name), p.Name); // } // else // { // } // } //} //// Write CTOR //if (ctor_parameters.Length > 0) //{ // sw.WriteLine("\t\t/// <summary>\r\n\t\t/// CTOR for all mandatory elements\r\n\t\t/// </summary>"); // sw.WriteLine("\t\tpublic {0}({1}) : base() {{ \r\n\t\t{2}\t\t}}", interaction.Name, ctor_parameters.Substring(0, ctor_parameters.Length - 1), ctor_body); //} Dictionary <String, String[]> ctors = ClassRenderer.CreateFactoryMethod(interaction.MessageType, "this", true); // Write CTOR List <String> wroteParms = new List <string>(); // Keep track of the parameters used foreach (KeyValuePair <String, String[]> kv in ctors) { if (kv.Value[0].Length > 0 && !wroteParms.Contains(kv.Value[0])) { wroteParms.Add(kv.Value[0]); sw.WriteLine("\t\t/// <summary>\r\n\t\t/// CTOR for all {0} elements\r\n\t\t/// </summary>", kv.Key); sw.WriteLine(kv.Value[2]); sw.WriteLine("\t\tpublic {0}({1}) : base() {{ \r\n\t\t{2}\t\t}}", interaction.Name, kv.Value[0].Substring(0, kv.Value[0].Length - 1), kv.Value[1]); } } #endregion #region Creator for payload, control act, etc... // Owner namespace if (interaction.MessageType.GenericSupplier != null) { foreach (TypeReference tr in interaction.MessageType.GenericSupplier) { sw.WriteLine(GenerateFactoryMethod(tr, OwnerNS)); } } if (isIInteraction) { // Version code sw.WriteLine("\t\t/// <summary>Implementation of version code</summary>"); sw.WriteLine("\t\tMARC.Everest.DataTypes.Interfaces.ICodedSimple MARC.Everest.Interfaces.IInteraction.VersionCode { get { return this.VersionCode; } }"); // Version code sw.WriteLine("\t\t/// <summary>Implementation of processing mode code</summary>"); sw.WriteLine("\t\tMARC.Everest.DataTypes.Interfaces.ICodedSimple MARC.Everest.Interfaces.IInteraction.ProcessingModeCode { get { return this.ProcessingModeCode; } }"); sw.WriteLine("\t\t/// <summary>Implementation of generic IInteraction.ControlActEvent Property</summary>"); // Create the control act event if (interaction.MessageType.GenericSupplier != null && interaction.MessageType.GenericSupplier.Count > 0) { Property cactProperty = interaction.MessageType.Class.Content.Find(o => o is Property && (o as Property).Type.Name == interaction.MessageType.Class.TypeParameters[0].ParameterName) as Property; if (cactProperty == null) { ; } string cactName = cactProperty.Type.Name == interaction.MessageType.Class.TypeParameters[0].ParameterName ? Util.Util.MakeFriendly(cactProperty.Name) : Util.Util.PascalCase(cactProperty.Name); sw.WriteLine("\t\tSystem.Object MARC.Everest.Interfaces.IInteraction.ControlAct {"); sw.WriteLine("\t\t\tget {{ return this.{0}; }}", cactName); sw.WriteLine("\t\t\tset {{ this.{1} = value as {0}; }}", CreateInteractionDatatype(interaction.MessageType.GenericSupplier[0]), cactName); sw.WriteLine("\t\t}"); } else { sw.WriteLine("\t\tSystem.Object MARC.Everest.Interfaces.IInteraction.ControlAct {"); sw.WriteLine("\t\t\tget { return null; }"); sw.WriteLine("\t\t\tset { ; }"); sw.WriteLine("\t\t}"); } } #endregion sw.WriteLine("\t}"); sw.WriteLine("}"); tw.WriteLine(sw); }
public void Render(string OwnerNS, string apiNs, MohawkCollege.EHR.gpmr.COR.Feature f, System.IO.TextWriter tw) { StringWriter sw = new StringWriter(); // Make a strong typed reference to enumeration Enumeration enu = f as Enumeration; // enumeration is a concept domain? do the binding if (enu is ConceptDomain && (enu as ConceptDomain).ContextBinding != null) { enu = (enu as ConceptDomain).ContextBinding[0]; } else if (enu is ConceptDomain) { throw new InvalidOperationException("Won't render unbound concept domains"); } #region Usings // Validate Usings string[] usings = new string[] { "Attributes", "Interfaces", "DataTypes" }; foreach (string s in usings) { sw.WriteLine("using {1}.{0};", s, apiNs); } sw.WriteLine("using System.ComponentModel;"); #endregion sw.WriteLine("namespace {0}.Vocabulary {{", OwnerNS); // start ns // Generate the documentation if (DocumentationRenderer.Render(enu.Documentation, 1).Length == 0) { sw.WriteLine("\t/// <summary>{0}</summary>", enu.BusinessName ?? "No Documentation Found"); } else { sw.Write(DocumentationRenderer.Render(enu.Documentation, 1)); } // Generate the structure attribute sw.WriteLine("\t[Structure(Name = \"{0}\", CodeSystem = \"{1}\", StructureType = StructureAttribute.StructureAttributeType.{2}, Publisher = {3})]", enu.Name, enu.ContentOid, enu.GetType().Name, enu.Documentation != null ? "\"" + enu.Documentation.Copyright + "\"" : "null"); sw.WriteLine("#if !WINDOWS_PHONE"); sw.WriteLine("\t[Serializable]"); sw.WriteLine("#endif"); string renderName = enu.Name; if (enu.Annotations != null && enu.Annotations.Exists(o => o is RenderAsAnnotation)) { renderName = (enu.Annotations.Find(o => o is RenderAsAnnotation) as RenderAsAnnotation).RenderName; } // Generate enum sw.WriteLine("\tpublic enum {0} {{ ", Util.Util.MakeFriendly(renderName)); List <String> rendered = new List <string>(), mnemonics = new List <string>(); RenderLiterals(sw, enu, rendered, mnemonics, enu.Literals); String tStr = sw.ToString(); tStr = tStr.Remove(tStr.LastIndexOf(",")); tStr += ("\r\n\t}"); tStr += ("\r\n}"); // end ns // Write to tw tw.Write(tStr); }