/// <summary> /// Vraca sve grane /// </summary> /// <param name="parentNode"></param> /// <returns></returns> public static List <imbTreeNodeBranch> allBranches(this imbTreeNodeBranch parentNode) { List <imbTreeNodeBranch> output = new List <imbTreeNodeBranch>(); IEnumerable <imbTreeNode> branchNodes = parentNode.items.Values.Where(x => x is imbTreeNodeBranch); branchNodes.ToList().ForEach(x => output.Add(x as imbTreeNodeBranch)); return(output); }
private void imbTreeNode_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (this is imbTreeNodeBranch) { imbTreeNodeBranch parentBranch = this as imbTreeNodeBranch; switch (e.Action) { case System.Collections.Specialized.NotifyCollectionChangedAction.Add: foreach (imbTreeNode item in e.NewItems) { item.parent = parentBranch; } break; case System.Collections.Specialized.NotifyCollectionChangedAction.Move: break; case System.Collections.Specialized.NotifyCollectionChangedAction.Remove: foreach (imbTreeNode item in e.OldItems) { item.parent = null; } break; case System.Collections.Specialized.NotifyCollectionChangedAction.Replace: foreach (imbTreeNode item in e.NewItems) { item.parent = parentBranch; } foreach (imbTreeNode item in e.OldItems) { item.parent = null; } break; case System.Collections.Specialized.NotifyCollectionChangedAction.Reset: break; } } else { Exception ex = new ArgumentOutOfRangeException("imnCollectionMeta operations are forbiden to non branch tree object"); throw ex; } }
/// <summary> /// Vraca sve node-ove koji ispunjavaju uslove upita date u imbTreeNodeType i imbTreeQueryFlag enumima /// </summary> /// <param name="parentNode">Node nad kojim se vrsi upit</param> /// <param name="depthLimit">Limit koliko duboko moze da ide upit</param> /// <param name="_flags">imbTreeNodeType i imbTreeQueryFlag enumi kojima se podesava upit</param> /// <returns>Kolekcija</returns> public static imbTreeNodeCollection query(this imbTreeNodeBranch parentNode, Int32 depthLimit, params Object[] _flags) { imbTreeQueryFlag flags = _flags.getFirstOfType <imbTreeQueryFlag>(); // = new imbTreeQueryFlags(_flags); imbTreeNodeType types = _flags.getFirstOfType <imbTreeNodeType>(); // = new imbTreeNodeTypes(_flags); imbTreeNodeCollection output = new imbTreeNodeCollection(); Int32 i = 0; List <imbTreeNode> toDo = new List <imbTreeNode>(); List <imbTreeNode> newToDo = new List <imbTreeNode>(); toDo.Add(parentNode); do { if (i > depthLimit) { //logSystem.log("Depth limit reached on imbTreeQuery :: ", logType.ExecutionError); break; } newToDo = new List <imbTreeNode>(); foreach (imbTreeNode nd in toDo) { if (nd.testNode(flags, types)) { output.Add(nd.path, nd); } if (nd is imbTreeNodeBranch) { imbTreeNodeBranch bnd = nd as imbTreeNodeBranch; foreach (var t in bnd) { newToDo.Add(t.Value); } } } i++; toDo = newToDo; } while (toDo.Count > 0); return(output); }
/// <summary> /// Dodaje novi "list" -- krajnji element u strukturi koji obicno nosi i vrednost -- pravi strukturu koja je neophodna da bi ova grana profukncionisala /// </summary> /// <param name="leafNameOfPath">Putanja se prosledjuje u sourcePath</param> /// <param name="value"></param> /// <param name="report"></param> /// <returns></returns> public imbTreeNodeLeaf AddNewLeaf(String leafNameOfPath, Object value, ITextRender report = null, String __sourceContent = "") { String __sourcePath = leafNameOfPath; pathSegments psq = new pathSegments(); String branchName = ""; psq.deployPath(leafNameOfPath, pathResolveFlag.autorenameCollectionIndexer); String leafName = psq.lastSegment.needle; //leafNameOfPath; if (psq.Count > 1) { psq.Remove(psq.lastSegment); branchName = psq.ToString(); } imbTreeNodeBranch head = this; if (!String.IsNullOrEmpty(branchName)) { head = AddNewBranch(branchName, report); } if (head == null) { head = this; } if (report != null) { //report.AppendLine("Adding new leaf: " + leafName + " inside: " + head.path); } imbTreeNodeLeaf newLeaf = new imbTreeNodeLeaf(leafName, value); newLeaf.sourcePath = __sourcePath; newLeaf.sourceContent = __sourceContent; head.Add(newLeaf); return(newLeaf); }
/// <summary> /// Pokrece detektovanje tipova /// </summary> /// <param name="parentNode"></param> /// <param name="depthLimit"></param> /// <param name="_flags"></param> public static void detectTypes(this imbTreeNodeBranch parentNode, Int32 depthLimit) { Int32 i = 0; List <imbTreeNode> toDo = new List <imbTreeNode>(); List <imbTreeNode> newToDo = new List <imbTreeNode>(); toDo.Add(parentNode); do { if (i > depthLimit) { // logSystem.log("Depth limit reached on imbTreeQuery :: ", logType.ExecutionError); break; } newToDo = new List <imbTreeNode>(); foreach (imbTreeNode nd in toDo) { if (nd.type == imbTreeNodeType.unknown) { nd.type = nd.nodeType(); } if (nd is imbTreeNodeBranch) { imbTreeNodeBranch bnd = nd as imbTreeNodeBranch; foreach (var t in bnd) { newToDo.Add(t.Value); } } } i++; toDo = newToDo; } while (toDo.Count > 0); //return output; }
/// <summary> /// Compresses the nodes. /// </summary> /// <param name="parentNode">The parent node.</param> /// <param name="iterationLimit">The iteration limit.</param> public static void compressNodes(this imbTreeNodeBranch parentNode, Int32 iterationLimit = 10000) { Int32 i = 0; List <imbTreeNode> toDo = new List <imbTreeNode>(); List <imbTreeNode> newToDo = new List <imbTreeNode>(); toDo.Add(parentNode); do { if (i > iterationLimit) { throw new ArgumentOutOfRangeException("imbTreeQuery.compressNodes(" + parentNode.keyHash + " ==> " + parentNode.sourcePath + ")"); break; } newToDo = new List <imbTreeNode>(); foreach (imbTreeNode nd in toDo) { nd.compressNode(); } foreach (imbTreeNode nd in toDo) { if (nd is imbTreeNodeBranch) { imbTreeNodeBranch bnd = nd as imbTreeNodeBranch; foreach (var t in bnd) { newToDo.Add(t.Value); } } } i++; toDo = newToDo; } while (toDo.Count > 0); }
/// <summary> /// Pravi novu granu - ili strukturu pod grana -- ako je prosledjena putanja umesto obicnog imena /// </summary> /// <param name="branchName"></param> /// <returns></returns> public imbTreeNodeBranch AddNewBranch(String branchNameOrPath, ITextRender report = null) { sourcePath = branchNameOrPath; pathResolverResult query = this.resolvePath(branchNameOrPath, pathResolveFlag.autorenameCollectionIndexer); imbTreeNodeBranch head = null; switch (query.type) { case pathResolverResultType.foundOne: case pathResolverResultType.foundMany: //head = query.nodeFound. as imbTreeNodeBranch; head = query.nodeFound.imbFirstSafe() as imbTreeNodeBranch; /* * Exception ex = new aceGeneralException("Branch is already found at: " + branchNameOrPath); * * var isb = new imbStringBuilder(0); * isb.AppendLine("Can't add new branch on place where the one already exists error"); * isb.AppendPair("Target is: ", this.toStringSafe()); * devNoteManager.note(this, ex, isb.ToString(), "Can't add new branch on place where the one already exists", devNoteType.unknown); */ break; default: case pathResolverResultType.nothingFound: case pathResolverResultType.folderFoundButItemMissing: case pathResolverResultType.folderFoundButFoldersMissing: imbTreeNode nd = query.nodeFound.imbFirstSafe() as imbTreeNode; if (nd is imbTreeNodeLeaf) { head = nd.parent; imbTreeNodeBranch newBranch = new imbTreeNodeBranch(nd.name); newBranch.learnFrom(nd); head.Remove(nd.keyHash); head.Add(newBranch); head = newBranch; if (report != null) { //report.AppendPair("replacing existing Leaf node with branch one", head.name); } } else { head = nd as imbTreeNodeBranch; } if (head == null) { head = this; } foreach (pathSegment ps in query.missing) { imbTreeNodeBranch newBranch = new imbTreeNodeBranch(ps.needle); head.Add(newBranch); if (report != null) { //report.AppendPair("Add new node to [" + head.path + "] : ", newBranch.name); } head = newBranch; } break; } return(head); }
/// <summary> /// Detektuje vrstu nodea /// </summary> /// <returns></returns> public imbTreeNodeType nodeType() { if (type != imbTreeNodeType.unknown) { return(type); } imbTreeNodeBranch this_imbTreeNodeBranch = (imbTreeNodeBranch)this; if (this_imbTreeNodeBranch.Count == 0) { return(imbTreeNodeType.dry); } List <imbTreeNodeBranch> subBranches = this_imbTreeNodeBranch.allBranches(); if (subBranches.Count == 0) { return(imbTreeNodeType.end); } else { #pragma warning disable CS0184 // The given expression is never of the provided ('imbTreeNodeLeaf') type if (this_imbTreeNodeBranch.Any(x => x is imbTreeNodeLeaf)) #pragma warning restore CS0184 // The given expression is never of the provided ('imbTreeNodeLeaf') type { return(imbTreeNodeType.leafed); } } if (this_imbTreeNodeBranch.parent.Count == 1) { if (this_imbTreeNodeBranch.Count == 1) { if (this_imbTreeNodeBranch.First().Value is imbTreeNodeBranch) { return(imbTreeNodeType.main); } } else { #pragma warning disable CS0184 // The given expression is never of the provided ('imbTreeNodeBranch') type if (this_imbTreeNodeBranch.All(x => x is imbTreeNodeBranch)) #pragma warning restore CS0184 // The given expression is never of the provided ('imbTreeNodeBranch') type { return(imbTreeNodeType.lateralFirst); } } } foreach (imbTreeNodeBranch subs in subBranches) { #pragma warning disable CS0184 // The given expression is never of the provided ('imbTreeNodeLeaf') type if (subs.Any(x => x is imbTreeNodeLeaf)) #pragma warning restore CS0184 // The given expression is never of the provided ('imbTreeNodeLeaf') type { return(imbTreeNodeType.lateralLast); } } return(imbTreeNodeType.lateral); }
/// <summary> /// Pravi "flat" formu iz hijerarhije /// </summary> /// <param name="parentNode"></param> /// <returns></returns> public static imbTreeNodeBlockCollection breakToBlocks(this imbTreeNodeBranch parentNode) { imbTreeNodeBlockCollection output = new imbTreeNodeBlockCollection(); Int32 i = 0; List <imbTreeNode> toDo = new List <imbTreeNode>(); List <imbTreeNode> newToDo = new List <imbTreeNode>(); toDo.Add(parentNode); if (parentNode == null) { throw new ArgumentNullException("parentNode", "parentNode is [null], have no content to break to blocks"); return(output); } //while (i < iterationLimit) //{ do { if (i > iterationLimit) { throw new ArgumentOutOfRangeException("Depth limit reached on imbTreeQuery :: logType.ExecutionError"); break; } newToDo = new List <imbTreeNode>(); output.newBlock(); //output.newBlock(); foreach (imbTreeNode nd in toDo) { switch (nd.type) { default: imbTreeNodeBranch bnd2 = nd as imbTreeNodeBranch; foreach (var t in bnd2) { newToDo.Add(t.Value); } break; case imbTreeNodeType.leafed: /// ovo je privremeno iskljuceno imbTreeNodeBranch bnd = nd as imbTreeNodeBranch; foreach (var t in bnd) { newToDo.Add(t.Value); } break; case imbTreeNodeType.leaf: // output.current.Add(nd); if (nd.Any()) { output.current.Add(nd); } else if (!nd.sourceContent.Trim().isNullOrEmpty()) { output.current.Add(nd); } else { } break; case imbTreeNodeType.end: imbTreeNodeBlock blc = output.newBlock(nd.path); imbTreeNodeBranch end = nd as imbTreeNodeBranch; foreach (var t in end) { blc.Add(t); } break; } } toDo = newToDo; i++; } while (toDo.Count > 0); //} output.removeEmptyBlocks(); return(output); }