upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / Relation.java
1 /**
2  * Portions Copyright 2001 Sun Microsystems, Inc.
3  * Portions Copyright 1999-2001 Language Technologies Institute, 
4  * Carnegie Mellon University.
5  * All Rights Reserved.  Use is subject to license terms.
6  * 
7  * See the file "license.terms" for information on usage and
8  * redistribution of this file, and for a DISCLAIMER OF ALL 
9  * WARRANTIES.
10  */
11 package com.sun.speech.freetts;
12
13 import java.io.PrintWriter;
14
15 import com.sun.speech.freetts.util.Utilities;
16
17 /**
18  * Represents an ordered set of <code>Items</code> and their associated
19  * children. A relation has a name and a list of items, and is
20  * added to an <code>Utterance</code> via an <code>UtteranceProcessor</code>.
21  */
22 public class Relation implements Dumpable {
23     private String name;
24     private Utterance owner;
25     private Item head;
26     private Item tail;
27
28     /**
29      * Name of the relation that contains tokens from the original
30      * input text.  This is the first thing to be added to the
31      * utterance.
32      *
33      * @see #WORD
34      */
35     public static final String TOKEN = "Token";
36
37     /**
38      * Name of the relation that contains the normalized version of
39      * the original input text.
40      *
41      * @see #TOKEN
42      */
43     public static final String WORD = "Word";
44
45     /**
46      * Name of the relation that groups elements from the Word relation
47      * into phrases.
48      */
49     public static final String PHRASE = "Phrase";
50
51     /**
52      * Name of the relation that contains the ordered list of the
53      * smallest units of speech (typically phonemes) for the entire
54      * utterance.
55      *
56      * @see #SYLLABLE
57      * @see #SYLLABLE_STRUCTURE
58      *
59      * @see Segmenter
60      */
61     public static final String SEGMENT = "Segment";
62
63     /**
64      * Name of the relation that contains the description of the
65      * syllables for the Utterance.  This is typically added to the
66      * utterance at the same time as the <code>Segment</code> and
67      * <code>SylStructure</code> relations.
68      *
69      * @see #SEGMENT
70      * @see #SYLLABLE_STRUCTURE
71      *
72      * @see Segmenter
73      */
74     public static final String SYLLABLE = "Syllable";
75
76     /**
77      * Name of the relation that contains the syllable structure
78      * for the utterance.
79      *
80      * @see #SEGMENT
81      * @see #SYLLABLE
82      *
83      * @see Segmenter
84      */
85     public static final String SYLLABLE_STRUCTURE = "SylStructure";
86
87     /**
88      * Name of the relation that maps fundamental frequency targets
89      * to absolute times for wave to be generated from the utterance.
90      */
91     public static final String TARGET = "Target";
92
93     /**
94      * Name of the relation that contains the ordered list of the
95      * units from the unit database that will be used to create
96      * the synthesized wave.
97      */
98     public static final String UNIT = "Unit";
99      
100     /**
101      * Creates a relation.
102      *
103      * @param name the name of the Relation
104      * @param owner the utterance that contains this relation
105      */
106     Relation(String name, Utterance owner) {
107         this.name = name;
108         this.owner = owner;
109         head = null;
110         tail = null;
111     }
112
113     /**
114      * Retrieves the name of this Relation.
115      *
116      * @return the name of this Relation
117      */
118     public String getName() {
119         return name;
120     }
121
122     /**
123      * Gets the head of the item list.
124      *
125      * @return the head item
126      */
127     public Item getHead() {
128         return head;
129     }
130
131     /**
132      * Sets the head of the item list.
133      *
134      * @param item the new head item
135      */
136     void setHead(Item item) {
137         head = item;
138     }
139
140     /**
141      * Gets the tail of the item list.
142      *
143      * @return the tail item
144      */
145     public Item getTail() {
146         return tail;
147     }
148
149     /**
150      * Sets the tail of the item list.
151      *
152      * @param item the new tail item
153      */
154     void setTail(Item item) {
155         tail = item;
156     }
157
158     /**
159      * Adds a new item to this relation. The item added does not share 
160      * its contents with any other item.
161      *
162      * @return the newly added item
163      */
164     public Item appendItem() {
165         return appendItem(null);
166     }
167
168     /**
169      * Adds a new item to this relation. The item added shares its
170      * contents with the original item.
171      *
172      * @param originalItem the ItemContents that will be
173      * shared by the new item
174      *
175      * @return the newly added item
176      */
177     public Item appendItem(Item originalItem) {
178         ItemContents contents;
179         Item newItem;
180
181         if (originalItem == null) {
182             contents = null;
183         } else {
184             contents = originalItem.getSharedContents();
185         }
186         newItem = new Item(this, contents);
187         if (head == null) {
188             head = newItem;
189         }
190
191         if (tail != null) {
192             tail.attach(newItem);
193         }
194         tail = newItem;
195         return newItem;
196     }
197
198
199     /**
200      * Returns the utterance that contains this relation.
201      *
202      * @return the utterance that contains this relation
203      */
204     public Utterance getUtterance() {
205         return owner;
206     }
207
208
209     /**
210      * Dumps this relation to the print writer.
211      *
212      * @param pw the output stream
213      *
214      * @param pad the padding
215      *
216      * @param title the title for the dump
217      */
218     public void dump(PrintWriter pw, int pad, String title) {
219         Utilities.dump(pw, pad, "========= Relation: " + title + 
220                                " =========");
221         Item item = head;
222         while (item != null) {
223             item.dump(pw, pad + 4, title);
224             item = item.getNext();
225         }
226     }
227 }