예제 #1
0
        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);

        }
예제 #2
0
        public void Render(string ownerPackage, string apiNs, Feature f, System.IO.TextWriter tw)
        {

            ClassRenderer.s_imports.Clear();

            // Validate arguments
            if (String.IsNullOrEmpty(ownerPackage))
                throw new ArgumentNullException("ownerPackage");
            if (String.IsNullOrEmpty(apiNs))
                throw new ArgumentNullException("apiNs");
            if (f == null || !(f is Interaction))
                throw new ArgumentException("Parameter must be of type Enumeration", "f");

            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();

            #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 this class is an interaction
            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.Write(DocumentationRenderer.Render(interaction.Documentation, 0));
            sw.WriteLine("@Structure(name = \"{0}\", structureType = StructureType.INTERACTION)", interaction.Name);
            sw.WriteLine("@Interaction(name=\"{1}\", triggerEvent = \"{0}\")", interaction.TriggerEvent, interaction.Name);
            sw.WriteLine("@InteractionResponses( value = {");
            List<String> resp = new List<string>();
            foreach (Interaction response in interaction.Responses)
            {
                if (resp.Contains(response.Name)) continue;
                sw.WriteLine("\t@Interaction(name = \"{0}\", triggerEvent = \"{1}\"){2}", response.Name, response.TriggerEvent, response == interaction.Responses.Last() ? "" : ",");
                resp.Add(response.Name);
            }
            sw.WriteLine("})");
            sw.WriteLine("public class {0} extends {1} {2} {{", interaction.Name, CreateInteractionDatatype(interaction.MessageType, ownerPackage), 
                isIInteraction ? "implements IInteraction" : "");

            #region Constants

            // Get the trigger event
            sw.WriteLine("\t/** Gets the default trigger event to be used for this interaction */");
            sw.WriteLine("\tpublic static CV<String> defaultTriggerEvent() {{ return new CV<String>(\"{0}\", \"{1}\"); }}", interaction.TriggerEvent, triggerEventOid, apiNs);

            // Get the interaction id
            sw.WriteLine("\t/** Gets the interaction ID of this interaction */");
            sw.WriteLine("\tpublic static II defaultInteractionId() {{ return new II(\"{1}\", \"{0}\"); }}", interaction.Name, interactionIdOid, apiNs);

            // Get the profile id
            if (!String.IsNullOrEmpty(profileId))
            {
                sw.WriteLine("\t/** Gets the profile id of this interaction */");
                sw.WriteLine("\tpublic static LIST<II> defaultProfileId() {{ LIST<II> retVal = new LIST<II>(); retVal.add(new II (\"{0}\", \"{1}\")); return retVal; }}", profileIdOid, profileId, apiNs);
            }


            #endregion

            #region Constructors

            sw.WriteLine("\t/** Creates a new, empty instance of {0} */", interaction.Name);
            sw.WriteLine("\tpublic {0}() {{ super(); }}\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, ownerPackage);

            // 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/**\r\n\t * CTOR for all {0} elements\r\n\t", kv.Key);
                    sw.WriteLine(kv.Value[2]);
                    sw.WriteLine("\t*/\r\n\tpublic {0}({1}) {{ \r\n\t\tsuper();\r\n{2}\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, ownerPackage));

            #endregion


            #region Interaction members

            if (isIInteraction)
            {
                sw.WriteLine("\t\t/** Implementation of generic IInteraction.ControlActEvent Property */");
                // 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 = Util.Util.PascalCase(cactProperty.Name);
                    sw.WriteLine("\t\tObject getControlAct() {");
                    sw.WriteLine("\t\t\treturn this.get{0}();", cactName);
                    sw.WriteLine("\t\t}");
                }
                else
                {
                    sw.WriteLine("\t\tObject getControlAct() {");
                    sw.WriteLine("\t\t\treturn null;");
                    sw.WriteLine("\t\t}");
                }
            }
            #endregion

            sw.WriteLine("}");

            #region Usings

            // Interactions package
            tw.WriteLine("package {0}.interaction;", ownerPackage);

            #region Render the imports
            string[] apiImports = { "annotations.*", "datatypes.*", "datatypes.generic.*", "interfaces.IGraphable", "interfaces.IInteraction" },
                jImports = { "java.lang.*", "java.util.*" };
            foreach (var import in apiImports)
                tw.WriteLine("import {0}.{1};", apiNs, import);
            foreach (var import in jImports)
                tw.WriteLine("import {0};", import);
            foreach (var import in ClassRenderer.s_imports)
                tw.WriteLine("import {0};", import);
            #endregion

            tw.WriteLine(sw.ToString());

        }