Click here to Skip to main content
15,896,153 members
Articles / Programming Languages / C#

Fuzzy Logic Dot Net Fuzzy Collections

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
12 Oct 20045 min read 50.5K   2K   27  
Upgrading the collections in the Fuzzy Dot Net Library.
using System;
using System.Xml;
using System.IO;
using System.Security;

namespace Fuzzy_Logic_Library
{

	/// <summary>
	/// Helper class for the fuzzy decision class this clas stakes care of the maintenance of the
	/// xml file used by the decision class.
	/// </summary>
	public class FuzzyDeciderReaderWriter
	{
		private FileStream fileStream;
		private XmlTextWriter xmlWriter;
		private XmlTextReader xmlReader;

		private string strFileName;
		private string strDocumentName;

		private string strError;
		private bool bIsError;


		public string Error
		{
			get
			{
				return strError;
			}
		}

		public bool IsError
		{
			get
			{
				return bIsError;
			}
		}

		public FuzzyDeciderReaderWriter( string deciderFile )
		{
			//
			// TODO: Add constructor logic here
			//

			if( deciderFile.EndsWith( ".xml" ) == true )
				deciderFile = deciderFile.Remove( deciderFile.Length-4, 4 );

			strFileName = deciderFile + "Decider.xml";
			strDocumentName = deciderFile;

			bIsError = false;
		}


		public bool OpenFileForReading()
		{
			bIsError = false;

			try
			{
				fileStream = new FileStream( strFileName, FileMode.Open, FileAccess.Read, FileShare.Read );
				try
				{
					xmlReader = new XmlTextReader( fileStream );
				}
				catch( ArgumentNullException argNullExp )
				{
					strError = "Error Argument Null Exception thrown opening the xml reader, reason :- " + argNullExp.Message;
					bIsError = true;
				}

			}
			catch( ArgumentNullException argNullExp )
			{
				strError = "Error Argument Null Exception thrown opening file :- " + strFileName + ", reason :- " + argNullExp.Message;
				bIsError = true;
			}
			catch( ArgumentOutOfRangeException argOutORExp )
			{
				strError = "Error Argument Out Of Range Exception thrown opening the file :- " + strFileName + ", reason :- " + argOutORExp.Message;
				bIsError = true;
			}
			catch( ArgumentException argExp )
			{
				strError = "Error Argument Exception thrown opeing the file :- " + strFileName + ", reason :- " + argExp.Message;
				bIsError = true;
			}
			catch( FileNotFoundException notFExp )
			{
				strError = "Error File Not Found Exception thrown opening the file := " + strFileName + ", reason :-" + notFExp.Message;
				bIsError = true;
			}
			catch( DirectoryNotFoundException dirNFExp )
			{
				strError = "Error Directory Not Found Exception thrown opening the file :- " + strFileName + ", reason :- " + dirNFExp.Message;
				bIsError = true;
			}
			catch( PathTooLongException pathTLExp )
			{
				strError = "Error Path Too Long Exception thrown opening the file :- " + strFileName + ", reason :- " + pathTLExp.Message;
				bIsError = true;
			}
			catch( IOException ioExp )
			{
				strError = "Error IO Exception thrown opening the file :- " + strFileName + ", reason :- " + ioExp.Message;
				bIsError = true;
			}
			catch( SecurityException secExp )
			{
				strError = "Error Security Exception thrown opening the file :- " + strFileName + ", reason :- " + secExp.Message;
				bIsError = true;
			}
			catch( UnauthorizedAccessException unAExp )
			{
				strError = "Error Unauthorised Access Exception thrown opening the file :- " + strFileName + ", reason :- " + unAExp.Message;
				bIsError = true;
			}


			return bIsError;
		}

	
		public bool OpenFileForWriting( bool bTruncate )
		{
			bIsError = false;

			try
			{
				if( bTruncate == false )
				{
					try
					{
						fileStream = new FileStream( strFileName, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite );
					}
					catch( IOException ioExp )
					{
						string completegarbagetogetridofwarning = ioExp.Message;
						fileStream = new FileStream( strFileName, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite );
					}
				}
				else
				{
					fileStream = new FileStream( strFileName, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite );
				}


				try
				{
					xmlWriter = new XmlTextWriter( fileStream, System.Text.Encoding.UTF8 );
				}
				catch( ArgumentNullException argNullExp )
				{
					strError = "Error Argument Null Exception thrown opening the xml writer, reason :- " + argNullExp.Message;
					bIsError = true;
				}
				catch( ArgumentException argExp )
				{
					strError = "Error Argument Exception thrown opening the xml writer, reason :- " + argExp.Message;
					bIsError = true;
				}

				try
				{
					xmlWriter.WriteStartDocument();
		///			xmlWriter.WriteStartElement( strDocumentName );
				}
				catch( InvalidOperationException invOpExp )
				{
					strError = "Error Invalid Operation Exception thrown writing the start of the xml document, reason :- " + invOpExp.Message;
					bIsError = true;
				}

			}
			catch( ArgumentNullException argNullExp )
			{
				strError = "Error Argument Null Exception thrown opening file :- " + strFileName + ", reason :- " + argNullExp.Message;
				bIsError = true;
			}
			catch( ArgumentOutOfRangeException argOutORExp )
			{
				strError = "Error Argument Out Of Range Exception thrown opening the file :- " + strFileName + ", reason :- " + argOutORExp.Message;
				bIsError = true;
			}
			catch( ArgumentException argExp )
			{
				strError = "Error Argument Exception thrown opeing the file :- " + strFileName + ", reason :- " + argExp.Message;
				bIsError = true;
			}
			catch( FileNotFoundException notFExp )
			{
				strError = "Error File Not Found Exception thrown opening the file := " + strFileName + ", reason :-" + notFExp.Message;
				bIsError = true;
			}
			catch( DirectoryNotFoundException dirNFExp )
			{
				strError = "Error Directory Not Found Exception thrown opening the file :- " + strFileName + ", reason :- " + dirNFExp.Message;
				bIsError = true;
			}
			catch( PathTooLongException pathTLExp )
			{
				strError = "Error Path Too Long Exception thrown opening the file :- " + strFileName + ", reason :- " + pathTLExp.Message;
				bIsError = true;
			}
			catch( IOException ioExp )
			{
				strError = "Error IO Exception thrown opening the file :- " + strFileName + ", reason :- " + ioExp.Message;
				bIsError = true;
			}
			catch( SecurityException secExp )
			{
				strError = "Error Security Exception thrown opening the file :- " + strFileName + ", reason :- " + secExp.Message;
				bIsError = true;
			}
			catch( UnauthorizedAccessException unAExp )
			{
				strError = "Error Unauthorised Access Exception thrown opening the file :- " + strFileName + ", reason :- " + unAExp.Message;
				bIsError = true;
			}

			return bIsError;
		}


		public void CloseReader()
		{
			xmlReader.Close();
		}

		public void CloseWriter()
		{
			xmlWriter.Close();
		}

		/// <summary>
		/// write a section ie <NumberofTimes>value</NumberofTimes>
		/// </summary>
		/// <param name="xmlName"></param>
		/// <param name="type"></param>
		public bool WriteXmlElement( string xmlName, string xmlValue )
		{
			bIsError = false;

			try
			{
				xmlWriter.WriteElementString( xmlName, xmlValue );
			}
			catch( InvalidOperationException invOpExp )
			{
				strError = "Error Invalid Operation thrown writing the xml element " + xmlName + ", reason :- " + invOpExp.Message;
				bIsError = true;
			}

			return bIsError;
		}

		/// <summary>
		///  for opening xmlSections ie PossibleDecsions
		/// </summary>
		/// <param name="xmlName"></param>
		/// <returns></returns>
		public bool StartXmlSection( string xmlName )
		{
			bIsError = false;

			try
			{
				xmlWriter.WriteStartElement( xmlName );
			}
			catch( InvalidOperationException invOpExp )
			{
				strError = "Error Invalid Operation thrown writing the start element " + xmlName + ", reason :- " + invOpExp.Message;
				bIsError = true;
			}

			return bIsError;
		}


		public bool EndXmlSection()
		{
			bIsError = false;

			try
			{
				xmlWriter.WriteEndElement();
			}
			catch( InvalidOperationException invOpExp )
			{
				strError = "Error Invalid Operation thrown writing the end Element, reason :- " + invOpExp.Message;
				bIsError = true;
			}

			return bIsError;
		}

		public bool StartXmlDocument()
		{
			bIsError = false;

			try
			{
				xmlWriter.WriteStartDocument();
			}
			catch( InvalidOperationException invOpExp )
			{
				strError = "Error Invalid Operation Exception thrown starting the document, reason :- " + invOpExp.Message;
				bIsError = true;
			}

			return bIsError;
		}

		public bool EndXmlDocument()
		{
			bIsError = false;

			try
			{
				xmlWriter.WriteEndDocument();
			}
			catch( ArgumentException argExp )
			{
				strError = "Error Argument Exception thrown writing the end of the document, reason :- " + argExp.Message;
				bIsError = true;
			}

			return bIsError;

		}


		public bool ReadXmlElement( string xmlName, out string xmlValue )
		{
			bIsError = false;
			xmlValue = "";
			
			try
			{
				while( xmlReader.Name != xmlName )
				{
					xmlReader.Read();
				}

				xmlReader.Read();
				xmlValue = xmlReader.Value;

			}
			catch( XmlException xmlExp )
			{
				strError = "Error Xml Exception thrown trying to read " + xmlName + ", reason :- " + xmlExp.Message;
				bIsError = true;
			}

			return bIsError;
		}

		public bool ReadXmlElementFromBeginning( string xmlName, out string xmlValue )
		{
			xmlReader.Close();		
			xmlValue = "";
		
			if( OpenFileForReading() != false )
			{
				return false;
			}

			return ReadXmlElement( xmlName, out xmlValue );

		}

		public bool GetDecisionCount( out int count )
		{
			bIsError = false;
			count = 0;

			try
			{
				string strTemp;
				bool bGotThem = false;

				/// false is error free
				if( ReadXmlElementFromBeginning( "PossibleDecisions", out strTemp ) == false )
				{
					count++;
					while( xmlReader.Read() == true )
					{
						if( xmlReader.NodeType == XmlNodeType.Element )
						{
							if( bGotThem == false && xmlReader.Depth == 2 )
								count++;
						}

						if( xmlReader.NodeType == XmlNodeType.EndElement )
						{
							if( xmlReader.Name == "PossibleDecsions" )
								bGotThem = true;
						}

					}
				}

				/// reset the reader to the start of possible decisions
				if( ReadXmlElementFromBeginning( "PossibleDecisions", out strTemp ) == true )
				{
					bIsError = true;
				}

			}
			catch( XmlException xmlExp )
			{
				strError = "Error reading the xml file in GetDecisionCount, reason " + xmlExp.Message;
				bIsError = true;
			}

			return bIsError;

		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions