private void parseExpression(IObjectiveFunction function, Expression<Func<bool>> constraint)
{
ConstraintType type;
switch (constraint.Body.NodeType)
{
case ExpressionType.Equal:
type = ConstraintType.EqualTo; break;
case ExpressionType.LessThanOrEqual:
type = ConstraintType.LesserThanOrEqualTo; break;
case ExpressionType.GreaterThanOrEqual:
type = ConstraintType.GreaterThanOrEqualTo; break;
default:
throw new FormatException("Unexpected expression.");
}
BinaryExpression b = constraint.Body as BinaryExpression;
var terms = new Dictionary<string, double>();
double value = 0;
parse(terms, b.Left, ref value);
ConstantExpression c = b.Right as ConstantExpression;
value = (double)c.Value - value;
List<int> indices = new List<int>();
List<double> scalars = new List<double>();
foreach (var term in terms)
{
indices.Add(function.Variables[term.Key]);
scalars.Add(term.Value);
}
NumberOfVariables = indices.Count;
VariablesAtIndices = indices.ToArray();
CombinedAs = scalars.ToArray();
ShouldBe = type;
Value = value;
}