#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.Linq;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.ExternalEvaluation {
[Item("IntegerConverter", "Converts a ValueTypeValue, ValueTypeArray, or ValueTypeMatrix and adds it to the SolutionMessage's IntegerVars or IntegerArrayVars. A matrix is encoded as array by concatenating all rows and setting length as the length of a row.")]
[StorableClass]
public class IntegerConverter : Item, IItemToSolutionMessageConverter {
private static readonly Type[] itemTypes = new Type[] { typeof(ValueTypeValue), typeof(ValueTypeArray), typeof(ValueTypeMatrix) };
#region IItemToSolutionMessageConverter Members
public Type[] ItemTypes {
get { return itemTypes; }
}
public void AddItemToBuilder(IItem item, string name, SolutionMessage.Builder builder) {
ValueTypeValue value = (item as ValueTypeValue);
if (value != null) {
SolutionMessage.Types.IntegerVariable.Builder var = SolutionMessage.Types.IntegerVariable.CreateBuilder();
var.SetName(name).SetData(value.Value);
builder.AddIntegerVars(var.Build());
} else {
ValueTypeArray array = (item as ValueTypeArray);
if (array != null) {
SolutionMessage.Types.IntegerArrayVariable.Builder var = SolutionMessage.Types.IntegerArrayVariable.CreateBuilder();
var.SetName(name).AddRangeData(array).SetLength(array.Length);
builder.AddIntegerArrayVars(var.Build());
} else {
ValueTypeMatrix matrix = (item as ValueTypeMatrix);
if (matrix != null) {
SolutionMessage.Types.IntegerArrayVariable.Builder var = SolutionMessage.Types.IntegerArrayVariable.CreateBuilder();
var.SetName(name).AddRangeData(matrix.AsEnumerable()).SetLength(matrix.Columns);
builder.AddIntegerArrayVars(var.Build());
} else {
throw new ArgumentException(ItemName + ": Item is not of a supported type.", "item");
}
}
}
}
#endregion
}
}