Exemplo n.º 1
0
        /// <summary> Returns source code for a JavaDoc snippet listing the contents of a Group
        /// or Message.
        /// </summary>
        public static String makeElementsDoc(IStructureDef[] structures)
        {
            StringBuilder elements = new StringBuilder();

            elements.Append("///<ol>\r\n");
            for (int i = 0; i < structures.Length; i++)
            {
                IStructureDef def = structures[i];
                elements.Append("///<li>");
                elements.Append(i);
                elements.Append(": ");
                elements.Append(def.Name);
                elements.Append(" (");
                elements.Append(def.Description);
                elements.Append(") ");
                if (!def.Required)
                {
                    elements.Append("optional ");
                }
                if (def.Repeating)
                {
                    elements.Append("repeating");
                }
                elements.Append("</li>\r\n");
            }
            elements.Append("///</ol>\r\n");
            return(elements.ToString());
        }
Exemplo n.º 2
0
		/// <summary> Creates source code for a Group and returns a GroupDef object that 
		/// describes the Group's name, optionality, repeatability.  The source 
		/// code is written under the given directory.
		/// The structures list may contain [] and {} pairs representing 
		/// nested groups and their optionality and repeastability.  In these cases
		/// this method is called recursively.
		/// If the given structures list begins and ends with repetition and/or 
		/// optionality markers the repetition and optionality of the returned 
		/// GroupDef are set accordingly.  
		/// <param name="structures">a list of the structures that comprise this group - must 
		/// be at least 2 long
		/// </param>
		/// <param name="groupName">The group name</param>
		/// <param name="version">The version of message</param>
		/// <param name="baseDirectory">the directory to which files should be written
		/// </param>
		/// <param name="message">the message to which this group belongs
		/// </param>
		/// <throws>  HL7Exception if the repetition and optionality markers are not  </throws>
		/// </summary>
		public static GroupDef writeGroup(IStructureDef[] structures, String groupName, String baseDirectory, String version,
			String message)
		{
			//make base directory
			if (!(baseDirectory.EndsWith("\\") || baseDirectory.EndsWith("/")))
			{
				baseDirectory = baseDirectory + "/";
			}
			FileInfo targetDir =
				SourceGenerator.makeDirectory(baseDirectory + PackageManager.GetVersionPackagePath(version) + "Group");

			// some group names are troublesome and have "/" which will cause problems when writing files
			groupName = groupName.Replace("/", "_");
			GroupDef group = getGroupDef(structures, groupName, baseDirectory, version, message);

			using (StreamWriter out_Renamed = new StreamWriter(targetDir.FullName + @"\" + group.Name + ".cs"))
			{
				out_Renamed.Write(makePreamble(group, version));
				out_Renamed.Write(makeConstructor(group, version));

				IStructureDef[] shallow = group.Structures;
				for (int i = 0; i < shallow.Length; i++)
				{
					out_Renamed.Write(makeAccessor(group, i));
				}
				out_Renamed.Write("}\r\n"); //Closing class
				out_Renamed.Write("}\r\n"); //Closing namespace
			}
			return group;
		}
        /// <summary>
        /// Creates new structure according to def belonging to factory's faction
        /// </summary>
        /// <param name="def">Definition of structure to be created</param>
        /// <returns>Structure defined by Def belonging to factory's faction</returns>
        public IStructure <IStructureDef> CreateNewBuilding(IStructureDef def)
        {
            IStructure <IStructureDef> buildingController;

            switch (def)
            {
            case ResourceFactoryDef resourceFactoryDef:
                buildingController = new ResourceFactoryController(resourceFactoryDef, faction);
                break;

            case TrainingStructureDef trainingStructureDef:
                TrainingStructureController structureController = new TrainingStructureController(trainingStructureDef, game.Spawner);
                structureController.Faction = faction;

                GameActionFactory gameActionFactory = new GameActionFactory(game);

                foreach (ITrainableDef trainableDef in trainingStructureDef.TrainableDefs)
                {
                    IGameAction gameAction = gameActionFactory.CreateTrainAction(trainableDef, structureController);
                    structureController.GameActions.Add(gameAction);
                }

                buildingController = structureController;

                break;

            default:
                buildingController         = new BuildingController((BuildingDef)def);
                buildingController.Faction = faction;
                break;
            }

            return(buildingController);
        }
Exemplo n.º 4
0
        /// <summary> Returns source code for the contructor for this Group class. </summary>
        public static String makeConstructor(GroupDef group, String version)
        {
            bool useFactory = ConfigurationSettings.UseFactory;

            StringBuilder source = new StringBuilder();

            source.Append("\t///<summary> \r\n");
            source.Append("\t/// Creates a new ");
            source.Append(group.Name);
            source.Append(" Group.\r\n");
            source.Append("\t///</summary>\r\n");
            source.Append("\tpublic ");
            source.Append(group.Name);
            source.Append("(IGroup parent, IModelClassFactory factory) : base(parent, factory){\r\n");
            source.Append("\t   try {\r\n");
            IStructureDef[] structs    = group.Structures;
            int             numStructs = structs.Length;

            for (int i = 0; i < numStructs; i++)
            {
                IStructureDef def = structs[i];

                if (def.Name.Equals("?"))
                {
                    source.Append("\t      this.addNonstandardSegment(\"ANY\");\r\n");
                }
                else
                {
                    if (useFactory)
                    {
                        source.Append("\t      this.add(factory.get");
                        source.Append((def is GroupDef) ? "Group" : "Segment");
                        source.Append("Class(\"");
                        source.Append(def.Name);
                        source.Append("\", \"");
                        source.Append(version);
                        source.Append("\"), ");
                    }
                    else
                    {
                        source.Append("\t      this.add(typeof(");
                        source.Append(def.Name);
                        source.Append("), ");
                    }

                    source.Append(def.Required.ToString().ToLower());
                    source.Append(", ");
                    source.Append(def.Repeating.ToString().ToLower());
                    source.Append(");\r\n");
                }
            }
            source.Append("\t   } catch(HL7Exception e) {\r\n");
            source.Append("\t      HapiLogFactory.GetHapiLog(GetType()).Error(\"Unexpected error creating ");
            source.Append(group.Name);
            source.Append(" - this is probably a bug in the source code generator.\", e);\r\n");
            source.Append("\t   }\r\n");
            source.Append("\t}\r\n\r\n");
            return(source.ToString());
        }
        public static ConstructingBuildingController CreateNewBUCAt(IStructureDef def, Coords topLeft, Faction_Controller factionController)
        {
            ConstructingBuildingController constructingBuildingController = CreateNewBUC((ConstructingBuildingDef)def, factionController);

            constructingBuildingController.ConstructingBuildingModel = new ConstructingBuildingModel()
            {
                StartCoords = topLeft
            };

            return(constructingBuildingController);
        }
        public void Test_ShouldReturnBuilding_WhenGivenDef(Type defType, Type expectedType)
        {
            //    Arrange
            BuildingFactory buildingFactory = new BuildingFactory(faction, game);
            IStructureDef   def             = (IStructureDef)Activator.CreateInstance(defType);


            //    Act
            IStructure <IStructureDef> building = buildingFactory.CreateNewBuilding(def);

            //    Assert
            Assert.AreEqual(expectedType, building.GetType());
        }
Exemplo n.º 7
0
        public void TestSetUp()
        {
            Mock <GameController> gameMock = new Mock <GameController>(GameSpeed.Regular, GameState.Paused);

            def = new BuildingDef
            {
                Cost       = 50,
                UpkeepCost = 1.4,
            };
            controller = new BuildingController((BuildingDef)def)
            {
                Faction     = new Faction_Controller("test-faction", gameMock.Object),
                StartCoords = new Coords()
            };
        }
Exemplo n.º 8
0
        /// <summary> Creates source code for a Group and returns a GroupDef object that 
        /// describes the Group's name, optionality, repeatability.  The source 
        /// code is written under the given directory.
        /// The structures list may contain [] and {} pairs representing 
        /// nested groups and their optionality and repeastability.  In these cases
        /// this method is called recursively.
        /// If the given structures list begins and ends with repetition and/or 
        /// optionality markers the repetition and optionality of the returned 
        /// GroupDef are set accordingly.  
        /// <param name="structures">a list of the structures that comprise this group - must 
        /// be at least 2 long
        /// </param>
        /// <param name="groupName">The group name</param>
        /// <param name="version">The version of message</param>
        /// <param name="baseDirectory">the directory to which files should be written
        /// </param>
        /// <param name="message">the message to which this group belongs
        /// </param>
        /// <throws>  HL7Exception if the repetition and optionality markers are not  </throws>
        /// </summary>
        public static GroupDef writeGroup(IStructureDef[] structures, System.String groupName, System.String baseDirectory, System.String version, System.String message)
        {
            //make base directory
            if (!(baseDirectory.EndsWith("\\") || baseDirectory.EndsWith("/")))
            {
                baseDirectory = baseDirectory + "/";
            }
            System.IO.FileInfo targetDir = SourceGenerator.makeDirectory(baseDirectory + PackageManager.GetVersionPackagePath(version) + "Group");

            GroupDef group = getGroupDef(structures, groupName, baseDirectory, version, message);
            using (System.IO.StreamWriter out_Renamed = new System.IO.StreamWriter(targetDir.FullName + "/" + group.Name + ".cs"))
            {
                out_Renamed.Write(makePreamble(group, version));
                out_Renamed.Write(makeConstructor(group, version));

                IStructureDef[] shallow = group.Structures;
                for (int i = 0; i < shallow.Length; i++)
                {
                    out_Renamed.Write(makeAccessor(group, i));
                }
                out_Renamed.Write("}\r\n"); //Closing class
                out_Renamed.Write("}\r\n"); //Closing namespace
            }
            return group;
        }
Exemplo n.º 9
0
        /// <summary> Returns source code for the contructor for this Message class.</summary>
        public static String makeConstructor(IStructureDef[] structs, String messageName, String version)
        {
            bool useFactory = ConfigurationSettings.UseFactory;

            StringBuilder source = new StringBuilder();

            source.Append("\t///<summary> \r\n");
            source.Append("\t/// Creates a new ");
            source.Append(messageName);
            source.Append(" Group with custom IModelClassFactory.\r\n");
            source.Append("\t///</summary>\r\n");
            source.Append("\tpublic ");
            source.Append(messageName);
            source.Append("(IModelClassFactory factory) : base(factory){\r\n");
            source.Append("\t   init(factory);\r\n");
            source.Append("\t}\r\n\r\n");
            source.Append("\t///<summary>\r\n");
            source.Append("\t/// Creates a new ");
            source.Append(messageName);
            source.Append(" Group with DefaultModelClassFactory. \r\n");
            source.Append("\t///</summary> \r\n");
            source.Append("\tpublic ");
            source.Append(messageName);
            source.Append("() : base(new DefaultModelClassFactory()) { \r\n");
            source.Append("\t   init(new DefaultModelClassFactory());\r\n");
            source.Append("\t}\r\n\r\n");
            source.Append("\t///<summary>\r\n");
            source.Append("\t/// initalize method for ");
            source.Append(messageName);
            source.Append(".  This does the segment setup for the message. \r\n");
            source.Append("\t///</summary> \r\n");
            source.Append("\tprivate void init(IModelClassFactory factory) {\r\n");
            source.Append("\t   try {\r\n");
            int numStructs = structs.Length;

            for (int i = 0; i < numStructs; i++)
            {
                IStructureDef def = structs[i];
                if (useFactory)
                {
                    source.Append("\t      this.add(factory.get");
                    source.Append((def is GroupDef) ? "Group" : "Segment");
                    source.Append("Class(\"");
                    source.Append(def.Name);
                    source.Append("\", \"");
                    source.Append(version);
                    source.Append("\"), ");
                }
                else
                {
                    source.Append("\t      this.add(typeof(");
                    source.Append(def.Name);
                    source.Append("), ");
                }
                source.Append(def.Required.ToString().ToLower());
                source.Append(", ");
                source.Append(def.Repeating.ToString().ToLower());
                source.Append(");\r\n");
            }
            source.Append("\t   } catch(HL7Exception e) {\r\n");
            source.Append("\t      HapiLogFactory.GetHapiLog(GetType()).Error(\"Unexpected error creating ");
            source.Append(messageName);
            source.Append(" - this is probably a bug in the source code generator.\", e);\r\n");
            source.Append("\t   }\r\n");
            source.Append("\t}\r\n\r\n");
            source.Append(Environment.NewLine);
            source.Append("\tpublic override string Version\r\n");
            source.Append("\t\t{\r\n");
            source.Append("\t\t\tget{\r\n");
            source.Append("\t\t\treturn Constants.VERSION;\r\n");
            source.Append("\t\t\t}\r\n");
            source.Append("\t\t}\r\n");
            return(source.ToString());
        }
Exemplo n.º 10
0
        /// <summary>   Adds an element (segment or group) to this group. </summary>
        ///
        /// <param name="s">    The IStructureDef to process. </param>

        public virtual void addStructure(IStructureDef s)
        {
            this.elements.Add(s);
        }
Exemplo n.º 11
0
        /// <summary> Returns header material for the source code of a Message class (including
        /// package, imports, JavaDoc, and class declaration).
        /// </summary>
        public static System.String makePreamble(IStructureDef[] contents, System.String message, System.String chapter, System.String version)
        {
            System.Text.StringBuilder preamble = new System.Text.StringBuilder();
            preamble.Append("using System;\r\n");
            preamble.Append("using NHapi.Base.Log;\r\n");
            preamble.Append("using ");
            preamble.Append(PackageManager.GetVersionPackageName(version));
            preamble.Append("Group;\r\n");
            preamble.Append("using ");
            preamble.Append(PackageManager.GetVersionPackageName(version));
            preamble.Append("Segment;\r\n");
            preamble.Append("using NHapi.Base;\r\n");
            preamble.Append("using NHapi.Base.Parser;\r\n");
            preamble.Append("using NHapi.Base.Model;\r\n\r\n");

            preamble.Append("namespace ");
            preamble.Append(PackageManager.GetVersionPackageName(version));
            preamble.Append("Message\r\n\r\n");
            preamble.Append("{\r\n");
            preamble.Append("///<summary>\r\n");
            preamble.Append("/// Represents a ");
            preamble.Append(message);
            preamble.Append(" message structure (see chapter ");
            preamble.Append(chapter);
            preamble.Append("). This structure contains the \r\n");
            preamble.Append("/// following elements:\r\n");
            preamble.Append(GroupGenerator.makeElementsDoc(contents));
            preamble.Append("///</summary>\r\n");
            preamble.Append("[Serializable]\r\n");
            preamble.Append("public class ");
            preamble.Append(message);
            preamble.Append(" : AbstractMessage ");

            //implement interface from model.control package if required
            /*Class correspondingControlInterface = Control.getInterfaceImplementedBy(message);
            if (correspondingControlInterface != null) {
            preamble.append("implements ");
            preamble.append(correspondingControlInterface.getName());
            }*/

            preamble.Append(" {\r\n\r\n");

            return preamble.ToString();
        }
Exemplo n.º 12
0
		/// <summary> Given a list of structures and the position of a SegmentDef that 
		/// indicates the start of a group (ie "{" or "["), returns the position
		/// of the corresponding end of the group.  Nested group markers are ignored.  
		/// </summary>
		/// <throws>  IllegalArgumentException if groupStart is out of range or does not  </throws>
		/// <summary>      point to a group opening marker. 
		/// </summary>
		/// <throws>  HL7Exception if the end of the group is not found or if other pairs  </throws>
		/// <summary>      are not properly nested inside this one.  
		/// </summary>
		public static int findGroupEnd(IStructureDef[] structures, int groupStart)
		{
			//  {} is rep; [] is optionality
			String endMarker = null;
			try
			{
				String startMarker = structures[groupStart].Name;
				if (startMarker.Equals("["))
				{
					endMarker = "]";
				}
				else if (startMarker.Equals("{"))
				{
					endMarker = "}";
				}
				else if (startMarker.Equals("[{"))
				{
					endMarker = "}]";
				}
				else
				{
					log.Error("Problem starting at " + groupStart);
					for (int i = 0; i < structures.Length; i++)
					{
						log.Error("Structure " + i + ": " + structures[i].Name);
					}
					throw new ArgumentException("The segment " + startMarker + " does not begin a group - must be [ or {");
				}
			}
			catch (IndexOutOfRangeException)
			{
				throw new ArgumentException("The given start location is out of bounds");
			}

			//loop, increment and decrement opening and closing markers until we get back to 0 
			String segName = null;
			int offset = 0;
			try
			{
				int nestedInside = 1;
				while (nestedInside > 0)
				{
					offset++;
					segName = structures[groupStart + offset].Name;
					if (segName.Equals("{") || segName.Equals("[") || segName.Equals("[{"))
					{
						nestedInside++;
					}
					else if (segName.Equals("}") || segName.Equals("]") || segName.Equals("}]"))
					{
						nestedInside--;
					}
				}
			}
			catch (IndexOutOfRangeException)
			{
				throw new HL7Exception("Couldn't find end of group", HL7Exception.APPLICATION_INTERNAL_ERROR);
			}
			if (!endMarker.Equals(segName))
				throw new HL7Exception("Group markers are not nested properly", HL7Exception.APPLICATION_INTERNAL_ERROR);
			return groupStart + offset;
		}
Exemplo n.º 13
0
        /// <summary> <p>Given a list of structures defining the deep content of a group (as provided in
        /// the normative database, some being pairs of optionality and repetition markers
        /// and segments nested within) returns a GroupDef including a short list of the shallow contents of the
        /// group (including segments and groups that are immediate children).</p>
        /// <p>For example given MSH [PID PV1] {[ERR NTE]}, short list would be something like
        /// MSH PID_GROUP ERR_GROUP (with PID_GROUP marked as optional and ERR_GROUP marked as
        /// optional and repeating).</p>
        /// <p>This method calls writeGroup(...) where necessary in order to create source code for
        /// any nested groups before returning corresponding GroupDefs.</p>
        /// </summary>
        public static GroupDef getGroupDef(IStructureDef[] structures, String groupName, String baseDirectory, String version,
                                           String message)
        {
            GroupDef ret       = null;
            bool     required  = true;
            bool     repeating = false;
            bool     rep_opt   = false;

            int len = structures.Length;

            IStructureDef[] shortList        = new IStructureDef[len];      //place to put final list of groups/seg's w/o opt & rep markers
            int             currShortListPos = 0;
            int             currLongListPos  = 0;

            try
            {
                //check for rep and opt (see if start & end elements are [] or {} AND they are each others' pair) ...
                //System.out.println(len + " " + structures[0].getName() +structures[1].getName()+ ".." +structures[len-2].getName() + structures[len-1].getName()+ " " + message);
                if (optMarkers(structures[0].Name, structures[len - 1].Name) && (findGroupEnd(structures, 0) == len - 1))
                {
                    required = false;
                }
                if (repMarkers(structures[0].Name, structures[len - 1].Name) && (findGroupEnd(structures, 0) == len - 1))
                {
                    repeating = true;
                }
                if (repoptMarkers(structures[0].Name, structures[len - 1].Name) && (findGroupEnd(structures, 0) == len - 1))
                {
                    rep_opt = true;
                }
                if (repeating || !required)
                {
                    if (optMarkers(structures[1].Name, structures[len - 2].Name) && (findGroupEnd(structures, 1) == len - 2))
                    {
                        required = false;
                    }
                    if (repMarkers(structures[1].Name, structures[len - 2].Name) && (findGroupEnd(structures, 1) == len - 2))
                    {
                        repeating = true;
                    }
                }

                //loop through, recurse nested groups, and build short list of structures for this group
                int skip = 0;
                if (!required)
                {
                    skip++;
                }
                if (repeating)
                {
                    skip++;
                }
                if (rep_opt)
                {
                    skip++;
                }
                currLongListPos = skip;
                while (currLongListPos < len - skip)
                {
                    String currSegName = structures[currLongListPos].Name;
                    if (currSegName.Equals("[") || currSegName.Equals("{") || currSegName.Equals("[{"))
                    {
                        //this is the opening of a new group ...
                        String          name               = ((SegmentDef)structures[currLongListPos]).GroupName;
                        int             endOfNewGroup      = findGroupEnd(structures, currLongListPos);
                        IStructureDef[] newGroupStructures = new IStructureDef[endOfNewGroup - currLongListPos + 1];
                        Array.Copy(structures, currLongListPos, newGroupStructures, 0, newGroupStructures.Length);
                        shortList[currShortListPos] = writeGroup(newGroupStructures, name, baseDirectory, version, message);
                        currLongListPos             = endOfNewGroup + 1;
                    }
                    else
                    {
                        //copy verbatim into short list ...
                        shortList[currShortListPos] = structures[currLongListPos];
                        currLongListPos++;
                    }
                    currShortListPos++;
                }
            }
            catch (ArgumentException e)
            {
                throw new HL7Exception("Problem creating nested group: " + e.GetType().FullName + ": " + e.Message,
                                       ErrorCode.APPLICATION_INTERNAL_ERROR);
            }

            if (rep_opt)
            {
                ret = new GroupDef(message, groupName, false, true, "a Group object");
            }
            else
            {
                ret = new GroupDef(message, groupName, required, repeating, "a Group object");
            }
            IStructureDef[] finalList = new IStructureDef[currShortListPos];             //note: incremented after last assignment
            Array.Copy(shortList, 0, finalList, 0, currShortListPos);
            for (int i = 0; i < finalList.Length; i++)
            {
                ret.addStructure(finalList[i]);
            }

            return(ret);
        }
Exemplo n.º 14
0
        /// <summary>   Returns source code for an accessor method for a particular Structure. </summary>
        ///
        /// <exception cref="Exception">    Thrown when an exception error condition occurs. </exception>
        ///
        /// <param name="group">        The group. </param>
        /// <param name="structure">    The structure. </param>
        ///
        /// <returns>   A System.String. </returns>

        public static System.String makeAccessor(GroupDef group, int structure)
        {
            System.Text.StringBuilder source = new System.Text.StringBuilder();

            IStructureDef def = group.Structures[structure];

            System.String name       = def.Name;
            System.String indexName  = group.getIndexName(name);
            System.String getterName = indexName;

            if (def is GroupDef)
            {
                System.String unqualifiedName = ((GroupDef)def).UnqualifiedName;
                getterName = group.getIndexName(unqualifiedName);
            }

            //make accessor for first (or only) rep ...
            source.Append("\t///<summary>\r\n");
            source.Append("\t/// Returns ");
            if (def.Repeating)
            {
                source.Append(" first repetition of ");
            }
            source.Append(indexName);
            source.Append(" (");
            source.Append(def.Description);
            source.Append(") - creates it if necessary\r\n");
            source.Append("\t///</summary>\r\n");
            source.Append("\tpublic ");
            source.Append(def.Name);
            source.Append(" ");
            if (def.Repeating)
            {
                source.Append("Get");
                source.Append(getterName);
                source.Append("() {\r\n");
            }
            else
            {
                source.Append(getterName);
                source.Append(" { \r\n");
                source.Append("get{\r\n");
            }
            source.Append("\t   ");
            source.Append(def.Name);
            source.Append(" ret = null;\r\n");
            source.Append("\t   try {\r\n");
            source.Append("\t      ret = (");
            source.Append(def.Name);
            source.Append(")this.GetStructure(\"");
            source.Append(getterName);
            source.Append("\");\r\n");
            source.Append("\t   } catch(HL7Exception e) {\r\n");
            source.Append(
                "\t      HapiLogFactory.GetHapiLog(GetType()).Error(\"Unexpected error accessing data - this is probably a bug in the source code generator.\", e);\r\n");
            source.Append("\t      throw new System.Exception(\"An unexpected error ocurred\",e);\r\n");
            source.Append("\t   }\r\n");
            source.Append("\t   return ret;\r\n");
            if (!def.Repeating)
            {
                source.Append("\t}\r\n");
            }
            source.Append("\t}\r\n\r\n");

            if (def.Repeating)
            {
                //make accessor for specific rep ...
                source.Append("\t///<summary>\r\n");
                source.Append("\t///Returns a specific repetition of ");
                source.Append(indexName);
                source.Append("\r\n");
                source.Append("\t/// * (");
                source.Append(def.Description);
                source.Append(") - creates it if necessary\r\n");
                source.Append("\t/// throws HL7Exception if the repetition requested is more than one \r\n");
                source.Append("\t///     greater than the number of existing repetitions.\r\n");
                source.Append("\t///</summary>\r\n");
                source.Append("\tpublic ");
                source.Append(def.Name);
                source.Append(" Get");
                source.Append(getterName);
                source.Append("(int rep) { \r\n");
                source.Append("\t   return (");
                source.Append(def.Name);
                source.Append(")this.GetStructure(\"");
                source.Append(getterName);
                source.Append("\", rep);\r\n");
                source.Append("\t}\r\n\r\n");

                //make accessor for number of reps
                source.Append("\t/** \r\n");
                source.Append("\t * Returns the number of existing repetitions of ");
                source.Append(indexName);
                source.Append(" \r\n");
                source.Append("\t */ \r\n");
                source.Append("\tpublic int ");
                source.Append(getterName);
                source.Append("RepetitionsUsed { \r\n");
                source.Append("get{\r\n");
                source.Append("\t    int reps = -1; \r\n");
                source.Append("\t    try { \r\n");
                source.Append("\t        reps = this.GetAll(\"");
                source.Append(getterName);
                source.Append("\").Length; \r\n");
                source.Append("\t    } catch (HL7Exception e) { \r\n");
                source.Append(
                    "\t        string message = \"Unexpected error accessing data - this is probably a bug in the source code generator.\"; \r\n");
                source.Append("\t        HapiLogFactory.GetHapiLog(GetType()).Error(message, e); \r\n");
                source.Append("\t        throw new System.Exception(message);\r\n");
                source.Append("\t    } \r\n");
                source.Append("\t    return reps; \r\n");
                source.Append("\t}\r\n");
                source.Append("\t} \r\n\r\n");
            }

            return(source.ToString());
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="completedBuildingDef">Definition of the to-be-built structure</param>
 /// <param name="constructionTime">Time it takes to build structure</param>
 public ConstructingBuildingDef(IStructureDef completedBuildingDef, int constructionTime)
 {
     CompletedBuildingDef = completedBuildingDef;
     ConstructionTime = constructionTime;
 }
Exemplo n.º 16
0
 /// <summary> Adds an element (segment or group) to this group.  </summary>
 public virtual void AddStructure(IStructureDef s)
 {
     Elements.Add(s);
 }
Exemplo n.º 17
0
		/// <summary> <p>Given a list of structures defining the deep content of a group (as provided in 
		/// the normative database, some being pairs of optionality and repetition markers
		/// and segments nested within) returns a GroupDef including a short list of the shallow contents of the 
		/// group (including segments and groups that are immediate children).</p> 
		/// <p>For example given MSH [PID PV1] {[ERR NTE]}, short list would be something like 
		/// MSH PID_GROUP ERR_GROUP (with PID_GROUP marked as optional and ERR_GROUP marked as 
		/// optional and repeating).</p>
		/// <p>This method calls writeGroup(...) where necessary in order to create source code for 
		/// any nested groups before returning corresponding GroupDefs.</p>
		/// </summary>
		public static GroupDef getGroupDef(IStructureDef[] structures, String groupName, String baseDirectory, String version,
			String message)
		{
			GroupDef ret = null;
			bool required = true;
			bool repeating = false;
			bool rep_opt = false;

			int len = structures.Length;
			IStructureDef[] shortList = new IStructureDef[len]; //place to put final list of groups/seg's w/o opt & rep markers
			int currShortListPos = 0;
			int currLongListPos = 0;

			try
			{
				//check for rep and opt (see if start & end elements are [] or {} AND they are each others' pair) ... 
				//System.out.println(len + " " + structures[0].getName() +structures[1].getName()+ ".." +structures[len-2].getName() + structures[len-1].getName()+ " " + message);
				if (optMarkers(structures[0].Name, structures[len - 1].Name) && (findGroupEnd(structures, 0) == len - 1))
					required = false;
				if (repMarkers(structures[0].Name, structures[len - 1].Name) && (findGroupEnd(structures, 0) == len - 1))
					repeating = true;
				if (repoptMarkers(structures[0].Name, structures[len - 1].Name) && (findGroupEnd(structures, 0) == len - 1))
					rep_opt = true;
				if (repeating || !required)
				{
					if (optMarkers(structures[1].Name, structures[len - 2].Name) && (findGroupEnd(structures, 1) == len - 2))
						required = false;
					if (repMarkers(structures[1].Name, structures[len - 2].Name) && (findGroupEnd(structures, 1) == len - 2))
						repeating = true;
				}

				//loop through, recurse nested groups, and build short list of structures for this group
				int skip = 0;
				if (!required)
					skip++;
				if (repeating)
					skip++;
				if (rep_opt)
					skip++;
				currLongListPos = skip;
				while (currLongListPos < len - skip)
				{
					String currSegName = structures[currLongListPos].Name;
					if (currSegName.Equals("[") || currSegName.Equals("{") || currSegName.Equals("[{"))
					{
						//this is the opening of a new group ... 
						String name = ((SegmentDef) structures[currLongListPos]).GroupName;
						int endOfNewGroup = findGroupEnd(structures, currLongListPos);
						IStructureDef[] newGroupStructures = new IStructureDef[endOfNewGroup - currLongListPos + 1];
						Array.Copy(structures, currLongListPos, newGroupStructures, 0, newGroupStructures.Length);
						shortList[currShortListPos] = writeGroup(newGroupStructures, name, baseDirectory, version, message);
						currLongListPos = endOfNewGroup + 1;
					}
					else
					{
						//copy verbatim into short list ... 
						shortList[currShortListPos] = structures[currLongListPos];
						currLongListPos++;
					}
					currShortListPos++;
				}
			}
			catch (ArgumentException e)
			{
				throw new HL7Exception("Problem creating nested group: " + e.GetType().FullName + ": " + e.Message,
					HL7Exception.APPLICATION_INTERNAL_ERROR);
			}

			if (rep_opt)
				ret = new GroupDef(message, groupName, false, true, "a Group object");
			else
				ret = new GroupDef(message, groupName, required, repeating, "a Group object");
			IStructureDef[] finalList = new IStructureDef[currShortListPos]; //note: incremented after last assignment
			Array.Copy(shortList, 0, finalList, 0, currShortListPos);
			for (int i = 0; i < finalList.Length; i++)
			{
				ret.addStructure(finalList[i]);
			}

			return ret;
		}
Exemplo n.º 18
0
 /// <summary> Adds an element (segment or group) to this group.  </summary>
 public virtual void addStructure(IStructureDef s)
 {
     elements.Add(s);
 }
Exemplo n.º 19
0
		/// <summary> Returns source code for a JavaDoc snippet listing the contents of a Group 
		/// or Message.  
		/// </summary>
		public static String makeElementsDoc(IStructureDef[] structures)
		{
			StringBuilder elements = new StringBuilder();
			elements.Append("///<ol>\r\n");
			for (int i = 0; i < structures.Length; i++)
			{
				IStructureDef def = structures[i];
				elements.Append("///<li>");
				elements.Append(i);
				elements.Append(": ");
				elements.Append(def.Name);
				elements.Append(" (");
				elements.Append(def.Description);
				elements.Append(") ");
				if (!def.Required)
					elements.Append("optional ");
				if (def.Repeating)
					elements.Append("repeating");
				elements.Append("</li>\r\n");
			}
			elements.Append("///</ol>\r\n");
			return elements.ToString();
		}
Exemplo n.º 20
0
        /// <summary> Returns source code for an accessor method for a particular Structure. </summary>
        public static String makeAccessor(GroupDef group, int structure)
        {
            StringBuilder source = new StringBuilder();

            IStructureDef def = group.Structures[structure];

            String name       = def.Name;
            String indexName  = group.getIndexName(name);
            String getterName = indexName;

            if (def is GroupDef)
            {
                String unqualifiedName = ((GroupDef)def).UnqualifiedName;
                getterName = group.getIndexName(unqualifiedName);
            }

            //make accessor for first (or only) rep ...
            source.Append("\t///<summary>\r\n");
            source.Append("\t/// Returns ");
            if (def.Repeating)
            {
                source.Append(" first repetition of ");
            }
            source.Append(indexName);
            source.Append(" (");
            source.Append(def.Description);
            source.Append(") - creates it if necessary\r\n");
            source.Append("\t///</summary>\r\n");
            source.Append("\tpublic ");
            source.Append(def.Name);
            source.Append(" ");
            if (def.Repeating)
            {
                source.Append("Get");
                source.Append(getterName);
                source.Append("() {\r\n");
            }
            else
            {
                source.Append(getterName);
                source.Append(" { \r\n");
                source.Append("get{\r\n");
            }
            source.Append("\t   ");
            source.Append(def.Name);
            source.Append(" ret = null;\r\n");
            source.Append("\t   try {\r\n");
            source.Append("\t      ret = (");
            source.Append(def.Name);
            source.Append(")this.GetStructure(\"");
            source.Append(getterName);
            source.Append("\");\r\n");
            source.Append("\t   } catch(HL7Exception e) {\r\n");
            source.Append(
                "\t      HapiLogFactory.GetHapiLog(GetType()).Error(\"Unexpected error accessing data - this is probably a bug in the source code generator.\", e);\r\n");
            source.Append("\t      throw new System.Exception(\"An unexpected error ocurred\",e);\r\n");
            source.Append("\t   }\r\n");
            source.Append("\t   return ret;\r\n");
            if (!def.Repeating)
            {
                source.Append("\t}\r\n");
            }
            source.Append("\t}\r\n\r\n");

            if (def.Repeating)
            {
                //make accessor for specific rep ...
                source.Append("\t///<summary>\r\n");
                source.Append("\t///Returns a specific repetition of ");
                source.Append(indexName);
                source.Append("\r\n");
                source.Append("\t/// * (");
                source.Append(def.Description);
                source.Append(") - creates it if necessary\r\n");
                source.Append("\t/// throws HL7Exception if the repetition requested is more than one \r\n");
                source.Append("\t///     greater than the number of existing repetitions.\r\n");
                source.Append("\t///</summary>\r\n");
                source.Append("\tpublic ");
                source.Append(def.Name);
                source.Append(" Get");
                source.Append(getterName);
                source.Append("(int rep) { \r\n");
                source.Append("\t   return (");
                source.Append(def.Name);
                source.Append(")this.GetStructure(\"");
                source.Append(getterName);
                source.Append("\", rep);\r\n");
                source.Append("\t}\r\n\r\n");

                //make accessor for number of reps
                source.Append("\t/** \r\n");
                source.Append("\t * Returns the number of existing repetitions of ");
                source.Append(indexName);
                source.Append(" \r\n");
                source.Append("\t */ \r\n");
                source.Append("\tpublic int ");
                source.Append(getterName);
                source.Append("RepetitionsUsed { \r\n");
                source.Append("get{\r\n");
                source.Append("\t    int reps = -1; \r\n");
                source.Append("\t    try { \r\n");
                source.Append("\t        reps = this.GetAll(\"");
                source.Append(getterName);
                source.Append("\").Length; \r\n");
                source.Append("\t    } catch (HL7Exception e) { \r\n");
                source.Append(
                    "\t        string message = \"Unexpected error accessing data - this is probably a bug in the source code generator.\"; \r\n");
                source.Append("\t        HapiLogFactory.GetHapiLog(GetType()).Error(message, e); \r\n");
                source.Append("\t        throw new System.Exception(message);\r\n");
                source.Append("\t    } \r\n");
                source.Append("\t    return reps; \r\n");
                source.Append("\t}\r\n");
                source.Append("\t} \r\n\r\n");

                // make enumerator
                source.Append("\t/** \r\n");
                source.Append("\t * Enumerate over the " + def.Name + " results \r\n");
                source.Append("\t */ \r\n");
                source.Append("\tpublic IEnumerable<" + def.Name + "> " + getterName + "s \r\n");
                source.Append("\t{ \r\n");
                source.Append("\t\tget\r\n");
                source.Append("\t\t{\r\n");
                source.Append("\t\t\tfor (int rep = 0; rep < " + getterName + "RepetitionsUsed; rep++)\r\n");
                source.Append("\t\t\t{\r\n");
                source.Append("\t\t\t\tyield return (" + def.Name + ")this.GetStructure(\"" + getterName + "\", rep);\r\n");
                source.Append("\t\t\t}\r\n");
                source.Append("\t\t}\r\n");
                source.Append("\t}\r\n\r\n");

                // make Add function
                source.Append("\t///<summary>\r\n");
                source.Append("\t///Adds a new " + def.Name + "\r\n");
                source.Append("\t///</summary>\r\n");
                source.Append("\tpublic " + def.Name + " Add" + getterName + "()\r\n");
                source.Append("\t{\r\n");
                source.Append("\t\treturn this.AddStructure(\"" + getterName + "\") as " + def.Name + ";\r\n");
                source.Append("\t}\r\n");

                source.Append("\r\n");

                // make Remove function
                source.Append("\t///<summary>\r\n");
                source.Append("\t///Removes the given " + def.Name + "\r\n");
                source.Append("\t///</summary>\r\n");
                source.Append("\tpublic void Remove" + getterName + "(" + def.Name + " toRemove)\r\n");
                source.Append("\t{\r\n");
                source.Append("\t\tthis.RemoveStructure(\"" + getterName + "\", toRemove);\r\n");
                source.Append("\t}\r\n");

                source.Append("\r\n");

                // make Remove At function
                source.Append("\t///<summary>\r\n");
                source.Append("\t///Removes the " + def.Name + " at the given index\r\n");
                source.Append("\t///</summary>\r\n");
                source.Append("\tpublic void Remove" + getterName + "At(int index)\r\n");
                source.Append("\t{\r\n");
                source.Append("\t\tthis.RemoveRepetition(\"" + getterName + "\", index);\r\n");
                source.Append("\t}\r\n");

                source.Append("\r\n");
            }

            return(source.ToString());
        }
        /// <summary> <p>Given a list of structures defining the deep content of a group (as provided in
        /// the normative database, some being pairs of optionality and repetition markers
        /// and segments nested within) returns a GroupDef including a short list of the shallow contents of the
        /// group (including segments and groups that are immediate children).</p>
        /// <p>For example given MSH [PID PV1] {[ERR NTE]}, short list would be something like
        /// MSH PID_GROUP ERR_GROUP (with PID_GROUP marked as optional and ERR_GROUP marked as
        /// optional and repeating).</p>
        /// <p>This method calls writeGroup(...) where necessary in order to create source code for
        /// any nested groups before returning corresponding GroupDefs.</p>
        /// </summary>
        public static GroupDef GetGroupDef(
            IStructureDef[] structures,
            string groupName,
            string baseDirectory,
            string version,
            string message)
        {
            GroupDef ret;
            var      required  = true;
            var      repeating = false;
            var      rep_opt   = false;

            var len              = structures.Length;
            var shortList        = new IStructureDef[len]; // place to put final list of groups/segments w/o opt & rep markers
            var currShortListPos = 0;

            try
            {
                // check for rep and opt (see if start & end elements are [] or {} AND they are each others' pair) ...
                // System.out.println(len + " " + structures[0].getName() +structures[1].getName()+ ".." +structures[len-2].getName() + structures[len-1].getName()+ " " + message);
                if (OptMarkers(structures[0].Name, structures[len - 1].Name) && (FindGroupEnd(structures, 0) == len - 1))
                {
                    required = false;
                }

                if (RepMarkers(structures[0].Name, structures[len - 1].Name) && (FindGroupEnd(structures, 0) == len - 1))
                {
                    repeating = true;
                }

                if (RepoptMarkers(structures[0].Name, structures[len - 1].Name) && (FindGroupEnd(structures, 0) == len - 1))
                {
                    rep_opt = true;
                }

                if (repeating || !required)
                {
                    if (OptMarkers(structures[1].Name, structures[len - 2].Name) && (FindGroupEnd(structures, 1) == len - 2))
                    {
                        required = false;
                    }

                    if (RepMarkers(structures[1].Name, structures[len - 2].Name) && (FindGroupEnd(structures, 1) == len - 2))
                    {
                        repeating = true;
                    }
                }

                // loop through, recurse nested groups, and build short list of structures for this group
                var skip = 0;
                if (!required)
                {
                    skip++;
                }

                if (repeating)
                {
                    skip++;
                }

                if (rep_opt)
                {
                    skip++;
                }

                var currLongListPos = skip;
                while (currLongListPos < len - skip)
                {
                    var currSegName = structures[currLongListPos].Name;
                    if (currSegName.Equals("[") || currSegName.Equals("{") || currSegName.Equals("[{"))
                    {
                        // this is the opening of a new group ...
                        var name = ((SegmentDef)structures[currLongListPos]).GroupName;

                        // Fix mistakes in DB (ported from hapi)
                        // Fix github issue https://github.com/nHapiNET/nHapi/issues/298
                        if (name is not null)
                        {
                            if (groupName == "OBSERVATION_REQUEST" && message == "ORL_O34" && name == "SPECIMEN" && version == "2.5.1")
                            {
                                name = "OBSERVATION_REQUEST_SPECIMEN";
                            }
                        }

                        var endOfNewGroup      = FindGroupEnd(structures, currLongListPos);
                        var newGroupStructures = new IStructureDef[endOfNewGroup - currLongListPos + 1];
                        Array.Copy(structures, currLongListPos, newGroupStructures, 0, newGroupStructures.Length);
                        shortList[currShortListPos] = WriteGroup(newGroupStructures, name, baseDirectory, version, message);
                        currLongListPos             = endOfNewGroup + 1;
                    }
                    else
                    {
                        // copy verbatim into short list ...
                        shortList[currShortListPos] = structures[currLongListPos];
                        currLongListPos++;
                    }

                    currShortListPos++;
                }
            }
            catch (ArgumentException e)
            {
                throw new HL7Exception(
                          "Problem creating nested group: " + e.GetType().FullName + ": " + e.Message,
                          ErrorCode.APPLICATION_INTERNAL_ERROR);
            }

            if (rep_opt)
            {
                ret = new GroupDef(message, groupName, false, true, "a Group object");
            }
            else
            {
                ret = new GroupDef(message, groupName, required, repeating, "a Group object");
            }

            var finalList = new IStructureDef[currShortListPos]; // note: incremented after last assignment

            Array.Copy(shortList, 0, finalList, 0, currShortListPos);
            foreach (var structureDef in finalList)
            {
                // Fix mistakes in the DB (ported from hapi)
                if (structureDef.UnqualifiedName == "ED")
                {
                    continue;
                }

                // Fix github issue https://github.com/nHapiNET/nHapi/issues/298
                if (ret.UnqualifiedName == "OBSERVATION_REQUEST" && message == "ORL_O34" && structureDef.UnqualifiedName == "SPECIMEN" && version == "2.5.1")
                {
                    ((GroupDef)structureDef).GroupName = "OBSERVATION_REQUEST_SPECIMEN";
                    Log.Info($"Forcing name to {((GroupDef)structureDef).GroupName}");
                }

                ret.AddStructure(structureDef);
            }

            return(ret);
        }
Exemplo n.º 22
0
        /// <summary> Returns source code for the contructor for this Message class.</summary>
        public static System.String makeConstructor(IStructureDef[] structs, System.String messageName, System.String version)
        {
            bool useFactory = NHapi.Base.ConfigurationSettings.UseFactory;

            System.Text.StringBuilder source = new System.Text.StringBuilder();

            source.Append("\t///<summary> \r\n");
            source.Append("\t/// Creates a new ");
            source.Append(messageName);
            source.Append(" Group with custom IModelClassFactory.\r\n");
            source.Append("\t///</summary>\r\n");
            source.Append("\tpublic ");
            source.Append(messageName);
            source.Append("(IModelClassFactory factory) : base(factory){\r\n");
            source.Append("\t   init(factory);\r\n");
            source.Append("\t}\r\n\r\n");
            source.Append("\t///<summary>\r\n");
            source.Append("\t/// Creates a new ");
            source.Append(messageName);
            source.Append(" Group with DefaultModelClassFactory. \r\n");
            source.Append("\t///</summary> \r\n");
            source.Append("\tpublic ");
            source.Append(messageName);
            source.Append("() : base(new DefaultModelClassFactory()) { \r\n");
            source.Append("\t   init(new DefaultModelClassFactory());\r\n");
            source.Append("\t}\r\n\r\n");
            source.Append("\t///<summary>\r\n");
            source.Append("\t/// initalize method for ");
            source.Append(messageName);
            source.Append(".  This does the segment setup for the message. \r\n");
            source.Append("\t///</summary> \r\n");
            source.Append("\tprivate void init(IModelClassFactory factory) {\r\n");
            source.Append("\t   try {\r\n");
            int numStructs = structs.Length;
            for (int i = 0; i < numStructs; i++)
            {
                IStructureDef def = structs[i];
                if (useFactory)
                {
                    source.Append("\t      this.add(factory.get");
                    source.Append((def is GroupDef) ? "Group" : "Segment");
                    source.Append("Class(\"");
                    source.Append(def.Name);
                    source.Append("\", \"");
                    source.Append(version);
                    source.Append("\"), ");
                }
                else
                {
                    source.Append("\t      this.add(typeof(");
                    source.Append(def.Name);
                    source.Append("), ");
                }
                source.Append(def.Required.ToString().ToLower());
                source.Append(", ");
                source.Append(def.Repeating.ToString().ToLower());
                source.Append(");\r\n");
            }
            source.Append("\t   } catch(HL7Exception e) {\r\n");
            source.Append("\t      HapiLogFactory.GetHapiLog(GetType()).Error(\"Unexpected error creating ");
            source.Append(messageName);
            source.Append(" - this is probably a bug in the source code generator.\", e);\r\n");
            source.Append("\t   }\r\n");
            source.Append("\t}\r\n\r\n");
            source.Append(System.Environment.NewLine);
            source.Append("\tpublic override string Version\r\n");
            source.Append("\t\t{\r\n");
            source.Append("\t\t\tget{\r\n");
            source.Append("\t\t\treturn Constants.VERSION;\r\n");
            source.Append("\t\t\t}\r\n");
            source.Append("\t\t}\r\n");
            return source.ToString();
        }