Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 28:f4be99977be2
commit
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/Node.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/core/Node.java Mon Jan 14 21:16:04 2013 +0900 @@ -1,8 +1,6 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Parent; - -public interface Node extends Parent<Node> +public interface Node { public Children getChildren(); public Attributes getAttributes();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/TraversableNode.java Mon Jan 14 21:16:04 2013 +0900 @@ -0,0 +1,10 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Evaluator; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Parent; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traversal; + +public interface TraversableNode extends Parent<TraversableNode> +{ + public Traversal<TraversableNode> traverse(Evaluator<TraversableNode> _e); +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChangeSet.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChangeSet.java Mon Jan 14 21:16:04 2013 +0900 @@ -12,9 +12,9 @@ private final long revision; private final ChangeList changeList; - private final Tree tree; + private final DefaultTree tree; - public DefaultChangeSet(Tree _tree,ChangeList _changeList,String _uuid,long _revision,ChangeSet _prev) + public DefaultChangeSet(DefaultTree _tree,ChangeList _changeList,String _uuid,long _revision,ChangeSet _prev) { tree = _tree; uuid = _uuid; @@ -48,7 +48,7 @@ } @Override - public Tree getTree() + public DefaultTree getTree() { return tree; }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java Mon Jan 14 21:16:04 2013 +0900 @@ -24,6 +24,21 @@ attrs = _attrs; } + public Either<Error,DefaultNode> replaceNode(DefaultNode _target,int _pos) + { + if(check(_pos)){ + return DefaultEither.newA(OUT_OF_RANGE); + } + + P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos); + List<DefaultNode> head = split._1(); + List<DefaultNode> tail = split._2().tail().cons(_target); + List<DefaultNode> newList = head.append(tail); + + DefaultNode newNode = new DefaultNode(newList,attrs); + return DefaultEither.newB(newNode); + } + @Override public int size() {
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultModifiedTreeEditor.java Mon Jan 14 04:07:15 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl; - -import java.nio.ByteBuffer; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TraversableTree; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditResult; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; - -public class DefaultModifiedTreeEditor implements TreeEditor -{ - - @Override - public TraversableTree getTree() - { - return null; - } - - @Override - public TreeEditResult appendChild(NodePath _path, int _pos) { - // TODO Auto-generated method stub - return null; - } - - @Override - public TreeEditResult deleteChild(NodePath _path, int _pos) { - // TODO Auto-generated method stub - return null; - } - - @Override - public TreeEditResult putAttribute(NodePath _path, String _key,ByteBuffer _value) { - // TODO Auto-generated method stub - return null; - } - - @Override - public TreeEditResult deleteAttribute(NodePath _path, String _key) { - // TODO Auto-generated method stub - return null; - } - - @Override - public TreeEditResult edit(NodePath _path, NodeEditor _transformer) { - // TODO Auto-generated method stub - return null; - } - - @Override - public boolean success() { - // TODO Auto-generated method stub - return false; - } - -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultNode.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultNode.java Mon Jan 14 21:16:04 2013 +0900 @@ -6,14 +6,13 @@ import fj.Ord; import fj.data.List; import fj.data.TreeMap; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableAttributes; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableChildren; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Children; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableWrapper; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Parent; -public class DefaultNode implements EditableNode +public class DefaultNode implements EditableNode , Parent<DefaultNode> { public List<DefaultNode> children; public TreeMap<String,ByteBuffer> attrs; @@ -31,27 +30,32 @@ attrs = _attrs; children = _children; } - + + public DefaultNode clone() + { + return new DefaultNode(children,attrs); + } + @Override - public EditableChildren getChildren() + public DefaultChildren getChildren() { return new DefaultChildren(children,attrs); } @Override - public EditableAttributes getAttributes() + public DefaultAttributes getAttributes() { return new DefaultAttributes(children,attrs); } @Override - public Children<Node> children() + public Children<DefaultNode> children() { - return new Children<Node>(){ + return new Children<DefaultNode>(){ @Override - public Iterator<Node> iterator() + public Iterator<DefaultNode> iterator() { - return (new IterableWrapper<Node>(children)).iterator(); + return children.iterator(); } @Override
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTree.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTree.java Mon Jan 14 21:16:04 2013 +0900 @@ -1,6 +1,5 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Tree; public class DefaultTree implements Tree @@ -15,7 +14,7 @@ } @Override - public Node getRoot() + public DefaultNode getRoot() { return root; }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java Mon Jan 14 21:16:04 2013 +0900 @@ -1,12 +1,10 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl; -import java.util.LinkedList; import java.nio.ByteBuffer; import java.util.Iterator; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Tree; + +import fj.data.List; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListWriter; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.ChangeSet; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TraversableTree; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; @@ -14,9 +12,11 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.AppendChildAt; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteAttribute; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteChildAt; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.PutAttribute; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultEvaluator; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Direction; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traversal; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traverser; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; @@ -25,17 +25,19 @@ public class DefaultTreeEditor implements TreeEditor { - private final ChangeSet tip; + private final DefaultChangeSet tip; + private final TraverserProvider tProvider; - public DefaultTreeEditor(ChangeSet _tip,ChangeListWriter _writer) + public DefaultTreeEditor(DefaultChangeSet _tip,ChangeListWriter _writer,TraverserProvider _tProvider) { tip = _tip; + tProvider = _tProvider; } @Override public TraversableTree getTree() { - return new DefaultTraversableTree(tip.getTree(),new DefaultTraverser<Node>()); + return null; } @Override @@ -72,35 +74,33 @@ @Override public Either<Error,TreeEditor> edit(NodePath _path, NodeEditor _editor) { - Tree tree = tip.getTree(); - Traverser traverser = new DefaultTraverser(); - DefaultTraverseEvaluator te = new DefaultTraverseEvaluator(_path); + DefaultTree tree = tip.getTree(); + DefaultNode root = tree.getRoot(); + Traverser<DefaultNode> traverser = tProvider.newTraverser(); + DefaultEvaluator<DefaultNode> e = new DefaultEvaluator<DefaultNode>(_path); - for(Traversal traversal : traverser.traverse(tree,te)){ - return clone(traversal); + Either<Error,Traversal<DefaultNode>> either = traverser.traverse(root,e); + if(either.isA()){ + return DefaultEither.newA(either.a()); } - return DefaultEither.newA(TreeEditorError.NODEPATH_NOTFOUND); + Traversal<DefaultNode> t = either.b(); + Either<Error,TreeEditor> ret = clone(t,_editor); + + return ret; } - public Either<Error,TreeEditor> clone(Traversal _traversal) + private Either<Error,TreeEditor> clone(Traversal<DefaultNode> _t,NodeEditor _editor) { - for(Node node : _traversal){ - DefaultNode defNode = (DefaultNode)node; - + List<Direction<DefaultNode>> path = List.nil(); + for(Direction<DefaultNode> direction : _t){ + path = path.cons(direction); } + + // target + Direction<DefaultNode> targetDirection = path.head(); + DefaultNode + return null; } - - public void cloneAndApplyToNodeEditorFromRoot(Traversal _r,NodeEditor _editor) - { - LinkedList<DefaultNode> path = new LinkedList<DefaultNode>(); - - for(Node n : _r){ - DefaultNode node = (DefaultNode)n; - path.add(node); - } - - - } } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/TraverserProvider.java Mon Jan 14 21:16:04 2013 +0900 @@ -0,0 +1,9 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Parent; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traverser; + +public interface TraverserProvider +{ + public <T extends Parent<T>> Traverser<T> newTraverser(); +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingNode.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingNode.java Mon Jan 14 21:16:04 2013 +0900 @@ -1,10 +1,8 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableAttributes; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableChildren; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Children; public class LoggingNode implements EditableNode { @@ -38,10 +36,4 @@ { return log; } - - @Override - public Children<Node> children() - { - return wrap.children(); - } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/AppendChildAt.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/AppendChildAt.java Mon Jan 14 21:16:04 2013 +0900 @@ -4,7 +4,7 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; -public class AppendChildAt implements NodeEditor +public class AppendChildAt<T extends EditableNode> implements NodeEditor<T> { private final int pos; @@ -14,9 +14,9 @@ } @Override - public Either<Error,EditableNode> edit(EditableNode _e) + public Either<Error,T> edit(T _e) { - Either<Error,EditableNode> either = _e.getChildren().addNewChildAt(pos); + Either<Error,T> either = _e.getChildren().addNewChildAt(pos); if(either.isA()){ // error return either;
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/NodeEditor.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/NodeEditor.java Mon Jan 14 21:16:04 2013 +0900 @@ -3,7 +3,7 @@ import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; -public interface NodeEditor +public interface NodeEditor<T extends EditableNode> { - public Either<Error,EditableNode> edit(EditableNode _e); + public Either<Error,T> edit(T _e); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraverser.java Mon Jan 14 21:16:04 2013 +0900 @@ -24,17 +24,17 @@ } }; - Either<Error,List<T>> ret = _traverse(wrapper,_evaluator); + Either<Error,List<Direction<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(); + final Iterable<Direction<T>> iterable = ret.b(); + final T destination = ret.b().last().getTarget(); Traversal<T> traversal = new Traversal<T>(){ @Override - public Iterator<T> iterator(){ + public Iterator<Direction<T>> iterator(){ return iterable.iterator(); } @@ -47,42 +47,55 @@ return DefaultEither.newB(traversal); } - private Either<Error,List<T>> _traverse(Children<T> _chs,Evaluator<T> _evaluator) + private Either<Error,List<Direction<T>>> _traverse(Children<T> _chs,Evaluator<T> _evaluator) { int pos = 0; for(T ch : _chs){ Evaluation<T> e = _evaluator.evaluate(ch,pos); Result r = e.result(); if(r == Result.ACCEPT){ - return _accept(ch,e.evaluator()); + return _accept(ch,pos,e.evaluator()); } if(r == Result.BREAK){ - break; + List<Direction<T>> nil = List.nil(); + return DefaultEither.newB(nil); } if(r == Result.CONTINUE){ + pos ++; continue; } return DefaultEither.newA(TraverserError.UNDEFINED_OPERATOR); } - List<T> empty = List.nil(); - Either<Error,List<T>> ret = DefaultEither.newB(empty); - return ret; + return DefaultEither.newA(TraverserError.PATH_NOT_FOUND); } - private Either<Error,List<T>> _accept(T _current,Evaluator<T> _evaluator) + private Either<Error,List<Direction<T>>> _accept(final T _current,final int _pos,Evaluator<T> _evaluator) { Children<T> chs = _current.children(); - Either<Error,List<T>> either = _traverse(chs,_evaluator); + Either<Error,List<Direction<T>>> either = _traverse(chs,_evaluator); if(either.isA()){ return either; } - List<T> list = either.b(); - List<T> newList = list.cons(_current); + List<Direction<T>> list = either.b(); + Direction<T> d = new Direction<T>(){ + @Override + public int getPosition() + { + return _pos; + } + @Override + public T getTarget() + { + return _current; + } + }; + + List<Direction<T>> newList = list.cons(d); return DefaultEither.newB(newList); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Direction.java Mon Jan 14 21:16:04 2013 +0900 @@ -0,0 +1,7 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; + +public interface Direction<T> +{ + public int getPosition(); + public T getTarget(); +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traversal.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/Traversal.java Mon Jan 14 21:16:04 2013 +0900 @@ -1,6 +1,6 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser; -public interface Traversal<T extends Parent<T>> extends Iterable<T> +public interface Traversal<T extends Parent<T>> extends Iterable<Direction<T>> { public T destination(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/TraverserError.java Mon Jan 14 04:07:15 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/TraverserError.java Mon Jan 14 21:16:04 2013 +0900 @@ -6,4 +6,5 @@ public class TraverserError { public static final Error UNDEFINED_OPERATOR = new DefaultError(); + public static final Error PATH_NOT_FOUND = new DefaultError(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/util/Container.java Mon Jan 14 04:07:15 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,47 +0,0 @@ -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); - } - } -} -