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 org.semanticweb.owlapi.model.OWLAxiom;
018    import org.semanticweb.owlapi.model.OWLClassExpression;
019    import org.semanticweb.owlapi.model.OWLIndividual;
020    import org.semanticweb.owlapi.model.OWLNamedIndividual;
021    import org.semanticweb.owlapi.model.OWLObjectOneOf;
022    import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
023    
024    import ch.uzh.ifi.attempto.acewiki.owl.OWLQuestion;
025    import ch.uzh.ifi.attempto.base.TextContainer;
026    
027    /**
028     * This class represents ACE questions.
029     * 
030     * @author Tobias Kuhn
031     */
032    public class ACEQuestion extends ACESentence implements OWLQuestion {
033            
034            private OWLClassExpression questionOWLClass;
035            private OWLNamedIndividual questionOWLIndividual;
036            private boolean recalculateOWLEntities = true;
037    
038            /**
039             * Creates a new ACE question.
040             * 
041             * @param serialized The serialized representation of the question.
042             */
043            public ACEQuestion(String serialized) {
044                    super(serialized);
045            }
046    
047            /**
048             * Creates a new ACE question
049             * 
050             * @param textContainer The text container with the question text.
051             */
052            public ACEQuestion(TextContainer textContainer) {
053                    super(textContainer);
054            }
055            
056            public OWLClassExpression getQuestionOWLClass() {
057                    calculateQuestionOWLEntities();
058                    return questionOWLClass;
059            }
060            
061            public OWLNamedIndividual getQuestionOWLIndividual() {
062                    calculateQuestionOWLEntities();
063                    return questionOWLIndividual;
064            }
065            
066            private void calculateQuestionOWLEntities() {
067                    if (!recalculateOWLEntities) return;
068                    questionOWLClass = null;
069                    questionOWLIndividual = null;
070                    
071                    OWLSubClassOfAxiom questionOWLAxiom = null;
072                    for (OWLAxiom ax : getOWLAxioms()) {
073                            if (ax instanceof OWLSubClassOfAxiom) {
074                                    questionOWLAxiom = (OWLSubClassOfAxiom) ax;
075                                    break;
076                            }
077                    }
078                    if (questionOWLAxiom != null) {
079                            questionOWLClass = questionOWLAxiom.getSubClass();
080                    }
081                    
082                    if (questionOWLClass instanceof OWLObjectOneOf) {
083                            OWLObjectOneOf oneof = ((OWLObjectOneOf) questionOWLClass);
084                            if (oneof != null && oneof.getIndividuals().size() == 1) {
085                                    OWLIndividual owlInd = oneof.getIndividuals().iterator().next();
086                                    if (owlInd instanceof OWLNamedIndividual) {
087                                            questionOWLIndividual = (OWLNamedIndividual) owlInd;
088                                    }
089                            }
090                    }
091                    
092                    recalculateOWLEntities = false;
093            }
094            
095            public void update() {
096                    recalculateOWLEntities = true;
097                    super.update();
098            }
099            
100            public boolean isReasonable() {
101                    return false;
102            }
103    
104    }