Mercurial > hg > Members > tatsuki > bench > jungle-core
changeset 26:075d6418e359
modified traverser
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/Children.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/Children.java Mon Jan 14 03:22:59 2013 +0900 @@ -1,6 +1,6 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core; -public interface Children extends Iterable<Node> +public interface Children<T extends Node> extends Iterable<T> { public int size(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/Node.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/Node.java Mon Jan 14 03:22:59 2013 +0900 @@ -1,7 +1,12 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core; -public interface Node +public interface Node<T extends Node<T>> { - public Children getChildren(); + public Children<T> getChildren(); public Attributes getAttributes(); } + +interface Container<T> +{ + +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/TraversableTree.java Sun Jan 13 20:52:19 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store; - -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Tree; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traversable; - -public interface TraversableTree extends Tree , Traversable -{ - -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/TreeEditor.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/TreeEditor.java Mon Jan 14 03:22:59 2013 +0900 @@ -7,7 +7,7 @@ public interface TreeEditor { - public TraversableTree getTree(); + public Tree getTree(); public Either<Error,TreeEditor> appendChild(NodePath _path,int _pos); public Either<Error,TreeEditor> deleteChild(NodePath _path,int _pos); public Either<Error,TreeEditor> putAttribute(NodePath _path,String _key,ByteBuffer _value);
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java Mon Jan 14 03:22:59 2013 +0900 @@ -69,8 +69,8 @@ } @Override - public Iterator<Node> iterator() + public Iterator<EditableNode> iterator() { - return (new IterableWrapper<Node>(children)).iterator(); + return (new IterableWrapper<EditableNode>(children)).iterator(); } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingChildren.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingChildren.java Mon Jan 14 03:22:59 2013 +0900 @@ -1,7 +1,6 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger; import java.util.Iterator; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.AppendChildAtOperation; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DeleteChildAtOperation; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation; @@ -30,9 +29,9 @@ } @Override - public Iterator<Node> iterator() + public Iterator<EditableNode> iterator() { - IterableWrapper<Node> wrapper = new IterableWrapper<Node>(this); + IterableWrapper<EditableNode> wrapper = new IterableWrapper<EditableNode>(this); return wrapper.iterator(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/EditableChildren.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/EditableChildren.java Mon Jan 14 03:22:59 2013 +0900 @@ -5,7 +5,7 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; -public interface EditableChildren extends Children +public interface EditableChildren extends Children<EditableNode> { public Either<Error,EditableNode> addNewChildAt(int _pos); public Either<Error,EditableNode> deleteChildAt(int _pos);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Children.java Mon Jan 14 03:22:59 2013 +0900 @@ -0,0 +1,6 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; + +public interface Children<T> extends Iterable<T> +{ + public int size(); +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultEvaluation.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultEvaluation.java Mon Jan 14 03:22:59 2013 +0900 @@ -1,24 +1,24 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; -public class DefaultEvaluation implements Evaluation +public class DefaultEvaluation<T extends Parent<T>> implements Evaluation<T> { - private final Evaluation.Result result; - private final TraverseEvaluator evaluator; + private final Result result; + private final Evaluator<T> evaluator; - public DefaultEvaluation(Evaluation.Result _result,TraverseEvaluator _evaluator) + public DefaultEvaluation(Result _result,Evaluator<T> _evaluator) { result = _result; evaluator = _evaluator; } @Override - public Evaluation.Result result() + public Result result() { return result; } @Override - public TraverseEvaluator evaluator() + public Evaluator<T> evaluator() { return evaluator; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultEvaluator.java Mon Jan 14 03:22:59 2013 +0900 @@ -0,0 +1,33 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair; + +public class DefaultEvaluator<T extends Parent<T>> implements Evaluator<T> +{ + private final NodePath path; + + public DefaultEvaluator(NodePath _path) + { + path = _path; + } + + @Override + public Evaluation<T> evaluate(T _current,int _pos) + { + Pair<Integer,NodePath> pop = path.pop(); + if(pop == null){ + return new DefaultEvaluation<T>(Result.BREAK,null); + } + + DefaultEvaluator<T> nextEvaluator = new DefaultEvaluator<T>(pop.right()); + int head = pop.left(); + + Result result = Result.CONTINUE; + if(head == _pos){ + result = Result.ACCEPT; + } + + return new DefaultEvaluation<T>(result,nextEvaluator); + } +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraversal.java Sun Jan 13 20:52:19 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; - -import java.util.Iterator; - -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; - -public class DefaultTraversal implements Traversal -{ - @Override - public Iterator<Node> iterator() - { - return null; - } - - @Override - public Node destination() - { - return null; - } -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverseEvaluator.java Sun Jan 13 20:52:19 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; - -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair; - -public class DefaultTraverseEvaluator implements TraverseEvaluator -{ - private final NodePath path; - - public DefaultTraverseEvaluator(NodePath _path) - { - path = _path; - } - - @Override - public Evaluation eval(Iterable<Node> _currentPosition,Node _child,int _pos) - { - Pair<Integer,NodePath> pop = path.pop(); - DefaultTraverseEvaluator nextEvaluator = new DefaultTraverseEvaluator(pop.right()); - int head = pop.left(); - - Evaluation.Result result = Evaluation.Result.DENY_CONTINUE; - if(head == _pos){ - result = Evaluation.Result.ACCEPT_BREAK; - } - - return new DefaultEvaluation(result,nextEvaluator); - } -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java Mon Jan 14 03:22:59 2013 +0900 @@ -1,84 +1,88 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; import java.util.Iterator; -import fj.P; -import fj.P2; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import fj.data.List; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Tree; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter.Converter; -public class DefaultTraverser implements Traverser +public class DefaultTraverser<T extends Parent<T>> implements Traverser<T> { @Override - public Iterable<Traversal> traverse(Tree _tree, TraverseEvaluator _evaluator) + public Either<Error,Traversal<T>> traverse(final T _root,Evaluator<T> _evaluator) { - Node root = _tree.getRoot(); - - List<Node> current = List.nil(); - TraverseEvaluator evaluator = _evaluator; - - List<List<Node>> totalResult = _traverse(current,root,evaluator); - - Converter<Traversal,List<Node>> converter = new IterableConverter.Converter<Traversal,List<Node>>(){ + Children<T> wrapper = new Children<T>(){ + @Override + public Iterator<T> iterator(){ + List<T> list = List.nil(); + return list.cons(_root).iterator(); + } @Override - public Traversal conv(final List<Node> _b) - { - Traversal t = new Traversal(){ - @Override - public Iterator<Node> iterator(){ - return _b.iterator(); - } + public int size(){ + return 1; + } + }; + + Either<Error,List<T>> ret = _traverse(wrapper,_evaluator); + if(ret.isA()){ + return DefaultEither.newA(ret.a()); + } + + final Iterable<T> iterable = ret.b(); + final T destination = ret.b().last(); + + Traversal<T> traversal = new Traversal<T>(){ + @Override + public Iterator<T> iterator(){ + return iterable.iterator(); + } - @Override - public Node destination() { - return _b.last(); - } - - }; - - return t; + @Override + public T destination(){ + return destination; } }; - return new IterableConverter<Traversal,List<Node>>(totalResult,converter); + return DefaultEither.newB(traversal); } - private List<List<Node>> _traverse(List<Node> _path,Node _current,TraverseEvaluator _evaluator) + private Either<Error,List<T>> _traverse(Children<T> _chs,Evaluator<T> _evaluator) { - List<Node> currentPath = _path.snoc(_current); - int pos = 0; - List<P2<Node,Evaluation>> accepted = List.nil(); - - for(Node child : _current.getChildren()){ - Evaluation e = _evaluator.eval(currentPath,child,pos); - Evaluation.Result result = e.result(); + for(T ch : _chs){ + Evaluation<T> e = _evaluator.evaluate(ch,pos); + Result r = e.result(); + if(r == Result.ACCEPT){ + return _accept(ch,e.evaluator()); + } - if(result == Evaluation.Result.ACCEPT_CONTINUE || - result == Evaluation.Result.ACCEPT_BREAK){ - accepted = accepted.snoc(P.p(child,e)); + if(r == Result.BREAK){ + break; + } + + if(r == Result.CONTINUE){ + continue; } - if(result == Evaluation.Result.DENY_BREAK || - result == Evaluation.Result.ACCEPT_BREAK){ - break; - } + return DefaultEither.newA(TraverserError.UNDEFINED_OPERATOR); } - List<List<Node>> totalResult = List.nil(); - for(P2<Node,Evaluation> next : accepted){ - Node node = next._1(); - TraverseEvaluator evaluator = next._2().evaluator(); - List<List<Node>> result = _traverse(currentPath,node,evaluator); - - - for(List<Node> list : result){ - totalResult = totalResult.snoc(list.cons(_current)); - } + List<T> empty = List.nil(); + Either<Error,List<T>> ret = DefaultEither.newB(empty); + return ret; + } + + private Either<Error,List<T>> _accept(T _current,Evaluator<T> _evaluator) + { + Children<T> chs = _current.children(); + Either<Error,List<T>> either = _traverse(chs,_evaluator); + if(either.isA()){ + return either; } - return totalResult; + List<T> list = either.b(); + List<T> newList = list.cons(_current); + return DefaultEither.newB(newList); } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Evaluation.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Evaluation.java Mon Jan 14 03:22:59 2013 +0900 @@ -1,7 +1,7 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; -public interface Evaluation +public interface Evaluation<T extends Parent<T>> { public Result result(); - public Evaluator evaluator(); + public Evaluator<T> evaluator(); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Evaluator.java Mon Jan 14 03:22:59 2013 +0900 @@ -0,0 +1,6 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; + +public interface Evaluator<T extends Parent<T>> +{ + public Evaluation<T> evaluate(T _current,int _pos); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Parent.java Mon Jan 14 03:22:59 2013 +0900 @@ -0,0 +1,6 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; + +public interface Parent<T> +{ + public Children<T> children(); +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Result.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Result.java Mon Jan 14 03:22:59 2013 +0900 @@ -6,4 +6,8 @@ public static final Result ACCEPT = new Result(); public static final Result CONTINUE = new Result(); public static final Result BREAK = new Result(); + + private Result() + { + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traversable.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traversable.java Mon Jan 14 03:22:59 2013 +0900 @@ -1,6 +1,8 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; -public interface Traversable +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; + +public interface Traversable<T extends Parent<T>> { - public Iterable<Traversal> traverse(TraverseEvaluator _e); + public Either<Error,Traversal<T>> traverse(Evaluator<T> _e); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traversal.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traversal.java Mon Jan 14 03:22:59 2013 +0900 @@ -1,8 +1,6 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; - -public interface Traversal<T extends Node> extends Iterable<T> +public interface Traversal<T extends Parent<T>> extends Iterable<T> { public T destination(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/TraverseEvaluator.java Sun Jan 13 20:52:19 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; - -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; - -public interface Evaluator -{ - public static final long ACCEPT_AND_CONTINUE = 0; - public static final long DENY_AND_CONTINUE = 1; - public static final long ACCEPT_AND_BREAK = 2; - public static final long DENY_AND_BREAK = 3; - - public Evaluation evaluate(Iterable<Node> _currentPosition,Node _child,int _pos); -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traverser.java Sun Jan 13 20:52:19 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traverser.java Mon Jan 14 03:22:59 2013 +0900 @@ -1,8 +1,9 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; -public interface Traverser<T extends Node> +public interface Traverser<T extends Parent<T>> { - public Traversal traverse(T _root,TraverseEvaluator _evaluator); + public Either<Error,Traversal<T>> traverse(T _root,Evaluator<T> _evaluator); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/TraverserError.java Mon Jan 14 03:22:59 2013 +0900 @@ -0,0 +1,9 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultError; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; + +public class TraverserError +{ + public static final Error UNDEFINED_OPERATOR = new DefaultError(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/util/Container.java Mon Jan 14 03:22:59 2013 +0900 @@ -0,0 +1,47 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util; + +import fj.data.List; + +public interface Container<T> +{ + public T get(); + public List<Container<T>> children(); +} + +interface Parent<T> +{ + public Children<T> children(); +} + +interface Children<T> extends Iterable<T> +{ + public int size(); +} + +interface Attribute +{ +} + +interface Node extends Parent<Node> +{ + public Attribute get(); +} + +class Hoge<T extends Parent<T>> +{ + public static void main(String _args[]) + { + + } + + + + public void visit(T _hoge) + { + Children<T> chs = _hoge.children(); + for(T ch : chs){ + visit(ch); + } + } +} +