public void TestSimpleInput() { var testDataTree = MakeTreeFromListofLists(new List <List <List <string> > >() { new List <List <string> >() { TagsTestClases.itemATags, TagsTestClases.itemBTags } }); var resultMetaData = new OSMListWithPaths(testDataTree); CollectionAssert.AreEquivalent(resultMetaData.items, expectedSimpleDataList); var catholicPaths = GetExpectedPathIndicesForTag(resultMetaData, expectedSimpleDataList[2]); // denom=catholic Assert.IsTrue(catholicPaths.Contains(itemAPath.ToString())); var futunaPaths = GetExpectedPathIndicesForTag(resultMetaData, expectedSimpleDataList[0]); // name=futuna Assert.IsTrue(futunaPaths.Contains(itemAPath.ToString())); var amenityPaths = GetExpectedPathIndicesForTag(resultMetaData, expectedSimpleDataList[3]); // amenity=* Assert.IsTrue(amenityPaths.Contains(itemAPath.ToString())); Assert.IsTrue(amenityPaths.Contains(itemBPath.ToString())); var buildingPaths = GetExpectedPathIndicesForTag(resultMetaData, expectedSimpleDataList[6]); // building=yes Assert.IsTrue(buildingPaths.Contains(itemAPath.ToString())); Assert.IsTrue(buildingPaths.Contains(itemBPath.ToString())); }
/// <summary> /// 返回一个路径中第n个数,从1开始 /// </summary> /// <param name="path"></param> /// <param name="index"></param> /// <returns></returns> public static int GetPathNumber(GH_Path path, int index) { string str = path.ToString(); char[] chr = str.ToArray(); int count = CountItem(chr, ';') + 1; int idx = index % count; if (idx == 0) { idx = count; } List <char> seperator_l = new List <char> { '{' }; for (int i = 0; i < count - 1; i++) { seperator_l.Add(';'); } seperator_l.Add('}'); char[] seperator = seperator_l.ToArray(); int number = int.Parse(str.Split(seperator)[idx]); return(number); }
public static GH_Structure <GH_String> DataTreeNaming(string name, GH_Structure <IGH_Goo> data) { // Output GH_Structure <GH_String> names = new GH_Structure <GH_String>(); // Paths var paths = data.Paths; // Make the output datatree with names for (int i = 0; i < data.Branches.Count; i++) { var branches = data.Branches[i]; GH_Path iPath = paths[i]; string pathString = iPath.ToString(); string newPath = pathString.Replace("{", "").Replace(";", "_").Replace("}", ""); for (int j = 0; j < branches.Count; j++) { string myName = name + "_" + newPath + "_" + j; GH_String converted = new GH_String(myName); names.Append(converted, iPath); } } return(names); }
protected override void SolveInstance(IGH_DataAccess DA) { //bool inputLock = false; string key = "Leopard(" + this.InstanceGuid + ")"; GH_Structure <IGH_GeometricGoo> inGeoTree; if (!DA.GetDataTree <IGH_GeometricGoo>(0, out inGeoTree)) { return; } //clear all stored selection if (resetStoredPath) { storedPath.Clear(); } //delete the preview baked objects if (!freezePreviewObjects) { RemovePreviewObjects(); } //generate the preview baked objects if (!inputLock && (generatePreview || !freezePreviewObjects)) { GeneratePreViewObjectsI(inGeoTree); // && !freezePreviewObjects) } //happens when unlock //if (addSelection) // SelectStoredPathObj(storedPath); GH_Structure <GH_String> pathTree = new GH_Structure <GH_String>(); //a tree that the data stored is its path for (int i = 0; i < inGeoTree.PathCount; i++) { string path = inGeoTree.Paths[i].ToString(); for (int j = 0; j < inGeoTree.Branches[i].Count; j++) { string str = path + "(" + j.ToString() + ")"; pathTree.Append(new GH_String(str)); } } List <string> pathOrder = new List <string>(); foreach (GH_String s in pathTree.AllData(false)) { pathOrder.Add(s.ToString()); } GH_Structure <GH_Integer> orderTree = new GH_Structure <GH_Integer>(); //a tree that the data is the order of each data, this tree is reference for sorting for (int i = 0; i < pathOrder.Count; i++) { string[] pathSeg; string indSeg; GH_Path.SplitPathLikeString(pathOrder[i], out pathSeg, out indSeg); int[] pInd = System.Array.ConvertAll(pathSeg, str => System.Convert.ToInt32(str)); int index = System.Convert.ToInt32(indSeg); orderTree.Insert(new GH_Integer(i), new GH_Path(pInd), index); } GH_Structure <IGH_Goo> outGeoTree = new GH_Structure <IGH_Goo>(); List <IGH_Goo> outGeoList = new List <IGH_Goo>(); GH_Structure <IGH_Goo> outPathTree = new GH_Structure <IGH_Goo>(); List <IGH_Goo> outPathList = new List <IGH_Goo>(); GH_Structure <IGH_Goo> outIndTree = new GH_Structure <IGH_Goo>(); List <IGH_Goo> outIndList = new List <IGH_Goo>(); GH_Structure <GH_Integer> outOrderTree = new GH_Structure <GH_Integer>(); List <GH_Integer> outOrderList = new List <GH_Integer>(); for (int i = 0; i < storedPath.Count; i++) { string p = pathOrder[System.Convert.ToInt32(storedPath[i])]; string[] pathSeg; string indSeg; if (GH_Path.SplitPathLikeString(p, out pathSeg, out indSeg)) { int[] pInd = System.Array.ConvertAll(pathSeg, str => System.Convert.ToInt32(str)); GH_Path path = new GH_Path(pInd); int index = System.Convert.ToInt32(indSeg); if (maintainPath) { outGeoTree.Append((IGH_GeometricGoo)inGeoTree.get_Branch(path)[index], path); outPathTree.Append(new GH_String(path.ToString()), path); outIndTree.Append(new GH_Integer(index), path); outOrderTree.Append((GH_Integer)(orderTree.get_Branch(path)[index]), path); } else { outGeoList.Add((IGH_GeometricGoo)inGeoTree.get_Branch(path)[index]); outPathList.Add(new GH_String(path.ToString())); outIndList.Add(new GH_Integer(index)); outOrderList.Add((GH_Integer)orderTree.get_Branch(path)[index]); } } } if (maintainPath) { if (this.sortByIndex) { outGeoTree = SortTreeByIndex(outGeoTree, outOrderTree); outIndTree = SortTreeByIndex(outIndTree, outOrderTree); } DA.SetDataTree(0, outGeoTree); DA.SetDataTree(1, outPathTree); DA.SetDataTree(2, outIndTree); DA.SetDataTree(3, outOrderTree); } else { if (this.sortByIndex) { List <IGH_Goo> gooCopy = outGeoList; outGeoList.Sort(new SelectCompareGoo(gooCopy, outOrderList)); gooCopy = outPathList; outPathList.Sort(new SelectCompareGoo(gooCopy, outOrderList)); gooCopy = outIndList; outIndList.Sort(new SelectCompareGoo(gooCopy, outOrderList)); } DA.SetDataList(0, outGeoList); DA.SetDataList(1, outPathList); DA.SetDataList(2, outIndList); DA.SetDataTree(3, outOrderTree); } }
protected override void SolveInstance(IGH_DataAccess DA) { int indexs = 0; if (!DA.GetData(1, ref indexs)) { return; } #region GH_Structure <IGH_Goo> x = (GH_Structure <IGH_Goo>) this.Params.Input[0].VolatileData; List <GH_Structure <IGH_Goo> > last = new List <GH_Structure <IGH_Goo> >(); List <int> contains = new List <int>(); for (int i = 0; i < x.PathCount; i++) { if (contains.Contains(i)) { continue; //重复的index } GH_Structure <IGH_Goo> tree = new GH_Structure <IGH_Goo>(); GH_Path path = x.get_Path(i); tree.AppendRange(x.Branches[i], path); contains.Add(i); string testPath = System.Text.RegularExpressions.Regex.Replace(path.ToString(), @";\d+}", ""); for (int q = 0; q < x.PathCount; q++) { if (i == q || contains.Contains(q)) { continue; } GH_Path path2 = x.get_Path(q); string testPath2 = System.Text.RegularExpressions.Regex.Replace(path2.ToString(), @";\d+}", ""); if (testPath == testPath2) { tree.AppendRange(x.Branches[q], path2); contains.Add(q); } } last.Add(tree); } NewTreePath = last.Count;/////// #endregion DA.DisableGapLogic(); if (DA.Iteration <= 0 && (x != null) && !x.IsEmpty) { if (last.Count > this.Params.Output.Count - 1 && this.Params.Output.Count > 1) { int count = newTreePath - this.Params.Output.Count + 1; string warning = string.Format("还有{0}个单一路径树形数据未显示在输出端", count); this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, warning); } int num2 = this.Params.Output.Count; for (int i = 1; i < num2; i++)///从第二项开始 { GH_Structure <IGH_Goo> structure2 = (GH_Structure <IGH_Goo>) this.Params.Output[i].VolatileData; structure2.Clear(); if (i > last.Count) { this.Params.Output[i].NickName = "-"; this.Params.Output[i].Name = "空数据"; this.Params.Output[i].Description = "无数据输出"; } else { string newName = System.Text.RegularExpressions.Regex.Replace(last[i - 1].get_Path(0).ToString(), @"\d+}", ""); this.Params.Output[i].NickName = newName + "xx}"; string newName2 = System.Text.RegularExpressions.Regex.Replace(this.Params.Output[i].NickName, @"\d+}", ""); this.Params.Output[i].Description = string.Format("路径类型:{0}", newName2); this.Params.Output[i].Name = "树形数据"; DA.SetDataTree(i, last[i - 1]); } } } if ((x != null) && !x.IsEmpty) { DA.SetDataTree(0, last[indexs]); } }
/// <summary> /// Updates the variable names in the data tree /// </summary> private void UpdateVariableNames() { // Check if it is a datatree with multiple branches that have one item bool check = true; for (int i = 0; i < _tree.Branches.Count; i++) { if (_tree.Branches[i].Count != 1) { check = false; break; } } if (_tree.Branches.Count == 1) { if (_tree.Branches[0].Count == 1) { // Do nothing: there is only one item in the whole datatree } else { // Only rename the items in this single branche with + "_0", "_1" etc... for (int i = 0; i < _tree.Branches[0].Count; i++) { _tree.Branches[0][i].Value.Name = _tree.Branches[0][i].Value.Name + "_" + i.ToString(); } } } else if (check == true) { // Multiple branches with only one item per branch for (int i = 0; i < _tree.Branches.Count; i++) { _tree.Branches[i][0].Value.Name = _tree.Branches[i][0].Value.Name + "_" + i.ToString(); } } else { // Rename everything. There are multiple branches with branches that have multiple items. List <GH_Path> originalPaths = new List <GH_Path>(); for (int i = 0; i < _tree.Paths.Count; i++) { originalPaths.Add(_tree.Paths[i]); } _tree.Simplify(GH_SimplificationMode.CollapseLeadingOverlaps); List <GH_Path> simplifiedPaths = new List <GH_Path>(); for (int i = 0; i < _tree.Paths.Count; i++) { simplifiedPaths.Add(_tree.Paths[i]); } for (int i = 0; i < _tree.Branches.Count; i++) { _tree.ReplacePath(simplifiedPaths[i], originalPaths[i]); } for (int i = 0; i < _tree.Branches.Count; i++) { GH_Path iPath = simplifiedPaths[i]; string pathString = iPath.ToString(); pathString = pathString.Replace("{", "").Replace(";", "_").Replace("}", ""); for (int j = 0; j < _tree.Branches[i].Count; j++) { _tree.Branches[i][j].Value.Name = _tree.Branches[i][j].Value.Name + "_" + pathString + "_" + j; } } } }