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.SequenceElementEvent;
027: import jacareto.jacshow.SequenceElementEventRecordable;
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 sequence elements.
036: *
037: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
038: * @version 1.0
039: */
040: public class SequenceElement extends StructureElement {
041: /** The name of the sequence element. */
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 duration. */
051: private long seDuration;
052:
053: /**
054: * Creates a new sequence structure element.
055: *
056: * @param env the environment
057: * @param children the child structure elements
058: */
059: public SequenceElement(Environment env, StructureElement[] children) {
060: super (env, children);
061:
062: SequenceElementEventRecordable seStart = (SequenceElementEventRecordable) children[0];
063: SequenceElementEventRecordable seStop = (SequenceElementEventRecordable) children[children.length - 1];
064: name = seStart.getSequenceElementName();
065: startTime = seStart.getWhen();
066: stopTime = seStop.getWhen();
067: }
068:
069: /**
070: * Returns the name of the sequence element
071: *
072: * @return the name as string
073: */
074: public String getName() {
075: return name;
076: }
077:
078: /**
079: * Returns the duration.
080: *
081: * @return the duration, in msec
082: */
083: public long getDuration() {
084: return stopTime - startTime;
085: }
086:
087: /**
088: * Parses a record which is tokenized by the given record tokenizer.
089: *
090: * @param env DOCUMENT ME!
091: * @param recordTokenizer the record tokenizer
092: *
093: * @return a structure element, or <code>null</code> if this class cannot parse the record at
094: * the current position
095: */
096: public static StructureElement parse(Environment env,
097: RecordTokenizer recordTokenizer) {
098: StructureElement result = null;
099:
100: RecordTokenizerState rtState = recordTokenizer.saveState();
101:
102: try {
103: SequenceElementEventRecordable seStart = (SequenceElementEventRecordable) recordTokenizer
104: .next();
105:
106: if (seStart.getID() != SequenceElementEvent.STARTED) {
107: throw new Exception();
108: }
109:
110: String startedName = seStart.getSequenceElementName();
111:
112: Vector elements = new Vector(20, 20);
113: elements.add(seStart);
114:
115: StructureElement next = null;
116:
117: do {
118: next = recordTokenizer.next();
119: elements.add(next);
120: } while ((next != null)
121: && !(next instanceof SequenceElementEventRecordable));
122:
123: if ((next != null)
124: && (next instanceof SequenceElementEventRecordable)) {
125: SequenceElementEventRecordable seStop = (SequenceElementEventRecordable) next;
126:
127: if ((seStop.getID() != SequenceElementEvent.STOPPED)
128: || !seStop.getSequenceElementName().equals(
129: startedName)) {
130: throw new Exception();
131: }
132: } else {
133: throw new Exception();
134: }
135:
136: StructureElement[] children = vectorToArray(elements);
137:
138: result = new SequenceElement(env, children);
139: } catch (Throwable t) {
140: recordTokenizer.restoreState(rtState);
141: }
142:
143: return result;
144: }
145:
146: /**
147: * Returns the name of the element.
148: *
149: * @return the name
150: */
151: public String getElementName() {
152: return language.getString("Structures.SequenceElement.Name");
153: }
154:
155: /**
156: * Returns a description of the element.
157: *
158: * @return the description
159: */
160: public String getElementDescription() {
161: return language
162: .getString("Structures.SequenceElement.Description");
163: }
164:
165: /**
166: * Returns a String which describes the content of the element shortly.
167: *
168: * @return a string with a short description of the element
169: */
170: public String toShortString() {
171: return getName();
172: }
173:
174: /**
175: * Clones the element.
176: *
177: * @return DOCUMENT ME!
178: */
179: public Object clone() {
180: StructureElement[] clonedChildren = getClonedChildren();
181:
182: return new SequenceElement(env, clonedChildren);
183: }
184: }
|