comparison src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/InterfaceTraverser.java @ 137:7334f78a92c3

name changed find to findInSubTree
author one
date Mon, 27 Oct 2014 18:25:32 +0900
parents 0f68cd7b2838
children ec166c8ff079
comparison
equal deleted inserted replaced
136:0f68cd7b2838 137:7334f78a92c3
23 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.Query; 23 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.Query;
24 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.IndexManager; 24 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.IndexManager;
25 25
26 public class InterfaceTraverser { 26 public class InterfaceTraverser {
27 27
28 //InterfaceTraverser traverser; 28 //InterfaceTraverser traverser;
29 TreeNode node; 29 TreeNode node;
30 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index; 30 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index;
31 IndexManager indexManager; 31 IndexManager indexManager;
32 32
33 public InterfaceTraverser(TreeNode _root, IndexManager indexManager) { 33 public InterfaceTraverser(TreeNode _root, IndexManager indexManager) {
34 this.node = _root; 34 this.node = _root;
35 this.index = TreeMap.empty(Ord.stringOrd); 35 this.index = TreeMap.empty(Ord.stringOrd);
36 this.indexManager = indexManager; 36 this.indexManager = indexManager;
37 } 37 }
38 38
39 public InterfaceTraverser( 39 public InterfaceTraverser(
40 TreeNode _root, 40 TreeNode _root,
41 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index, 41 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index,
42 IndexManager indexManager) { 42 IndexManager indexManager) {
43 this.node = _root; 43 this.node = _root;
44 this.index = index; 44 this.index = index;
45 this.indexManager = indexManager; 45 this.indexManager = indexManager;
46 } 46 }
47 47
48 public void commitIndex(){ 48 public void commitIndex(){
49 indexManager.commit(index); 49 indexManager.commit(index);
50 } 50 }
51 51
52 public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> getIndex() { 52 public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> getIndex() {
53 return index; 53 return index;
54 } 54 }
55 55
56 public void setIndex( 56 public void setIndex(
57 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) { 57 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) {
58 this.index = index; 58 this.index = index;
59 } 59 }
60 60
61 61
62 62
63 /* public InterfaceTraverser getTraverser(JungleTree tree) { 63 /* public InterfaceTraverser getTraverser(JungleTree tree) {
64 return new InterfaceTraverser(tree.getRootNode(), tree.getIndex(), 64 return new InterfaceTraverser(tree.getRootNode(), tree.getIndex(),
65 tree.getIndexTreeEditor()); 65 tree.getIndexTreeEditor());
66 }*/ 66 }*/
67 67
68 public void set(TreeNode root) { 68 public void set(TreeNode root) {
69 this.node = root; 69 this.node = root;
70 } 70 }
71 71
72 72
73 73 /**
74 public Iterator<Pair<TreeNode, NodePath>> find(Query query, TreeNode searchNode, String key, String searchValue) { 74 * subTree以下のNodeに対してKey,Valueのペアでindexを使って探索を行う
75 75 * @param query
76 final PathNodeIterator itNode = new PathNodeIterator(searchNode); 76 * @param subTree
77 return new Iterator<Pair<TreeNode, NodePath>>() { 77 * @param key
78 78 * @param searchValue
79 private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode); 79 * @return
80 80 */
81 private Pair<TreeNode, NodePath> nextmatch( 81 public Iterator<Pair<TreeNode, NodePath>> findInSubTree(Query query, TreeNode subTree, String key, String searchValue){
82 PathNodeIterator itNode) { 82 /*
83 83 * indexを使って取ってきたNodeのPathと、subTreeのPathを先頭から1つずつ比較し、
84 for (; itNode.hasNext();) { 84 * indexから取ってきたNodeのPathが一致した場合、そのNodeを返す
85 Pair<TreeNode, NodePath> pathNode = itNode.next(); 85 */
86 if (query.condition(pathNode.left())) 86 final PathNodeIterator itNode = new PathNodeIterator(subTree);
87 return pathNode; 87 return new Iterator<Pair<TreeNode, NodePath>>() {
88 } 88
89 return null; 89 private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode);
90 } 90
91 91 private Pair<TreeNode, NodePath> nextmatch(
92 @Override 92 PathNodeIterator itNode) {
93 public boolean hasNext() { 93
94 if (matchPair == null) { 94 for (; itNode.hasNext();) {
95 // index = itNode.getIndex(); 95 Pair<TreeNode, NodePath> pathNode = itNode.next();
96 return false; 96 if (query.condition(pathNode.left()))
97 } 97 return pathNode;
98 return true; 98 }
99 } 99 return null;
100 100 }
101 @Override 101
102 public Pair<TreeNode, NodePath> next() { 102 @Override
103 Pair<TreeNode, NodePath> currentPair = matchPair; 103 public boolean hasNext() {
104 matchPair = nextmatch(itNode); 104 if (matchPair == null) {
105 return currentPair; 105 // index = itNode.getIndex();
106 } 106 return false;
107 107 }
108 @Override 108 return true;
109 public void remove() { 109 }
110 // TODO Auto-generated method stub 110
111 111 @Override
112 } 112 public Pair<TreeNode, NodePath> next() {
113 113 Pair<TreeNode, NodePath> currentPair = matchPair;
114 }; 114 matchPair = nextmatch(itNode);
115 115 return currentPair;
116 } 116 }
117 117
118 public Iterator<Pair<TreeNode, NodePath>> find(Query query) { 118 @Override
119 return find(query, node, null, null); 119 public void remove() {
120 } 120 // TODO Auto-generated method stub
121 121
122 public Iterator<Pair<TreeNode, NodePath>> find(Query query, TreeNode searchNode) { 122 }
123 return find(query, searchNode, null, null); 123
124 } 124 };
125 125
126 public Iterator<Pair<TreeNode, NodePath>> find(Query query, String key, String searchValue) { 126 }
127 127
128 if (index.get(key).isSome()) { 128
129 129 public Iterator<Pair<TreeNode, NodePath>> find(Query query, String key,
130 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = this.index.get(key).some(); 130 String searchValue) {
131 Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex.get(searchValue); 131
132 132 if (index.get(key).isSome()) {
133 if (opList.isNone()) 133
134 return null;//空のIteratorを返す 134 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = this.index
135 135 .get(key).some();
136 final List<Pair<TreeNode, NodePath>> list = opList.some(); 136 Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex
137 return list.iterator(); 137 .get(searchValue);
138 138
139 } else { 139 if (opList.isNone())
140 140 return null;// 空のIteratorを返す
141 final PathNodeIterator itNode = new PathNodeIterator(node); 141
142 return new Iterator<Pair<TreeNode, NodePath>>() { 142 final List<Pair<TreeNode, NodePath>> list = opList.some();
143 143 return list.iterator();
144 private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode); 144
145 145 } else {
146 private Pair<TreeNode, NodePath> nextmatch( 146
147 PathNodeIterator itNode) { 147 final PathNodeIterator itNode = new PathNodeIterator(node);
148 148 return new Iterator<Pair<TreeNode, NodePath>>() {
149 for (; itNode.hasNext();) { 149
150 Pair<TreeNode, NodePath> pathNode = itNode.next(); 150 private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode);
151 String value = pathNode.left().getAttributes().getString(key); 151
152 Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> innerIndexOp = index.get(key); 152 private Pair<TreeNode, NodePath> nextmatch(
153 153 PathNodeIterator itNode) {
154 if (value != null) { 154
155 if (innerIndexOp.isNone()) { 155 for (; itNode.hasNext();) {
156 156 Pair<TreeNode, NodePath> pathNode = itNode.next();
157 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = TreeMap.empty(Ord.stringOrd); 157 String value = pathNode.left().getAttributes()
158 List<Pair<TreeNode, NodePath>> list = List.nil(); 158 .getString(key);
159 list = list.cons(pathNode); 159 Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> innerIndexOp = index
160 innerIndex = innerIndex.set(value, list); 160 .get(key);
161 index = index.set(key, innerIndex); 161
162 162 if (value != null) {
163 } else { 163 if (innerIndexOp.isNone()) {
164 164
165 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = innerIndexOp.some(); 165 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = TreeMap
166 Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex.get(value); 166 .empty(Ord.stringOrd);
167 167 List<Pair<TreeNode, NodePath>> list = List
168 if (opList.isNone()) { 168 .nil();
169 169 list = list.cons(pathNode);
170 List<Pair<TreeNode, NodePath>> list = List.nil(); 170 innerIndex = innerIndex.set(value, list);
171 list = list.cons(pathNode); 171 index = index.set(key, innerIndex);
172 innerIndex = innerIndex.set(value, list); 172
173 173 } else {
174 } else { 174
175 175 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = innerIndexOp
176 List<Pair<TreeNode, NodePath>> list = opList.some(); 176 .some();
177 list = list.cons(pathNode); 177 Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex
178 innerIndex = innerIndex.set(value, list); 178 .get(value);
179 179
180 } 180 if (opList.isNone()) {
181 index = index.set(key, innerIndex); 181
182 182 List<Pair<TreeNode, NodePath>> list = List
183 } 183 .nil();
184 } 184 list = list.cons(pathNode);
185 185 innerIndex = innerIndex.set(value, list);
186 if (query.condition(pathNode.left())) 186
187 return pathNode; 187 } else {
188 } 188
189 return null; 189 List<Pair<TreeNode, NodePath>> list = opList
190 } 190 .some();
191 191 list = list.cons(pathNode);
192 @Override 192 innerIndex = innerIndex.set(value, list);
193 public boolean hasNext() { 193
194 if (matchPair == null) { 194 }
195 // index = itNode.getIndex(); 195 index = index.set(key, innerIndex);
196 return false; 196
197 } 197 }
198 return true; 198 }
199 } 199
200 200 if (query.condition(pathNode.left()))
201 @Override 201 return pathNode;
202 public Pair<TreeNode, NodePath> next() { 202 }
203 Pair<TreeNode, NodePath> currentPair = matchPair; 203 return null;
204 matchPair = nextmatch(itNode); 204 }
205 return currentPair; 205
206 } 206 @Override
207 207 public boolean hasNext() {
208 @Override 208 if (matchPair == null) {
209 public void remove() { 209 // index = itNode.getIndex();
210 // TODO Auto-generated method stub 210 return false;
211 211 }
212 } 212 return true;
213 213 }
214 }; 214
215 } 215 @Override
216 } 216 public Pair<TreeNode, NodePath> next() {
217 Pair<TreeNode, NodePath> currentPair = matchPair;
218 matchPair = nextmatch(itNode);
219 return currentPair;
220 }
221
222 @Override
223 public void remove() {
224 // TODO Auto-generated method stub
225
226 }
227
228 };
229 }
230 }
217 } 231 }