/// <summary> /// Gives a string representation of the mass function using the given reference list. /// </summary> /// <param name="refList">The reference list to gives sense to the elements.</param> /// <returns>Returns a string representation of the mass function.</returns> /// <exception cref="IncompatibleReferenceListException">Thrown if the given reference /// list surely does not correspond to this discrete mass function.</exception> public string ToString <T>(ReferenceList <T> refList) { if (NbPossibleWorlds == -1) { return("Mass function: void"); } if (refList.Count != NbPossibleWorlds) { throw new IncompatibleReferenceListException("The given ReferenceList is not adapted to this MassFunction!"); } StringBuilder toReturn = new StringBuilder("Mass function:\n"); int i = 0; foreach (FocalElement <DiscreteElement> e in Focals) { i++; if (i == NbFocals) { toReturn.Append(String.Format("m({0}) = {1}", e.Element.ToString(refList), e.Value)); } else { toReturn.Append(String.Format("m({0}) = {1}\n", e.Element.ToString(refList), e.Value)); } } return(toReturn.ToString()); }
//------------------------------------------------------------------------------------------------ /// <summary> /// Builds a discrete element from a reference list and an array of objects corresponding to /// the states/worlds to include. /// </summary> /// <param name="refList">The reference list.</param> /// <param name="worlds">The states/worlds of the frame of discernment to include.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown is the reference list is too short to /// create a valid element</exception> /// <exception cref="IncompatibleReferenceListException">Thrown if one of the given states/worlds /// could not be found in the reference list.</exception> public static DiscreteElement ConstructDiscreteElement <T>(ReferenceList <T> refList, params T[] worlds) { //Checking arguments: if (refList.Count <= 1) { throw new ArgumentOutOfRangeException("refList.Length. The size of an DiscreteElement cannot be null, negative or too small!"); } foreach (T world in worlds) { if (!refList.Contains(world)) { throw new IncompatibleReferenceListException(String.Format("The given ReferenceList does not contain the world \"{0}\"!", world.ToString())); } } //Constructing: int size = refList.Count; int card = worlds.Length; uint[] numbers = new uint[size / NB_BITS_UINT + 1]; foreach (T w in worlds) { int index = refList.IndexOf(w); numbers[index / NB_BITS_UINT] += 1U << (index % NB_BITS_UINT); } DiscreteElement newlyBuilt = new DiscreteElement(size, numbers); newlyBuilt._card = card; return(newlyBuilt); }
//------------------------------------------------------------------------------------------------ /// <summary> /// Gets a string representation of the current discrete element given a reference list with an element per line. /// </summary> /// <param name="refList">The reference list to give sense to the states/worlds.</param> /// <returns>Returns a string representation of the current discrete element.</returns> /// <exception cref="IncompatibleReferenceListException">Thrown if the given reference list /// could not correspond to frame of discernment on which the discrete element has been defined.</exception> public string ToStringLines <T>(ReferenceList <T> refList) { bool[] localWorlds = this.Worlds; //Checking the arguments: if (refList.Count != localWorlds.Length) { throw new IncompatibleReferenceListException("The given ReferenceList is not adapted to this DiscreteElement!"); } //Building the string: StringBuilder toReturn = new StringBuilder(); int added = 0; for (int i = 0; i < localWorlds.Length; i++) { if (localWorlds[i]) { toReturn.Append(refList[i].ToString()); added++; if (added < Card) { toReturn.Append("\n"); } } } return(toReturn.ToString()); }
//------------------------------------------------------------------------------------------------ /// <summary> /// Tests equality between the current reference list and the given one. /// </summary> /// <param name="list">The list to compare to.</param> /// <returns>Returns true if both reference lists are equals, false otherwise.</returns> public bool Equals(ReferenceList <T> list) { foreach (T element in list) { if (!this.Contains(element)) { return(false); } } foreach (T element in this) { if (!list.Contains(element)) { return(false); } } return(true); }
//------------------------------------------------------------------------------------------------ /// <summary> /// Gives a string representation of the current set using the given reference list to represent /// the stored elements. /// </summary> /// <param name="refList">The list of reference giving sense to the elements.</param> /// <returns>Returns a string representation of the current set.</returns> public string ToString <T>(ReferenceList <T> refList) { //Checking argument: if (refList.Count != ElementSize && ElementSize != -1) { throw new IncompatibleReferenceListException("The given ReferenceList does not correspond to the given DiscreteSet!"); } //ToString: StringBuilder toReturn = new StringBuilder("{"); for (int i = 0; i < Card - 1; i++) { toReturn.Append(this[i].ToString(refList)); toReturn.Append(", "); } if (Card > 0) { toReturn.Append(Elements[Card - 1].ToString(refList)); } toReturn.Append("}"); return(toReturn.ToString()); }