[29] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * Element.java |
---|
| 19 | * Copyright (C) 2007 Sebastian Beer |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.associations.gsp; |
---|
| 24 | |
---|
| 25 | import weka.core.FastVector; |
---|
| 26 | import weka.core.Instance; |
---|
| 27 | import weka.core.Instances; |
---|
| 28 | import weka.core.RevisionHandler; |
---|
| 29 | import weka.core.RevisionUtils; |
---|
| 30 | |
---|
| 31 | import java.io.Serializable; |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * Class representing an Element, i.e., a set of events/items. |
---|
| 35 | * |
---|
| 36 | * @author Sebastian Beer |
---|
| 37 | * @version $Revision: 1.2 $ |
---|
| 38 | */ |
---|
| 39 | public class Element |
---|
| 40 | implements Cloneable, Serializable, RevisionHandler { |
---|
| 41 | |
---|
| 42 | /** for serialization */ |
---|
| 43 | private static final long serialVersionUID = -7900701276019516371L; |
---|
| 44 | |
---|
| 45 | /** events/items stored as an array of ints */ |
---|
| 46 | protected int[] m_Events; |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Constructor |
---|
| 50 | */ |
---|
| 51 | public Element() { |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * Constructor accepting an initial size of the events Array as parameter. |
---|
| 56 | * |
---|
| 57 | * @param size the size |
---|
| 58 | */ |
---|
| 59 | public Element(int size) { |
---|
| 60 | m_Events = new int[size]; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Returns all events of the given data set as Elements containing a single |
---|
| 65 | * event. The order of events is determined by the header information of |
---|
| 66 | * the corresponding ARFF file. |
---|
| 67 | * |
---|
| 68 | * @param instances the data set |
---|
| 69 | * @return the set of 1-Elements |
---|
| 70 | */ |
---|
| 71 | public static FastVector getOneElements (Instances instances) { |
---|
| 72 | FastVector setOfOneElements = new FastVector(); |
---|
| 73 | Element curElement; |
---|
| 74 | |
---|
| 75 | for (int i = 0; i < instances.numAttributes(); i++) { |
---|
| 76 | for (int j = 0; j < instances.attribute(i).numValues(); j++) { |
---|
| 77 | curElement = new Element(); |
---|
| 78 | curElement.setEvents(new int [instances.numAttributes()]); |
---|
| 79 | for (int k = 0; k < instances.numAttributes(); k++) { |
---|
| 80 | curElement.getEvents()[k] = -1; |
---|
| 81 | } |
---|
| 82 | curElement.getEvents()[i] = j; |
---|
| 83 | setOfOneElements.addElement(curElement); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | return setOfOneElements; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Merges two Elements into one. |
---|
| 91 | * |
---|
| 92 | * @param element1 first Element |
---|
| 93 | * @param element2 second Element |
---|
| 94 | * @return the merged Element |
---|
| 95 | */ |
---|
| 96 | public static Element merge(Element element1, Element element2) { |
---|
| 97 | int[] element1Events = element1.getEvents(); |
---|
| 98 | int[] element2Events = element2.getEvents(); |
---|
| 99 | Element resultElement = new Element(element1Events.length); |
---|
| 100 | int[] resultEvents = resultElement.getEvents(); |
---|
| 101 | |
---|
| 102 | for (int i = 0; i < element1Events.length; i++) { |
---|
| 103 | if (element2Events[i] > -1) { |
---|
| 104 | resultEvents[i] = element2Events[i]; |
---|
| 105 | } else { |
---|
| 106 | resultEvents[i] = element1Events[i]; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | resultElement.setEvents(resultEvents); |
---|
| 110 | |
---|
| 111 | return resultElement; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * Returns a deep clone of an Element. |
---|
| 116 | * |
---|
| 117 | * @return the cloned Element |
---|
| 118 | */ |
---|
| 119 | public Element clone() { |
---|
| 120 | try { |
---|
| 121 | Element clone = (Element) super.clone(); |
---|
| 122 | int[] cloneEvents = new int[m_Events.length]; |
---|
| 123 | |
---|
| 124 | for (int i = 0; i < m_Events.length; i++) { |
---|
| 125 | cloneEvents[i] = m_Events[i]; |
---|
| 126 | } |
---|
| 127 | clone.setEvents(cloneEvents); |
---|
| 128 | |
---|
| 129 | return clone; |
---|
| 130 | } catch (CloneNotSupportedException exc) { |
---|
| 131 | exc.printStackTrace(); |
---|
| 132 | } |
---|
| 133 | return null; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * Checks if an Element contains over one event. |
---|
| 138 | * |
---|
| 139 | * @return true, if the Element contains over one event, else false |
---|
| 140 | */ |
---|
| 141 | public boolean containsOverOneEvent() { |
---|
| 142 | int numEvents = 0; |
---|
| 143 | for (int i = 0; i < m_Events.length; i++) { |
---|
| 144 | if (m_Events[i] > -1) { |
---|
| 145 | numEvents++; |
---|
| 146 | } |
---|
| 147 | if (numEvents == 2) { |
---|
| 148 | return true; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | return false; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * Deletes the first or last event of an Element. |
---|
| 156 | * |
---|
| 157 | * @param position the position of the event to be deleted (first or last) |
---|
| 158 | */ |
---|
| 159 | public void deleteEvent(String position) { |
---|
| 160 | if (position.equals("first")) { |
---|
| 161 | //delete first event |
---|
| 162 | for (int i = 0; i < m_Events.length; i++) { |
---|
| 163 | if (m_Events[i] > -1) { |
---|
| 164 | m_Events[i] = -1; |
---|
| 165 | break; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | if (position.equals("last")) { |
---|
| 170 | //delete last event |
---|
| 171 | for (int i = m_Events.length-1; i >= 0; i--) { |
---|
| 172 | if (m_Events[i] > -1) { |
---|
| 173 | m_Events[i] = -1; |
---|
| 174 | break; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * Checks if two Elements are equal. |
---|
| 182 | * |
---|
| 183 | * @return true, if the two Elements are equal, else false |
---|
| 184 | */ |
---|
| 185 | public boolean equals(Object obj) { |
---|
| 186 | Element element2 = (Element) obj; |
---|
| 187 | |
---|
| 188 | for (int i=0; i < m_Events.length; i++) { |
---|
| 189 | if (!(m_Events[i] == element2.getEvents()[i])) { |
---|
| 190 | return false; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | return true; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | * Returns the events Array of an Element. |
---|
| 198 | * |
---|
| 199 | * @return the events Array |
---|
| 200 | */ |
---|
| 201 | public int[] getEvents() { |
---|
| 202 | return m_Events; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /** |
---|
| 206 | * Checks if an Element is contained by a given Instance. |
---|
| 207 | * |
---|
| 208 | * @param instance the given Instance |
---|
| 209 | * @return true, if the Instance contains the Element, else false |
---|
| 210 | */ |
---|
| 211 | public boolean isContainedBy(Instance instance) { |
---|
| 212 | for (int i=0; i < instance.numAttributes(); i++) { |
---|
| 213 | if (m_Events[i] > -1) { |
---|
| 214 | if (instance.isMissing(i)) { |
---|
| 215 | return false; |
---|
| 216 | } |
---|
| 217 | if (m_Events[i] != (int) instance.value(i)) { |
---|
| 218 | return false; |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | return true; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | * Checks if the Element contains any events. |
---|
| 227 | * |
---|
| 228 | * @return true, if the Element contains no event, else false |
---|
| 229 | */ |
---|
| 230 | public boolean isEmpty() { |
---|
| 231 | for (int i=0; i < m_Events.length; i++) { |
---|
| 232 | if (m_Events[i] > -1) { |
---|
| 233 | return false; |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | return true; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * Sets the events Array of an Element. |
---|
| 241 | * |
---|
| 242 | * @param events the events Array to set |
---|
| 243 | */ |
---|
| 244 | protected void setEvents(int[] events) { |
---|
| 245 | m_Events = events; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | * Returns a String representation of an Element where the numeric value |
---|
| 250 | * of each event/item is represented by its respective nominal value. |
---|
| 251 | * |
---|
| 252 | * @param dataSet the corresponding data set containing the header information |
---|
| 253 | * @return the String representation |
---|
| 254 | */ |
---|
| 255 | public String toNominalString(Instances dataSet) { |
---|
| 256 | StringBuffer result = new StringBuffer(); |
---|
| 257 | int addedValues = 0; |
---|
| 258 | |
---|
| 259 | result.append("{"); |
---|
| 260 | |
---|
| 261 | for (int i=0; i < m_Events.length; i++) { |
---|
| 262 | if (m_Events[i] > -1) { |
---|
| 263 | result.append(dataSet.attribute(i).value(m_Events[i]) + ","); |
---|
| 264 | addedValues++; |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | result.deleteCharAt(result.length()-1); |
---|
| 268 | result.append("}"); |
---|
| 269 | |
---|
| 270 | return result.toString(); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | /** |
---|
| 274 | * Returns a String representation of an Element. |
---|
| 275 | * |
---|
| 276 | * @return the String representation |
---|
| 277 | */ |
---|
| 278 | public String toString() { |
---|
| 279 | String result = ""; |
---|
| 280 | |
---|
| 281 | result += "{"; |
---|
| 282 | |
---|
| 283 | for (int i=0; i < m_Events.length; i++) { |
---|
| 284 | result += m_Events[i]; |
---|
| 285 | if (i+1 < m_Events.length) { |
---|
| 286 | result += ","; |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | result += "}"; |
---|
| 290 | |
---|
| 291 | return result; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | /** |
---|
| 295 | * Returns the revision string. |
---|
| 296 | * |
---|
| 297 | * @return the revision |
---|
| 298 | */ |
---|
| 299 | public String getRevision() { |
---|
| 300 | return RevisionUtils.extract("$Revision: 1.2 $"); |
---|
| 301 | } |
---|
| 302 | } |
---|