1// BEGIN LICENSE BLOCK 2// Version: CMPL 1.1 3// 4// The contents of this file are subject to the Cisco-style Mozilla Public 5// License Version 1.1 (the "License"); you may not use this file except 6// in compliance with the License. You may obtain a copy of the License 7// at www.eclipse-clp.org/license. 8// 9// Software distributed under the License is distributed on an "AS IS" 10// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 11// the License for the specific language governing rights and limitations 12// under the License. 13// 14// The Original Code is The ECLiPSe Constraint Logic Programming System. 15// The Initial Developer of the Original Code is Cisco Systems, Inc. 16// Portions created by the Initial Developer are 17// Copyright (C) 2006 Cisco Systems, Inc. All Rights Reserved. 18// 19// Contributor(s): 20// 21// END LICENSE BLOCK 22 23package com.parctechnologies.eclipse.visualisation; 24 25import java.util.List; 26import java.util.LinkedList; 27import java.util.Iterator; 28import java.io.Serializable; 29import java.io.*; 30 31/** 32 * This is class designed to hold a sequence of changes to the 33 * visualisation client state, intermixed with eclipse events with a 34 * view to replaying these as a response to sufficiently similar 35 * sequence of eclipse events. 36 * 37 * Scenarios will be associated with specific Viewables and will 38 * record user commands carried out on all Viewers associated with that 39 * Viewable. 40 */ 41 42public class Scenario implements Serializable { 43 /** 44 * Holds the name of the viewable for which this scenario applies 45 */ 46 private Object viewableName; 47 48 /** 49 * Stores (in order) the list of events that have been received 50 * from eclipse for this viewable. 51 * 52 * This list will contain instances of the inner class EventCommandList 53 */ 54 private LinkedList eventList; 55 56 /** 57 * Constructs a Scenario for the given Viewable. 58 */ 59 public Scenario(String viewableName) { 60 setViewableName(viewableName); 61 this.eventList = new LinkedList(); 62 } 63 64 65 /** 66 * Add an either an Event or an Command to the Scenario 67 */ 68 public void add(Object obj, int state) { 69 if (obj instanceof VisEvent) { 70 addEvent((VisEvent)obj, state); 71 } else if (obj instanceof Command) { 72 addCommand((Command)obj, state); 73 } else { 74 75 if (DebuggingSupport.logMessages) { 76 DebuggingSupport.logMessage(this, 77 "Unable to add unknown object"); 78 } 79 80 throw new IllegalArgumentException("Scenario was unable to add an object of type "+obj.getClass()); 81 } 82 83 } 84 /** 85 * Records that a visualisation event has taken place 86 */ 87 public void addEvent(VisEvent event, int state) { 88 eventList.add(new EventCommandList(event)); 89 90 if (DebuggingSupport.logMessages) { 91 DebuggingSupport.logMessage(this, "Adding event " + event); 92 } 93 94 } 95 96 /** 97 * Records that a user command has taken place 98 */ 99 public void addCommand(Command command, int state) { 100 ((EventCommandList)(eventList.getLast())).add(command, state); 101 if (DebuggingSupport.logMessages) { 102 DebuggingSupport.logMessage(this, 103 "Adding command " + command + 104 " state=" + state); 105 } 106 107 } 108 109 /** 110 * Get the value of viewableName. 111 * @return value of viewableName. 112 */ 113 public Object getViewableName() { 114 return viewableName; 115 } 116 117 /** 118 * Set the value of viewableName. 119 * @param v Value to assign to viewableName. 120 */ 121 public void setViewableName(String v) { 122 this.viewableName = v; 123 } 124 125 126 /** 127 * Returns the list of EventCommandList objects 128 * 129 */ 130 List getEventList() { 131 return eventList; 132 } 133 134 135 /** 136 * Returns a ScenarioIterator for this scenario 137 */ 138 public ScenarioIterator iterator() { 139 return new ScenarioIterator(this); 140 } 141 142 143 /** 144 * Save the Scenario to a file 145 */ 146 public void save(String fname) { 147 try { 148 FileOutputStream ostream = new FileOutputStream(fname); 149 ObjectOutputStream p = new ObjectOutputStream(ostream); 150 151 p.writeObject(this); 152 153 p.flush(); 154 ostream.close(); 155 } catch(IOException e) { 156 e.printStackTrace(); 157 } 158 } 159 160 161 /** 162 * Return contents of scenario as a string 163 */ 164 public String toString() { 165 StringBuffer sb = 166 new StringBuffer("\n\nScenario for " + viewableName + "\n" ); 167 for(Iterator it = eventList.iterator(); it.hasNext(); ) { 168 EventCommandList ecl = (EventCommandList)(it.next()); 169 sb.append(ecl.toString()); 170 } 171 return sb.toString(); 172 } 173 174 175 /** 176 * A class used to record the user commands which have happened 177 * since the last VisEvent on this Viewable 178 */ 179 final class EventCommandList implements Serializable { 180 /** 181 * Holds the event for which the commands exist 182 */ 183 private VisEvent visEvent; 184 185 /** 186 * Holds the commands associated with this event 187 */ 188 private List commandList; 189 190 /** 191 * Holds the state of the VisClient when the commands were issued 192 */ 193 private List stateList; 194 195 /** 196 * Get the value of commandList. 197 * @return value of commandList. 198 */ 199 public final List getCommandList() { 200 return commandList; 201 } 202 203 /** 204 * Get the value of commandList, filtered by state. 205 * @return list of cammands which were recorded in the given state. 206 */ 207 public final List getCommandList(int state) { 208 List list = new LinkedList(); 209 Iterator itCommand = commandList.iterator(); 210 Iterator itState = stateList.iterator(); 211 while (itCommand.hasNext()) { 212 Command command = (Command)itCommand.next(); 213 Integer integer = (Integer)itState.next(); 214 if (integer.intValue() == state) { 215 list.add(command); 216 } 217 } 218 return list; 219 } 220 221 /** 222 * Set the value of commandList. 223 * @param v Value to assign to commandList. 224 */ 225 private final void setCommandList(List v) { 226 this.commandList = v; 227 } 228 229 /** 230 * Get the value of visEvent. 231 * @return value of visEvent. 232 */ 233 public final VisEvent getVisEvent() { 234 return visEvent; 235 } 236 237 /** 238 * Set the value of visEvent. 239 * @param v Value to assign to visEvent. 240 */ 241 private final void setVisEvent(VisEvent v) { 242 this.visEvent = v; 243 } 244 245 246 /** 247 * Create a new CommandEventList to store the commandsassociated 248 * with the given VisEvent. 249 */ 250 public EventCommandList(VisEvent visEvent) { 251 setCommandList(new LinkedList()); 252 this.stateList = new LinkedList(); 253 setVisEvent(visEvent); 254 } 255 256 /** 257 * Adds an command to the list 258 */ 259 public final void add(Command command, int state) { 260 commandList.add(command); 261 stateList.add(new Integer(state)); 262 } 263 264 265 /** 266 * Returns the command list as a string 267 */ 268 public String toString() { 269 StringBuffer sb = new StringBuffer("\nCommandList\n"); 270 List list = new LinkedList(); 271 Iterator itCommand = commandList.iterator(); 272 Iterator itState = stateList.iterator(); 273 while (itCommand.hasNext()) { 274 Command command = (Command)itCommand.next(); 275 Integer integer = (Integer)itState.next(); 276 sb.append("state:"+integer+" command:"+command); 277 } 278 return sb.toString(); 279 } 280 } 281 282} 283