- Timestamp:
- 12/15/21 10:34:24 (3 years ago)
- Location:
- branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/SymbolicExpressionImporter.cs
r18114 r18123 97 97 98 98 public ISymbolicExpressionTree Import(string str) { 99 str = str.Replace("(", " ( ").Replace(")", " ) "); 99 str = str.Replace("(", " ( ").Replace(")", " ) ") 100 .Replace("<", " < ").Replace(">", " > ") 101 .Replace("=", " = "); 100 102 ISymbolicExpressionTreeNode root = programRootSymbol.CreateTreeNode(); 101 103 ISymbolicExpressionTreeNode start = startSymbol.CreateTreeNode(); … … 159 161 Expect(Token.RPAR, tokens); 160 162 return tree; 161 } else if (tokens.Peek().Symbol == TokenSymbol. NUMBER) {163 } else if (tokens.Peek().Symbol == TokenSymbol.CONSTANT) { 162 164 var t = (INumericTreeNode)number.CreateTreeNode(); 163 165 t.Value = tokens.Dequeue().DoubleValue; 166 return t; 167 } else if (tokens.Peek().Symbol == TokenSymbol.LBRACKET) { 168 Expect(Token.LBRACKET, tokens); 169 Expect(Token.NUM, tokens); 170 var t = (INumericTreeNode)number.CreateTreeNode(); 171 if (tokens.Peek().Symbol == TokenSymbol.EQ) { 172 Expect(Token.EQ, tokens); 173 var initValToken = tokens.Dequeue(); 174 if(initValToken.Symbol == TokenSymbol.CONSTANT) { 175 t.Value = initValToken.DoubleValue; 176 } else { 177 throw new FormatException("Expected a real value"); 178 } 179 } 180 Expect(Token.RBRACKET, tokens); 164 181 return t; 165 182 } else throw new FormatException("Expected function or number symbol"); … … 224 241 225 242 var weights = new List<double>(); 226 while (tokens.Peek().Symbol == TokenSymbol. NUMBER) {243 while (tokens.Peek().Symbol == TokenSymbol.CONSTANT) { 227 244 weights.Add(tokens.Dequeue().DoubleValue); 228 245 } … … 252 269 253 270 var weightTok = tokens.Dequeue(); 254 Debug.Assert(weightTok.Symbol == TokenSymbol. NUMBER);271 Debug.Assert(weightTok.Symbol == TokenSymbol.CONSTANT); 255 272 t.Weight = weightTok.DoubleValue; 256 273 … … 270 287 271 288 private ISymbolicExpressionTreeNode CreateTree(Token token) { 272 if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue); 289 if (token.Symbol != TokenSymbol.SYMB && 290 token.Symbol != TokenSymbol.LBRACKET && // LBRACKET and RBRACKET are used for <num=..> and as LT, GT operators 291 token.Symbol != TokenSymbol.RBRACKET 292 ) throw new FormatException("Expected function symbol, but got: " + token.StringValue); 273 293 return knownSymbols[token.StringValue].CreateTreeNode(); 274 294 } -
branches/3140_NumberSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/Token.cs
r17180 r18123 23 23 24 24 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 25 internal enum TokenSymbol { LPAR, RPAR, SYMB, NUMBER }; 25 internal enum TokenSymbol { 26 LPAR, RPAR, SYMB, CONSTANT, 27 NUM, // num 28 EQ, // = 29 LBRACKET, // < 30 RBRACKET // > 31 }; 26 32 internal class Token { 27 33 public static readonly Token LPAR = Token.Parse("("); 28 34 public static readonly Token RPAR = Token.Parse(")"); 35 public static readonly Token LBRACKET = Token.Parse("<"); 36 public static readonly Token RBRACKET = Token.Parse(">"); 37 public static readonly Token EQ = Token.Parse("="); 38 public static readonly Token NUM = Token.Parse("num"); 29 39 30 40 public TokenSymbol Symbol { get; set; } … … 46 56 public static Token Parse(string strToken) { 47 57 strToken = strToken.Trim(); 48 Tokent = new Token();58 var t = new Token(); 49 59 t.StringValue = strToken.Trim(); 50 double temp;51 60 if (strToken == "") { 52 61 t = null; … … 55 64 } else if (strToken == ")") { 56 65 t.Symbol = TokenSymbol.RPAR; 57 } else if (double.TryParse(strToken, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out temp)) { 58 t.Symbol = TokenSymbol.NUMBER; 59 t.DoubleValue = double.Parse(strToken, CultureInfo.InvariantCulture.NumberFormat); 66 } else if (strToken == "<") { 67 t.Symbol = TokenSymbol.LBRACKET; 68 } else if (strToken == ">") { 69 t.Symbol = TokenSymbol.RBRACKET; 70 } else if (strToken == "=") { 71 t.Symbol = TokenSymbol.EQ; 72 } else if (strToken.ToLower() == "num") { 73 t.Symbol = TokenSymbol.NUM; 74 } else if (double.TryParse(strToken, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out var val)) { 75 t.Symbol = TokenSymbol.CONSTANT; 76 t.DoubleValue = val; 60 77 } else { 61 78 t.Symbol = TokenSymbol.SYMB;
Note: See TracChangeset
for help on using the changeset viewer.