/// <summary> /// Moves to new parent node <c>moveInto</c> /// </summary> /// <param name="graphToMove">The graph to move.</param> /// <param name="moveInto">The move into (future parent graph)</param> /// <param name="flags">The operation flags</param> /// <returns>Resulting graph, relevant in case of merging</returns> public static IGraphNode MoveTo(this IGraphNode graphToMove, IGraphNode moveInto, graphOperationFlag flags = graphOperationFlag.mergeOnSameName) { if (moveInto.getChildNames().Contains(graphToMove.name)) { if (flags.HasFlag(graphOperationFlag.mergeOnSameName)) { graphToMove.MergeWith(moveInto[graphToMove.name], flags); } else if (flags.HasFlag(graphOperationFlag.overwriteOnSameName)) { moveInto.RemoveByKey(graphToMove.name); moveInto.Add(graphToMove); } } else { moveInto.Add(graphToMove); } return(graphToMove); }
/// <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); } }