コード例 #1
0
        private static void AddStackToRoot(CompoundStack root, ElementStack stack)
        {
            foreach (var node in root)
            {
                if (!(node is ElementStack elementStack) ||
                    elementStack.Element != stack.Element)
                {
                    continue;
                }

                elementStack.IncreaseStackSize();
                return;
            }

            root.Nodes.Add(stack);
        }
コード例 #2
0
 protected override void WriteTree(StringBuilder builder, CompoundStack root)
 {
     foreach (var node in root)
     {
         var numberOfAtoms = node.Count > 1 ? node.Count.ToString() : "";
         if (node is ElementStack elementStack)
         {
             builder.Append($"{ elementStack.Element.Symbol }{ numberOfAtoms }");
         }
         else if (node is CompoundStack compoundStack)
         {
             builder.Append("(");
             WriteTree(builder, compoundStack);
             builder.Append(")");
             builder.Append(numberOfAtoms);
         }
         else
         {
             WriteUnknownStack(builder, node);
         }
     }
 }
コード例 #3
0
        protected override void ReadTree(string input, CompoundStack root, ref int currentIndex)
        {
            if (currentIndex >= input.Length)
            {
                return;
            }

            var currentChar = input[currentIndex];

            currentIndex++;
            var nextChar = currentIndex < input.Length ? input[currentIndex] : char.Parse("@");

            if (char.IsUpper(currentChar))
            {
                if (char.IsLower(nextChar))
                {
                    ReadTree(input, root, ref currentIndex);
                    return;
                }

                var numberOfAtoms = 1;

                if (nextChar == ')' && currentIndex + 1 < input.Length)
                {
                    if (char.IsNumber(input[currentIndex + 1])) // We can have a number after closing bracket ')
                    {
                        currentIndex++;
                    }
                    else if (char.IsNumber(input[currentIndex + 2])) // We can have a number after line '|' which is after the closing bracket ')
                    {
                        currentIndex += 2;
                    }

                    numberOfAtoms = GetFullNumber(input, ref currentIndex);
                }

                var stack = new ElementStack(currentChar.ToString().ToElement(), numberOfAtoms);
                AddStackToRoot(root, stack);
                ReadTree(input, root, ref currentIndex);
            }
            else if (char.IsLower(currentChar))
            {
                var numberOfAtoms = 1;
                var toMoveBack    = 1;

                if ((nextChar == ')' || nextChar == '|') &&
                    currentIndex + 1 < input.Length)
                {
                    if (char.IsNumber(input[currentIndex + 1])) // We can have a number after closing bracket ')
                    {
                        currentIndex++;
                        toMoveBack = 3;
                    }
                    else if (char.IsNumber(input[currentIndex + 2])) // We can have a number after line '|' which is after the closing bracket ')
                    {
                        currentIndex += 2;
                        toMoveBack    = 4;
                    }

                    numberOfAtoms = GetFullNumber(input, ref currentIndex);
                }

                var previousChar = input[currentIndex - numberOfAtoms.ToString().Length - toMoveBack];
                var stack        = new ElementStack($"{ previousChar }{ currentChar }".ToElement(), numberOfAtoms);
                AddStackToRoot(root, stack);

                ReadTree(input, root, ref currentIndex);
            }
            else if (currentChar.Equals('('))
            {
                var stack = new CompoundStack();
                root.Nodes.Add(stack);
                InnerStacks.Push(stack);
                ReadTree(input, stack, ref currentIndex);
                ReadTree(input, InnerStacks.Peek(), ref currentIndex);
            }
            else if (currentChar.Equals(')'))
            {
                var numberOfAtoms = 1;

                if (char.IsNumber(nextChar))
                {
                    numberOfAtoms = GetFullNumber(input, ref currentIndex);
                }

                root.IncreaseStackSize(numberOfAtoms);
                InnerStacks.Pop();
            }
            else if (currentChar == '-' || currentChar == '=')
            {
                // Possible format's of chars next to the '-' or '='
                // Where: C - upper char; r - lower char; 0 - number;
                // C - (
                // ) - C
                // C - C
                // ) - (
                // 0 - (
                // 0 - C
                // r - (

                // For now on it will skip parsing the connections - not supported yet
                // There is no support (yet) of how to store the number of connections between atoms
                // This is something that will need to be added later on
                // TODO: Add support for storing the information about number of connections between atoms

                if (char.IsNumber(nextChar))
                {
                    while (input[currentIndex] != '|')
                    {
                        currentIndex++;
                    }
                }

                ReadTree(input, root, ref currentIndex);
            }
            else if (currentChar == '|')
            {
                // Possible format's of chars next to the '|'
                // Where: C - upper char; r - lower char; 0 - number;
                // C | 0
                // r | 0
                // 0 | C
                // 0 | (

                // For now on it will skip parsing the number of connections - not supported yet
                // There is no support (yet) of how to store the number of connections between atoms
                // This is something that will need to be added later on
                // TODO: Add support for storing the information about number of connections between atoms

                if (char.IsNumber(nextChar))
                {
                    while (input[currentIndex] != '|')
                    {
                        currentIndex++;
                    }
                }

                ReadTree(input, root, ref currentIndex);
            }
            else
            {
                throw new FormatException($"Unknown char: '{ currentChar }', at position: { currentIndex }");
            }
        }
コード例 #4
0
 protected override void WriteTree(StringBuilder builder, CompoundStack root)
 {
     throw new NotImplementedException("[W.I.P.] For now on please use CondensedChemicalCompoundParser");
 }
コード例 #5
0
 /// <summary>
 /// Builds a Compound structure tree from given input.
 /// </summary>
 /// <param name="input"></param>
 /// <param name="root"></param>
 /// <param name="currentIndex"></param>
 protected virtual void ReadTree(string input, CompoundStack root, ref int currentIndex)
 {
 }
コード例 #6
0
 /// <summary>
 /// Parses given stack and serializes it into it's string representation.
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="stack"></param>
 protected virtual void WriteTree(StringBuilder builder, CompoundStack stack)
 {
 }
コード例 #7
0
        protected override void ReadTree(string input, CompoundStack root, ref int currentIndex)
        {
            if (currentIndex >= input.Length)
            {
                return;
            }

            var currentChar = input[currentIndex];

            currentIndex++;
            var nextChar = currentIndex < input.Length ? input[currentIndex] : char.Parse("@");

            if (char.IsUpper(currentChar))
            {
                if (char.IsLower(nextChar))
                {
                    ReadTree(input, root, ref currentIndex);
                    return;
                }

                var numberOfAtoms = 1;

                if (char.IsNumber(nextChar))
                {
                    numberOfAtoms = GetFullNumber(input, ref currentIndex);
                }

                var stack = new ElementStack(currentChar.ToString().ToElement(), numberOfAtoms);
                root.Nodes.Add(stack);
                ReadTree(input, root, ref currentIndex);
            }
            else if (currentChar.Equals('('))
            {
                var stack = new CompoundStack();
                root.Nodes.Add(stack);
                InnerStacks.Push(stack);
                ReadTree(input, stack, ref currentIndex);
                ReadTree(input, InnerStacks.Peek(), ref currentIndex);
            }
            else if (currentChar.Equals(')'))
            {
                var numberOfAtoms = 1;

                if (char.IsNumber(nextChar))
                {
                    numberOfAtoms = GetFullNumber(input, ref currentIndex);
                }

                root.IncreaseStackSize(numberOfAtoms);
                InnerStacks.Pop();
            }
            else if (char.IsLower(currentChar))
            {
                var numberOfAtoms = 1;

                if (char.IsNumber(nextChar))
                {
                    numberOfAtoms = GetFullNumber(input, ref currentIndex);
                }

                var previousChar = GetPreviousUpperChar(input, currentIndex, numberOfAtoms);
                var stack        = new ElementStack($"{ previousChar }{ currentChar }".ToElement(), numberOfAtoms);
                root.Nodes.Add(stack);
                ReadTree(input, root, ref currentIndex);
            }
            else if (char.IsNumber(currentChar) && currentIndex < 2) // Only for the number before the compound
            {
                currentIndex = 0;

                var numberOfMoles = GetFullNumber(input, ref currentIndex);

                if (input[currentIndex] == '(')
                {
                    currentIndex++;
                    numberOfMoles--;
                }

                root.Count = numberOfMoles;

                ReadTree(input, root, ref currentIndex);
            }
            else
            {
                throw new FormatException($"Unknown char: '{ currentChar }', at position: { currentIndex }");
            }
        }