1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.IO;
|
---|
6 | using HeuristicLab.Modeling.Database;
|
---|
7 | using HeuristicLab.Modeling.Database.SQLServerCompact;
|
---|
8 | using HeuristicLab.GP;
|
---|
9 | using HeuristicLab.GP.Interfaces;
|
---|
10 | using HeuristicLab.GP.StructureIdentification;
|
---|
11 | using System.Diagnostics;
|
---|
12 |
|
---|
13 | namespace 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 | internal IFunctionTree Import(StreamReader streamReader) {
|
---|
48 | return ParseSexp(new Queue<Token>(GetTokenStream(streamReader)));
|
---|
49 | }
|
---|
50 |
|
---|
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 | }
|
---|