001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.struct;
025:
026: import jacareto.jacshow.SequenceEvent;
027: import jacareto.jacshow.SequenceEventRecordable;
028: import jacareto.parse.RecordTokenizer;
029: import jacareto.parse.RecordTokenizerState;
030: import jacareto.system.Environment;
031:
032: import java.util.Vector;
033:
034: /**
035: * A structure for JacShow sequences.
036: *
037: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
038: * @version 1.0
039: */
040: public class Sequence extends StructureElement {
041: /** The name of the sequence. */
042: private String name;
043:
044: /** The start time. */
045: private long startTime;
046:
047: /** The stop time. */
048: private long stopTime;
049:
050: /** The performance duration. */
051: private long performanceDuration;
052:
053: /** Special performance values */
054: private long durationA;
055:
056: /** Special performance values */
057: private long durationB;
058: private int countB;
059:
060: /**
061: * Creates a new sequence.
062: *
063: * @param env the environment
064: * @param children the child structure elements
065: */
066: public Sequence(Environment env, StructureElement[] children) {
067: super (env, children);
068:
069: SequenceEventRecordable seStart = (SequenceEventRecordable) children[0];
070: SequenceEventRecordable seStop = (SequenceEventRecordable) children[children.length - 1];
071: name = seStart.getSequenceName();
072: startTime = seStart.getWhen();
073: stopTime = seStop.getWhen();
074: calculatePerformanceValues();
075: }
076:
077: /**
078: * Returns the name of the sequence
079: *
080: * @return the name as string
081: */
082: public String getName() {
083: return name;
084: }
085:
086: /**
087: * Returns the duration.
088: *
089: * @return the duration, in msec
090: */
091: public long getDuration() {
092: return stopTime - startTime;
093: }
094:
095: /**
096: * Calculates the performance duration.
097: */
098: private void calculatePerformanceValues() {
099: // performance duration
100: performanceDuration = getDuration();
101: durationA = durationB = 0L;
102: countB = 0;
103:
104: StructureElement[] seChildren = getChildren("jacareto.struct.SequenceElement");
105:
106: for (int i = 0; i < seChildren.length; i++) {
107: SequenceElement element = (SequenceElement) seChildren[i];
108: String elementName = element.getName();
109:
110: performanceDuration -= element.getDuration();
111:
112: if (elementName.startsWith("A ")) {
113: durationA += element.getDuration();
114: } else if (elementName.startsWith("B ")) {
115: durationB += element.getDuration();
116: countB++;
117: }
118: }
119: }
120:
121: /**
122: * Returns the performance duration
123: *
124: * @return the duration without the duration of children which are sequence elements.
125: */
126: public long getPerformanceDuration() {
127: return performanceDuration;
128: }
129:
130: /**
131: * Returns the duration A
132: *
133: * @return the duration
134: */
135: public long getDurationA() {
136: return durationA;
137: }
138:
139: /**
140: * Returns the duration B
141: *
142: * @return the duration
143: */
144: public long getDurationB() {
145: return durationB;
146: }
147:
148: /**
149: * Returns count B
150: *
151: * @return the count
152: */
153: public int getCountB() {
154: return countB;
155: }
156:
157: /**
158: * Parses a record which is tokenized by the given record tokenizer.
159: *
160: * @param env DOCUMENT ME!
161: * @param recordTokenizer the record tokenizer
162: *
163: * @return a structure element, or <code>null</code> if this class cannot parse the record at
164: * the current position
165: */
166: public static StructureElement parse(Environment env,
167: RecordTokenizer recordTokenizer) {
168: StructureElement result = null;
169:
170: RecordTokenizerState rtState = recordTokenizer.saveState();
171:
172: try {
173: SequenceEventRecordable seStart = (SequenceEventRecordable) recordTokenizer
174: .next();
175:
176: if (seStart.getID() != SequenceEvent.STARTED) {
177: throw new Exception();
178: }
179:
180: String startedName = seStart.getSequenceName();
181:
182: Vector elements = new Vector(20, 20);
183: elements.add(seStart);
184:
185: StructureElement next = null;
186:
187: do {
188: next = SequenceElement.parse(env, recordTokenizer);
189:
190: if (next == null) {
191: next = recordTokenizer.next();
192: }
193:
194: elements.add(next);
195: } while ((next != null)
196: && !(next instanceof SequenceEventRecordable));
197:
198: if ((next != null)
199: && (next instanceof SequenceEventRecordable)) {
200: SequenceEventRecordable seStop = (SequenceEventRecordable) next;
201:
202: if ((seStop.getID() != SequenceEvent.STOPPED)
203: || !seStop.getSequenceName()
204: .equals(startedName)) {
205: throw new Exception();
206: }
207: } else {
208: throw new Exception();
209: }
210:
211: StructureElement[] children = vectorToArray(elements);
212:
213: result = new Sequence(env, children);
214: } catch (Throwable t) {
215: recordTokenizer.restoreState(rtState);
216: }
217:
218: return result;
219: }
220:
221: /**
222: * Returns the name of the element.
223: *
224: * @return the name
225: */
226: public String getElementName() {
227: return language.getString("Structures.Sequence.Name");
228: }
229:
230: /**
231: * Returns a description of the element.
232: *
233: * @return the description
234: */
235: public String getElementDescription() {
236: return language.getString("Structures.Sequence.Description");
237: }
238:
239: /**
240: * Returns a String which describes the content of the element shortly.
241: *
242: * @return a string with a short description of the element
243: */
244: public String toShortString() {
245: return getName();
246: }
247:
248: /**
249: * Clones the element.
250: *
251: * @return DOCUMENT ME!
252: */
253: public Object clone() {
254: StructureElement[] clonedChildren = getClonedChildren();
255:
256: return new Sequence(env, clonedChildren);
257: }
258: }
|