[2261] | 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 {
|
---|
[2287] | 15 | private const string DIFFSTART = "dif";
|
---|
| 16 | private const string VARSTART = "var";
|
---|
[2261] | 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 |
|
---|
[2263] | 48 | internal IFunctionTree Import(StreamReader streamReader) {
|
---|
| 49 | return ParseSexp(new Queue<Token>(GetTokenStream(streamReader)));
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[2261] | 52 | private IEnumerable<Token> GetTokenStream(StreamReader reader) {
|
---|
| 53 | return from line in GetLineStream(reader)
|
---|
[2287] | 54 | let strTokens = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable()
|
---|
[2261] | 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) {
|
---|
[2287] | 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);
|
---|
[2261] | 74 | } else {
|
---|
| 75 | Token curToken = tokens.Dequeue();
|
---|
[2287] | 76 | tree = CreateTree(curToken);
|
---|
[2261] | 77 | while (!tokens.Peek().Equals(Token.RPAR)) {
|
---|
| 78 | tree.AddSubTree(ParseSexp(tokens));
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
[2287] | 81 | Expect(Token.RPAR, tokens);
|
---|
| 82 | return tree;
|
---|
[2261] | 83 | } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
|
---|
| 84 | ConstantFunctionTree t = (ConstantFunctionTree)constant.GetTreeNode();
|
---|
| 85 | t.Value = tokens.Dequeue().DoubleValue;
|
---|
| 86 | return t;
|
---|
[2287] | 87 | } else throw new FormatException("Expected function or constant symbol");
|
---|
[2261] | 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 | }
|
---|