Free cookie consent management tool by TermsFeed Policy Generator

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

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

fixed #146 (serialization of function-trees in linear form)

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