001    // This file is part of AceWiki.
002    // Copyright 2008-2012, AceWiki developers.
003    // 
004    // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005    // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006    // the License, or (at your option) any later version.
007    // 
008    // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009    // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010    // Lesser General Public License for more details.
011    // 
012    // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013    // not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.chartparser;
016    
017    import java.util.HashMap;
018    import java.util.List;
019    
020    /**
021     * This class represents a lexical rule. Lexical rules can also be called "lexicon entries". A
022     * lexical rule consists of a word (in the form of a terminal category) and a pre-terminal
023     * category. In the pre-terminal category of a lexical rule is simply called its "category".
024     * 
025     * @author Tobias Kuhn
026     */
027    public class LexicalRule {
028            
029            private final Preterminal category;
030            private final Terminal word;
031            private final Annotation annotation;
032            
033            /**
034             * Creates a new lexical rule.
035             * 
036             * @param annotation The annotation object.
037             * @param category The category of the lexical rule.
038             * @param word The word of the lexical rule as a terminal category.
039             */
040            public LexicalRule(Annotation annotation, Preterminal category, Terminal word) {
041                    this.category = category;
042                    this.word = word;
043                    if (annotation == null) {
044                            this.annotation = new Annotation();
045                    } else {
046                            this.annotation = annotation;
047                    }
048            }
049    
050            /**
051             * Creates a new lexical rule.
052             * 
053             * @param category The category of the lexical rule.
054             * @param word The word of the lexical rule as a terminal category.
055             */
056            public LexicalRule(Preterminal category, Terminal word) {
057                    this(null, category, word);
058            }
059    
060            /**
061             * Creates a new lexical rule.
062             * 
063             * @param category The category of the lexical rule.
064             * @param word The word of the lexical rule.
065             */
066            public LexicalRule(Preterminal category, String word) {
067                    this(category, new Terminal(word));
068            }
069    
070            /**
071             * Creates a new lexical rule.
072             * 
073             * @param categoryName The category name of the lexical rule.
074             * @param word The word of the lexical rule.
075             */
076            public LexicalRule(String categoryName, String word) {
077                    this(new Preterminal(categoryName), new Terminal(word));
078            }
079            
080            /**
081             * Creates a new lexical rule.
082             * 
083             * @param annotation The annotation object.
084             * @param categories This list must contain exactly two elements. The first one must be a
085             *     pre-terminal category representing the category of the lexical rule. The second one
086             *     must be a terminal category representing the word of the lexical rule.
087             */
088            public LexicalRule(Annotation annotation, List<Category> categories) {
089                    this.category = (Preterminal) categories.get(0);
090                    this.word = (Terminal) categories.get(1);
091                    if (annotation == null) {
092                            this.annotation = new Annotation();
093                    } else {
094                            this.annotation = annotation;
095                    }
096            }
097            
098            /**
099             * Returns the category of the lexical rule.
100             * 
101             * @return The category of the lexical rule.
102             */
103            public Preterminal getCategory() {
104                    return category;
105            }
106            
107            /**
108             * Returns the word of the lexical rule.
109             * 
110             * @return The word as a terminal category.
111             */
112            public Terminal getWord() {
113                    return word;
114            }
115    
116            /**
117             * Returns the annotation object of this rule.
118             * 
119             * @return The annotation object.
120             */
121            public Annotation getAnnotation() {
122                    return annotation;
123            }
124            
125            /**
126             * Creates a deep copy of this lexicon entry.
127             * 
128             * @return A deep copy.
129             */
130            public LexicalRule deepCopy() {
131                    return deepCopy(new HashMap<Integer, StringObject>());
132            }
133            
134            /**
135             * Creates a deep copy of this lexicon entry using the given string objects.
136             * This method is usually called form another deepCopy-method.
137             * 
138             * @param stringObjs The string objects to be used.
139             * @return A deep copy.
140             */
141            LexicalRule deepCopy(HashMap<Integer, StringObject> stringObjs) {
142                    Preterminal categoryC = (Preterminal) category.deepCopy(stringObjs);
143                    Terminal wordC = (Terminal) word.deepCopy(stringObjs);
144                    Annotation annotationC = annotation.deepCopy(stringObjs);
145                    return new LexicalRule(annotationC, categoryC, wordC);
146            }
147            
148            public String toString() {
149                    return category + " => " + word;
150            }
151    
152    }