Example #1
0
        /// <summary>
        /// Makes the unique name for a child, based on proposal and counter, formatted by limit digit width: e.g. if <c>limit</c> is 100, format is: D3, producing: <c>proposal</c>+001, +002, +003...
        /// </summary>
        /// <param name="parent">The parent for whom the child name is made</param>
        /// <param name="proposal">The proposal form, neither it already exist or not</param>
        /// <param name="limit">The limit: number of cycles to terminate the search</param>
        /// <param name="toSkip">To skip.</param>
        /// <param name="addNumberSufixForFirst">if set to <c>true</c> it adds number sufix even if it is the first child with proposed name</param>
        /// <returns>
        /// Unique name for new child in format: <c>proposal</c>001 up to <c>limit</c>
        /// </returns>
        public static String MakeUniqueChildName(this IGraphNode parent, String proposal, Int32 limit = 999, Int32 toSkip = 0, Boolean addNumberSufixForFirst = true)
        {
            String originalProposal = proposal;

            if (originalProposal == null)
            {
                originalProposal = "G";
            }

            limit = limit + toSkip;

            String format = "D" + limit.ToString().Length.ToString();

            Int32 c = toSkip;

            if (addNumberSufixForFirst)
            {
                c++;
                proposal = originalProposal + c.ToString(format);
            }

            while (parent.ContainsKey(proposal))
            {
                c++;
                proposal = originalProposal + c.ToString(format);
                if (c > limit)
                {
                    break;
                }
            }

            return(proposal);
        }
Example #2
0
        /// <summary>
        /// Builds graph defined by <c>path</c> or selecte existing graphnode, as pointed by path. Builds only one branch. Use <see cref="BuildGraphFromPaths{T}(IEnumerable{string}, string, bool, bool)"/> to construct complete graph from paths
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parent">Parent graph.</param>
        /// <param name="path">Path to construct from.</param>
        /// <param name="isAbsolutePath">if set to <c>true</c> [is absolute path] (that starts with leading /)</param>
        /// <param name="splitter">The splitter - by default: directory separator.</param>
        /// <param name="returnHead">if set to <c>true</c> if will return the created node</param>
        /// <returns>
        /// Leaf instance
        /// </returns>
        public static T ConvertPathToGraph <T>(this T parent, String path, Boolean isAbsolutePath = true, String splitter = "", Boolean returnHead = true) where T :  class, IGraphNode, new()
        {
            if (splitter == "")
            {
                splitter = System.IO.Path.DirectorySeparatorChar.ToString();
            }

            if (isAbsolutePath)
            {
                if (parent != null)
                {
                    if (!path.StartsWith(parent.path))
                    {
                        return(parent);
                    }
                    else
                    {
                        path = path.removeStartsWith(parent.path);
                    }
                }
            }

            List <string> pathParts = imbSciStringExtensions.SplitSmart(path, splitter);

            IGraphNode head = parent;

            foreach (string part in pathParts)
            {
                if (head == null)
                {
                    parent      = new T();
                    parent.name = part;
                    head        = parent;
                }
                else
                {
                    if (head.ContainsKey(part))
                    {
                        head = head[part];
                    }
                    else
                    {
                        T sp = new T();
                        sp.name = part;
                        if (head.Add(sp))
                        {
                            head = sp;
                        }
                        ;  //.Add(part, CAPTION_FOR_TUNNELFOLDER, CAPTION_FOR_TUNNELFOLDER);
                    }
                }
            }
            if (head == null)
            {
                return(null);
            }
            if (returnHead)
            {
                return((T)head);
            }
            else
            {
                return((T)head.root);
            }
        }