Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactoring: removed duplicate methods. #719

File size: 4.5 KB
RevLine 
[2261]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
[2263]47    internal IFunctionTree Import(StreamReader streamReader) {
48      return ParseSexp(new Queue<Token>(GetTokenStream(streamReader)));
49    }
50
[2261]51    private IEnumerable<Token> GetTokenStream(StreamReader reader) {
52      return from line in GetLineStream(reader)
53             let strTokens = line.Split(new string[] { " ", "\t", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable()
54             from strToken in strTokens
55             let t = Token.Parse(strToken)
56             where t != null
57             select t;
58    }
59
60    private IEnumerable<string> GetLineStream(StreamReader reader) {
61      while (!reader.EndOfStream) yield return reader.ReadLine().Replace("(", " ( ").Replace(")", " ) ");
62      yield break;
63    }
64
65    private HeuristicLab.GP.Interfaces.IFunctionTree ParseSexp(Queue<Token> tokens) {
66      Expect(Token.LPAR, tokens);
67
68      if (tokens.Peek().Symbol == TokenSymbol.SYMB) {
69        if (tokens.Peek().StringValue.Equals("variable")) {
70          return ParseVariable(tokens);
71        } else if (tokens.Peek().StringValue.Equals("differential")) {
72          return ParseDifferential(tokens);
73        } else {
74          Token curToken = tokens.Dequeue();
75          IFunctionTree tree = CreateTree(curToken);
76          while (!tokens.Peek().Equals(Token.RPAR)) {
77            tree.AddSubTree(ParseSexp(tokens));
78          }
79          Expect(Token.RPAR, tokens);
80          return tree;
81        }
82      } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
83        ConstantFunctionTree t = (ConstantFunctionTree)constant.GetTreeNode();
84        t.Value = tokens.Dequeue().DoubleValue;
85        return t;
86      } else {
87        throw new FormatException("Expected function or constant symbol");
88      }
89    }
90
91    private IFunctionTree ParseDifferential(Queue<Token> tokens) {
92      Debug.Assert(tokens.Dequeue().StringValue == "differential");
93      VariableFunctionTree t = (VariableFunctionTree)differential.GetTreeNode();
94      t.Weight = tokens.Dequeue().DoubleValue;
95      t.VariableName = tokens.Dequeue().StringValue;
96      t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
97      Expect(Token.RPAR, tokens);
98      return t;
99    }
100
101    private IFunctionTree ParseVariable(Queue<Token> tokens) {
102      Debug.Assert(tokens.Dequeue().StringValue == "variable");
103      VariableFunctionTree t = (VariableFunctionTree)variable.GetTreeNode();
104      t.Weight = tokens.Dequeue().DoubleValue;
105      t.VariableName = tokens.Dequeue().StringValue;
106      t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
107      Expect(Token.RPAR, tokens);
108      return t;
109    }
110
111    private IFunctionTree CreateTree(Token token) {
112      if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
113      return knownFunctions[token.StringValue].GetTreeNode();
114    }
115
116    private void Expect(Token token, Queue<Token> tokens) {
117      Token cur = tokens.Dequeue();
118      if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got found: " + cur.StringValue);
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.