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.ArrayList;
018 import java.util.List;
019
020 import ch.uzh.ifi.attempto.base.ConcreteOption;
021
022 /**
023 * This class represents a concrete option for the chart parser. Such a concrete option consists of
024 * a word in the form of a terminal category standing for a possible next token, and optionally of
025 * a pre-terminal category from which the terminal category was derived.
026 *
027 * @author Tobias Kuhn
028 */
029 public class CPConcreteOption implements ConcreteOption {
030
031 private final Terminal terminal;
032 private final Preterminal category;
033 private final String identifier;
034
035 CPConcreteOption(Grammar grammar, Terminal word, Preterminal category) {
036 this.terminal = word;
037 this.category = category;
038 identifier = calculateIdentifier(grammar.getFeatureNamesArray());
039 }
040
041 CPConcreteOption(Grammar grammar, LexicalRule lexRule) {
042 this(grammar, lexRule.getWord(), lexRule.getCategory());
043 }
044
045 public String getWord() {
046 return terminal.getName();
047 }
048
049 /**
050 * Returns the pre-terminal category of this concrete option, or null if no pre-terminal
051 * category was involved.
052 *
053 * @return The pre-terminal category.
054 */
055 public Preterminal getCategory() {
056 return category;
057 }
058
059 public String getCategoryName() {
060 if (category == null) {
061 return null;
062 } else {
063 return category.getName();
064 }
065 }
066
067 String calculateIdentifier(String[] usedFeatureNames) {
068 if (category == null) {
069 return terminal + " <-";
070 } else {
071 List<Integer> vars = new ArrayList<Integer>();
072 List<Integer> mvars = new ArrayList<Integer>();
073
074 vars.clear();
075 mvars.clear();
076 category.collectVars(vars, mvars);
077 return terminal + " <- " + category.getIdentifier(mvars, usedFeatureNames);
078 }
079 }
080
081 public boolean equals(Object obj) {
082 if (!(obj instanceof CPConcreteOption)) return false;
083 CPConcreteOption other = (CPConcreteOption) obj;
084 return this.identifier.equals(other.identifier);
085 }
086
087 public int hashCode() {
088 return identifier.hashCode();
089 }
090
091 public String toString() {
092 if (category == null) {
093 return terminal + " <-";
094 } else {
095 return terminal + " <- " + category;
096 }
097 }
098
099 }