/// <summary>
        /// Parse the given line.
        /// </summary>
        /// <param name="line">The line to parse.</param>
        /// <returns>The parsed probability.</returns>
        public ParsedProbability Parse(String line)
        {

            ParsedProbability result = new ParsedProbability();

            SimpleParser parser = new SimpleParser(line);
            parser.EatWhiteSpace();
            if (!parser.LookAhead("P(", true))
            {
                throw new EncogError("Bayes table lines must start with P(");
            }
            parser.Advance(2);

            // handle base
            AddEvents(parser, result.BaseEvents, "|,)=[]");

            // handle conditions
            if (parser.Peek() == '|')
            {
                parser.Advance();
                AddEvents(parser, result.GivenEvents, ",)=[]");

            }

            if (parser.Peek() != ')')
            {
                throw new BayesianError("Probability not properly terminated.");
            }

            return result;

        }
        /// <summary>
        /// Add events, as they are pased.
        /// </summary>
        /// <param name="parser">The parser.</param>
        /// <param name="results">The events found.</param>
        /// <param name="delim">The delimiter to use.</param>
        private void AddEvents(SimpleParser parser, IList<ParsedEvent> results, String delim)
        {
            bool done = false;
            StringBuilder l = new StringBuilder();

            while (!done && !parser.EOL())
            {
                char ch = parser.Peek();
                if (delim.IndexOf(ch) != -1)
                {
                    if (ch == ')' || ch == '|')
                        done = true;

                    ParsedEvent parsedEvent;

                    // deal with a value specified by + or -
                    if (l.Length > 0 && l[0] == '+')
                    {
                        String l2 = l.ToString().Substring(1);
                        parsedEvent = new ParsedEvent(l2.Trim());
                        parsedEvent.Value = "true";
                    }
                    else if (l.Length > 0 && l[0] == '-')
                    {
                        String l2 = l.ToString().Substring(1);
                        parsedEvent = new ParsedEvent(l2.Trim());
                        parsedEvent.Value = "false";
                    }
                    else
                    {
                        String l2 = l.ToString();
                        parsedEvent = new ParsedEvent(l2.Trim());
                    }

                    // parse choices
                    if (ch == '[')
                    {
                        parser.Advance();
                        int index = 0;
                        while (ch != ']' && !parser.EOL())
                        {

                            String labelName = parser.ReadToChars(":,]");
                            if (parser.Peek() == ':')
                            {
                                parser.Advance();
                                parser.EatWhiteSpace();
                                double min = double.Parse(parser.ReadToWhiteSpace());
                                parser.EatWhiteSpace();
                                if (!parser.LookAhead("to", true))
                                {
                                    throw new BayesianError("Expected \"to\" in probability choice range.");
                                }
                                parser.Advance(2);
                                double max = CSVFormat.EgFormat.Parse(parser.ReadToChars(",]"));
                                parsedEvent.ChoiceList.Add(new ParsedChoice(labelName, min, max));

                            }
                            else
                            {
                                parsedEvent.ChoiceList.Add(new ParsedChoice(labelName, index++));
                            }
                            parser.EatWhiteSpace();
                            ch = parser.Peek();

                            if (ch == ',')
                            {
                                parser.Advance();
                            }
                        }
                    }

                    // deal with a value specified by =
                    if (parser.Peek() == '=')
                    {
                        parser.ReadChar();
                        String value = parser.ReadToChars(delim);
                        //					BayesianEvent evt = this.network.getEvent(parsedEvent.getLabel());
                        parsedEvent.Value = value;
                    }

                    if (ch == ',')
                    {
                        parser.Advance();
                    }

                    if (ch == ']')
                    {
                        parser.Advance();
                    }

                    if (parsedEvent.Label.Length > 0)
                    {
                        results.Add(parsedEvent);
                    }
                    l.Length = 0;
                }
                else
                {
                    parser.Advance();
                    l.Append(ch);
                }
            }

        }
        /// <summary>
        ///     Read the specified parameter.
        /// </summary>
        /// <param name="parser">The parser to use.</param>
        /// <returns>The parsed parameter.</returns>
        private ParamTemplate ReadParam(SimpleParser parser)
        {
            var result = new ParamTemplate();

            if (!parser.LookAhead("{", true))
            {
                throw new EACompileError("Expected {");
            }
            parser.Advance();

            bool done = false;
            var buffer = new StringBuilder();

            while (!done)
            {
                if (parser.Peek() == '}')
                {
                    done = true;
                    parser.Advance();
                }
                else if (parser.Peek() == '{')
                {
                    throw new EACompileError("Unexpected {");
                }
                else if (parser.Peek() == '{')
                {
                    done = true;
                    parser.Advance();
                }
                else if (parser.Peek() == ',')
                {
                    result.AddType(buffer.ToString().Trim().ToLower());
                    parser.Advance();
                    buffer.Length = 0;
                }
                else
                {
                    buffer.Append(parser.ReadChar());
                }
            }

            String s = buffer.ToString().Trim();
            if (s.Length > 0)
            {
                result.AddType(s);
            }

            return result;
        }
Пример #4
0
        public ProgramNode Parse(String expression)
        {
            this.parser = new SimpleParser(expression);

            while (!this.parser.EOL())
            {
                this.parser.EatWhiteSpace();

                // read in the command
                if (this.parser.ReadChar() != '[')
                {
                    throw new EACompileError("Expected [");
                }
                this.parser.EatWhiteSpace();
                StringBuilder cmd = new StringBuilder();
                while (this.parser.Peek() != ']' && !this.parser.EOL())
                {
                    cmd.Append(this.parser.ReadChar());
                }

                if (this.parser.Peek() != ']')
                {
                    throw new EACompileError("Expected ]");
                }
                this.parser.Advance();

                // parse out the command
                string[] tok = cmd.ToString().Split(':');
                int idx = 0;
                String name = tok[idx++];
                int childCount = int.Parse(tok[idx++]);
                IProgramExtensionTemplate temp = EncogOpcodeRegistry.Instance.FindOpcode(name, childCount);
                if (temp == null)
                {
                    throw new EACompileError("Invalid instruction: " + name);
                }

                // build the arguments
                ProgramNode[] args = new ProgramNode[childCount];
                for (int i = args.Length - 1; i >= 0; i--)
                {
                    args[i] = this.nodeStack.Pop();
                }

                // factor the node
                ProgramNode node = this.holder.Functions.FactorProgramNode(name, this.holder, args);
                this.nodeStack.Push(node);

                // add any needed data to the node
                for (int i = 0; i < temp.DataSize; i++)
                {
                    String str = tok[idx++].Trim();
                    int strIdx = str.IndexOf('#');
                    if (strIdx != -1)
                    {
                        int enumType = int.Parse(str.Substring(0, strIdx));
                        int enumVal = int.Parse(str.Substring(strIdx + 1));
                        node.Data[0] = new ExpressionValue(enumType, enumVal);

                    }
                    // is it boolean?
                    else if (str.Length == 1 && "tf".IndexOf(char.ToLower(str[0])) != -1)
                    {
                        node.Data[i] = new ExpressionValue(string.Compare(str, "t", true));
                    }
                    // is it a string?
                    else if (str[0] == '\"')
                    {
                        node.Data[i] = new ExpressionValue(str.Substring(1, str.Length - 1));
                    }
                    // is it an integer
                    else if (str.IndexOf('.') == -1 && str.ToLower().IndexOf('e') == -1)
                    {
                        long l;
                        try
                        {
                            l = long.Parse(str);
                        }
                        catch (FormatException ex)
                        {
                            // sometimes C# will output a long value that is larger than can be parsed
                            // this is very likely not a useful genome and we just set it to zero so that
                            // the population load does not fail.
                            l = 0;
                        }
                        node.Data[i] = new ExpressionValue(l);
                    }
                    // At this point, must be a float
                    else
                    {
                        node.Data[i] = new ExpressionValue(CSVFormat.EgFormat.Parse(str));
                    }
                }
            }

            return this.nodeStack.Pop();
        }
Пример #5
0
 private static List<string> x5dc02619c7b497cb(string x311e7a92306d7199)
 {
     List<string> list = new List<string>();
     SimpleParser parser = new SimpleParser(x311e7a92306d7199);
     if (0 == 0)
     {
         goto Label_0036;
     }
     Label_0012:
     list.Add((parser.Peek() == '"') ? parser.ReadQuotedString() : parser.ReadToWhiteSpace());
     parser.EatWhiteSpace();
     Label_0036:
     if (!parser.EOL())
     {
         goto Label_0012;
     }
     return list;
 }
        /// <summary>
        ///     Construct a basic template object.
        /// </summary>
        /// <param name="thePrecedence">The precedence.</param>
        /// <param name="theSignature">The opcode signature.</param>
        /// <param name="theType">The opcode type.</param>
        /// <param name="isVariable">True, if this opcode is a variable.</param>
        /// <param name="theDataSize">The data size kept for this opcode.</param>
        /// <param name="theEvaluate">The evaluator delegate.</param>
        /// <param name="theIsPossibleReturnType">The return type delegate.</param>
        /// <param name="theRandomize">The randomizer delegate.</param>
        public BasicTemplate(int thePrecedence, String theSignature,
                             NodeType theType, bool isVariable,
                             int theDataSize,
                             DelEvaluate theEvaluate,
                             DelIsPossibleReturnType theIsPossibleReturnType,
                             DelRandomize theRandomize)
        {
            _precedence = thePrecedence;
            _signature = theSignature;
            _varValue = isVariable;
            _dataSize = theDataSize;
            _nodeType = theType;

            _delEvaluate = theEvaluate;
            _delIsPossibleReturnType = theIsPossibleReturnType;
            _delRandomize = theRandomize;

            if (theSignature.Trim().Equals("("))
            {
                // special case, we add a left-paren for the shunting yard alg.
                _name = theSignature;
                _returnValue = null;
            }
            else
            {
                // non-special case, find the name of the function/operator
                var parser = new SimpleParser(theSignature);
                bool pass = false;

                parser.EatWhiteSpace();
                _name = parser.ReadToChars("(").Trim();
                parser.Advance();

                bool done = false;
                while (!done)
                {
                    if (parser.Peek() == ')')
                    {
                        parser.Advance();
                        done = true;
                    }
                    else if (parser.Peek() == ':')
                    {
                        parser.Advance();
                        pass = true;
                    }
                    else if (parser.Peek() == '{')
                    {
                        ParamTemplate temp = ReadParam(parser);
                        temp.PassThrough = pass;
                        pass = false;
                        _param.Add(temp);
                    }
                    else
                    {
                        parser.Advance();
                        if (parser.EOL())
                        {
                            throw new EncogError("Invalid opcode template.");
                        }
                    }
                }

                // get the return type
                parser.EatWhiteSpace();
                if (!parser.LookAhead(":", true))
                {
                    throw new EACompileError("Return type not specified.");
                }
                parser.Advance();
                parser.EatWhiteSpace();
                _returnValue = ReadParam(parser);
            }
        }
 /// <summary>
 /// Parse a name.
 /// </summary>
 ///
 /// <param name="parser">The parser to use.</param>
 /// <returns>The name.</returns>
 private static String ParseName(SimpleParser parser)
 {
     var result = new StringBuilder();
     parser.EatWhiteSpace();
     while (parser.IsIdentifier())
     {
         result.Append(parser.ReadChar());
     }
     return result.ToString();
 }
        /// <summary>
        /// Parse a value.
        /// </summary>
        ///
        /// <param name="parser">The parser to use.</param>
        /// <returns>The newly parsed value.</returns>
        private static String ParseValue(SimpleParser parser)
        {
            bool quoted = false;
            var str = new StringBuilder();

            parser.EatWhiteSpace();

            if (parser.Peek() == '\"')
            {
                quoted = true;
                parser.Advance();
            }

            while (!parser.EOL())
            {
                if (parser.Peek() == '\"')
                {
                    if (quoted)
                    {
                        parser.Advance();
                        if (parser.Peek() == '\"')
                        {
                            str.Append(parser.ReadChar());
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        str.Append(parser.ReadChar());
                    }
                }
                else if (!quoted
                         && (parser.IsWhiteSpace() || (parser.Peek() == ',')))
                {
                    break;
                }
                else
                {
                    str.Append(parser.ReadChar());
                }
            }
            return str.ToString();
        }
        /// <summary>
        /// Parse parameters.
        /// </summary>
        ///
        /// <param name="line">The line to parse.</param>
        /// <returns>The parsed values.</returns>
        public static IDictionary<String, String> ParseParams(String line)
        {
            IDictionary<String, String> result = new Dictionary<String, String>();

            var parser = new SimpleParser(line);

            while (!parser.EOL())
            {
                String name = ParseName(parser)
                    .ToUpper();

                parser.EatWhiteSpace();
                if (!parser.LookAhead("=", false))
                {
                    throw new EncogError("Missing equals(=) operator.");
                }
                parser.Advance();

                String v = ParseValue(parser);

                result[name.ToUpper()] = v;

                if (!parser.ParseThroughComma())
                {
                    break;
                }
            }

            return result;
        }
        public ProgramNode Parse(String expression)
        {
            this.parser = new SimpleParser(expression);
            this.unary = true;

            while (!parser.EOL())
            {
                parser.EatWhiteSpace();
                char ch = parser.Peek();
                if (ch == '.' || char.IsDigit(ch))
                {
                    HandleNumeric();
                    this.unary = false;
                }
                else if (char.IsLetter(ch))
                {
                    handleAlpha(false);
                    this.unary = false;
                }
                else if (ch == '(')
                {
                    this.parser.Advance();
                    this.functionStack.Push(LEFT_PAREN);
                    this.unary = true;
                }
                else if (ch == ')')
                {
                    handleRightParen();
                    this.unary = false;
                }
                else if ("<>^*/+-=&|".IndexOf(ch) != -1)
                {
                    handleSymbol();
                    this.unary = true;
                }
                else if (ch == '\"')
                {
                    handleString();
                    this.unary = false;
                }
                else if (ch == ',')
                {
                    HandleFunctionSeparator();
                    this.unary = true;
                }
                else
                {
                    throw new EACompileError("Unparsable character: " + ch);
                }
            }

            // pop off any functions still on the stack
            while (this.functionStack.Count > 0)
            {
                IProgramExtensionTemplate f = this.functionStack.Pop();
                OutputQueue(f);
            }

            // were there no operators?
            if (this.rootNode == null)
            {
                this.rootNode = this.outputStack.Pop();
            }

            return this.rootNode;
        }
Пример #11
0
 private static string xe1cd55dd1e3e1253(SimpleParser xbce90b56ab411c23)
 {
     StringBuilder builder = new StringBuilder();
     xbce90b56ab411c23.EatWhiteSpace();
     while (xbce90b56ab411c23.IsIdentifier())
     {
         builder.Append(xbce90b56ab411c23.ReadChar());
     }
     return builder.ToString();
 }
Пример #12
0
 private static string x0738845e2abbf3d2(SimpleParser xbce90b56ab411c23)
 {
     bool flag = false;
     StringBuilder builder = new StringBuilder();
     xbce90b56ab411c23.EatWhiteSpace();
     if (0 == 0)
     {
         goto Label_001E;
     }
     goto Label_0160;
     Label_000F:
     builder.Append(xbce90b56ab411c23.ReadChar());
     goto Label_002B;
     Label_001E:
     if (xbce90b56ab411c23.Peek() == '"')
     {
         goto Label_0160;
     }
     Label_002B:
     if (!xbce90b56ab411c23.EOL())
     {
         goto Label_00AE;
     }
     goto Label_01A9;
     Label_0061:
     if (((uint) flag) >= 0)
     {
         goto Label_000F;
     }
     Label_008C:
     if (!flag)
     {
         if (xbce90b56ab411c23.IsWhiteSpace())
         {
             goto Label_01A9;
         }
         if (xbce90b56ab411c23.Peek() != ',')
         {
             goto Label_0061;
         }
         if (0 != 0)
         {
             goto Label_008C;
         }
         if ((((uint) flag) - ((uint) flag)) <= uint.MaxValue)
         {
             goto Label_01A9;
         }
         goto Label_000F;
     }
     if (-2 != 0)
     {
         goto Label_000F;
     }
     Label_00AE:
     if (xbce90b56ab411c23.Peek() == '"')
     {
         goto Label_0109;
     }
     if ((((uint) flag) - ((uint) flag)) <= uint.MaxValue)
     {
         goto Label_008C;
     }
     goto Label_000F;
     Label_00D2:
     if ((((uint) flag) + ((uint) flag)) < 0)
     {
         goto Label_001E;
     }
     goto Label_002B;
     Label_0109:
     if (!flag)
     {
         builder.Append(xbce90b56ab411c23.ReadChar());
         goto Label_002B;
     }
     xbce90b56ab411c23.Advance();
     if (xbce90b56ab411c23.Peek() != '"')
     {
         goto Label_01A9;
     }
     builder.Append(xbce90b56ab411c23.ReadChar());
     goto Label_00D2;
     Label_0160:
     flag = true;
     if ((((uint) flag) + ((uint) flag)) <= uint.MaxValue)
     {
         if ((((uint) flag) | 15) == 0)
         {
             if (((uint) flag) >= 0)
             {
                 goto Label_0061;
             }
             goto Label_008C;
         }
         xbce90b56ab411c23.Advance();
         goto Label_002B;
     }
     if ((((uint) flag) - ((uint) flag)) >= 0)
     {
         goto Label_0109;
     }
     goto Label_00D2;
     Label_01A9:
     return builder.ToString();
 }
Пример #13
0
 public static IDictionary<string, string> ParseParams(string line)
 {
     SimpleParser parser;
     string str;
     string str2;
     IDictionary<string, string> dictionary = new Dictionary<string, string>();
     if (-1 != 0)
     {
         parser = new SimpleParser(line);
     }
     else
     {
         goto Label_002F;
     }
     Label_0012:
     if (!parser.EOL())
     {
         str = xe1cd55dd1e3e1253(parser).ToUpper();
         goto Label_007A;
     }
     if (0 == 0)
     {
         return dictionary;
     }
     Label_0022:
     if (parser.ParseThroughComma())
     {
         goto Label_0012;
     }
     if (0 != 0)
     {
         goto Label_007A;
     }
     return dictionary;
     Label_002F:
     str2 = x0738845e2abbf3d2(parser);
     dictionary[str.ToUpper()] = str2;
     goto Label_0022;
     Label_007A:
     parser.EatWhiteSpace();
     if (!parser.LookAhead("=", false))
     {
         throw new EncogError("Missing equals(=) operator.");
     }
     parser.Advance();
     goto Label_002F;
 }