[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Xml;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Data {
|
---|
[737] | 29 | /// <summary>
|
---|
| 30 | /// A class representing all basic data types having some constraints.
|
---|
| 31 | /// </summary>
|
---|
[2] | 32 | public class ConstrainedObjectData : ConstrainedItemBase, IObjectData {
|
---|
| 33 | private object myData;
|
---|
[737] | 34 | /// <summary>
|
---|
| 35 | /// Gets or sets the object with constraints to represent.
|
---|
| 36 | /// </summary>
|
---|
| 37 | /// <remarks>Calls <see cref="HeuristicLab.Core.ItemBase.OnChanged"/> in the setter.
|
---|
| 38 | /// </remarks>
|
---|
[2] | 39 | public virtual object Data {
|
---|
| 40 | get { return myData; }
|
---|
| 41 | set {
|
---|
| 42 | if (myData != value) {
|
---|
| 43 | myData = value;
|
---|
| 44 | OnChanged();
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[737] | 49 | /// <summary>
|
---|
| 50 | /// Assigns the new <paramref name="data"/> if it is valid according to the constraints.
|
---|
| 51 | /// </summary>
|
---|
| 52 | /// <param name="data">The data to assign.</param>
|
---|
| 53 | /// <returns><c>true</c> if the new <paramref name="data"/> could be assigned, <c>false</c> otherwise.</returns>
|
---|
[2] | 54 | public virtual bool TrySetData(object data) {
|
---|
| 55 | if (myData != data) {
|
---|
| 56 | object backup = myData;
|
---|
| 57 | myData = data;
|
---|
| 58 | if (IsValid()) {
|
---|
| 59 | OnChanged();
|
---|
| 60 | return true;
|
---|
| 61 | } else {
|
---|
| 62 | myData = backup;
|
---|
| 63 | return false;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | return true;
|
---|
| 67 | }
|
---|
[737] | 68 | /// <summary>
|
---|
| 69 | /// Assigns the new object <paramref name="data"/> if it is valid according to the constraints.
|
---|
| 70 | /// </summary>
|
---|
| 71 | /// <param name="data">The data to assign.</param>
|
---|
| 72 | /// <param name="violatedConstraints">Output parameter, containing all constraints that could not be fulfilled.</param>
|
---|
| 73 | /// <returns><c>true</c> if the new <paramref name="data"/> could be assigned, <c>false</c> otherwise.</returns>
|
---|
[2] | 74 | public virtual bool TrySetData(object data, out ICollection<IConstraint> violatedConstraints) {
|
---|
| 75 | if (myData != data) {
|
---|
| 76 | object backup = myData;
|
---|
| 77 | myData = data;
|
---|
| 78 | if (IsValid(out violatedConstraints)) {
|
---|
| 79 | OnChanged();
|
---|
| 80 | return true;
|
---|
| 81 | } else {
|
---|
| 82 | myData = backup;
|
---|
| 83 | return false;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | violatedConstraints = new List<IConstraint>();
|
---|
| 87 | return true;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[737] | 90 | /// <summary>
|
---|
| 91 | /// Clones the current object.
|
---|
| 92 | /// </summary>
|
---|
| 93 | /// <remarks>HeuristicLab data items are cloned with the <see cref="HeuristicLab.Core.Auxiliary.Clone"/> method of
|
---|
| 94 | /// class <see cref="Auxiliary"/> (deep copy), all other items (like basic data types)
|
---|
| 95 | /// are cloned with their own <c>Clone</c> methods (shadow copy).</remarks>
|
---|
| 96 | /// <param name="clonedObjects">All already cloned objects.</param>
|
---|
| 97 | /// <returns>The cloned object as <see cref="ConstrainedObjectData"/>.</returns>
|
---|
[2] | 98 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 99 | ConstrainedObjectData clone = (ConstrainedObjectData)base.Clone(clonedObjects);
|
---|
| 100 | if (Data is IStorable)
|
---|
| 101 | clone.myData = Auxiliary.Clone((IStorable)Data, clonedObjects);
|
---|
| 102 | else if (Data is ICloneable)
|
---|
| 103 | clone.myData = ((ICloneable)Data).Clone();
|
---|
| 104 | else
|
---|
| 105 | throw new InvalidOperationException("contained object is not cloneable");
|
---|
| 106 | return clone;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[737] | 109 | /// <summary>
|
---|
| 110 | /// Compares the current instance to the given <paramref name="obj"/>.
|
---|
| 111 | /// </summary>
|
---|
| 112 | /// <remarks>Can also compare basic data types with their representing HeuristicLab data types
|
---|
| 113 | /// (e.g. a <see cref="ConstrainedDoubleData"/> with a double value).</remarks>
|
---|
| 114 | /// <exception cref="InvalidOperationException">Thrown when the current
|
---|
| 115 | /// instance does not implement the interface <see cref="IComparable"/></exception>
|
---|
| 116 | /// <param name="obj">The object to compare the current instance to.</param>
|
---|
| 117 | /// <returns><c>0</c> if the objects are the same, a value smaller than zero when the current instance
|
---|
| 118 | /// is smaller than the given <paramref name="obj"/> and a value greater than zero
|
---|
| 119 | /// when the current instance is greater than the given <paramref name="obj"/>.</returns>
|
---|
[2] | 120 | public int CompareTo(object obj) {
|
---|
| 121 | IComparable comparable = Data as IComparable;
|
---|
| 122 | if (comparable != null) {
|
---|
| 123 | IObjectData other = obj as IObjectData;
|
---|
| 124 | if (other != null)
|
---|
| 125 | return comparable.CompareTo(other.Data);
|
---|
| 126 | else
|
---|
| 127 | return comparable.CompareTo(obj);
|
---|
| 128 | }
|
---|
| 129 | throw new InvalidOperationException("Cannot compare as contained object doesn't implement IComparable");
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[737] | 132 | /// <summary>
|
---|
| 133 | /// The string representation of the current instance.
|
---|
| 134 | /// </summary>
|
---|
| 135 | /// <returns>The current instance as a string.</returns>
|
---|
[2] | 136 | public override string ToString() {
|
---|
| 137 | return Data.ToString();
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|