Exemplo n.º 1
0
 private Token GenerateTestToken(TokenType tt, string strValue)
 {
     if (tt == TokenType.Variable)
     {
         return(MathExpressionTokenizer.GetVariableToken(strValue));
     }
     return(MathExpressionTokenizer.GetModifiedToken(MathExpressionTokenizer.GetToken(tt), strValue));
 }
Exemplo n.º 2
0
 private void FormatChild(StringBuilder builder, int iIndex)
 {
     if (this[iIndex].RecursiveCount() > 1 && this[iIndex].Type != TokenType.Function &&
         MathExpressionTokenizer.GetToken(Value)?.Precedence >= TokenPrecedence.Multiplication)
     {
         builder.Append("(");
         builder.Append(this[iIndex]);
         builder.Append(")");
     }
     else
     {
         builder.Append(this[iIndex]);
     }
 }
        /// <summary>
        /// Filtert einen mathematischen Ausdruck oder Term.
        /// </summary>
        /// <param name="tokens">Ein Token-Iterator mit den einzelnen Teilen des mathematischen Ausdruckes</param>
        /// <param name="tokenizer">Der <see cref="MathExpressionTokenizer"/>, der den Term zuvor geparst hat.</param>
        /// <returns>Ein neuer Token-Iterator, der es ermöglicht über den gefilterten Term zu iterieren.</returns>
        public IEnumerable <Token> PostProcessTokens(IEnumerable <Token> tokens)
        {
            Queue <Token> lastTokens = new Queue <Token>(3);

            foreach (Token t in tokens)
            {
                lastTokens.Enqueue(t);

                if (lastTokens.Count == 2)
                {
                    Token t1 = lastTokens.Dequeue();
                    Token t2 = lastTokens.Dequeue();

                    if (t1.Type == TokenType.Parenthesis && t1.Value == ")" &&
                        t2.Type == TokenType.Parenthesis && t2.Value == "(")
                    {
                        yield return(t1);

                        yield return(MathExpressionTokenizer.GetToken("*"));

                        yield return(t2);
                    }
                    else if ((t1.Type == TokenType.Number || t1.Type == TokenType.Variable || t1.Type == TokenType.Constant || (t1.Type == TokenType.Parenthesis && t1.Value == ")")) &&
                             (t2.Type == TokenType.Number || t2.Type == TokenType.Variable || t2.Type == TokenType.Constant || (t2.Type == TokenType.Parenthesis && t1.Value == "(") || t2.Type == TokenType.Function))
                    {
                        yield return(t1);

                        yield return(MathExpressionTokenizer.GetToken("*"));

                        yield return(t2);
                    }
                    else
                    {
                        yield return(t1);

                        lastTokens.Enqueue(t2);
                    }
                }
            }

            while (lastTokens.Count > 0)
            {
                yield return(lastTokens.Dequeue());
            }
        }
        /// <summary>
        /// Gruppiert aufeinanderfolgende Token.
        /// </summary>
        /// <param name="tokens">Potenziell zu gruppierende Token</param>
        /// <param name="tt">Typ der zu gruppierenden Token</param>
        /// <returns>Gefilterte Liste, in der aufeinanderfolgende Token des angegebenen Typs zu einem Token zusammengefasst worden sind.</returns>
        private IEnumerable <Token> GroupByType(IEnumerable <Token> tokens, TokenType tt)
        {
            Token lastToken = MathExpressionTokenizer.GetToken(TokenType.Unknown);

            foreach (Token t in tokens)
            {
                if (t.Type == tt && lastToken.Type == tt)
                {
                    lastToken = MathExpressionTokenizer.GetModifiedToken(t, lastToken.Value + t.Value);
                }
                else if (lastToken.Type == TokenType.Unknown)
                {
                    lastToken = t;
                }
                else
                {
                    yield return(lastToken);

                    lastToken = t;
                }
            }

            yield return(lastToken);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Ergänzt die Wissensbasis anhand einer Datenbank.
        /// </summary>
        /// <param name="strFile">Die Datei, welche die Datenbank enthält.</param>
        public static void LoadKnowledgeBaseFromFile(string strFile)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(strFile);
                XmlNode root = doc.DocumentElement;

                if (root?.Name != "knowledgebase")
                {
                    return;
                }

                XmlNode xmlRules = root.SelectSingleNode("functions");

                if (xmlRules == null || !xmlRules.HasChildNodes ||
                    !(xmlRules.SelectNodes("function") is XmlNodeList listFunctions))
                {
                    return;
                }

                foreach (XmlNode node in listFunctions)
                {
                    if (!(node.SelectSingleNode("arguments")?.SelectNodes("argument") is XmlNodeList listArguments))
                    {
                        continue;
                    }
                    XmlNode xmlName = node.SelectSingleNode("name");

                    if (xmlName == null)
                    {
                        continue;
                    }

                    List <DefaultFunctionArgument> listDefaultArguments = new List <DefaultFunctionArgument>();

                    foreach (XmlNode nodeArgument in listArguments)
                    {
                        if (!(nodeArgument.SelectSingleNode("hasdefaultvalue") is XmlNode xmlHasDefaultArgument) ||
                            !bool.TryParse(xmlHasDefaultArgument.InnerText, out bool bHasDefaultArgument))
                        {
                            continue;
                        }

                        if (bHasDefaultArgument &&
                            nodeArgument.SelectSingleNode("defaultvalue") is XmlNode xmlDefaultArgument)
                        {
                            listDefaultArguments.Add(new DefaultFunctionArgument
                            {
                                HasDefaultValue = true,
                                DefaultValue    = MathExpressionTokenizer.GetToken(xmlDefaultArgument.InnerText)
                            });
                        }
                        else
                        {
                            listDefaultArguments.Add(new DefaultFunctionArgument
                            {
                                HasDefaultValue = false
                            });
                        }
                    }

                    FunctionArgumentFilter.RegisterFunction(xmlName.InnerText, listDefaultArguments);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }