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.acewiki.core.ontology; 016 017 import java.util.ArrayList; 018 import java.util.List; 019 020 /** 021 * This abstract class represents a concept (other terminologies call it "unary relation", "class", 022 * or "type"). 023 * 024 * @author Tobias Kuhn 025 */ 026 public abstract class Concept extends OntologyElement { 027 028 private List<Individual> individualsCache; 029 private long individualsCacheStateID = -1; 030 031 private List<Concept> superConceptsCache; 032 private long superConceptsCacheStateID = -1; 033 034 private List<Concept> subConceptsCache; 035 private long subConceptsCacheStateID = -1; 036 037 /** 038 * Initializes the concept. 039 */ 040 protected Concept() { 041 } 042 043 /** 044 * Calculates all individuals that belong to this concept. 045 * 046 * @return A list of all individuals of this concept. 047 * @see Ontology#getIndividuals(Concept) 048 */ 049 public synchronized List<Individual> getIndividuals() { 050 Ontology o = getOntology(); 051 if (individualsCacheStateID != o.getStateID()) { 052 individualsCache = o.getIndividuals(this); 053 individualsCacheStateID = o.getStateID(); 054 } 055 return new ArrayList<Individual>(individualsCache); 056 } 057 058 /** 059 * Returns the cached individuals or null if there are no cached individuals. The returned 060 * individuals might not be up-to-date. 061 * 062 * @return A list of the cached individuals of this concept. 063 */ 064 public List<Individual> getCachedIndividuals() { 065 if (individualsCache == null) return null; 066 return new ArrayList<Individual>(individualsCache); 067 } 068 069 /** 070 * Returns true if the individuals of this concept are cached and up-to-date and thus 071 * do not have to be recalculated. 072 * 073 * @return true if the individuals are cached. 074 */ 075 public boolean areIndividualsCached() { 076 return individualsCacheStateID == getOntology().getStateID(); 077 } 078 079 /** 080 * Calculates all super-concepts of this concept. 081 * 082 * @return A list of all super-concepts. 083 * @see Ontology#getSuperConcepts(Concept) 084 */ 085 public synchronized List<Concept> getSuperConcepts() { 086 Ontology o = getOntology(); 087 if (superConceptsCacheStateID != o.getStateID()) { 088 superConceptsCache = o.getSuperConcepts(this); 089 superConceptsCacheStateID = o.getStateID(); 090 } 091 return new ArrayList<Concept>(superConceptsCache); 092 } 093 094 /** 095 * Returns the cached super-concepts or null if there are no cached super-concepts. The returned 096 * super-concepts might not be up-to-date. 097 * 098 * @return A list of the cached super-concepts of this concept. 099 */ 100 public List<Concept> getCachedSuperConcepts() { 101 if (superConceptsCache == null) return null; 102 return new ArrayList<Concept>(superConceptsCache); 103 } 104 105 /** 106 * Returns true if the suber-concepts of this concept are cached and up-to-date and thus 107 * do not have to be recalculated. 108 * 109 * @return true if the super-concepts are cached. 110 */ 111 public boolean areSuperConceptsCached() { 112 return superConceptsCacheStateID == getOntology().getStateID(); 113 } 114 115 /** 116 * Calculates all sub-concepts of this concept. 117 * 118 * @return A list of all sub-concepts. 119 * @see Ontology#getSubConcepts(Concept) 120 */ 121 public synchronized List<Concept> getSubConcepts() { 122 Ontology o = getOntology(); 123 if (subConceptsCacheStateID != o.getStateID()) { 124 subConceptsCache = o.getSubConcepts(this); 125 subConceptsCacheStateID = o.getStateID(); 126 } 127 return new ArrayList<Concept>(subConceptsCache); 128 } 129 130 /** 131 * Returns the cached sub-concepts or null if there are no cached sub-concepts. The returned 132 * sub-concepts might not be up-to-date. 133 * 134 * @return A list of the cached sub-concepts of this concept. 135 */ 136 public List<Concept> getCachedSubConcepts() { 137 if (subConceptsCache == null) return null; 138 return new ArrayList<Concept>(subConceptsCache); 139 } 140 141 /** 142 * Returns true if the sub-concepts of this concept are cached and up-to-date and thus 143 * do not have to be recalculated. 144 * 145 * @return true if the sub-concepts are cached. 146 */ 147 public boolean areSubConceptsCached() { 148 return subConceptsCacheStateID == getOntology().getStateID(); 149 } 150 151 }