[305] | 1 | package de.ugoe.cs.eventbench.jfc.data;
|
---|
[299] | 2 |
|
---|
[300] | 3 | import java.util.HashMap;
|
---|
| 4 | import java.util.Map;
|
---|
| 5 |
|
---|
[299] | 6 | import de.ugoe.cs.eventbench.data.IReplayable;
|
---|
| 7 | import de.ugoe.cs.eventbench.data.ReplayableEvent;
|
---|
[359] | 8 | import de.ugoe.cs.eventbench.jfc.JFCLogParser;
|
---|
[299] | 9 |
|
---|
| 10 | /**
|
---|
| 11 | * <p>
|
---|
| 12 | * This class defines JFC events.
|
---|
| 13 | * </p>
|
---|
| 14 | *
|
---|
| 15 | * @author Steffen Herbold
|
---|
| 16 | * @version 1.0
|
---|
| 17 | */
|
---|
| 18 | public class JFCEvent extends ReplayableEvent<IReplayable> {
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * <p>
|
---|
| 22 | * Id for object serialization.
|
---|
| 23 | * </p>
|
---|
| 24 | */
|
---|
| 25 | private static final long serialVersionUID = 1L;
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * <p>
|
---|
[300] | 29 | * Internal map of parameters associated with the event.
|
---|
| 30 | * </p>
|
---|
| 31 | */
|
---|
| 32 | private Map<String, String> parameters;
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * <p>
|
---|
| 36 | * Information about the event source.
|
---|
| 37 | * </p>
|
---|
| 38 | */
|
---|
| 39 | private Map<String, String> sourceParameters;
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * <p>
|
---|
| 43 | * Information about the parent of the event source.
|
---|
| 44 | * </p>
|
---|
| 45 | */
|
---|
| 46 | private Map<String, String> parentParameters;
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * <p>
|
---|
[299] | 50 | * Constructor. Creates a new JFCEvent.
|
---|
| 51 | * </p>
|
---|
| 52 | *
|
---|
| 53 | * @param type
|
---|
| 54 | * type of the event
|
---|
| 55 | */
|
---|
| 56 | public JFCEvent(String type) {
|
---|
| 57 | super(type);
|
---|
[300] | 58 | parameters = new HashMap<String, String>();
|
---|
[304] | 59 | sourceParameters = new HashMap<String, String>();
|
---|
| 60 | parentParameters = new HashMap<String, String>();
|
---|
[299] | 61 | }
|
---|
| 62 |
|
---|
[300] | 63 | /**
|
---|
| 64 | * <p>
|
---|
| 65 | * Adds a new parameter to the event.
|
---|
| 66 | * </p>
|
---|
| 67 | *
|
---|
| 68 | * @param name
|
---|
| 69 | * name of the parameter
|
---|
| 70 | * @param value
|
---|
| 71 | * value of the parameter
|
---|
| 72 | */
|
---|
| 73 | public void addParameter(String name, String value) {
|
---|
| 74 | parameters.put(name, value);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * <p>
|
---|
| 79 | * Retrieves the value of a parameter.
|
---|
| 80 | * </p>
|
---|
| 81 | *
|
---|
| 82 | * @param name
|
---|
| 83 | * name of the parameter
|
---|
| 84 | * @return value of the parameter
|
---|
| 85 | */
|
---|
| 86 | public String getParameter(String name) {
|
---|
| 87 | return parameters.get(name);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * <p>
|
---|
| 92 | * Adds new information about the source of the event.
|
---|
| 93 | * </p>
|
---|
| 94 | *
|
---|
| 95 | * @param name
|
---|
| 96 | * name of the information
|
---|
| 97 | * @param value
|
---|
| 98 | * value of the information
|
---|
| 99 | */
|
---|
| 100 | public void addSourceInformation(String name, String value) {
|
---|
| 101 | sourceParameters.put(name, value);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * <p>
|
---|
| 106 | * Retrieves information about the source of the event.
|
---|
| 107 | * </p>
|
---|
| 108 | *
|
---|
| 109 | * @param name
|
---|
| 110 | * name of the information
|
---|
| 111 | * @return value of the information
|
---|
| 112 | */
|
---|
| 113 | public String getSourceInformation(String name) {
|
---|
| 114 | return sourceParameters.get(name);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * <p>
|
---|
| 119 | * Adds new information about the parent of the source of the event.
|
---|
| 120 | * </p>
|
---|
| 121 | *
|
---|
| 122 | * @param name
|
---|
| 123 | * name of the information
|
---|
| 124 | * @param value
|
---|
| 125 | * value of the information
|
---|
| 126 | */
|
---|
| 127 | public void addParentInformation(String name, String value) {
|
---|
| 128 | parentParameters.put(name, value);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | * <p>
|
---|
[359] | 133 | * Used by the {@link JFCLogParser} to extend the target string of the
|
---|
| 134 | * current event with a further ancestor. The resulting target string will
|
---|
| 135 | * have the structure {@code etc.grandparent.parent.eventtarget}.
|
---|
| 136 | * </p>
|
---|
| 137 | *
|
---|
| 138 | * @param extension
|
---|
| 139 | * extension for the target.
|
---|
| 140 | */
|
---|
| 141 | public void extendTarget(String extension) {
|
---|
| 142 | if (target == null || "".equals(target)) {
|
---|
| 143 | target = extension;
|
---|
| 144 | } else {
|
---|
| 145 | target += "." + extension;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
| 150 | * <p>
|
---|
[300] | 151 | * Retrieves information about the parent of the source of the event.
|
---|
| 152 | * </p>
|
---|
| 153 | *
|
---|
| 154 | * @param name
|
---|
| 155 | * name of the information
|
---|
| 156 | * @return value of the information
|
---|
| 157 | */
|
---|
| 158 | public String getParentInformation(String name) {
|
---|
| 159 | return parentParameters.get(name);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[376] | 162 | /**
|
---|
| 163 | * <p>
|
---|
| 164 | * This method implements the comparison between two targets of JFCEvents.
|
---|
| 165 | * The targets are equal, if they have the same placement in the widget
|
---|
| 166 | * hierarchy, i.e., the target strings describe the same widgets, according
|
---|
| 167 | * to the implementation of widget equality provided by
|
---|
| 168 | * {@link #compareWidgets(String, String)}.
|
---|
| 169 | * </p>
|
---|
[300] | 170 | *
|
---|
[376] | 171 | * @see de.ugoe.cs.eventbench.data.Event#targetEquals(java.lang.String)
|
---|
[300] | 172 | */
|
---|
| 173 | @Override
|
---|
[376] | 174 | protected boolean targetEquals(String otherTarget) {
|
---|
| 175 | if( target==null || otherTarget==null ) {
|
---|
| 176 | return target==otherTarget;
|
---|
[300] | 177 | }
|
---|
[376] | 178 | String[] targetParts = target.split("\\]\\.\\[");
|
---|
| 179 | String[] otherParts = otherTarget.split("\\]\\.\\[");
|
---|
| 180 | if (targetParts.length != otherParts.length) {
|
---|
| 181 | return false;
|
---|
[300] | 182 | }
|
---|
[376] | 183 |
|
---|
| 184 | boolean retVal;
|
---|
| 185 | if (targetParts.length == 0) {
|
---|
| 186 | retVal = compareWidgets(target, otherTarget);
|
---|
| 187 | } else {
|
---|
| 188 | retVal = true;
|
---|
| 189 | for (int i = 0; retVal && i < targetParts.length; i++) {
|
---|
| 190 | retVal &= compareWidgets(targetParts[i], otherParts[i]);
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | return retVal;
|
---|
[300] | 194 | }
|
---|
| 195 |
|
---|
[376] | 196 | /**
|
---|
| 197 | * <p>
|
---|
| 198 | * Compares two widget strings of the form
|
---|
| 199 | * {@code ['title','class','index','text','hashCode']}.
|
---|
| 200 | * </p>
|
---|
[300] | 201 | *
|
---|
[376] | 202 | * @param widget1
|
---|
| 203 | * @param widget2
|
---|
| 204 | * @return
|
---|
[300] | 205 | */
|
---|
[376] | 206 | private boolean compareWidgets(String widget1, String widget2) {
|
---|
| 207 | String[] widgetInfo1 = widget1.split("','");
|
---|
| 208 | String[] widgetInfo2 = widget2.split("','");
|
---|
| 209 | // ensure size is equal
|
---|
| 210 | if (widgetInfo1.length != 5 || widgetInfo2.length != 5) {
|
---|
| 211 | return false;
|
---|
| 212 | }
|
---|
| 213 | // ensure that class [1], index [2], and text [3] are equal
|
---|
| 214 | // and title [0] or hashCode [4] are equal
|
---|
| 215 | return (widgetInfo1[1].equals(widgetInfo2[1])
|
---|
| 216 | && widgetInfo1[2].equals(widgetInfo2[2]) && widgetInfo1[3]
|
---|
| 217 | .equals(widgetInfo2[3]))
|
---|
| 218 | && (widgetInfo1[0].equals(widgetInfo2[0]) || widgetInfo1[4]
|
---|
| 219 | .equals(widgetInfo2[4]));
|
---|
| 220 |
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[300] | 223 | @Override
|
---|
[376] | 224 | protected int targetHashCode() {
|
---|
| 225 | int hashCode = 0;
|
---|
| 226 | int multiplier = 29;
|
---|
| 227 | if( target!=null ) {
|
---|
| 228 | String[] targetParts = target.split("\\[\\.\\[");
|
---|
| 229 | if( targetParts.length==0 ) {
|
---|
| 230 | hashCode = widgetHashCode(target);
|
---|
| 231 | } else {
|
---|
| 232 | for( String widgetString : targetParts ) {
|
---|
| 233 | hashCode = hashCode * multiplier + widgetHashCode(widgetString);
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[300] | 238 | return hashCode;
|
---|
| 239 | }
|
---|
[376] | 240 |
|
---|
| 241 | private int widgetHashCode(String widget) {
|
---|
| 242 | int hashCode = 0;
|
---|
| 243 | int multiplier = 37;
|
---|
| 244 | String[] widgetInfo = widget.split("','");
|
---|
| 245 | if( widgetInfo.length==5 ) {
|
---|
| 246 | hashCode = hashCode * multiplier + widgetInfo[1].hashCode();
|
---|
| 247 | hashCode = hashCode * multiplier + widgetInfo[2].hashCode();
|
---|
| 248 | hashCode = hashCode * multiplier + widgetInfo[3].hashCode();
|
---|
| 249 | }
|
---|
| 250 | return hashCode;
|
---|
| 251 | }
|
---|
[300] | 252 |
|
---|
[299] | 253 | }
|
---|