Пример #1
0
        public static MutableTree GetParent(MutableTree node, MutableTree root)
        {
            if (root == null || node == null)
            {
                return(null);
            }

            if (root == node)
            {
                return(null);
            }

            if (root.GetChildren().Contains(node))
            {
                return(root);
            }


            foreach (MutableTree child in root.GetChildren())
            {
                MutableTree found = GetParent(node, child);
                if (found != null)
                {
                    return(found);
                }
            }

            return(null);
        }
Пример #2
0
        public static string ToJson(MutableTree tree)
        {
            JObject jobject = new JObject();

            ToJsonHelper(tree, jobject);

            return(jobject.ToString());
        }
Пример #3
0
Файл: Tree.cs Проект: nunb/code
        public static Tree FromMutable(MutableTree mutable)
        {
            Tree node = Tree.FromBoundingBox(mutable, mutable.Tags);

            foreach (MutableTree child in mutable.GetChildren())
            {
                node._children.Add(FromMutable(child));
            }

            return(node);
        }
Пример #4
0
        public static MutableTree FromBoundingBox(IBoundingBox bb, Dictionary<string, object> tags)
        {
            MutableTree tree = new MutableTree();
            tree.Top = bb.Top;
            tree.Left = bb.Left;
            tree.Width = bb.Width;
            tree.Height = bb.Height;

            if (tags != null)
                tree._tags = new Dictionary<string, object>(tags);
            else
                tree._tags = new Dictionary<string, object>();

            tree._children = new List<MutableTree>();

            return tree;
        }
Пример #5
0
        public static IEnumerable <MutableTree> GetSiblings(MutableTree node, MutableTree root)
        {
            MutableTree        parent   = GetParent(node, root);
            List <MutableTree> siblings = new List <MutableTree>();

            if (parent != null)
            {
                foreach (MutableTree child in parent.GetChildren())
                {
                    if (child != node)
                    {
                        siblings.Add(node);
                    }
                }
            }

            return(siblings);
        }
Пример #6
0
        public static MutableTree FromTree(Tree tree)
        {
            var tags = new Dictionary <string, object>();

            foreach (var pair in tree.GetTags())
            {
                tags[pair.Key] = pair.Value;
            }

            MutableTree node = MutableTree.FromBoundingBox(tree, tags);

            foreach (Tree child in tree.GetChildren())
            {
                node._children.Add(FromTree(child));
            }

            return(node);
        }
Пример #7
0
        public static MutableTree GetParent(MutableTree node, MutableTree root)
        {
            if (root == null || node == null)
                return null;

            if (root == node)
                return null;

            if (root.GetChildren().Contains(node))
                return root;

            foreach (MutableTree child in root.GetChildren())
            {
                MutableTree found = GetParent(node, child);
                if (found != null)
                    return found;
            }

            return null;
        }
Пример #8
0
        public static MutableTree FromBoundingBox(IBoundingBox bb, Dictionary <string, object> tags)
        {
            MutableTree tree = new MutableTree();

            tree.Top    = bb.Top;
            tree.Left   = bb.Left;
            tree.Width  = bb.Width;
            tree.Height = bb.Height;

            if (tags != null)
            {
                tree._tags = new Dictionary <string, object>(tags);
            }
            else
            {
                tree._tags = new Dictionary <string, object>();
            }

            tree._children = new List <MutableTree>();

            return(tree);
        }
Пример #9
0
        private static void ToJsonHelper(MutableTree currnode, JObject jo)
        {
            foreach (string tagname in currnode._tags.Keys)
            {
                jo.Add(tagname, currnode._tags[tagname].ToString());
            }

            jo.Add("top", currnode.Top);
            jo.Add("left", currnode.Left);
            jo.Add("width", currnode.Width);
            jo.Add("height", currnode.Height);

            JArray array = new JArray();

            foreach (MutableTree child in currnode._children)
            {
                JObject childjo = new JObject();
                ToJsonHelper(child, childjo);
                array.Add(childjo);
            }

            jo.Add("children", array);
        }
Пример #10
0
        public static IEnumerable<MutableTree> GetSiblings(MutableTree node, MutableTree root)
        {
            MutableTree parent = GetParent(node, root);
            List<MutableTree> siblings = new List<MutableTree>();
            if (parent != null)
            {
                foreach (MutableTree child in parent.GetChildren())
                {
                    if (child != node)
                        siblings.Add(node);
                }
            }

            return siblings;
        }
Пример #11
0
        private static int GetNodeIndex(MutableTree node, MutableTree parent)
        {
            int index = 0;

            string pixelhash = null;
            string type = "none";
            if (node.ContainsAttribute("type"))
                type = node["type"].ToString();

            if (node.ContainsAttribute("type") && node["type"].Equals("content"))
                pixelhash = node["pixel_hash"].ToString();

            if (parent != null)
            {
                var children = parent.GetChildren();
                children.Sort(BoundingBox.CompareByTopThenLeft);
                foreach (MutableTree sibling in children)
                {
                    if (sibling == node)
                        return index;
                    else
                    {
                        string sibtype = "none";
                        if (sibling.ContainsAttribute("type"))
                            sibtype = sibling["type"].ToString();

                        if (sibtype.Equals("ptype") && type.Equals("ptype"))
                        {
                            if (sibling["ptype_id"].Equals(node["ptype_id"]))
                                index++;
                        }
                        else if (sibtype.Equals(type))
                        {
                            index++;
                        }
                    }
                }
            }

            return index;
        }
Пример #12
0
        private static void GetPathHelper(MutableTree node, MutableTree root, StringBuilder sb)
        {
            if (node == null)
                return;

            MutableTree parent = GetParent(node, root);
            GetLocalPath(node, parent, sb);

            if (parent != null)
            {
                sb.Append("/");
                GetPathHelper(parent, root, sb);
            }
        }
Пример #13
0
 public static string GetPath(MutableTree node, MutableTree root)
 {
     StringBuilder sb = new StringBuilder();
     GetPathHelper(node, root, sb);
     return sb.ToString();
 }
Пример #14
0
        public static Tree FromMutable(MutableTree mutable)
        {
            Tree node = Tree.FromBoundingBox(mutable, mutable.Tags);

            foreach (MutableTree child in mutable.GetChildren())
            {
                node._children.Add(FromMutable(child));
            }

            return node;
        }
Пример #15
0
 public PrefabSingleInterpretArgs(MutableTree tree, IRuntimeStorage runtimeStorage)
 {
     Tree = tree;
     RuntimeStorage = new PythonRuntimeStorageWrapper(runtimeStorage);
 }
Пример #16
0
        public static string ToJson(MutableTree tree)
        {
            JObject jobject = new JObject();
            ToJsonHelper(tree, jobject);

            return jobject.ToString();
        }
Пример #17
0
        private static void GetLocalPath(MutableTree node, MutableTree parent, StringBuilder sb)
        {
            if (node.ContainsAttribute("type"))
            {
                switch (node["type"].ToString())
                {
                    case "ptype":
                        sb.Append(node["ptype_id"].ToString());
                        break;

                    case "frame":
                        sb.Append("frame");
                        break;

                    case "content":
                        sb.Append("content[@pixel_hash=" + node["pixel_hash"] + "]");

                        break;

                    default:
                        sb.Append(node["type"]);
                        break;

                }
            }

            int index = GetNodeIndex(node, parent);
            sb.Append("[" + index + "]");
        }
Пример #18
0
        private static void ToJsonHelper(MutableTree currnode, JObject jo)
        {
            foreach (string tagname in currnode._tags.Keys)
            {
                jo.Add(tagname, currnode._tags[tagname].ToString());
            }

            jo.Add("top", currnode.Top);
            jo.Add("left", currnode.Left);
            jo.Add("width", currnode.Width);
            jo.Add("height", currnode.Height);

            JArray array = new JArray();

            foreach (MutableTree child in currnode._children)
            {
                JObject childjo = new JObject();
                ToJsonHelper(child, childjo);
                array.Add(childjo);
            }

            jo.Add("children", array);
        }