Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/CedmaImporter/SymbolicExpressionImporter.cs @ 2261

Last change on this file since 2261 was 2261, checked in by gkronber, 15 years ago

Refactoring: extracted classes. #719

File size: 4.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6using HeuristicLab.Modeling.Database;
7using HeuristicLab.Modeling.Database.SQLServerCompact;
8using HeuristicLab.GP;
9using HeuristicLab.GP.Interfaces;
10using HeuristicLab.GP.StructureIdentification;
11using System.Diagnostics;
12
13namespace CedmaImporter {
14  public class SymbolicExpressionImporter {
15
16    private Dictionary<string, IFunction> knownFunctions = new Dictionary<string, IFunction>()
17      {
18        {"+", new Addition()},
19        {"and", new And()},
20        {"mean", new Average()},
21        {"cos", new Cosinus()},
22        {"/", new Division()},
23        {"equ", new Equal()},
24        {"exp", new Exponential()},
25        {">", new GreaterThan()},
26        {"if", new IfThenElse()},
27        {"<", new LessThan()},
28        {"log", new Logarithm()},
29        {"*", new Multiplication()},
30        {"not", new Not()},
31        {"or", new Or()},
32        {"expt", new Power()},
33        {"sign", new Signum()},
34        {"sin",new Sinus()},
35        {"sqrt", new Sqrt()},
36        {"-", new Subtraction()},
37        {"tan", new Tangens()},
38        {"xor", new Xor()}
39      };
40    Constant constant = new Constant();
41    HeuristicLab.GP.StructureIdentification.Variable variable = new HeuristicLab.GP.StructureIdentification.Variable();
42    Differential differential = new Differential();
43
44    public SymbolicExpressionImporter() {
45    }
46
47    private IEnumerable<Token> GetTokenStream(StreamReader reader) {
48      return from line in GetLineStream(reader)
49             let strTokens = line.Split(new string[] { " ", "\t", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable()
50             from strToken in strTokens
51             let t = Token.Parse(strToken)
52             where t != null
53             select t;
54    }
55
56    private IEnumerable<string> GetLineStream(StreamReader reader) {
57      while (!reader.EndOfStream) yield return reader.ReadLine().Replace("(", " ( ").Replace(")", " ) ");
58      yield break;
59    }
60
61    private HeuristicLab.GP.Interfaces.IFunctionTree ParseSexp(Queue<Token> tokens) {
62      Expect(Token.LPAR, tokens);
63
64      if (tokens.Peek().Symbol == TokenSymbol.SYMB) {
65        if (tokens.Peek().StringValue.Equals("variable")) {
66          return ParseVariable(tokens);
67        } else if (tokens.Peek().StringValue.Equals("differential")) {
68          return ParseDifferential(tokens);
69        } else {
70          Token curToken = tokens.Dequeue();
71          IFunctionTree tree = CreateTree(curToken);
72          while (!tokens.Peek().Equals(Token.RPAR)) {
73            tree.AddSubTree(ParseSexp(tokens));
74          }
75          Expect(Token.RPAR, tokens);
76          return tree;
77        }
78      } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
79        ConstantFunctionTree t = (ConstantFunctionTree)constant.GetTreeNode();
80        t.Value = tokens.Dequeue().DoubleValue;
81        return t;
82      } else {
83        throw new FormatException("Expected function or constant symbol");
84      }
85    }
86
87    private IFunctionTree ParseDifferential(Queue<Token> tokens) {
88      Debug.Assert(tokens.Dequeue().StringValue == "differential");
89      VariableFunctionTree t = (VariableFunctionTree)differential.GetTreeNode();
90      t.Weight = tokens.Dequeue().DoubleValue;
91      t.VariableName = tokens.Dequeue().StringValue;
92      t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
93      Expect(Token.RPAR, tokens);
94      return t;
95    }
96
97    private IFunctionTree ParseVariable(Queue<Token> tokens) {
98      Debug.Assert(tokens.Dequeue().StringValue == "variable");
99      VariableFunctionTree t = (VariableFunctionTree)variable.GetTreeNode();
100      t.Weight = tokens.Dequeue().DoubleValue;
101      t.VariableName = tokens.Dequeue().StringValue;
102      t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
103      Expect(Token.RPAR, tokens);
104      return t;
105    }
106
107    private IFunctionTree CreateTree(Token token) {
108      if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
109      return knownFunctions[token.StringValue].GetTreeNode();
110    }
111
112    private void Expect(Token token, Queue<Token> tokens) {
113      Token cur = tokens.Dequeue();
114      if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got found: " + cur.StringValue);
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.