Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 211:74648c746dd4
List fix
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/DefaultNode.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/DefaultNode.java Tue Aug 04 07:54:30 2015 +0900 @@ -53,7 +53,6 @@ @Override public Node<T> delete(int currentNum, int deleteNum) { - currentNum++; if (currentNum == deleteNum) { return new DefaultNode(this.attribute, this.next.getNext()); } @@ -66,16 +65,16 @@ } @Override - public Node<T> replaceNode(int num, T attribute) { - if (this.num == num) { - return new DefaultNode(attribute, num, this.getNext()); + public Node<T> replaceNode(int currentNum, int num, T attribute) { + if (currentNum == num) { + return new DefaultNode(attribute, this.getNext()); } - Node<T> newNode = next.replaceNode(num, attribute); + Node<T> newNode = next.replaceNode(currentNum + 1, num, attribute); if (newNode == null) return null; - return new DefaultNode(this.attribute, this.num, newNode); + return new DefaultNode(this.attribute, newNode); } @Override
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/List.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/List.java Tue Aug 04 07:54:30 2015 +0900 @@ -3,15 +3,13 @@ import java.util.Iterator; import java.util.Stack; -/** - * Created by e115731 on 15/05/16. - * 非破壊であるためこのListは逆順になっています - */ + + public class List<T> implements Iterable<T> { final private Node<T> head; public List() { - this.head = new headNode(); + this.head = new headNode<T>(); } public List(T... attributes) { @@ -104,7 +102,7 @@ } public List<T> replace(int num, T attribute) { - Node<T> newHead = head.replaceNode(num, attribute); + Node<T> newHead = head.replaceNode(0, num, attribute); if (newHead == null) return this; return new List<T>(newHead); @@ -146,7 +144,7 @@ } public List<T> append(List<T> list) { - Iterator<T> iterator = list.reverseIterator(); + Iterator<T> iterator = list.iterator(); List<T> newList = this; while (iterator.hasNext()) { T attribute = iterator.next();
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/Node.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/Node.java Tue Aug 04 07:54:30 2015 +0900 @@ -16,7 +16,7 @@ public Node<T> delete(int currentNum, int num); - public Node<T> replaceNode(int num, T attribute); + public Node<T> replaceNode(int currentNum, int num, T attribute); public int length(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/TailNode.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/TailNode.java Tue Aug 04 07:54:30 2015 +0900 @@ -31,7 +31,7 @@ } @Override - public Node<T> replaceNode(int num, T attribute) { + public Node<T> replaceNode(int currentNum, int num, T attribute) { return null; }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/headNode.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/list/headNode.java Tue Aug 04 07:54:30 2015 +0900 @@ -48,28 +48,23 @@ return new headNode(this.next.getNext()); } - Node<T> newNode = next.delete(currentNum, deleteNum); + Node<T> newNode = next.delete(currentNum + 1, deleteNum); if (newNode == null) return this; return new headNode(newNode); } @Override - public Node<T> replaceNode(int num, T attribute) { + public Node<T> replaceNode(int currentNum, int num, T attribute) { Node<T> nextNode = getNext(); - if (nextNode.getNum() == num) { - Node<T> newNode = new DefaultNode(attribute, num, nextNode.getNext()); - return new headNode(newNode); - } - - Node<T> newNode = nextNode.replaceNode(num, attribute); + Node<T> newNode = nextNode.replaceNode(currentNum, num, attribute); if (newNode == null) return this; return new headNode(newNode); } @Override - public int length(){ + public int length() { return next.length(); } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/impl/DefaultNodePath.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/impl/DefaultNodePath.java Tue Aug 04 07:54:30 2015 +0900 @@ -39,7 +39,6 @@ public Pair<Integer, NodePath> pop() { Integer head = path.head(); List<Integer> tail = path.deleteHead(); - return new Pair<Integer, NodePath>(head, new DefaultNodePath(tail)); } @@ -72,10 +71,10 @@ public List<DefaultNodePath> inits() { List<DefaultNodePath> paths = new List(); List<Integer> coursePath = new List(); - Iterator<Integer> iterator = path.reverseIterator(); + Iterator<Integer> iterator = path.iterator(); while (iterator.hasNext()) { List<Integer> tmp = coursePath.addLast(iterator.next()); - paths = paths.add(1,new DefaultNodePath(tmp)); + paths = paths.addLast(new DefaultNodePath(tmp)); coursePath = tmp; } return paths;
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/impl/DefaultTreeEditor.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/impl/DefaultTreeEditor.java Tue Aug 04 07:54:30 2015 +0900 @@ -62,7 +62,7 @@ TreeNode child = newWrap.getWrap(); - for(Direction<TreeNode> parentDirection : path.deleteLast()){ + for(Direction<TreeNode> parentDirection : path.deleteHead()){ TreeNodeChildren chs = parentDirection.getTarget().getChildren();
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/traverser/DefaultEvaluator.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/traverser/DefaultEvaluator.java Tue Aug 04 07:54:30 2015 +0900 @@ -4,37 +4,38 @@ import jp.ac.u_ryukyu.ie.cr.jungle.util.Pair; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode; -public class DefaultEvaluator implements Evaluator -{ - private final NodePath path; - - public DefaultEvaluator(NodePath _path) - { - path = _path; - } - - @Override - public Evaluation evaluate(TreeNode _current,int _pos) - { - Pair<Integer,NodePath> pop = path.pop(); - int head = pop.left(); - - if(path.size() == 1){ - if(head == _pos){ - return new DefaultEvaluation(Result.GOAL,null); - } - } - - DefaultEvaluator nextEvaluator; - Result result; - if(head == _pos){ - result = Result.ACCEPT; - nextEvaluator = new DefaultEvaluator(pop.right()); - }else{ - result = Result.CONTINUE; - nextEvaluator = null; - } - - return new DefaultEvaluation(result,nextEvaluator); - } +public class DefaultEvaluator implements Evaluator { + private final NodePath path; + + public DefaultEvaluator(NodePath _path) { + path = _path; + } + + @Override + public Evaluation evaluate(TreeNode _current, int _pos) { + Pair<Integer, NodePath> pop = path.pop(); + int head = pop.left(); + + if (path.size() == 1) { + if (head == _pos) { + return new DefaultEvaluation(Result.GOAL, null); + } + } + + DefaultEvaluator nextEvaluator; + Result result; + if (head == _pos) { + result = Result.ACCEPT; + nextEvaluator = new DefaultEvaluator(pop.right()); + } else if (_pos < _current.getChildren().size() - 1) { + result = Result.BREAK; + nextEvaluator = null; + } else { + result = Result.CONTINUE; + nextEvaluator = null; + } + + + return new DefaultEvaluation(result, nextEvaluator); + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/traverser/DefaultTraverser.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/traverser/DefaultTraverser.java Tue Aug 04 07:54:30 2015 +0900 @@ -11,134 +11,127 @@ import java.util.Iterator; -public class DefaultTraverser implements Traverser -{ - @Override - public Either<Error,Traversal> traverse(final TreeNode _root,Evaluator _evaluator) - { - Children wrapper = new Children(){ - @Override - public Iterator<TreeNode> iterator(){ - List<TreeNode> list = new List(); - return list.addLast(_root).iterator(); - } - - @Override - public int size(){ - return 1; - } - - @Override - public Either<Error,TreeNode> at(int _pos){ - if(_pos != 0){ - return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); - } - return DefaultEither.newB(_root); - } - }; - - //Children<T> chs = _root.getChildren(); - Children chs = wrapper; - - Either<Error,List<Direction<TreeNode>>> ret = _traverse(chs,_evaluator,-1); - - if(ret.isA()){ - return DefaultEither.newA(ret.a()); - } - - List<Direction<TreeNode>> list = ret.b(); - - final Iterable<Direction<TreeNode>> iterable = list; - final TreeNode destination = ret.b().last().getTarget(); - - Traversal traversal = new Traversal(){ - @Override - public Iterator<Direction<TreeNode>> iterator(){ - return iterable.iterator(); - } +public class DefaultTraverser implements Traverser { + @Override + public Either<Error, Traversal> traverse(final TreeNode _root, Evaluator _evaluator) { + Children wrapper = new Children() { + @Override + public Iterator<TreeNode> iterator() { + List<TreeNode> list = new List(); + return list.addLast(_root).iterator(); + } + + @Override + public int size() { + return 1; + } + + @Override + public Either<Error, TreeNode> at(int _pos) { + if (_pos != 0) { + return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); + } + return DefaultEither.newB(_root); + } + }; + + //Children<T> chs = _root.getChildren(); + Children chs = wrapper; + + Either<Error, List<Direction<TreeNode>>> ret = _traverse(chs, _evaluator, -1); + + if (ret.isA()) { + return DefaultEither.newA(ret.a()); + } + + List<Direction<TreeNode>> list = ret.b(); + + final Iterable<Direction<TreeNode>> iterable = list; + final TreeNode destination = ret.b().last().getTarget(); + + Traversal traversal = new Traversal() { + @Override + public Iterator<Direction<TreeNode>> iterator() { + return iterable.iterator(); + } + + @Override + public TreeNode destination() { + return destination; + } + }; + + return DefaultEither.newB(traversal); + } + + private Either<Error, List<Direction<TreeNode>>> _traverse(Children _chs, Evaluator _evaluator, int _pos) { + int pos = _pos; + for (TreeNode ch : _chs) { + Evaluation e = _evaluator.evaluate(ch, pos); + Result r = e.result(); + if (r == Result.ACCEPT) { + return _accept(ch, pos, e.evaluator()); + } - @Override - public TreeNode destination(){ - return destination; - } - }; - - return DefaultEither.newB(traversal); - } - - private Either<Error,List<Direction<TreeNode>>> _traverse(Children _chs,Evaluator _evaluator,int _pos) - { - int pos = _pos; - for(TreeNode ch : _chs){ - Evaluation e = _evaluator.evaluate(ch,pos); - Result r = e.result(); - if(r == Result.ACCEPT){ - return _accept(ch,pos,e.evaluator()); - } - - if(r == Result.GOAL){ - return DefaultEither.newB(_goal(ch,pos)); - } - - if(r == Result.BREAK){ - break; - } - - if(r == Result.CONTINUE){ - pos --; - continue; - } - - return DefaultEither.newA(TraverserError.UNDEFINED_OPERATOR); - } - - return DefaultEither.newA(TraverserError.PATH_NOT_FOUND); - } - - private List<Direction<TreeNode>> _goal(final TreeNode _current,final int _pos) - { - Direction<TreeNode> d = new Direction<TreeNode>(){ - @Override - public int getPosition() - { - return _pos; - } - @Override - public TreeNode getTarget() - { - return _current; - } - }; - - List<Direction<TreeNode>> list = new List(); - List<Direction<TreeNode>> newList = list.addLast(d); - - return newList; - } - - private <T extends TreeNode> Either<Error,List<Direction<TreeNode>>> _accept(final T _current,final int _pos,Evaluator _evaluator) - { - Children chs = _current.getChildren(); - Either<Error,List<Direction<TreeNode>>> either = _traverse(chs,_evaluator,chs.size() - 1); - if(either.isA()){ - return either; - } - - List<Direction<TreeNode>> list = either.b(); - Direction<TreeNode> d = new Direction<TreeNode>(){ - @Override - public int getPosition() - { - return _pos; - } - @Override - public T getTarget() - { - return _current; - } - }; - - List<Direction<TreeNode>> newList = list.addLast(d); - return DefaultEither.newB(newList); - } + if (r == Result.GOAL) { + return DefaultEither.newB(_goal(ch, pos)); + } + + if (r == Result.BREAK) { + break; + } + + if (r == Result.CONTINUE) { + pos++; + continue; + } + + return DefaultEither.newA(TraverserError.UNDEFINED_OPERATOR); + } + + return DefaultEither.newA(TraverserError.PATH_NOT_FOUND); + } + + private List<Direction<TreeNode>> _goal(final TreeNode _current, final int _pos) { + Direction<TreeNode> d = new Direction<TreeNode>() { + @Override + public int getPosition() { + return _pos; + } + + @Override + public TreeNode getTarget() { + return _current; + } + }; + + List<Direction<TreeNode>> list = new List(); + List<Direction<TreeNode>> newList = list.addLast(d); + + return newList; + } + + private <T extends TreeNode> Either<Error, List<Direction<TreeNode>>> _accept(final T _current, final int _pos, Evaluator _evaluator) { + Children chs = _current.getChildren(); + Either<Error, List<Direction<TreeNode>>> either = _traverse(chs, _evaluator, 0); + if (either.isA()) { + return either; + } + + List<Direction<TreeNode>> list = either.b(); + Direction<TreeNode> d = new Direction<TreeNode>() { + @Override + public int getPosition() { + return _pos; + } + + @Override + public T getTarget() { + return _current; + } + }; + + List<Direction<TreeNode>> newList = list.addLast(d); + return DefaultEither.newB(newList); + } }
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/core/GetNodeOfPathTest.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/core/GetNodeOfPathTest.java Tue Aug 04 07:54:30 2015 +0900 @@ -29,17 +29,17 @@ editor = editor.putAttribute(path, "KEY", ByteBuffer.wrap("tatsuki".getBytes())).b(); editor = editor.addNewChildAt(path, 0).b(); path = path.add(0); - editor = editor.putAttribute(path, "KEY", ByteBuffer.wrap("tatsuki".getBytes())).b(); + editor = editor.putAttribute(path, "KEY", ByteBuffer.wrap("kanagawa".getBytes())).b(); editor.success(); InterfaceTraverser traverser = tree.getTraverser(true); Iterator<TreeNode> nodeIterator = traverser.find((TreeNode node) -> { String str = node.getAttributes().getString("KEY"); if (str == null) return false; - if (str.equals("tatsuki")) + if (str.equals("kanagawa")) return true; return false; - }, "KEY", "tatsuki"); + }, "KEY", "kanagawa"); TreeNode node = tree.getNodeOfPath(path).b(); Assert.assertTrue(nodeIterator.hasNext());
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/core/impl/treeeditor/DefaultTreeEditorTest.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/core/impl/treeeditor/DefaultTreeEditorTest.java Tue Aug 04 07:54:30 2015 +0900 @@ -1,15 +1,15 @@ package jp.ac.u_ryukyu.ie.cr.jungle.core.impl.treeeditor; -import jp.ac.u_ryukyu.ie.cr.jungle.tests.util.TestUtil; -import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.LoggingNode; import jp.ac.u_ryukyu.ie.cr.jungle.store.trasnformer.AppendChildAt; import jp.ac.u_ryukyu.ie.cr.jungle.store.trasnformer.PutAttribute; +import jp.ac.u_ryukyu.ie.cr.jungle.tests.util.TestUtil; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultTreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultEvaluator; +import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.Traversal; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.Error; @@ -18,77 +18,72 @@ import java.nio.ByteBuffer; -public class DefaultTreeEditorTest extends TestCase -{ - public DefaultTreeEditor instance() - { - DefaultTraverser traverser = new DefaultTraverser(); - return new DefaultTreeEditor(traverser); - } +public class DefaultTreeEditorTest extends TestCase { + public DefaultTreeEditor instance() { + DefaultTraverser traverser = new DefaultTraverser(); + return new DefaultTreeEditor(traverser); + } - public void testEdittingDoesNotEffectToOtherTree() - { - TreeNode root = TestUtil.createMockTree(3); - DefaultTreeEditor editor = new DefaultTreeEditor(new DefaultTraverser()); - DefaultNodePath path = new DefaultNodePath().add(0).add(2); + public void testEdittingDoesNotEffectToOtherTree() { + TreeNode root = TestUtil.createMockTree(3); + DefaultTreeEditor editor = new DefaultTreeEditor(new DefaultTraverser()); + DefaultNodePath path = new DefaultNodePath().add(0).add(2); - TreeNode oldRoot = root; + TreeNode oldRoot = root; + + DefaultTreeEditor currentEditor = editor; + String key = "path"; - DefaultTreeEditor currentEditor = editor; - String key = "path"; - + String aaa = root.getChildren().at(0).b().getAttributes().getString(key); + TreeNode currentRoot = root; + for (DefaultNodePath part : path.inits()) { + String str = part.toString(); + ByteBuffer value = ByteBuffer.wrap(str.getBytes()); + PutAttribute putAttribute = new PutAttribute(key, value); + Either<Error, LoggingNode> either = currentEditor.edit(currentRoot, part, putAttribute); + if (either.isA()) { + Assert.fail(); + } + currentRoot = either.b().getWrap(); + } - TreeNode currentRoot = root; - for(DefaultNodePath part : path.inits()){ - String str = part.toString(); - ByteBuffer value = ByteBuffer.wrap(str.getBytes()); - PutAttribute putAttribute = new PutAttribute(key,value); - Either<Error,LoggingNode> either = currentEditor.edit(currentRoot,part,putAttribute); - if(either.isA()){ - Assert.fail(); - } - currentRoot = either.b().getWrap(); - } - - TreeNode newRoot = currentRoot; - DefaultTraverser traverser = new DefaultTraverser(); + TreeNode newRoot = currentRoot; + DefaultTraverser traverser = new DefaultTraverser(); - for(DefaultNodePath part : path.inits()){ - Either<Error,Traversal> either = traverser.traverse(newRoot,new DefaultEvaluator(part)); - if(either.isA()){ - Assert.fail(); - } - TreeNode target = either.b().destination(); - String expected = part.toString(); - String actual = new String(target.getAttributes().get(key).array()); + for (DefaultNodePath part : path.inits()) { + Either<Error, Traversal> either = traverser.traverse(newRoot, new DefaultEvaluator(part)); + if (either.isA()) { + Assert.fail(); + } + TreeNode target = either.b().destination(); + String expected = part.toString(); + String actual = new String(target.getAttributes().get(key).array()); - Assert.assertEquals(expected,actual); - } - - for(DefaultNodePath part : path.inits()){ - Either<Error,Traversal> either = traverser.traverse(oldRoot,new DefaultEvaluator(part)); - if(either.isA()){ - Assert.fail(); - } - TreeNode target = either.b().destination(); - ByteBuffer actual = target.getAttributes().get(key); + Assert.assertEquals(expected, actual); + } + for (DefaultNodePath part : path.inits()) { + Either<Error, Traversal> either = traverser.traverse(oldRoot, new DefaultEvaluator(part)); + if (either.isA()) { + Assert.fail(); + } + TreeNode target = either.b().destination(); + ByteBuffer actual = target.getAttributes().get(key); - Assert.assertNull(actual); - } + Assert.assertNull(actual); + } - } + } - public void testEdit() - { - DefaultTreeEditor instance = instance(); - DefaultTreeNode node = new DefaultTreeNode(); - DefaultNodePath path = new DefaultNodePath(); + public void testEdit() { + DefaultTreeEditor instance = instance(); + DefaultTreeNode node = new DefaultTreeNode(); + DefaultNodePath path = new DefaultNodePath(); - Either<Error,LoggingNode> either = instance.edit(node,path,new AppendChildAt(0)); - if(either.isA()){ - Assert.fail(); - } - TreeNode newRoot = either.b().getWrap(); - Assert.assertEquals(1,newRoot.getChildren().size()); - } + Either<Error, LoggingNode> either = instance.edit(node, path, new AppendChildAt(0)); + if (either.isA()) { + Assert.fail(); + } + TreeNode newRoot = either.b().getWrap(); + Assert.assertEquals(1, newRoot.getChildren().size()); + } }
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/appendTest.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/appendTest.java Tue Aug 04 07:54:30 2015 +0900 @@ -24,7 +24,7 @@ } List<Integer> newList = list.append(list2); - Iterator<Integer> iterator = newList.reverseIterator(); + Iterator<Integer> iterator = newList.iterator(); for (int count = 1; count <= 20; count++) { Assert.assertTrue(iterator.hasNext()); int num = iterator.next();
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/listIterator.java Tue Jul 28 08:55:10 2015 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/tatsuki/list/listIterator.java Tue Aug 04 07:54:30 2015 +0900 @@ -17,14 +17,14 @@ list = list.addLast(count); } Iterator<Integer> iterator = list.iterator(); - for (int count = 100; count > 0; count--) { + for (int count = 1; count < 100; count++) { Assert.assertTrue(iterator.hasNext()); int attribute = iterator.next(); Assert.assertEquals(attribute, count); } iterator = list.reverseIterator(); - for (int count = 1; count <= 100; count++) { + for (int count = 100; count > 0; count--) { Assert.assertTrue(iterator.hasNext()); int attribute = iterator.next(); Assert.assertEquals(attribute, count);