# HG changeset patch # User Shoshi TAMAKI # Date 1358101379 -32400 # Node ID 075d6418e35948e0e2888bca72b0db07cc7b0e60 # Parent 1b448f9249ee9aa73f5953b00616078e4eeb6869 modified traverser diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/Children.java --- 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 +public interface Children extends Iterable { public int size(); } diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/Node.java --- 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> { - public Children getChildren(); + public Children getChildren(); public Attributes getAttributes(); } + +interface Container +{ + +} diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/TraversableTree.java --- 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 -{ - -} diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/TreeEditor.java --- 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 appendChild(NodePath _path,int _pos); public Either deleteChild(NodePath _path,int _pos); public Either putAttribute(NodePath _path,String _key,ByteBuffer _value); diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java --- 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 iterator() + public Iterator iterator() { - return (new IterableWrapper(children)).iterator(); + return (new IterableWrapper(children)).iterator(); } } diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingChildren.java --- 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 iterator() + public Iterator iterator() { - IterableWrapper wrapper = new IterableWrapper(this); + IterableWrapper wrapper = new IterableWrapper(this); return wrapper.iterator(); } diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/EditableChildren.java --- 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 { public Either addNewChildAt(int _pos); public Either deleteChildAt(int _pos); diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Children.java --- /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 extends Iterable +{ + public int size(); +} diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultEvaluation.java --- 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> implements Evaluation { - private final Evaluation.Result result; - private final TraverseEvaluator evaluator; + private final Result result; + private final Evaluator evaluator; - public DefaultEvaluation(Evaluation.Result _result,TraverseEvaluator _evaluator) + public DefaultEvaluation(Result _result,Evaluator _evaluator) { result = _result; evaluator = _evaluator; } @Override - public Evaluation.Result result() + public Result result() { return result; } @Override - public TraverseEvaluator evaluator() + public Evaluator evaluator() { return evaluator; } diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultEvaluator.java --- /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> implements Evaluator +{ + private final NodePath path; + + public DefaultEvaluator(NodePath _path) + { + path = _path; + } + + @Override + public Evaluation evaluate(T _current,int _pos) + { + Pair pop = path.pop(); + if(pop == null){ + return new DefaultEvaluation(Result.BREAK,null); + } + + DefaultEvaluator nextEvaluator = new DefaultEvaluator(pop.right()); + int head = pop.left(); + + Result result = Result.CONTINUE; + if(head == _pos){ + result = Result.ACCEPT; + } + + return new DefaultEvaluation(result,nextEvaluator); + } +} diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraversal.java --- 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 iterator() - { - return null; - } - - @Override - public Node destination() - { - return null; - } -} diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverseEvaluator.java --- 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 _currentPosition,Node _child,int _pos) - { - Pair 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); - } -} diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java --- 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> implements Traverser { @Override - public Iterable traverse(Tree _tree, TraverseEvaluator _evaluator) + public Either> traverse(final T _root,Evaluator _evaluator) { - Node root = _tree.getRoot(); - - List current = List.nil(); - TraverseEvaluator evaluator = _evaluator; - - List> totalResult = _traverse(current,root,evaluator); - - Converter> converter = new IterableConverter.Converter>(){ + Children wrapper = new Children(){ + @Override + public Iterator iterator(){ + List list = List.nil(); + return list.cons(_root).iterator(); + } @Override - public Traversal conv(final List _b) - { - Traversal t = new Traversal(){ - @Override - public Iterator iterator(){ - return _b.iterator(); - } + public int size(){ + return 1; + } + }; + + Either> ret = _traverse(wrapper,_evaluator); + if(ret.isA()){ + return DefaultEither.newA(ret.a()); + } + + final Iterable iterable = ret.b(); + final T destination = ret.b().last(); + + Traversal traversal = new Traversal(){ + @Override + public Iterator iterator(){ + return iterable.iterator(); + } - @Override - public Node destination() { - return _b.last(); - } - - }; - - return t; + @Override + public T destination(){ + return destination; } }; - return new IterableConverter>(totalResult,converter); + return DefaultEither.newB(traversal); } - private List> _traverse(List _path,Node _current,TraverseEvaluator _evaluator) + private Either> _traverse(Children _chs,Evaluator _evaluator) { - List currentPath = _path.snoc(_current); - int pos = 0; - List> 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 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> totalResult = List.nil(); - for(P2 next : accepted){ - Node node = next._1(); - TraverseEvaluator evaluator = next._2().evaluator(); - List> result = _traverse(currentPath,node,evaluator); - - - for(List list : result){ - totalResult = totalResult.snoc(list.cons(_current)); - } + List empty = List.nil(); + Either> ret = DefaultEither.newB(empty); + return ret; + } + + private Either> _accept(T _current,Evaluator _evaluator) + { + Children chs = _current.children(); + Either> either = _traverse(chs,_evaluator); + if(either.isA()){ + return either; } - return totalResult; + List list = either.b(); + List newList = list.cons(_current); + return DefaultEither.newB(newList); } } diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Evaluation.java --- 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> { public Result result(); - public Evaluator evaluator(); + public Evaluator evaluator(); } diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Evaluator.java --- /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> +{ + public Evaluation evaluate(T _current,int _pos); +} diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Parent.java --- /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 +{ + public Children children(); +} diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Result.java --- 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() + { + } } diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traversable.java --- 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> { - public Iterable traverse(TraverseEvaluator _e); + public Either> traverse(Evaluator _e); } diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traversal.java --- 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 extends Iterable +public interface Traversal> extends Iterable { public T destination(); } diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/TraverseEvaluator.java --- 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 _currentPosition,Node _child,int _pos); -} diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traverser.java --- 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 +public interface Traverser> { - public Traversal traverse(T _root,TraverseEvaluator _evaluator); + public Either> traverse(T _root,Evaluator _evaluator); } diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/TraverserError.java --- /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(); +} diff -r 1b448f9249ee -r 075d6418e359 src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/util/Container.java --- /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 +{ + public T get(); + public List> children(); +} + +interface Parent +{ + public Children children(); +} + +interface Children extends Iterable +{ + public int size(); +} + +interface Attribute +{ +} + +interface Node extends Parent +{ + public Attribute get(); +} + +class Hoge> +{ + public static void main(String _args[]) + { + + } + + + + public void visit(T _hoge) + { + Children chs = _hoge.children(); + for(T ch : chs){ + visit(ch); + } + } +} +