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.acewiki.gf;
016    
017    import java.util.HashMap;
018    import java.util.List;
019    import java.util.Map;
020    
021    import org.grammaticalframework.parser.ParseState;
022    
023    import ch.uzh.ifi.attempto.acewiki.core.AbstractSentence;
024    import ch.uzh.ifi.attempto.acewiki.core.Declaration;
025    import ch.uzh.ifi.attempto.acewiki.core.OntologyElement;
026    import ch.uzh.ifi.attempto.acewiki.core.SentenceDetail;
027    import ch.uzh.ifi.attempto.base.TextContainer;
028    import ch.uzh.ifi.attempto.base.TextElement;
029    
030    /**
031     * This class represents a declaration statement for the GF AceWiki engine.
032     * 
033     * @author Tobias Kuhn
034     */
035    public class GFDeclaration extends AbstractSentence implements Declaration {
036            
037            private GFGrammar gfGrammar;
038            private ParseState parseState;
039            private Map<String, TextContainer> textContainers = new HashMap<String, TextContainer>();
040            
041            /**
042             * Creates a new GF declaration object from a parse state.
043             * 
044             * @param parseState The parse state.
045             * @param gfGrammar The grammar object.
046             */
047            public GFDeclaration(ParseState parseState, GFGrammar gfGrammar) {
048                    this.gfGrammar = gfGrammar;
049                    this.parseState = parseState;
050            }
051            
052            /**
053             * Creates a new GF declaration object from a text in a given language.
054             * 
055             * @param text The declaration text.
056             * @param language The language.
057             * @param gfGrammar The grammar object.
058             */
059            public GFDeclaration(String text, String language, GFGrammar gfGrammar) {
060                    this.gfGrammar = gfGrammar;
061                    parseState = getGFGrammar().parse(text, language);
062            }
063            
064            protected TextContainer getTextContainer(String language) {
065                    TextContainer tc = textContainers.get(language);
066                    if (tc == null) {
067                            tc = new TextContainer();
068                            for (String s : getGFGrammar().linearizeAsTokens(parseState, language)) {
069                                    tc.addElement(new TextElement(s));
070                            }
071                            textContainers.put(language, tc);
072                    }
073                    return tc;
074            }
075            
076            public List<TextElement> getTextElements(String language) {
077                    return getTextContainer(language).getTextElements();
078            }
079            
080            public boolean contains(OntologyElement e) {
081                    // TODO
082                    return false;
083            }
084            
085            public List<SentenceDetail> getDetails(String language) {
086                    return null;
087            }
088            
089            public boolean isReasonable() {
090                    return true;
091            }
092            
093            public void update() {
094            }
095            
096            public String serialize() {
097                    return getGFGrammar().serialize(parseState);
098            }
099            
100            /**
101             * Returns the grammar object.
102             * 
103             * @return The grammar object.
104             */
105            public GFGrammar getGFGrammar() {
106                    return gfGrammar;
107            }
108    
109    }