Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.Tests/Token.cs @ 4075

Last change on this file since 4075 was 2625, checked in by gkronber, 15 years ago

Fixed bugs in network transformation operator. Added test cases. #833.

File size: 2.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.IO;
27using HeuristicLab.GP;
28using HeuristicLab.GP.Interfaces;
29using HeuristicLab.GP.StructureIdentification;
30using System.Diagnostics;
31using System.Globalization;
32
33namespace HeuristicLab.GP.Test {
34  public enum TokenSymbol { LPAR, RPAR, SYMB, NUMBER };
35  public class Token {
36    public static readonly Token LPAR = Token.Parse("(");
37    public static readonly Token RPAR = Token.Parse(")");
38
39    public TokenSymbol Symbol { get; set; }
40    public string StringValue { get; set; }
41    public double DoubleValue { get; set; }
42    public Token() { }
43
44    public override bool Equals(object obj) {
45      Token other = (obj as Token);
46      if (other == null) return false;
47      if (other.Symbol != Symbol) return false;
48      return other.StringValue == this.StringValue;
49    }
50
51    public static Token Parse(string strToken) {
52      strToken = strToken.Trim();
53      Token t = new Token();
54      t.StringValue = strToken.Trim();
55      double temp;
56      if (strToken == "") {
57        t = null;
58      } else if (strToken == "(") {
59        t.Symbol = TokenSymbol.LPAR;
60      } else if (strToken == ")") {
61        t.Symbol = TokenSymbol.RPAR;
62      } else if (double.TryParse(strToken, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out temp)) {
63        t.Symbol = TokenSymbol.NUMBER;
64        t.DoubleValue = double.Parse(strToken, CultureInfo.InvariantCulture.NumberFormat);
65      } else {
66        t.Symbol = TokenSymbol.SYMB;
67      }
68      return t;
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.