Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Functions/SymbolTable.cs @ 274

Last change on this file since 274 was 266, checked in by gkronber, 16 years ago

added missing GPL headers

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28
29namespace HeuristicLab.Functions {
30  class EvaluatorSymbolTable : StorableBase{
31    public const int ADDITION = 10010;
32    public const int AND = 10020;
33    public const int AVERAGE = 10030;
34    public const int CONSTANT = 10040;
35    public const int COSINUS = 10050;
36    public const int DIVISION = 10060;
37    public const int EQU = 10070;
38    public const int EXP = 10080;
39    public const int GT = 10090;
40    public const int IFTE = 10100;
41    public const int LT = 10110;
42    public const int LOG = 10120;
43    public const int MULTIPLICATION = 10130;
44    public const int NOT = 10140;
45    public const int OR = 10150;
46    public const int POWER = 10160;
47    public const int SIGNUM = 10170;
48    public const int SINUS = 10180;
49    public const int SQRT = 10190;
50    public const int SUBSTRACTION = 10200;
51    public const int TANGENS = 10210;
52    public const int VARIABLE = 10220;
53    public const int XOR = 10230;
54
55    private int nextFunctionSymbol = 10240;
56    private Dictionary<int, IFunction> table;
57    private Dictionary<IFunction, int> reverseTable;
58    private Dictionary<Type, int> staticTypes;
59
60    private static EvaluatorSymbolTable symbolTable = new EvaluatorSymbolTable();
61    public static EvaluatorSymbolTable SymbolTable {
62      get { return EvaluatorSymbolTable.symbolTable; }
63    }
64
65    // needs to be public for persistence mechanism (Activator.CreateInstance needs empty constructor)
66    public EvaluatorSymbolTable () {
67      table = new Dictionary<int, IFunction>();
68      reverseTable = new Dictionary<IFunction, int>();
69      staticTypes = new Dictionary<Type, int>();
70      staticTypes[typeof(Addition)] = ADDITION;
71      staticTypes[typeof(And)] = AND;
72      staticTypes[typeof(Average)] = AVERAGE;
73      staticTypes[typeof(Constant)] = CONSTANT;
74      staticTypes[typeof(Cosinus)] = COSINUS;
75      staticTypes[typeof(Division)] = DIVISION;
76      staticTypes[typeof(Equal)] = EQU;
77      staticTypes[typeof(Exponential)] = EXP;
78      staticTypes[typeof(GreaterThan)] = GT;
79      staticTypes[typeof(IfThenElse)] = IFTE;
80      staticTypes[typeof(LessThan)] = LT;
81      staticTypes[typeof(Logarithm)] = LOG;
82      staticTypes[typeof(Multiplication)] = MULTIPLICATION;
83      staticTypes[typeof(Not)] = NOT;
84      staticTypes[typeof(Or)] = OR;
85      staticTypes[typeof(Power)] = POWER;
86      staticTypes[typeof(Signum)] = SIGNUM;
87      staticTypes[typeof(Sinus)] = SINUS;
88      staticTypes[typeof(Sqrt)] = SQRT;
89      staticTypes[typeof(Substraction)] = SUBSTRACTION;
90      staticTypes[typeof(Tangens)] = TANGENS;
91      staticTypes[typeof(Variable)] = VARIABLE;
92      staticTypes[typeof(Xor)] = XOR;
93    }
94
95    internal int MapFunction(IFunction function) {
96      if(!reverseTable.ContainsKey(function)) {
97        int curFunctionSymbol;
98        if(staticTypes.ContainsKey(function.GetType())) curFunctionSymbol = staticTypes[function.GetType()];
99        else {
100          curFunctionSymbol = nextFunctionSymbol;
101          nextFunctionSymbol++;
102        }
103        reverseTable[function] = curFunctionSymbol;
104        table[curFunctionSymbol] = function;
105      }
106      return reverseTable[function];
107    }
108
109    internal IFunction MapSymbol(int symbol) {
110      return table[symbol];
111    }
112
113    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
114      XmlNode node =  base.GetXmlNode(name, document, persistedObjects);
115      XmlAttribute nextFunctionSymbolAttribute = document.CreateAttribute("NextFunctionSymbol");
116      nextFunctionSymbolAttribute.Value = nextFunctionSymbol.ToString();
117      node.Attributes.Append(nextFunctionSymbolAttribute);
118      XmlNode symbolTableNode = document.CreateNode(XmlNodeType.Element, "Table", null);
119      foreach(KeyValuePair<int, IFunction> entry in table) {
120        XmlNode entryNode = PersistenceManager.Persist("Entry", entry.Value, document, persistedObjects);
121        XmlAttribute symbolAttr = document.CreateAttribute("Symbol");
122        symbolAttr.Value = entry.Key.ToString();
123        entryNode.Attributes.Append(symbolAttr);
124        symbolTableNode.AppendChild(entryNode);
125      }
126      node.AppendChild(symbolTableNode);
127      return node;
128    }
129
130    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
131      base.Populate(node, restoredObjects);
132      table.Clear();
133      reverseTable.Clear();
134      nextFunctionSymbol = int.Parse(node.Attributes["NextFunctionSymbol"].Value);
135      XmlNode symbolTableNode = node.SelectSingleNode("Table");
136      foreach(XmlNode entry in symbolTableNode.ChildNodes) {
137        IFunction function = (IFunction)PersistenceManager.Restore(entry, restoredObjects);
138        int symbol = int.Parse(entry.Attributes["Symbol"].Value);
139        table[symbol] = function;
140        reverseTable[function] = symbol;
141      }
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.