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;
|
124
|
7 import fj.data.Option;
|
97
|
8 import fj.data.TreeMap;
|
98
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
101
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
97
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
|
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
|
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;
|
99
|
14 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.PathNodeIterator;
|
110
|
15 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.query.Query;
|
97
|
16
|
100
|
17 public class InterfaceTraverser {
|
124
|
18
|
100
|
19 InterfaceTraverser traverser;
|
98
|
20 TreeNode node;
|
111
|
21 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index;
|
125
|
22 public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> getIndex() {
|
|
23 return index;
|
|
24 }
|
|
25
|
|
26 public void setIndex(
|
|
27 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) {
|
|
28 this.index = index;
|
|
29 }
|
|
30
|
|
31
|
|
32
|
101
|
33 JungleTreeEditor editor;
|
|
34
|
112
|
35 public InterfaceTraverser(TreeNode _root, JungleTreeEditor editor) {
|
124
|
36 this.node = _root;
|
|
37 this.index = TreeMap.empty(Ord.stringOrd);
|
|
38 this.editor = editor;
|
112
|
39 }
|
124
|
40
|
|
41 public InterfaceTraverser(
|
|
42 TreeNode _root,
|
|
43 TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index,
|
|
44 JungleTreeEditor editor) {
|
101
|
45 this.node = _root;
|
110
|
46 this.index = index;
|
101
|
47 this.editor = editor;
|
98
|
48 }
|
101
|
49
|
100
|
50 public InterfaceTraverser getTraverser(JungleTree tree) {
|
124
|
51 return new InterfaceTraverser(tree.getRootNode(), tree.getIndex(),
|
|
52 tree.getTreeEditor());
|
97
|
53 }
|
98
|
54
|
124
|
55 public void set(TreeNode root) {
|
101
|
56 this.node = root;
|
|
57 }
|
124
|
58
|
|
59
|
101
|
60
|
124
|
61 public Iterator<Pair<TreeNode, NodePath>> find(final Query query, String key, String searchValue) {
|
105
|
62
|
124
|
63 if (!index.get(key).isNone()) {
|
|
64
|
|
65 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = this.index.get(key).some();
|
|
66 Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex.get(searchValue);
|
|
67
|
|
68 if (opList.isNone())
|
|
69 return null;
|
99
|
70
|
124
|
71 final List<Pair<TreeNode, NodePath>> list = opList.some();
|
|
72 return list.iterator();
|
|
73
|
|
74 } else {
|
|
75
|
|
76 final PathNodeIterator itNode = new PathNodeIterator(node);
|
|
77 return new Iterator<Pair<TreeNode, NodePath>>() {
|
|
78
|
|
79 private Pair<TreeNode, NodePath> matchPair = nextmatch(itNode);
|
|
80
|
|
81 private Pair<TreeNode, NodePath> nextmatch(
|
|
82 PathNodeIterator itNode) {
|
101
|
83
|
124
|
84 for (; itNode.hasNext();) {
|
|
85 Pair<TreeNode, NodePath> pathNode = itNode.next();
|
|
86 String value = pathNode.left().getAttributes().getString(key);
|
|
87 Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> innerIndexOp = index.get(key);
|
|
88 System.out.println("value = "+ value);
|
|
89
|
|
90 if (value != null) {
|
|
91 if (innerIndexOp.isNone()) {
|
|
92
|
|
93 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = TreeMap.empty(Ord.stringOrd);
|
|
94 List<Pair<TreeNode, NodePath>> list = List.nil();
|
|
95 list = list.cons(pathNode);
|
|
96 innerIndex = innerIndex.set(value, list);
|
|
97 index = index.set(key, innerIndex);
|
|
98
|
|
99 } else {
|
|
100
|
|
101 TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = innerIndexOp.some();
|
|
102 innerIndex.set(value, null);
|
|
103 Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex.get(value);
|
|
104
|
|
105 if (opList.isNone()) {
|
99
|
106
|
124
|
107 List<Pair<TreeNode, NodePath>> list = List.nil();
|
|
108 list = list.cons(pathNode);
|
|
109 innerIndex = innerIndex.set(value, list);
|
|
110
|
|
111 } else {
|
|
112
|
|
113 List<Pair<TreeNode, NodePath>> list = opList.some();
|
|
114 list = list.cons(pathNode);
|
|
115 innerIndex = innerIndex.set(value, list);
|
|
116
|
|
117 }
|
|
118 index = index.set(key, innerIndex);
|
|
119
|
|
120 }
|
|
121 }
|
|
122
|
|
123 if (query.condition(pathNode.left()))
|
|
124 return pathNode;
|
|
125 }
|
|
126 return null;
|
|
127 }
|
99
|
128
|
124
|
129 @Override
|
|
130 public boolean hasNext() {
|
|
131 if (matchPair == null) {
|
|
132 // index = itNode.getIndex();
|
|
133 return false;
|
|
134 }
|
|
135 return true;
|
|
136 }
|
99
|
137
|
124
|
138 @Override
|
|
139 public Pair<TreeNode, NodePath> next() {
|
|
140 Pair<TreeNode, NodePath> currentPair = matchPair;
|
|
141 matchPair = nextmatch(itNode);
|
|
142 return currentPair;
|
|
143 }
|
101
|
144
|
124
|
145 @Override
|
|
146 public void remove() {
|
|
147 // TODO Auto-generated method stub
|
99
|
148
|
124
|
149 }
|
99
|
150
|
124
|
151 };
|
|
152 }
|
98
|
153 }
|
97
|
154 }
|