WrapperSample_ja - Houzkin/TreeStructures GitHub Wiki

ITreeNodeを実装していない、階層構造をなすオブジェクトのラッピングをする

ソースとなる階層構造をなすオブジェクト

public class OtherHierarchy{

	public OtherHierarchy(string name){ 
		Name = name;
	}
	public string Name { get; }

	public virtual IList<OtherHierarchy> Nests { get; } = new List<OtherHierarchy>();
}

public class ObservableOtherHierarchy : OtherHierarchy{

	public ObservableOtherHierarchy(string name):base(name){ }

	public override IList<OtherHierarchy> Nests { get; } = new ObservableCollection<OtherHierarchy>();
}

   階層構造をなすオブジェクトのラッパーを定義

public class OtherHierarchyWrapper : HierarchyWrapper<OtherHierarchy, OtherHierarchyWrapper> {

	public OtherHierarchyWrapper(OtherHierarchy composite) : base(composite) { }

	protected override IEnumerable<OtherHierarchy>? SourceChildren => Source.Nests;

	protected override OtherHierarchyWrapper GenerateChild(OtherHierarchy sourceChildNode) {
		return new OtherHierarchyWrapper(sourceChildNode);
	}

	public string SourceName => Source.Name;
}

Dictionaryから階層構造を組み立てて、ラップしてみます。

internal class SampleD {
	public static void Method(){
		
		var dic = new Dictionary<int[], string>() {
			[new int[] { }] = "A",
			[new int[] { 0 }] = "B",
			[new int[] { 0, 0 }] = "C",
			[new int[] { 0, 1 }] = "D",
			[new int[] { 1 }] = "E",
			[new int[] { 2 }] = "F",
			[new int[] { 2, 0 }]= "G",
		};
		//Assemble Hierarchy object
		var root = dic.AssembleTree(x=>new OtherHierarchy(x),(p,c)=>p.Nests.Add(c));

		//Wrapping
		var wrapRt = new OtherHierarchyWrapper(root);
		Console.WriteLine(wrapRt.ToTreeDiagram(x => x.SourceName));

screenshot 28

ソースとなる階層構造と連動させるにはコレクションがObservableである必要があるため変換します。

		//Assemble Observable Hierarchy object
		var obvableRt = wrapRt.Convert(x => new ObservableOtherHierarchy(x.SourceName), (p, c) => p.Nests.Add(c));
		// OR
		//var obvableRt =  dic.AssembleTree(x => new ObservableOtherHierarchy(x), (p, c) => p.Nests.Add(c));

		//Wrapping
		var wrappingObvableRt = new OtherHierarchyWrapper(obvableRt);
		Console.WriteLine("Before");
		Console.WriteLine(wrappingObvableRt.ToTreeDiagram(x=>x.SourceName));

		obvableRt.Nests.First().Nests.Clear();
		Console.WriteLine("After");
		Console.WriteLine(wrappingObvableRt.ToTreeDiagram(x => x.SourceName));

		//For simple data like in this example, you can also use the AsValuedTreeNode method.
		Console.WriteLine("used AsValuedTreeNode method");
		var valuedNode = (obvableRt as OtherHierarchy).AsValuedTreeNode(x => x.Nests, x => x.Name);
		Console.WriteLine(valuedNode.ToTreeDiagram(x => x.Value));

	}
}

screenshot 29

ラッピングする子孫ノードは遅延評価されます。

	public static void Method2(){
		var root = "ABCDEFG".ToCharArray().Select(x=>x.ToString())
			.AssembleAsNAryTree(2,x=>new NamedNode(){ Name = x });

		var wrpRt = root.AsValuedTreeNode(x=>x.Name);

		root.Preorder().First(x => x.Name == "B").TryRemoveOwn();

		Console.WriteLine(root.ToTreeDiagram(x => x.Name));
	}

screenshot 30

ITreeNodeを実装しているオブジェクトのラッピング

ITreeNodeを実装しているノードをラップする場合はTreeNodeWrapperを使用できます。
SourceChildrenプロパティをoverrideする手間を省けます。それ以外に違いはありません。

⚠️ **GitHub.com Fallback** ⚠️