#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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.Collections.Generic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Optimization.Operators {
///
/// An operator that evaluates an expression.
///
[Item("ExpressionCalculator", "An operator that evaluates an expression.")]
[StorableType("35926194-81F8-41E1-8B60-4D7C9887342E")]
public class ExpressionCalculator : ValuesCollector {
[Storable]
public Calculator Calculator { get; set; }
#region Parameter Properties
public IValueLookupParameter ExpressionParameter {
get { return (IValueLookupParameter)Parameters["Expression"]; }
}
public IValueLookupParameter ExpressionResultParameter {
get { return (IValueLookupParameter)Parameters["ExpressionResult"]; }
}
#endregion
#region Properties
private string Formula {
get { return ExpressionParameter.Value.Value; }
}
private IItem ExpressionResult {
set { ExpressionResultParameter.ActualValue = value; }
}
#endregion
[StorableConstructor]
protected ExpressionCalculator(bool deserializing) : base(deserializing) { }
protected ExpressionCalculator(ExpressionCalculator original, Cloner cloner)
: base(original, cloner) {
this.Calculator = cloner.Clone(original.Calculator);
}
public ExpressionCalculator()
: base() {
Parameters.Add(new ValueLookupParameter("ExpressionResult", "The result of the evaluated expression."));
Parameters.Add(new ValueLookupParameter("Expression",
@"RPN formula for new value in postfix notation.
This can contain the following elements:
literals:
numbers, true, false, null and strings in single quotes
variables (run parameters or results):
unquoted or in double quotes if they contain special characters or whitespace
mathematical functions (resulting in double values):
+, -, *, /, ^ (power), log
predicates:
==, <, >, isnull, not
conversions:
toint, todouble
array indexing:
[]
stack manipulation:
drop swap dup
string matching:
ismatch
string replacing:
rename
conditionals:
if
If the final value is null, the result variable is removed if it exists."));
Calculator = new Calculator();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new ExpressionCalculator(this, cloner);
}
public override IOperation Apply() {
Calculator.Formula = Formula;
var variables = new Dictionary();
foreach (var collectedValue in CollectedValues)
variables[collectedValue.Name] = collectedValue.ActualValue;
ExpressionResult = Calculator.GetValue(variables);
return base.Apply();
}
}
}