[2447] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[3733] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2447] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[4068] | 24 | using System.Diagnostics;
|
---|
[2447] | 25 | using System.Linq;
|
---|
[4068] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[3733] | 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
[2447] | 29 |
|
---|
[3733] | 30 | namespace HeuristicLab.Problems.DataAnalysis.Tests {
|
---|
| 31 | internal class SymbolicExpressionImporter {
|
---|
[3746] | 32 | private const string VARSTART = "VAR";
|
---|
| 33 | private const string DEFUNSTART = "DEFUN";
|
---|
| 34 | private const string ARGSTART = "ARG";
|
---|
| 35 | private const string INVOKESTART = "CALL";
|
---|
[3733] | 36 | private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>()
|
---|
[2447] | 37 | {
|
---|
| 38 | {"+", new Addition()},
|
---|
| 39 | {"/", new Division()},
|
---|
| 40 | {"*", new Multiplication()},
|
---|
| 41 | {"-", new Subtraction()},
|
---|
[3841] | 42 | {"EXP", new Exponential()},
|
---|
| 43 | {"LOG", new Logarithm()},
|
---|
| 44 | {"SIN",new Sine()},
|
---|
| 45 | {"COS", new Cosine()},
|
---|
| 46 | {"TAN", new Tangent()},
|
---|
| 47 | {"MEAN", new Average()},
|
---|
| 48 | {"IF", new IfThenElse()},
|
---|
| 49 | {">", new GreaterThan()},
|
---|
| 50 | {"<", new LessThan()},
|
---|
| 51 | {"AND", new And()},
|
---|
| 52 | {"OR", new Or()},
|
---|
| 53 | {"NOT", new Not()},
|
---|
[3746] | 54 | {"PROG", new ProgramRootSymbol()},
|
---|
[3841] | 55 | {"MAIN", new StartSymbol()},
|
---|
[3746] | 56 | };
|
---|
[2622] | 57 |
|
---|
[2447] | 58 | Constant constant = new Constant();
|
---|
[3733] | 59 | Variable variable = new Variable();
|
---|
[3746] | 60 | Defun defun = new Defun();
|
---|
| 61 |
|
---|
[3733] | 62 | ProgramRootSymbol programRootSymbol = new ProgramRootSymbol();
|
---|
| 63 | StartSymbol startSymbol = new StartSymbol();
|
---|
| 64 |
|
---|
[2447] | 65 | public SymbolicExpressionImporter() {
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[3733] | 68 | internal SymbolicExpressionTree Import(string str) {
|
---|
[2447] | 69 | str = str.Replace("(", " ( ").Replace(")", " ) ");
|
---|
[3733] | 70 | SymbolicExpressionTreeNode root = programRootSymbol.CreateTreeNode();
|
---|
| 71 | SymbolicExpressionTreeNode start = startSymbol.CreateTreeNode();
|
---|
| 72 | SymbolicExpressionTreeNode mainBranch = ParseSexp(new Queue<Token>(GetTokenStream(str)));
|
---|
[3746] | 73 | if (mainBranch.Symbol is ProgramRootSymbol) {
|
---|
| 74 | // when a root symbol was parsed => use main branch as root
|
---|
| 75 | root = mainBranch;
|
---|
| 76 | } else {
|
---|
| 77 | // only a main branch was given => insert the main branch into the default tree template
|
---|
| 78 | root.AddSubTree(start);
|
---|
| 79 | start.AddSubTree(mainBranch);
|
---|
| 80 | }
|
---|
[3733] | 81 | return new SymbolicExpressionTree(root);
|
---|
[2447] | 82 | }
|
---|
| 83 |
|
---|
| 84 | private IEnumerable<Token> GetTokenStream(string str) {
|
---|
| 85 | return
|
---|
| 86 | from strToken in str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).AsEnumerable()
|
---|
| 87 | let t = Token.Parse(strToken)
|
---|
| 88 | where t != null
|
---|
| 89 | select t;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[3733] | 92 | private SymbolicExpressionTreeNode ParseSexp(Queue<Token> tokens) {
|
---|
[2447] | 93 | if (tokens.Peek().Symbol == TokenSymbol.LPAR) {
|
---|
[3733] | 94 | SymbolicExpressionTreeNode tree;
|
---|
[2447] | 95 | Expect(Token.LPAR, tokens);
|
---|
| 96 | if (tokens.Peek().StringValue.StartsWith(VARSTART)) {
|
---|
| 97 | tree = ParseVariable(tokens);
|
---|
[3746] | 98 | } else if (tokens.Peek().StringValue.StartsWith(DEFUNSTART)) {
|
---|
| 99 | tree = ParseDefun(tokens);
|
---|
| 100 | while (!tokens.Peek().Equals(Token.RPAR)) {
|
---|
| 101 | tree.AddSubTree(ParseSexp(tokens));
|
---|
| 102 | }
|
---|
| 103 | } else if (tokens.Peek().StringValue.StartsWith(ARGSTART)) {
|
---|
| 104 | tree = ParseArgument(tokens);
|
---|
| 105 | } else if (tokens.Peek().StringValue.StartsWith(INVOKESTART)) {
|
---|
| 106 | tree = ParseInvoke(tokens);
|
---|
| 107 | while (!tokens.Peek().Equals(Token.RPAR)) {
|
---|
| 108 | tree.AddSubTree(ParseSexp(tokens));
|
---|
| 109 | }
|
---|
[2447] | 110 | } else {
|
---|
| 111 | Token curToken = tokens.Dequeue();
|
---|
| 112 | tree = CreateTree(curToken);
|
---|
| 113 | while (!tokens.Peek().Equals(Token.RPAR)) {
|
---|
| 114 | tree.AddSubTree(ParseSexp(tokens));
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | Expect(Token.RPAR, tokens);
|
---|
| 118 | return tree;
|
---|
| 119 | } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
|
---|
[3733] | 120 | ConstantTreeNode t = (ConstantTreeNode)constant.CreateTreeNode();
|
---|
[2447] | 121 | t.Value = tokens.Dequeue().DoubleValue;
|
---|
| 122 | return t;
|
---|
| 123 | } else throw new FormatException("Expected function or constant symbol");
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[3746] | 126 | private SymbolicExpressionTreeNode ParseInvoke(Queue<Token> tokens) {
|
---|
| 127 | Token invokeTok = tokens.Dequeue();
|
---|
| 128 | Debug.Assert(invokeTok.StringValue == "CALL");
|
---|
| 129 | InvokeFunction invokeSym = new InvokeFunction(tokens.Dequeue().StringValue);
|
---|
| 130 | SymbolicExpressionTreeNode invokeNode = invokeSym.CreateTreeNode();
|
---|
| 131 | return invokeNode;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | private SymbolicExpressionTreeNode ParseArgument(Queue<Token> tokens) {
|
---|
| 135 | Token argTok = tokens.Dequeue();
|
---|
| 136 | Debug.Assert(argTok.StringValue == "ARG");
|
---|
| 137 | Argument argument = new Argument((int)tokens.Dequeue().DoubleValue);
|
---|
| 138 | SymbolicExpressionTreeNode argNode = argument.CreateTreeNode();
|
---|
| 139 | return argNode;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | private SymbolicExpressionTreeNode ParseDefun(Queue<Token> tokens) {
|
---|
| 143 | Token defTok = tokens.Dequeue();
|
---|
| 144 | Debug.Assert(defTok.StringValue == "DEFUN");
|
---|
| 145 | DefunTreeNode t = (DefunTreeNode)defun.CreateTreeNode();
|
---|
| 146 | t.FunctionName = tokens.Dequeue().StringValue;
|
---|
| 147 | return t;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[3733] | 150 | private SymbolicExpressionTreeNode ParseVariable(Queue<Token> tokens) {
|
---|
[2447] | 151 | Token varTok = tokens.Dequeue();
|
---|
[3746] | 152 | Debug.Assert(varTok.StringValue == "VARIABLE");
|
---|
[3733] | 153 | VariableTreeNode t = (VariableTreeNode)variable.CreateTreeNode();
|
---|
[2447] | 154 | t.Weight = tokens.Dequeue().DoubleValue;
|
---|
| 155 | t.VariableName = tokens.Dequeue().StringValue;
|
---|
| 156 | return t;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[3733] | 159 | private SymbolicExpressionTreeNode CreateTree(Token token) {
|
---|
[2447] | 160 | if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
|
---|
[3733] | 161 | return knownSymbols[token.StringValue].CreateTreeNode();
|
---|
[2447] | 162 | }
|
---|
| 163 |
|
---|
| 164 | private void Expect(Token token, Queue<Token> tokens) {
|
---|
| 165 | Token cur = tokens.Dequeue();
|
---|
[3733] | 166 | if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got: " + cur.StringValue);
|
---|
[2447] | 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|