1   /*
2    * OntologyImpl.java
3    * Copyright:    Copyright (c) 2001, OntoText Lab.
4    * Company:      OntoText Lab.
5    * borislav popov 02/2002 */
6   package com.ontotext.gate.ontology;
7   
8   import gate.creole.ontology.*;
9   import java.util.*;
10  
11  /**
12   * An Ontology Implementation Class
13   * 
14   * @author borislav popov
15   * @author Kalina Bontcheva
16   * @author Valentin Tablan
17   */
18  public class OntologyImpl extends TaxonomyImpl implements Ontology {
19  
20    private static final boolean DEBUG = false;
21  
22    protected Map instancesByName = new HashMap();
23    protected Set instances = new HashSet();
24    protected Set propertyDefinitionSet = new HashSet();
25  
26    public OInstance addInstance(String name, OClass theClass) {
27      if (instancesByName.containsKey(name))
28        return (OInstance) instancesByName.get(name);
29      OInstance newInstance = new OInstanceImpl(name, null, theClass, this);
30      instancesByName.put(name, newInstance);
31      instances.add(newInstance);
32      setModified(true);
33      fireObjectModificationEvent(this);
34      return newInstance;
35    }
36  
37    public void addInstance(OInstance theInstance) {
38      if (instancesByName.containsKey(theInstance.getName()))
39        return;
40      instancesByName.put(theInstance.getName(), theInstance);
41      instances.add(theInstance);
42      setModified(true);
43      fireObjectModificationEvent(this);
44    }
45  
46    public void removeInstance(OInstance theInstance) {
47      if (! instancesByName.containsKey(theInstance.getName()))
48        return;
49      instancesByName.remove(theInstance.getName());
50      instances.remove(theInstance);
51    }
52  
53    public Set getInstances() {
54      return instances;
55    }
56  
57    public Set getInstances(OClass aClass) {
58      Set theInstances = getDirectInstances(aClass);
59      Iterator classIter = aClass.
60        getSuperClasses(OClass.TRANSITIVE_CLOSURE).iterator();
61      while(classIter.hasNext())
62        theInstances.addAll(getDirectInstances((OClass)classIter.next()));
63      return theInstances;
64    }
65  
66    public Set getDirectInstances(OClass aClass) {
67      Set theInstances = new HashSet();
68  
69      //iterate through all instances and only include those
70      //that either have the same class or their class is a subclass
71      //of the given class; not an efficient implementation but fine for now
72      Iterator instIter = instances.iterator();
73      while(instIter.hasNext()) {
74        OInstance anInstance = (OInstance)instIter.next(); 
75        if(anInstance.getOClasses().contains(aClass)) 
76          theInstances.add(anInstance);
77      }//for
78      return theInstances;
79    }
80  
81    public OInstance getInstanceByName(String aName) {
82      return (OInstance) instancesByName.get(aName);
83    }
84  
85  
86    public TClass createClass(String aName, String aComment) {
87      this.modified = true;
88      TClass theClass
89        = new OClassImpl(Long.toString(++lastGeneratedId),aName,aComment,this);
90      addClass(theClass);
91      nullBuffers = true;
92      fireObjectModificationEvent(this);
93      return theClass;
94    }
95  
96    public DatatypeProperty addDatatypeProperty(String name, String comment, 
97            Set domain, Class range) {
98      DatatypeProperty theProperty = new DatatypePropertyImpl(name, 
99              comment, domain,range, this);
100     theProperty.setURI(getDefaultNameSpace() + name);
101     addPropertyDefinition(theProperty);
102     return theProperty;
103   }
104     
105   public DatatypeProperty addDatatypeProperty(String name, String comment, 
106           OClass domain, Class range){
107     DatatypeProperty theProperty = new DatatypePropertyImpl(name, 
108             comment, domain, range, this);
109     theProperty.setURI(getDefaultNameSpace() + name);
110     addPropertyDefinition(theProperty);
111     return theProperty;
112   }
113 
114 
115   public Property addProperty(String name, String comment, Set domain, 
116           Set range) {
117     Property theProperty = new PropertyImpl(name, comment, domain, range, this);
118     theProperty.setURI(getDefaultNameSpace() + name);
119     addPropertyDefinition(theProperty);
120     return theProperty;
121   }  
122   
123   public Property addProperty(String name, String comment, OClass domain, Class range){
124     Property theProperty = new PropertyImpl(name, comment, domain, range, this);
125     theProperty.setURI(getDefaultNameSpace() + name);
126     addPropertyDefinition(theProperty);
127     return theProperty;    
128   }
129   
130   public ObjectProperty addObjectProperty(String name, String comment, 
131           Set domain, Set range) {
132     ObjectProperty theProperty = new ObjectPropertyImpl(name, comment, domain, 
133             range, this);
134     theProperty.setURI(getDefaultNameSpace() + name);
135     addPropertyDefinition(theProperty);
136     return theProperty;
137   }
138 
139   public ObjectProperty addObjectProperty(String name, String comment, 
140           OClass domain, OClass range){
141     ObjectProperty theProperty = new ObjectPropertyImpl(name, comment, domain, 
142             range, this);
143     theProperty.setURI(getDefaultNameSpace() + name);
144     addPropertyDefinition(theProperty);
145     return theProperty;
146   }
147 
148   public SymmetricProperty addSymmetricProperty(String name, String comment, 
149           Set domain, Set range) {
150     SymmetricProperty theProperty = new SymmetricPropertyImpl(name, comment, 
151             domain, range, this);
152     theProperty.setURI(getDefaultNameSpace() + name);
153     addPropertyDefinition(theProperty);
154     return theProperty;
155   }
156 
157   public SymmetricProperty addSymmetricProperty(String name, String comment, 
158           OClass domain, OClass range){
159     SymmetricProperty theProperty = new SymmetricPropertyImpl(name, comment, 
160             domain, range, this);
161     theProperty.setURI(getDefaultNameSpace() + name);
162     addPropertyDefinition(theProperty);
163     return theProperty;
164   }  
165 
166   public TransitiveProperty addTransitiveProperty(String name, String comment, 
167           Set domain, Set range) {
168     TransitiveProperty theProperty = new TransitivePropertyImpl(name, comment, 
169             domain, range, this);
170     theProperty.setURI(getDefaultNameSpace() + name);
171     addPropertyDefinition(theProperty);
172     return theProperty;
173   }
174 
175   public TransitiveProperty addTransitiveProperty(String name, String comment, 
176           OClass domain, OClass range){
177     TransitiveProperty theProperty = new TransitivePropertyImpl(name, comment, 
178             domain, range, this);
179     theProperty.setURI(getDefaultNameSpace() + name);
180     addPropertyDefinition(theProperty);
181     return theProperty;
182   }  
183   
184   protected void addPropertyDefinition(gate.creole.ontology.Property theProperty) {
185     this.propertyDefinitionSet.add(theProperty);
186     setModified(true);
187   }
188 
189   public Set getPropertyDefinitions() {
190     return this.propertyDefinitionSet;
191   }
192 
193   public gate.creole.ontology.Property getPropertyDefinitionByName(String name){
194     if (name == null)
195       return null;
196     Iterator iter = this.propertyDefinitionSet.iterator();
197     while (iter.hasNext()) {
198       gate.creole.ontology.Property theProperty = (gate.creole.ontology.Property) iter.next();
199       if (name.equals(theProperty.getName()))
200         return theProperty;
201     }
202     return null;
203   }
204 
205   
206   /**
207    * Eliminates the more general classes from a set, keeping only the most
208    * specific ones. The changes are made to the set provided as a parameter.
209    * This is a utility method for ontologies.
210    * @param classSet
211    *          a set of {@link OClass} objects.
212    */
213   public static void reduceToMostSpecificClasses(Set classSet) {
214     Map superClassesForClass = new HashMap();
215     for(Iterator classIter = classSet.iterator(); classIter.hasNext();){
216       Object aGateClassValue = classIter.next();
217       if(!(aGateClassValue instanceof OClass)) continue;
218       OClass aGateClass = (OClass)aGateClassValue;
219       superClassesForClass.put(aGateClass, aGateClass
220               .getSuperClasses(OClass.TRANSITIVE_CLOSURE));
221     }
222     Set classesToRemove = new HashSet();
223     List resultList = new ArrayList(classSet);
224     for(int i = 0; i < resultList.size() - 1; i++)
225       for(int j = i + 1; j < resultList.size(); j++){
226         OClass aClass = (OClass)resultList.get(i);
227         OClass anotherClass = (OClass)resultList.get(j);
228         if(((Set)superClassesForClass.get(aClass)).contains(anotherClass))
229           classesToRemove.add(anotherClass);
230         else if(((Set)superClassesForClass.get(anotherClass)).contains(aClass))
231           classesToRemove.add(aClass);
232       }
233     classSet.removeAll(classesToRemove);
234   }
235 }