/// <summary>This method builds a Conformance Segment Class</summary>
		/// <param name="seg">the Segment to build
		/// </param>
		/// <param name="parentUnderlyingType">the data type of the parent Message or SegGroup for this segment
		/// example "Genetibase.NuGenHL7.model.v24.group.ADR_A19_..."    
		/// </param>
		/// <param name="profileName">this is the profile name associated with this Class
		/// </param>
		public virtual void  buildClass(Seg seg, System.String parentUnderlyingType, ProfileName profileName)
		{
			GeneratedConformanceContainer gcc = new GeneratedConformanceContainer();
			System.String underlyingDataType; // The underlying HAPI Type
			
			// Check for possible snags in the Runtime Profile Segment
			if (seg.Name == null || seg.Name.Length < 1)
				throw new ConformanceError("Error building ConformanceSegment: Runtime Segment does not contain a name.");
			
			// Set up class
			gcc.ClassPackage = packageName;
			gcc.addClassImport("Genetibase.NuGenHL7.conf.classes.abs.*");
			gcc.addClassImport("Genetibase.NuGenHL7.conf.classes.exceptions.*");
			gcc.addClassImport("Genetibase.NuGenHL7.*");
			gcc.addClassImport(packageName + "." + profileName.PackageName + ".*");
			
			gcc.Name = profileName.ClassName;
			gcc.Properties = "extends AbstractConformanceContainer implements Repeatable";
			gcc.setMinMaxReps(seg.Min, seg.Max);
			underlyingDataType = "Genetibase.NuGenHL7.model." + versionString + ".segment." + seg.Name;
			
			System.String segClassName = UnderlyingAccessor.getHapiModelClass(underlyingDataType).FullName;
			gcc.addMemberVariable("private " + segClassName + " hapiSegment;");
			
			docBuilder.decorateConstructor(gcc.Constructor, profileName.ClassName);
			
			// Set up underlying Segment type
			GeneratedMethod theConstructor = gcc.Constructor;
			theConstructor.addParam(parentUnderlyingType + " underlyingMessage", "The underlying message object");
			theConstructor.addParam("int rep", "The underlying rep number");
			UnderlyingAccessor underlyingAccessor = new UnderlyingAccessor(parentUnderlyingType, profileName.AccessorName);
			theConstructor.addToBody("this.hapiSegment = (" + segClassName + ") underlyingMessage." + underlyingAccessor.ToString() + ";");
			theConstructor.addToThrows("HL7Exception");
			
			// Loop through each child. Note that array is 1-offset
			for (int i = 1; i <= seg.Fields; i++)
			{
				//don't build not supported, backward, or unknown types
				System.String usage = seg.getField(i).Usage;
				if (usage != null && (usage.Equals("X") || usage.Equals("B") || usage.Equals("U")))
					continue;
				
				// Create the names for each type of child
				//         //CHANGED to use field # instead of name, in support of Z-segments 
				//         ProfileName childProfileName = profileName.newInstance(String.valueOf(i), ProfileName.PS_FIELD);
				ProfileName childProfileName = profileName.newInstance(seg.getField(i).Name, ProfileName.PS_FIELD);
				
				// Add the member variable vector to hold them
				gcc.addMemberVariable("private FiniteList " + childProfileName.MemberName + ";");
				
				// Set up the constructor
				theConstructor.addToBody(childProfileName.MemberName + " = new FiniteList( " + childProfileName.ClassName + ".class, hapiSegment );");
				
				// Add the getter
				UnderlyingAccessor childAccessor = new UnderlyingAccessor(underlyingDataType, childProfileName.AccessorName);
				GeneratedRepGetter repGetter = new GeneratedRepGetter(childProfileName, childAccessor.AcceptsRep);
				docBuilder.decorateRepGetter(repGetter, seg.getField(i), childProfileName.ClassName);
				gcc.addMethod(repGetter);
				
				// If the field has no components it is a primitive, so build as such.
				if (seg.getField(i).Components > 0)
				{
					ConformanceFieldBuilder childBuilder = new ConformanceFieldBuilder(packageName + "." + profileName.PackageName, versionString, depManager);
					childBuilder.buildClass(seg.getField(i), segClassName, childProfileName.clearNameMap());
				}
				else
				{
					ConformancePrimitiveBuilder childBuilder = new ConformancePrimitiveBuilder(packageName + "." + profileName.PackageName, depManager);
					childBuilder.buildClass(seg.getField(i), segClassName, childProfileName.clearNameMap());
				}
			}
			
			// Decorate with comments
			docBuilder.decorateSegment(gcc, seg);
			
			if (depManager.Verbose)
				System.Console.Out.WriteLine("Generating Segment: " + packageName + "." + gcc.Name);
			
			depManager.generateFile(gcc, packageName, profileName.ClassName);
		}
		/// <summary> Tests a segment against a segment section of a profile.</summary>
		public virtual NuGenHL7Exception[] testSegment(Genetibase.NuGenHL7.model.Segment segment, Seg profile, System.String profileID)
		{
			System.Collections.ArrayList exList = new System.Collections.ArrayList(20);
			System.Collections.ArrayList allowedFields = new System.Collections.ArrayList(20);
			
			for (int i = 1; i <= profile.Fields; i++)
			{
				Field field = profile.getField(i);
				
				//only test a field in detail if it isn't X
				if (!field.Usage.ToUpper().Equals("X".ToUpper()))
				{
					allowedFields.Add((System.Int32) i);
					
					//see which instances have content
					try
					{                        
						Genetibase.NuGenHL7.model.Type[] instances = segment.getField(i);
						System.Collections.ArrayList instancesWithContent = new System.Collections.ArrayList(10);
						for (int j = 0; j < instances.Length; j++)
						{
							if (hasContent(instances[j]))
								instancesWithContent.Add(instances[j]);
						}
						
						NuGenHL7Exception ce = testCardinality(instancesWithContent.Count, field.Min, field.Max, field.Usage, field.Name);
						if (ce != null)
						{
							ce.FieldPosition = i;
							exList.Add(ce);
						}
						
						//test field instances with content
						for (int j = 0; j < instancesWithContent.Count; j++)
						{
							Genetibase.NuGenHL7.model.Type s = (Genetibase.NuGenHL7.model.Type) instancesWithContent[j];
							
							bool escape = true; //escape field value when checking length
							if (profile.Name.ToUpper().Equals("MSH".ToUpper()) && i < 3)
							{
								escape = false;
							}
							NuGenHL7Exception[] childExceptions = testField(s, field, escape, profileID);
							for (int k = 0; k < childExceptions.Length; k++)
							{
								childExceptions[k].FieldPosition = i;
							}
							addToList(childExceptions, exList);
						}
					}
					catch (NuGenHL7Exception)
					{
						exList.Add(new NuGenProfileNotHL7CompliantException("Field " + i + " not found in message"));
					}
				}
			}
			
			//complain about X fields with content
			this.addToList(checkForExtraFields(segment, allowedFields), exList);
			
			NuGenHL7Exception[] ret = toArray(exList);
			for (int i = 0; i < ret.Length; i++)
			{
				ret[i].SegmentName = profile.Name;
			}
			return ret;
		}