97
|
1 package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser;
|
|
2
|
98
|
3 import java.util.Iterator;
|
|
4
|
112
|
5 import fj.Ord;
|
104
|
6 import fj.data.List;
|
97
|
7 import fj.data.TreeMap;
|
98
|
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
101
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
97
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
|
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
|
101
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
|
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
|
97
|
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;
|
99
|
15 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.PathNodeIterator;
|
110
|
16 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.Query;
|
101
|
17 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.SearchQuery;
|
|
18 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.UpdateQuery;
|
97
|
19
|
100
|
20 public class InterfaceTraverser {
|
112
|
21
|
100
|
22 InterfaceTraverser traverser;
|
98
|
23 TreeNode node;
|
111
|
24 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index;
|
101
|
25 JungleTreeEditor editor;
|
|
26
|
112
|
27 public InterfaceTraverser(TreeNode _root, JungleTreeEditor editor) {
|
|
28 this.node = _root;
|
|
29 this.index = TreeMap.empty(Ord.stringOrd);
|
|
30 this.editor = editor;
|
|
31 }
|
|
32
|
111
|
33 public InterfaceTraverser(TreeNode _root, TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index, JungleTreeEditor editor) {
|
101
|
34 this.node = _root;
|
110
|
35 this.index = index;
|
101
|
36 this.editor = editor;
|
98
|
37 }
|
101
|
38
|
107
|
39
|
100
|
40 public InterfaceTraverser getTraverser(JungleTree tree) {
|
105
|
41 return new InterfaceTraverser(tree.getRootNode(), tree.getIndex(), tree.getTreeEditor());
|
97
|
42 }
|
98
|
43
|
101
|
44 public void set(TreeNode root){
|
|
45 this.node = root;
|
|
46 }
|
|
47 /*
|
|
48 * public IteratorPathNode traverse(TreeNode _node ,NodePath _path ,int
|
|
49 * _pos){
|
|
50 *
|
|
51 * Children children = _node.getChildren(); Either<Error,TreeNode> either =
|
|
52 * children.at(0); IteratorPathNode list = new IteratorPathNodeImpl(); int
|
|
53 * pathCount = _pos; if(children.size() == 0){ list = list.add(new
|
|
54 * Pair<TreeNode, NodePath>(node, _path.add(_pos))); return list; }
|
|
55 *
|
|
56 * for(TreeNode child : children){ list =
|
|
57 * list.append(traverse(child,_path,pathCount)); pathCount++; }
|
|
58 *
|
|
59 * list = list.add(new Pair<TreeNode,NodePath>(_node, _path.add(_pos)));
|
|
60 * return list; }
|
|
61 *
|
|
62 * public int count(Query _query, String _key, String _attribute){ return
|
|
63 * this.find(_query,_key,_attribute); }
|
|
64 *
|
|
65 * public List<Pair<NodePath,TreeNode>> distinct(String _key ,String...
|
|
66 * _attribute){ return null; }
|
|
67 */
|
102
|
68
|
110
|
69 public JungleTreeEditor update(final Query query) {
|
|
70 /*Iterator<Pair<TreeNode, NodePath>> findNode = find(query);
|
101
|
71 //do {
|
|
72 for (; findNode.hasNext();) {
|
|
73 Either<Error, JungleTreeEditor> either = editor.putAttribute(findNode.next().right(), "KEY", query.getUpdateAttribute());
|
|
74 if (either.isA())
|
|
75 ;// wait delay write
|
|
76 editor = either.b();
|
97
|
77 }
|
101
|
78 //} while (editor.success().isA());
|
110
|
79 */
|
101
|
80 return editor;
|
97
|
81 }
|
101
|
82
|
110
|
83 public Iterator<Pair<TreeNode, NodePath>> find(final Query query) {
|
122
|
84 final PathNodeIterator itNode = new PathNodeIterator(node);
|
105
|
85
|
99
|
86 return new Iterator<Pair<TreeNode, NodePath>>() {
|
|
87
|
101
|
88 private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode);
|
|
89
|
|
90 private Pair<TreeNode, NodePath> nextmatch(PathNodeIterator itNode) {
|
|
91 for (; itNode.hasNext();) {
|
|
92 Pair<TreeNode, NodePath> pathNode = itNode.next();
|
|
93 if (query.condition(pathNode.left()))
|
|
94 return pathNode;
|
|
95 }
|
|
96 return null;
|
|
97 }
|
99
|
98
|
101
|
99 @Override
|
|
100 public boolean hasNext() {
|
|
101 return matchPair != null;
|
|
102 }
|
99
|
103
|
101
|
104 @Override
|
|
105 public Pair<TreeNode, NodePath> next() {
|
|
106 Pair<TreeNode, NodePath> currentPair = matchPair;
|
|
107 matchPair = nextmatch(itNode);
|
|
108 return currentPair;
|
|
109 }
|
99
|
110
|
101
|
111 @Override
|
|
112 public void remove() {
|
|
113 // TODO Auto-generated method stub
|
|
114
|
|
115 }
|
99
|
116
|
|
117 };
|
|
118
|
98
|
119 }
|
97
|
120 }
|