Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed bug in s-expression importer. #719

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