View Javadoc

1   package net.obsearch.example.ted;
2   
3   
4   
5   import java.io.DataInputStream;
6   import java.io.DataOutputStream;
7   import java.io.IOException;
8   
9   import net.obsearch.example.SliceParseException;
10  import net.obsearch.exception.OBException;
11  import net.obsearch.ob.OBShort;
12  
13  
14  
15  
16  public class OBTed  implements OBShort {
17  
18  
19      /**
20       * The root node of the tree.
21       */
22      protected SliceForest tree;
23      private int hashCode;
24  
25      /**
26       * Default constructor must be provided by every object that implements the
27       * interface OB.
28       */
29      public OBTed() {
30  
31      }
32  
33      /**
34       * Creates an slice object.
35       * @param slice
36       *                A string representation of a tree.
37       */
38      public OBTed(final String slice) throws OBException {
39          this.updateTree(slice);
40          assert tree.equalsTree( this.parseTree(tree.toFuriaChanTree())) : "This: " + tree.toFuriaChanTree() + " slice: " + slice + " size: " + tree.getSize();
41      }
42  
43      /**
44       * Calculates the distance between two trees. TODO: traverse the smallest
45       * tree.
46       * @param object
47       *                The other object to compare
48       * @see net.obsearch.OB#distance(net.obsearch.OB,
49       *      net.obsearch.result.Dim)
50       * @throws OBException
51       *                 if something wrong happens.
52       * @return A short that indicates how similar or different the trees are.
53       */
54      public final short distance(final OBShort object) throws OBException {
55          OBTed b = (OBTed) object;
56          return distance(b.tree, this.tree);        
57      }
58  
59      /**
60       * Calculates the distance between trees a and b.
61       * @param a
62       *                The first tree (should be smaller than b)
63       * @param b
64       *                The second tree
65       * @return The distance of the trees.
66       */
67      private final short distance(SliceForest a, SliceForest b) {
68          
69              DMRW t = new DMRW();
70              return (short)t.ted(a, b);
71      }
72  
73      /**
74       * Internal method that updates the Tree from the String
75       * @throws OBException
76       */
77      protected final void updateTree(String x) throws OBException {
78          tree = parseTree(x);
79          this.hashCode = tree.toFuriaChanTree().hashCode();
80      }
81  
82      private final SliceForest parseTree(String x) throws SliceParseException {
83          try {
84              
85              
86              return SliceFactory.createSliceForest(x);
87          } catch (Exception e) {
88              throw new SliceParseException(x, e);
89          }
90      }
91      
92      
93  
94      /**
95       * Returns the size (in nodes) of the tree.
96       * @return The size of the tree.
97       * @throws OBException
98       *                 If something goes wrong.
99       */
100     public final int size() throws OBException {
101         return tree.getSize();
102     }
103 
104     /**
105      * @return A String representation of the tree.
106      */
107     public final String toString() {
108         String res = ":)";
109         try {
110             res = tree.toFuriaChanTree() + "|" + tree.getSize() + "|";
111         } catch (Exception e) {
112             assert false;
113         }
114         return res;
115     }
116 
117     /**
118      * Re-creates this object from the given byte stream
119      * @param in
120      *                A byte stream with the data that must be loaded.
121      * @see net.obsearch.Storable#load(com.sleepycat.bind.tuple.TupleInput)
122      */
123     public final void load(byte[] in) throws OBException, IOException {
124         
125         updateTree(new String(in));
126     }
127 
128     /**
129      * Stores this object into the given byte stream.
130      * @param out
131      *                The byte stream to be used
132      * @see net.obsearch.Storable#store(com.sleepycat.bind.tuple.TupleOutput)
133      */
134     public final byte[] store() throws IOException{
135         return tree.toFuriaChanTree().getBytes();
136     }
137 
138     /**
139      * Returns true of this.tree.equals(obj.tree). For this distance function
140      * this.distance(obj) == 0 implies that this.equals(obj) == true
141      * @param obj
142      *                Object to compare.
143      * @return true if this == obj
144      */
145     public final boolean equals(final Object obj) {
146         return this.tree.equalsTree(((OBTed) obj).tree);
147     }
148 
149     /**
150      * A hashCode based on the string representation of the tree.
151      * @return a hash code of the string representation of this tree.
152      */
153     public final int hashCode() {
154         return this.hashCode;
155     }
156 
157 }