[8385] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[8642] | 30 | namespace HeuristicLab.Optimization.Operators {
|
---|
[8385] | 31 | /// <summary>
|
---|
| 32 | /// An operator that evaluates an expression.
|
---|
| 33 | /// </summary>
|
---|
[8642] | 34 | [Item("ExpressionCalculator", "An operator that evaluates an expression.")]
|
---|
[8385] | 35 | [StorableClass]
|
---|
[8642] | 36 | public class ExpressionCalculator : ValuesCollector {
|
---|
[8385] | 37 | [Storable]
|
---|
[8642] | 38 | public Calculator Calculator { get; set; }
|
---|
[8385] | 39 |
|
---|
| 40 | #region Parameter Properties
|
---|
| 41 | public IValueLookupParameter<StringValue> ExpressionParameter {
|
---|
| 42 | get { return (IValueLookupParameter<StringValue>)Parameters["Expression"]; }
|
---|
| 43 | }
|
---|
| 44 | public IValueLookupParameter<IItem> ExpressionResultParameter {
|
---|
| 45 | get { return (IValueLookupParameter<IItem>)Parameters["ExpressionResult"]; }
|
---|
| 46 | }
|
---|
| 47 | #endregion
|
---|
| 48 |
|
---|
| 49 | #region Properties
|
---|
| 50 | private string Formula {
|
---|
| 51 | get { return ExpressionParameter.Value.Value; }
|
---|
| 52 | }
|
---|
| 53 | private IItem ExpressionResult {
|
---|
| 54 | set { ExpressionResultParameter.ActualValue = value; }
|
---|
| 55 | }
|
---|
| 56 | #endregion
|
---|
| 57 |
|
---|
| 58 | [StorableConstructor]
|
---|
[8642] | 59 | protected ExpressionCalculator(bool deserializing) : base(deserializing) { }
|
---|
| 60 | protected ExpressionCalculator(ExpressionCalculator original, Cloner cloner)
|
---|
[8385] | 61 | : base(original, cloner) {
|
---|
[8642] | 62 | this.Calculator = cloner.Clone(original.Calculator);
|
---|
[8385] | 63 | }
|
---|
[8642] | 64 | public ExpressionCalculator()
|
---|
[8385] | 65 | : base() {
|
---|
| 66 | Parameters.Add(new ValueLookupParameter<IItem>("ExpressionResult", "The result of the evaluated expression."));
|
---|
| 67 | Parameters.Add(new ValueLookupParameter<StringValue>("Expression",
|
---|
| 68 | @"RPN formula for new value in postfix notation.
|
---|
| 69 |
|
---|
| 70 | This can contain the following elements:
|
---|
| 71 |
|
---|
| 72 | literals:
|
---|
| 73 | numbers, true, false, null and strings in single quotes
|
---|
| 74 | variables (run parameters or results):
|
---|
| 75 | unquoted or in double quotes if they contain special characters or whitespace
|
---|
| 76 | mathematical functions:
|
---|
[8642] | 77 | +, -, *, /, ^ (power), log
|
---|
[8385] | 78 | predicates:
|
---|
| 79 | ==, <, >, isnull, not
|
---|
| 80 | stack manipulation:
|
---|
| 81 | drop swap dup
|
---|
| 82 | string matching:
|
---|
| 83 | <string> <pattern> ismatch
|
---|
| 84 | string replacing:
|
---|
| 85 | <string> <pattern> <replacement> rename
|
---|
| 86 | conditionals:
|
---|
| 87 | <then> <else> <condition> if
|
---|
| 88 |
|
---|
| 89 | If the final value is null, the result variable is removed if it exists."));
|
---|
| 90 |
|
---|
[8642] | 91 | Calculator = new Calculator();
|
---|
[8385] | 92 | }
|
---|
| 93 |
|
---|
| 94 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8642] | 95 | return new ExpressionCalculator(this, cloner);
|
---|
[8385] | 96 | }
|
---|
| 97 |
|
---|
| 98 | public override IOperation Apply() {
|
---|
[8642] | 99 | Calculator.Formula = Formula;
|
---|
[8385] | 100 | var variables = new Dictionary<string, IItem>();
|
---|
| 101 | foreach (var collectedValue in CollectedValues)
|
---|
| 102 | variables[collectedValue.Name] = collectedValue.ActualValue;
|
---|
[8642] | 103 | ExpressionResult = Calculator.GetValue(variables);
|
---|
[8385] | 104 | return base.Apply();
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | } |
---|