using System; using System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.BioBoost.Data { [StorableClass] [Item("Logistic", "Describes a logistic modality for a certain product.")] public class Logistic : DataItem { #region Parameters public IValueParameter ModeParameter { get { return (IValueParameter)Parameters["Mode"]; } } public IValueParameter ProductParameter { get { return (IValueParameter)Parameters["Product"]; } } public IValueParameter DistancesParameter { get { return (IValueParameter)Parameters["Distances"]; } } public IValueParameter TransportParameter { get { return (IValueParameter)Parameters["Transport"]; } } public IValueParameter HandlingParameter { get { return (IValueParameter)Parameters["Handling"]; } } public IValueParameter MinAmountParameter { get { return (IValueParameter)Parameters["MinAmount"]; } } public IValueParameter MaxAmountParameter { get { return (IValueParameter)Parameters["MaxAmount"]; } } public IValueParameter MaxDistanceParameter { get { return (IValueParameter)Parameters["MaxDistance"]; } } public IValueParameter WaterContentParameter { get { return (IValueParameter)Parameters["WaterContent"]; } } #endregion #region Parameter Values public string Mode { get { return ModeParameter.Value.Value; } } public string Product { get { return ProductParameter.Value.Value; } } public string Distances { get { return DistancesParameter.Value.Value; } } public double MinAmount { get { return MinAmountParameter.Value.Value; } } public double MaxAmount { get { return MaxAmountParameter.Value.Value; } } public double MaxDistance { get { return MaxDistanceParameter.Value.Value; } } public double WaterContent { get { return WaterContentParameter.Value.Value; } } public LogisticAction Transport { get { return TransportParameter.Value; } } public LogisticAction Handling { get { return HandlingParameter.Value; } } #endregion #region Construction & Cloning public Logistic() { Parameters.Add(new ValueParameter("Mode", "The logistic's mode.")); Parameters.Add(new ValueParameter("Product", "The logistic's transported product (from source to target).")); Parameters.Add(new ValueParameter("Distances", "The distances matrix to be used.")); Parameters.Add(new ValueParameter("MinAmount", "The minimum amount per year to be transported (if any)", new DoubleValue(0))); Parameters.Add(new ValueParameter("MaxAmount", "The maximum amount per year to be transported (if any)", new DoubleValue(Double.MaxValue))); Parameters.Add(new ValueParameter("MaxDistance", "The maximum distance this logistic should cover", new DoubleValue(Double.MaxValue))); Parameters.Add(new ValueParameter("WaterContent", "The amount of water considered in tranport/handling costs", new DoubleValue(0))); Parameters.Add(new ValueParameter("Transport", "The single or compound transport cost per tkm.")); Parameters.Add(new ValueParameter("Handling", "The single or compound handling cost per t.")); RegisterEventHandlers(); } [StorableConstructor] protected Logistic(bool isDeserializing) : base(isDeserializing) { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { if (!Parameters.ContainsKey("MinAmount")) Parameters.Add(new ValueParameter("MinAmount", "The minimum amount per year to be transported (if any)", new DoubleValue(0))); if (!Parameters.ContainsKey("MaxAmount")) Parameters.Add(new ValueParameter("MaxAmount", "The maximum amount per year to be transported (if any)", new DoubleValue(Double.MaxValue))); if (!Parameters.ContainsKey("MaxDistance")) Parameters.Add(new ValueParameter("MaxDistance", "The maximum distance this logistic should cover", new DoubleValue(Double.MaxValue))); if (!Parameters.ContainsKey("WaterContent")) Parameters.Add(new ValueParameter("WaterContent", "The amount of water considered in tranport/handling costs", new DoubleValue(0))); RegisterEventHandlers(); } protected Logistic(Logistic orig, Cloner cloner) : base(orig, cloner) { RegisterEventHandlers(); } public override IDeepCloneable Clone(Cloner cloner) { return new Logistic(this, cloner); } #endregion public override void CollectParameterValues(System.Collections.Generic.IDictionary values) { var tmpDict = new Dictionary(); base.CollectParameterValues(tmpDict); foreach (var entry in tmpDict) { if (entry.Key == "Mode") continue; if (entry.Key == "Product") continue; values.Add("Logistics " + Product + "." + Mode + "." + entry.Key, entry.Value); } } private void RegisterEventHandlers() { // when the HL value is changed ModeParameter.ValueChanged += (sender, args) => { UpdateName(); ModeParameter.Value.ValueChanged += (s, a) => { UpdateName(); }; }; ProductParameter.ValueChanged += (sender, args) => { UpdateName(); ProductParameter.Value.ValueChanged += (s, a) => { UpdateName(); }; }; // when the HL value's value is changed ModeParameter.Value.ValueChanged += (sender, args) => { UpdateName(); }; ProductParameter.Value.ValueChanged += (sender, args) => { UpdateName(); }; } private void UpdateName() { this.Name = ItemName + ": " + Product + " (" + Mode + ")"; } public override bool IsEquivalentTo(DataItem other) { var l = other as Logistic; if (l == null) return false; return l.Mode == Mode && l.Product == Product; } } }