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.aceowl;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    
020    import ch.uzh.ifi.attempto.acewiki.core.InvalidWordException;
021    import ch.uzh.ifi.attempto.acewiki.core.LanguageUtils;
022    import ch.uzh.ifi.attempto.acewiki.core.LexiconChanger;
023    import ch.uzh.ifi.attempto.acewiki.core.LexiconDetail;
024    import ch.uzh.ifi.attempto.acewiki.core.Ontology;
025    import ch.uzh.ifi.attempto.acewiki.core.OntologyElement;
026    import ch.uzh.ifi.attempto.ape.FunctionWords;
027    
028    /**
029     * This class is used to modify or create of-constructs.
030     * 
031     * @author Tobias Kuhn
032     */
033    public class NounOfChanger implements LexiconChanger {
034            
035            public String getDescription() {
036                    return "Every of-construct represents a certain relation between things. " +
037                            "For example, the of-construct \"child of\" relates persons to their " +
038                            "parents. Every of-construct consists of a noun plus the preposition " +
039                            "\"of\".";
040            }
041            
042            public List<LexiconDetail> getDetails(OntologyElement el) {
043                    OfRelation relation = (OfRelation) el;
044                    List<LexiconDetail> l = new ArrayList<LexiconDetail>();
045                    l.add(new LexiconDetail(
046                                    "noun",
047                                    "examples: part, child, owner",
048                                    relation.getNoun()
049                            ));
050                    return l;
051            }
052            
053            public void save(OntologyElement el, int wordNumber, List<Object> newValues, Ontology ontology)
054                            throws InvalidWordException {
055                    OfRelation relation = (OfRelation) el;
056                    String name = ACEOWLLexicon.normalize((String) newValues.get(0));
057                    if (name.toLowerCase().endsWith("_of")) {
058                            name = name.substring(0, name.length()-3);
059                    }
060                    String nameP = LanguageUtils.getPrettyPrinted(name);
061                    
062                    if (name.equals("")) {
063                            throw new InvalidWordException("No noun defined: Please specify the singular form " +
064                                    "of a noun.");
065                    }
066                    if (!ACEOWLLexicon.isValidWordOrEmpty(name)) {
067                            throw new InvalidWordException("Invalid character: Only a-z, A-Z, 0-9, -, and " +
068                                    "spaces are allowed, and the first character must be one of a-z A-Z.");
069                    }
070                    if (FunctionWords.isFunctionWord(name)) {
071                            throw new InvalidWordException("'" + nameP + "' is a predefined word and cannot be " +
072                                    "used here.");
073                    }
074                    OntologyElement oe = ontology.getElement(name + " of");
075                    if (oe != null && oe != relation) {
076                            throw new InvalidWordException("The word '" + nameP + "' is already used. Please " +
077                                    "use a different one.");
078                    }
079                    ontology.change(relation, name);
080            }
081    
082    }