#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Core { [StorableClass] public abstract class Constraint : Item, IConstraint { protected Constraint() { this.Active = false; if (AllowedConstraintOperations != null && AllowedConstraintOperations.Count() != 0) this.ConstraintOperation = AllowedConstraintOperations.ElementAt(0); } [StorableConstructor] protected Constraint(bool deserializing) { } protected Constraint(IItem constrainedValue, ConstraintOperation constraintOperation, object constraintData) : this() { this.ConstrainedValue = constrainedValue; this.ConstraintOperation = constraintOperation; this.ConstraintData = constraintData; } protected Constraint(IItem constrainedValue, ConstraintOperation constraintOperation, object constraintData, bool active) { this.ConstrainedValue = constrainedValue; this.ConstraintOperation = constraintOperation; this.ConstraintData = constraintData; this.Active = active; } [Storable] private bool active; public bool Active { get { return this.active; } set { if (this.active != value) { this.active = value; this.OnActiveChanged(); } } } [Storable] private IItem constrainedValue; public IItem ConstrainedValue { get { return this.constrainedValue; } set { if (value == null) throw new ArgumentNullException("Constraint value cannot be null."); if (this.constrainedValue != value) { this.constrainedValue = value; this.OnConstrainedValueChanged(); this.OnToStringChanged(); } } } [Storable] private object constraintData; public object ConstraintData { get { return this.constraintData; } set { if (this.constraintData != value) { this.constraintData = value; this.OnConstraintDataChanged(); this.OnToStringChanged(); } } } public abstract IEnumerable AllowedConstraintOperations { get; } [Storable] private ConstraintOperation constraintOperation; public ConstraintOperation ConstraintOperation { get { return this.constraintOperation; } set { if (value == null) throw new ArgumentNullException("Comparison operation cannot be null."); if (!AllowedConstraintOperations.Contains(value)) throw new ArgumentException("Comparison operation is not contained in the allowed ComparisonOperations."); if (this.constraintOperation != value) { this.constraintOperation = value; this.OnConstraintOperationChanged(); this.OnToStringChanged(); } } } /// /// This method is called to determine which member of the constrained value should be compared. /// /// protected virtual IItem GetConstrainedMember() { return this.constrainedValue; } public bool Check() { if (!Active) return true; IItem constrainedMember = this.GetConstrainedMember(); return this.Check(constrainedMember); } protected abstract bool Check(object constrainedMember); public bool Check(out string errorMessage) { errorMessage = string.Empty; if (!Active) return true; IItem constrainedMember = this.GetConstrainedMember(); return this.Check(constrainedMember, out errorMessage); } protected abstract bool Check(object constrainedMember, out string errorMessage); #region events public event EventHandler ActiveChanged; protected virtual void OnActiveChanged() { EventHandler handler = ActiveChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler ConstrainedValueChanged; protected virtual void OnConstrainedValueChanged() { EventHandler handler = ConstrainedValueChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler ConstraintDataChanged; protected virtual void OnConstraintDataChanged() { EventHandler handler = ConstraintDataChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler ConstraintOperationChanged; protected virtual void OnConstraintOperationChanged() { EventHandler handler = ConstraintOperationChanged; if (handler != null) handler(this, EventArgs.Empty); } #endregion #region overriden item methods public override string ToString() { IItem constrainedMember = GetConstrainedMember(); string s = string.Empty; if (constrainedMember != null) s += constrainedMember.ToString() + " "; if (constraintOperation != null) s += ConstraintOperation.ToString() + " "; if (constraintData != null) s += constraintData.ToString(); else s += "null"; s += "."; return s; } public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) { Constraint clone = (Constraint)base.Clone(cloner); clone.constrainedValue = null; //mkommend: intentional set to null and must be reset in the clone IItem constraintDataItem = this.constraintData as IItem; ICloneable constraintDataCloneable = this.constraintData as ICloneable; if (constraintDataItem != null) clone.constraintData = cloner.Clone(constraintDataItem); else if (constraintDataCloneable != null) clone.constraintData = constraintDataCloneable.Clone(); else clone.constraintData = constraintData; clone.constraintOperation = this.constraintOperation; return clone; } #endregion } }