PageRenderTime 56ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/src/edu/ucr/cs/jumpwisely/topk_research/data_model/data_mdint/DataObject.java

http://jumpwisely.googlecode.com/
Java | 287 lines | 203 code | 28 blank | 56 comment | 51 complexity | aeaf092cc486044afcdf01bcf915b3b2 MD5 | raw file
  1. /**
  2. * Topk_Research DataObject.java
  3. *
  4. * Copyright (C) 2010 JArod Wen
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>
  18. **/
  19. package edu.ucr.cs.jumpwisely.topk_research.data_model.data_mdint;
  20. import java.util.Vector;
  21. public class DataObject {
  22. Vector<DimValuePair> dim_values;
  23. int value;
  24. public DataObject(Vector<DimValuePair> dim_values, int value){
  25. Vector<DimValuePair> dvps = new Vector<DimValuePair>();
  26. for(int i = 0; i < dim_values.size(); i++){
  27. dvps.add(dim_values.get(i).copy());
  28. }
  29. this.dim_values = dvps;
  30. this.value = value;
  31. }
  32. public int getDimValue(int dim){
  33. for(int i = 0; i < this.dim_values.size(); i++){
  34. if(this.dim_values.get(i).dim == dim){
  35. return this.dim_values.get(i).value;
  36. }
  37. }
  38. throw new UnknownError("Cannot find the dimension " + dim + " from " + this.toString());
  39. }
  40. public void setDimValue(int dim, int dim_value){
  41. for(int i = 0; i < this.dim_values.size(); i++){
  42. if(this.dim_values.get(i).dim == dim){
  43. this.dim_values.get(i).setValue(dim_value);
  44. return;
  45. }
  46. }
  47. throw new UnknownError("Set value " + dim_value + " for dim " + dim + " out of the dimensions of " + this.toString() + "!");
  48. }
  49. public boolean isDominatedBy(DataObject another_obj){
  50. if(another_obj.getDimNum() != this.getDimNum()){
  51. throw new UnknownError("Compare two objects with different dimensions: " + this.toString() + " and " + another_obj.toString());
  52. }else{
  53. for(DimValuePair dv_pair : dim_values){
  54. if(dv_pair.getValue() > another_obj.getDimValue(dv_pair.dim)){
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. }
  61. public int getValue(){
  62. return this.value;
  63. }
  64. public int getDimNum(){
  65. return this.dim_values.size();
  66. }
  67. public Vector<DimValuePair> getDims(){
  68. Vector<DimValuePair> dim_list = new Vector<DimValuePair>();
  69. for(int i = 0; i < this.dim_values.size(); i++){
  70. dim_list.add(new DimValuePair(this.dim_values.get(i).dim, 0));
  71. }
  72. return dim_list;
  73. }
  74. public Vector<DimValuePair> getDimsWithValues(){
  75. Vector<DimValuePair> dim_list = new Vector<DimValuePair>();
  76. for(int i = 0; i < this.dim_values.size(); i++){
  77. dim_list.add(new DimValuePair(this.dim_values.get(i).dim, this.dim_values.get(i).value));
  78. }
  79. return dim_list;
  80. }
  81. public String toXMLString(){
  82. String xmlstr = "<DataObject dims=\"" + this.getDimNum() + "\" value=\"" + value + "\">";
  83. for(int i = 0; i < dim_values.size(); i++){
  84. xmlstr += dim_values.get(i).toString();
  85. }
  86. return xmlstr + "</DataObject>";
  87. }
  88. /**
  89. * Return the size of the object, which is the sum of
  90. * - dimension
  91. * - value on each dimension
  92. * - the weight value assigned
  93. *
  94. * @return the size of the object in byte
  95. */
  96. public int getSize(){
  97. return this.getFullSize();
  98. }
  99. /**
  100. * Project the data object onto a dimension, which
  101. * returns another data object with a lower dimensionality.
  102. * @param dim The dim to be projected
  103. * @return The data object after projected.
  104. */
  105. public DataObject projectOnToDim(int dim){
  106. DataObject newObj = this.copy();
  107. newObj.dim_values.remove(newObj.getIndexOfDim(dim));
  108. return newObj;
  109. }
  110. public String toString(){
  111. return this.toXMLString();
  112. }
  113. /**
  114. * Return the dim of the dim-value pair at the
  115. * given index.
  116. * @param i
  117. * @return
  118. */
  119. public int getDimAtIndex(int i){
  120. if(i < 0 || i >= this.getDimNum())
  121. return -1;
  122. else
  123. return this.dim_values.get(i).dim;
  124. }
  125. /**
  126. * Return the index of the dim-value pair for a
  127. * given dim.
  128. * @param dim
  129. * @return
  130. */
  131. public int getIndexOfDim(int dim){
  132. for(int i = 0; i < this.dim_values.size(); i++){
  133. if(this.dim_values.get(i).dim == dim)
  134. return i;
  135. }
  136. return -1;
  137. }
  138. public void setValue(int value){
  139. this.value = value;
  140. }
  141. public boolean equals(Object obj){
  142. if(obj instanceof DataObject){
  143. DataObject dobj = (DataObject)obj;
  144. if(dobj.getDimNum() == this.getDimNum()){
  145. for(int i = 0; i < this.getDimNum(); i++){
  146. int jj = -1;
  147. for(int j = 0; j < this.dim_values.size(); j++){
  148. if(this.dim_values.get(j).getDim() == dobj.getDims().get(i).getDim()
  149. && this.dim_values.get(j).getValue() == dobj.dim_values.get(i).getValue()){
  150. jj = j;
  151. break;
  152. }else{
  153. continue;
  154. }
  155. }
  156. if(jj < 0){
  157. return false;
  158. }else{
  159. continue;
  160. }
  161. }
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. public boolean isSimlar(Object obj){
  168. if(obj instanceof DataObject){
  169. DataObject dobj = (DataObject)obj;
  170. if(dobj.getDimNum() == this.getDimNum()){
  171. for(int i = 0; i < this.getDimNum(); i++){
  172. boolean isFound = false;
  173. for(int j = 0; j < this.dim_values.size(); j++){
  174. if(this.dim_values.get(j).dim == dobj.getDims().get(i).dim)
  175. isFound = true;
  176. }
  177. if(isFound)
  178. continue;
  179. else
  180. return false;
  181. }
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. public DataObject copy(){
  188. Vector<DimValuePair> new_dim_values = new Vector<DimValuePair>();
  189. for(DimValuePair dvp : dim_values){
  190. new_dim_values.add(dvp.copy());
  191. }
  192. return new DataObject(new_dim_values, this.value);
  193. }
  194. public DataObject removeDim(int dim){
  195. DataObject new_obj = this.copy();
  196. new_obj.dim_values.remove(new_obj.getIndexOfDim(dim));
  197. return new_obj;
  198. }
  199. public boolean projectable(DataObject obj){
  200. if(this.getDimNum() >= obj.getDimNum()){
  201. for(DimValuePair dvp : obj.dim_values){
  202. if(this.getIndexOfDim(dvp.dim) < 0)
  203. return false;
  204. }
  205. return true;
  206. }
  207. return false;
  208. }
  209. public boolean projectable(Vector<DimValuePair> dim_list){
  210. if(this.getDimNum() >= dim_list.size()){
  211. for(DimValuePair dvp : dim_list){
  212. if(this.getIndexOfDim(dvp.dim) < 0)
  213. return false;
  214. }
  215. return true;
  216. }
  217. return false;
  218. }
  219. public DataObject projectOnToDims(Vector<DimValuePair> dim_list){
  220. DataObject newObj = this.copy();
  221. if(newObj.projectable(dim_list)){
  222. int i = 0;
  223. while(i < newObj.getDimNum()){
  224. boolean proj_flag = true;
  225. for(int j = 0; j < dim_list.size(); j++){
  226. if(dim_list.get(j).dim == newObj.dim_values.get(i).dim){
  227. proj_flag = false;
  228. }
  229. }
  230. if(proj_flag){
  231. newObj.dim_values.remove(i);
  232. continue;
  233. }
  234. i++;
  235. }
  236. return newObj;
  237. }else{
  238. return null;
  239. }
  240. }
  241. /**
  242. * Return the actual size of this data object.
  243. *
  244. * Notice that although only two elements {@link #dim_values} and {@link #value}
  245. * are in this class, when dumping an object onto the disk, we also need to store
  246. * the number of dimensions as an integer onto the disk, in case that when we need
  247. * to load this object, we can know when to stop loading.
  248. *
  249. * @return
  250. */
  251. public int getFullSize(){
  252. // The size of a data object consists of three elements: number of dimensions,
  253. // dimensionality, and the value.
  254. if(this.value != 0)
  255. return 4 + this.dim_values.size() * 4 + 4;
  256. else
  257. return 0;
  258. }
  259. }