001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008-2009, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch).
003    //
004    // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the
005    // terms of the GNU Lesser General Public License as published by the Free Software Foundation,
006    // either version 3 of the License, or (at your option) any later version.
007    //
008    // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY
009    // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
010    // PURPOSE. See the GNU Lesser General Public License for more details.
011    //
012    // You should have received a copy of the GNU Lesser General Public License along with the Attempto
013    // Java Packages. If 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    /**
021     * This class describes a restriction for the next token of a given token sequence. It
022     * contains of a category and of a number of exceptions which are again categories.
023     * In order to fulfill the restriction, the category of a token must be subsumed by the
024     * category of the restriction while not being subsumed by any of the exceptions. The
025     * exceptions come from negative backwards references in the grammar.
026     * 
027     * @author Tobias Kuhn
028     */
029    public class Restriction {
030            
031            private Terminal category;
032            private List<Terminal> exceptions;
033            
034            /**
035             * Creates a new restrictions with the given category and no exceptions.
036             * 
037             * @param category The category.
038             */
039            Restriction(Terminal category) {
040                    this.category = category;
041                    exceptions = new ArrayList<Terminal>();
042            }
043            
044            /**
045             * Creates a new restriction with the given category and exceptions.
046             * 
047             * @param category The category.
048             * @param exceptions A list of exceptions.
049             */
050            Restriction(Terminal category, List<Terminal> exceptions) {
051                    this.category = category;
052                    this.exceptions = exceptions;
053            }
054            
055            /**
056             * Returns the category of this restriction.
057             * 
058             * @return The category.
059             */
060            public Terminal getCategory() {
061                    return category;
062            }
063            
064            /**
065             * Returns the list of exceptions of this restriction.
066             * 
067             * @return The list of exceptions.
068             */
069            public List<Terminal> getExceptions() {
070                    return exceptions;
071            }
072            
073            /**
074             * Returns true if the given category fulfills this restriction.
075             * 
076             * @param t The category to be tested.
077             * @return true if the category fulfills this restriction.
078             */
079            public boolean isFulfilledBy(Terminal t) {
080                    if (!category.subsumes(t)) return false;
081                    for (Terminal r : exceptions) {
082                            if (r.subsumes(t)) return false;
083                    }
084                    return true;
085            }
086    
087    }