Example #1
0
File: Tree.cs Project: nunb/code
        public static Tree GetParent(Tree node, Tree root)
        {
            if (root == null || node == null)
            {
                return(null);
            }

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

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


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

            return(null);
        }
		/// <summary>
		/// Finds the unpredictable content using the backround regions of elements.
		/// </summary>
		/// <param name="node"></param>
		/// <param name="image"></param>
		/// <param name="background"></param>
		/// <param name="isForeground"></param>
		private static List<Tree> FindAndAddContent(InterpretArgs args, Tree node, Bitmap image, Bitmap isForeground){
			Ptype ptype = (Ptype)node["ptype"];
			List<Tree> allFound = new List<Tree>();
			if(ptype != null){
				ptype.Model.Finder.SetForeground(node, image, isForeground);
			}
			//recurse. if no siblings are overlapping (the majority case) then we can run each child in parallel.
//        if (!anyOverlapping(node.children())){
//            List<Future<ICollection<Tree>>> asyncResults = new List<Future<ICollection<Tree>>>();
//            for(final Tree child : node.children()){
//                Callable<ICollection<Tree>> callable = new Callable<ICollection<Tree>>(){
//                   public ICollection<Tree> call(){
//                        return findAndAddContent(args, ptype, child, image, isForeground);
//                  }
//                };
//                Future<ICollection<Tree>> results =  Utils.service.submit(callable);
//                asyncResults.add(results);
//            }
//
//            for(Future<ICollection<Tree>> res : asyncResults){
//
//                try {
//                    allFound.addAll(res.get());
//                } catch (InterruptedException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                } catch (ExecutionException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                }
//            }
//        }
//        else //we can't run children in parallel if any nodes overlap.
//        //nodes only overlap when there's a false positive. so we might be able
//        //to trigger something here and automatically correct that.
//        {
			foreach (Tree child in node.GetChildren())
				FindAndAddContent(args, child, image, isForeground);


			if(ptype != null){
				ptype.Model.Finder.FindContent(node, image, isForeground, allFound);
				foreach(Tree found  in allFound)
					args.SetAncestor(found, node);

                allFound.AddRange(node.GetChildren());
                PrototypeDetectionLayer.SetAncestorsByContainment(allFound, args);
			}



			// }

			return allFound;
		}
Example #3
0
File: Tree.cs Project: nunb/code
            private GraphNode GetGraphCorrespondingToTree(Tree treeNode, Dictionary <Tree, GraphNode> graphNodesByCorrespondingTree)
            {
                GraphNode correspondingNode = null;

                if (!graphNodesByCorrespondingTree.TryGetValue(treeNode, out correspondingNode))
                {
                    correspondingNode = new GraphNode();
                    correspondingNode.CorrespondingTreeNode = treeNode;
                    graphNodesByCorrespondingTree[treeNode] = correspondingNode;
                    foreach (Tree child in treeNode.GetChildren())
                    {
                        GraphNode childGraphNode = GetGraphCorrespondingToTree(child, graphNodesByCorrespondingTree);
                        if (IsToDelete(child))
                        {
                            correspondingNode.Neighbors.AddRange(childGraphNode.Neighbors);
                        }
                        else
                        {
                            correspondingNode.Neighbors.Add(childGraphNode);
                        }
                    }
                }

                return(correspondingNode);
            }
Example #4
0
        public ViewableTreeNode(Tree node)
        {
            Node = node;
            Children = new BindingList<ViewableTreeNode>();
            foreach (Tree child in node.GetChildren())
            {
                Children.Add(new ViewableTreeNode(child));
            }

            if (node.HasTag("type")){

                Id = "{x=" + node.Left + ", y=" + node.Top + ", width=" + node.Width + ", height=" + node.Height + "}";

                //switch(node["type"].ToString()){

                //    case "ptype":
                //        Id = ((string)node["ptype_id"]);
                //        break;

                //    default:
                //        Id = node["type"].ToString();
                //        break;
                //}
                

            }

            else
                Id = "node";
        }
Example #5
0
File: Tree.cs Project: nunb/code
 public static void AddNodesToCollection(Tree tree, ICollection <Tree> toadd)
 {
     toadd.Add(tree);
     foreach (Tree child in tree.GetChildren())
     {
         AddNodesToCollection(child, toadd);
     }
 }
Example #6
0
        private void InterpretHelper(InterpretArgs args, Tree currNode)
        {
            if(currNode.Height > 3 && currNode["type"] != null && currNode["type"].Equals("content"))
            {
                args.Tag(currNode, "is_text", true);
            }

            foreach (Tree child in currNode.GetChildren())
                InterpretHelper(args, child);
        }
Example #7
0
        public static Tree GetParent(Tree node, Tree root)
        {
            if (root == null || node == null)
                return null;

            if (root == node)
                return null;

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

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

            return null;
        }
Example #8
0
        private void InterpretHelper(InterpretArgs args, Tree node)
        {
            if (node.ContainsTag("type") && node["type"].Equals("ptype"))
            {
                args.Tag(node, "is_target", "true");
            }
            else if (node.ContainsTag("type") && !node["type"].Equals("ptype"))
            {
                foreach (Tree child in node.GetChildren())
                {
                    if (child.ContainsTag("type") && !child["type"].Equals("feature") && child.GetChildren().Count() == 0)
                    {
                        args.Tag(child, "is_target", "true");
                    }
                }
            }

            foreach (Tree child in node.GetChildren())
                InterpretHelper(args, child);
        }
Example #9
0
		private Tree(Tree toCopy)
		{
			_tags = new Dictionary<string, object>();
			foreach (var item in toCopy.GetTags())
				_tags.Add (item.Key, item.Value);

			Occurrence = toCopy.Occurrence;

			List<Tree> children = new List<Tree>();
			foreach (Tree child in toCopy.GetChildren())
			{
				children.Add(new Tree(child));
			}

			_children = children;

		}
Example #10
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);
        }
Example #11
0
        public void InterpretHelper(InterpretArgs args, Tree currNode)
        {
            IEnumerable<Tree> textChildren = currNode.GetChildren().Where(c => c.ContainsTag("is_text") && (bool)c["is_text"]);

            List<Tree> readingOrder = SortNodesInReadingOrder(textChildren);

            for (int i = 0; i < readingOrder.Count - 1; i++)
            {
                Tree curr = readingOrder[i];
                Tree next = readingOrder[i + 1];

                args.Tag(curr, "group_next", GroupNext(curr, next));
            }

            //recurse
            foreach (Tree child in currNode.GetChildren())
                InterpretHelper(args, child);
        }
Example #12
0
File: Tree.cs Project: nunb/code
        public static IEnumerable <Tree> GetSiblings(Tree node, Tree root)
        {
            Tree        parent   = GetParent(node, root);
            List <Tree> siblings = new List <Tree>();

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

            return(siblings);
        }
Example #13
0
        private static Tree MatchingNode(Tree tree, IBoundingBox region)
        {
            if (BoundingBox.Equals(region, tree))
            {
                return(tree);
            }


            foreach (Tree child in tree.GetChildren())
            {
                if (BoundingBox.IsInsideInclusive(region, child))
                {
                    return(MatchingNode(child, region));
                }
            }

            return(null);
        }
Example #14
0
File: Tree.cs Project: nunb/code
        private Tree(Tree toCopy)
        {
            _tags = new Dictionary <string, object>();
            foreach (var item in toCopy.GetTags())
            {
                _tags.Add(item.Key, item.Value);
            }

            Occurrence = toCopy.Occurrence;

            List <Tree> children = new List <Tree>();

            foreach (Tree child in toCopy.GetChildren())
            {
                children.Add(new Tree(child));
            }

            _children = children;
        }
		private static Tree MatchingNode(Tree tree, IBoundingBox region)
		{
			if (BoundingBox.Equals(region, tree))
				return tree;


			foreach (Tree child in tree.GetChildren())
			{
				if (BoundingBox.IsInsideInclusive(region, child))
					return MatchingNode(child, region);
			}

			return null;
		}
Example #16
0
        private static bool TestLeaf(Tree node, string value)
        {
            bool boolValue = false;
            bool couldParse = bool.TryParse(value, out boolValue);

            if (!couldParse)
                return false;

            bool isLeaf = ( !(node.HasTag("type") && node["type"].Equals("frame")) && node.GetChildren().Count() == 0 );

            return boolValue == isLeaf;
        }
Example #17
0
        private void GetNodesWithMatchingBoundingBox(Tree currNode, IBoundingBox bb, List<Tree> matches)
        {
            if (BoundingBox.Equals(currNode, bb))
                matches.Add(currNode);

            foreach (Tree child in currNode.GetChildren())
                GetNodesWithMatchingBoundingBox(child, bb, matches);

            
        }
Example #18
0
        private Tree GetNodeUnder(int x, int y, Tree tree)
        {
            if (BoundingBox.Contains(tree, x, y))
            {
                foreach (Tree child in tree.GetChildren())
                {
                    Tree smaller = GetNodeUnder(x, y, child);
                    if (smaller != null)
                        return smaller;
                }

                return tree;
            }

            return null;
        }
Example #19
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;
        }
Example #20
0
        private static int GetNodeIndex(Tree node, Tree parent)
        {
            int index = 0;

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

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

            if(parent != null){
                var children = parent.GetChildren();
                List<Tree> sorted = new List<Tree>(children);
                sorted.Sort(BoundingBox.CompareByTopThenLeft);
                foreach(Tree sibling in sorted){
                    if(sibling == node)
                        return index;
                    else{
                        string sibtype = "none";
                        if(sibling.HasTag("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;
        }
        private static void GetFeaturesFromTree(Tree tree, List<Tree> features)
        {
            if (tree.HasTag("type") && tree["type"].Equals("feature"))
                features.Add(tree);

            foreach (Tree child in tree.GetChildren())
                GetFeaturesFromTree(child, features);
        }
Example #22
0
		public static void AddNodesToCollection(Tree tree, ICollection<Tree> toadd)
		{

			toadd.Add(tree);
			foreach (Tree child in tree.GetChildren())
				AddNodesToCollection(child, toadd);
		}
Example #23
0
        private void InterpretHelper(InterpretArgs args, Tree currNode)
        {
            var children = currNode.GetChildren().Where( n => n["is_text"] != null && (bool)n["is_text"]);
            var sorted = GroupNextLayer.SortNodesInReadingOrder(children);

            bool createNew = true;
            List<Tree> togroup = new List<Tree>();
            for (int i = 1; i < sorted.Count; i++ )
            {

                Tree currChild = sorted[i];
                Tree prevChild = sorted[i - 1];
                if (prevChild["group_next"] != null && (bool)prevChild["group_next"])
                {
                    if (createNew)
                    {
                        createNew = false;
                        togroup.Add(prevChild);
                    }

                    togroup.Add(currChild);
                }
                else
                {
                    if (togroup.Count > 0)
                    {
                        IBoundingBox bounding = BoundingBox.Union(togroup);
                        var tags = new Dictionary<string, object>();
                        tags["type"] = "grouped_text";
                        tags["is_text"] = true;
                        Tree grouped = Tree.FromBoundingBox(bounding, tags);

                        foreach (Tree t in togroup)
                            args.SetAncestor(t, grouped);

                        args.SetAncestor(grouped, currNode);

                        togroup.Clear();
                    }

                    createNew = true;

                }
            }

            if (togroup.Count > 0)
            {
                IBoundingBox bounding = BoundingBox.Union(togroup);
                var tags = new Dictionary<string, object>();
                tags["type"] = "grouped_text";
                tags["is_text"] = true;
                Tree grouped = Tree.FromBoundingBox(bounding, tags);

                foreach (Tree t in togroup)
                    args.SetAncestor(t, grouped);

                args.SetAncestor(grouped, currNode);

                togroup.Clear();
            }

            foreach (Tree child in currNode.GetChildren())
                InterpretHelper(args, child);
        }
        private static Tree GetClosestHelper(int left, int top, Tree node, out double closestDistance)
        {
            double currBestDist = double.MaxValue;
            Tree closestTarget = null;
            if(node.HasTag("is_target") && node["is_target"].ToString().ToLower().Equals("true") 
                && (!node.HasTag("is_minor") || node["is_minor"].ToString().ToLower().Equals("false") || Keyboard.IsKeyDown(Key.LeftCtrl) ) )
            {
                currBestDist = DistanceBetweenPointAndRectangle(left, top, node);
                closestTarget = node;
            }

            

            double bestChildDist = double.MaxValue;
            Tree closestChild = null;

            foreach (Tree child in node.GetChildren())
            {
                double childDist = double.MaxValue;
                Tree childCandidate = GetClosestHelper(left, top, child, out childDist);
                if (childCandidate != null && childDist < bestChildDist)
                {
                    bestChildDist = childDist;
                    closestChild = childCandidate;
                }
            }

            if (bestChildDist < currBestDist)
            {
                closestTarget = closestChild;
                currBestDist = bestChildDist;
            }

            closestDistance = currBestDist;

            return closestTarget;
        }
Example #25
0
			private GraphNode GetGraphCorrespondingToTree(Tree treeNode, Dictionary<Tree, GraphNode> graphNodesByCorrespondingTree)
			{
				GraphNode correspondingNode = null;
				if (!graphNodesByCorrespondingTree.TryGetValue(treeNode, out correspondingNode))
				{
					correspondingNode = new GraphNode();
					correspondingNode.CorrespondingTreeNode = treeNode;
					graphNodesByCorrespondingTree[treeNode] = correspondingNode;
					foreach (Tree child in treeNode.GetChildren())
					{
						GraphNode childGraphNode = GetGraphCorrespondingToTree(child, graphNodesByCorrespondingTree);
                        if (IsToDelete(child))
                        {
                            correspondingNode.Neighbors.AddRange(childGraphNode.Neighbors);
                        }else
						    correspondingNode.Neighbors.Add(childGraphNode);
					}
				}

				return correspondingNode;
			}
Example #26
0
        private void InterpretHelper(InterpretArgs args, Tree node)
        {
            string path = PathDescriptor.GetPath(node, args.Tree);

            JObject data = (JObject)_runtimeIntent.GetData(path);

            if(data != null)
            {
                foreach (var key in data.Properties())
                {
                    bool parsedbool = false;

                    if (bool.TryParse(data[key.Name].Value<string>(), out parsedbool))
                    {
                        args.Tag(node, key.Name, parsedbool);
                    }else{
                        args.Tag(node, key.Name, data[key.Name].Value<string>());
                    }

                }
            }

            foreach (Tree child in node.GetChildren())
            {
                InterpretHelper(args, child);
            }
        }