view src/main/csharp/jp.ac.u-ryukyu.ie.cr/jungle-main/store/impl/DefaultTreeEditor.cs @ 10:abe0c247f5a5

Add Network module. but, unComplete NetworkDefaultJungleTreeEditor.cs
author Kazuma Takeda <kazuma-arashi@hotmail.co.jp>
date Sun, 23 Oct 2016 07:40:50 +0900
parents
children
line wrap: on
line source

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DefaultTreeEditor : TreeEditor {
	private Traverser traverser;
	public DefaultTreeEditor(Traverser traverser){
		this.traverser = traverser;
	}

	public Either<Error,LoggingNode> edit(TreeNode root,NodePath path, NodeEditor editor){
		DefaultEvaluator e = new DefaultEvaluator (path);
		Either<Error, Traversal> either = traverser.traverse (root, e);

		if (either.isA ()) {
			return DefaultEither<Error, LoggingNode>.newA (either.a ());
		}
		Traversal t = either.b ();
		return clone (t, editor);
	}

	private Either<Error, LoggingNode> clone(Traversal t, NodeEditor editor){
		List<Direction<TreeNode>> path = new List<Direction<TreeNode>> ();

		foreach (Direction<TreeNode> direction in t) {
			path = path.addLast (direction);
		}

		Direction<TreeNode> targetDirection = path.headList ();
		TreeNode target = targetDirection.getTarget ();
		Either<Error, LoggingNode> either = editor.edit (target);
		if (either.isA ()) {
			return DefaultEither<Error, LoggingNode>.newA (either.a ());
		}

		LoggingNode newWrap = either.b ();

		int pos = targetDirection.getPosition ();
		TreeNode child = newWrap.getWrap ();

		foreach (Direction<TreeNode> parentDirection in path.deleteHead()) {
			TreeNodeChildren chs = parentDirection.getTarget ().getChildren ();

			Either<Error, TreeNode> ret = chs.replaceNode (pos, child);
			if (ret.isA ()) {
				return DefaultEither<Error, LoggingNode>.newA (ret.a ());
			}

			child = ret.b ();
			pos = parentDirection.getPosition ();
		}

		TreeNode newRoot = child;
		LoggingNode logNode = editor.wrap (newRoot, newWrap.getOperationLog ());
		return DefaultEither<Error, LoggingNode>.newB (logNode);

	}

}