Exemple #1
0
        private void MakeFromColoredTreeNode(ColoredTree.Node parent, ColoredTree.Node node, int parentIndex)
        {
            int vertexIndex;

            if (node.IsColored())
            {
                if (this.colorToVertexIndexMap.ContainsKey(node.color))
                {
                    vertexIndex = this.colorToVertexIndexMap[node.color];
                }
                else
                {
                    this.MakeVertex(node.label);
                    this.vertexCount++;
                    vertexIndex = this.vertexCount - 1;
                    this.colorToVertexIndexMap[node.color] = vertexIndex;
                }
            }
            else
            {
                this.MakeVertex(node.label);
                this.vertexCount++;
                vertexIndex = this.vertexCount - 1;
            }

            this.MakeEdge(parentIndex, vertexIndex, parent.label, node.label, node.edgeLabel);
            foreach (var child in node.children)
            {
                this.MakeFromColoredTreeNode(node, child, vertexIndex);
            }
        }
Exemple #2
0
        public void MakeFromColoredTree(ColoredTree tree)
        {
            this.MakeGraph();
            ColoredTree.Node root = tree.GetRoot();
            this.MakeVertex(root.label);
            this.vertexCount = 1;
            foreach (var child in root.children)
            {
                this.MakeFromColoredTreeNode(root, child, 0);
            }

            // Important! resets so that the builder can be used again
            this.vertexCount = 0;
            colorToVertexIndexMap.Clear();
        }
Exemple #3
0
        public static ColoredTree Parse(string s)
        {
            ColoredTree tree = null;

            ColoredTree.Node parent  = null;
            ColoredTree.Node current = null;
            int    currentHeight     = 1;
            int    color             = -1;
            int    j          = 0;
            int    k          = 0;
            int    l          = 0;
            string edgeSymbol = null;

            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                if (c == AbstractVertexSignature.StartBranchSymbolChar)
                {
                    parent = current;
                    currentHeight++;
                    tree.UpdateHeight(currentHeight);
                    l = i;
                }
                else if (c == AbstractVertexSignature.EndBranchSymbolChar)
                {
                    parent = parent.parent;
                    currentHeight--;
                    l = i;
                }
                else if (c == StartNodeSymbolChar)
                {
                    if (l < i)
                    {
                        edgeSymbol = s.Substring(l + 1, i - (l + 1));
                        l          = i;
                    }
                    j = i + 1;
                }
                else if (c == EndNodeSymbolChar)
                {
                    string ss;
                    if (k < j)
                    {    // no color
                        ss    = s.Substring(j, i - j);
                        color = -1;
                    }
                    else
                    {        // color
                        ss    = s.Substring(j, k - 1 - j);
                        color = int.Parse(s.Substring(k, i - k), NumberFormatInfo.InvariantInfo);
                    }
                    if (tree == null)
                    {
                        tree    = new ColoredTree(ss);
                        parent  = tree.GetRoot();
                        current = tree.GetRoot();
                    }
                    else
                    {
                        if (edgeSymbol == null)
                        {
                            current = tree.MakeNode(
                                ss, parent, currentHeight, color);
                        }
                        else
                        {
                            current = tree.MakeNode(
                                ss, parent, currentHeight, color, edgeSymbol);
                        }
                    }
                    edgeSymbol = null;
                    l          = i;
                }
                else if (c == ',')
                {
                    k = i + 1;
                }
            }
            return(tree);
        }