1   package com.ontotext.gate.ontology;
2   
3   import gate.creole.ontology.*;
4   import java.util.List;
5   import java.net.URL;
6   import gate.creole.ResourceInstantiationException;
7   import java.util.Set;
8   import java.util.Iterator;
9   import java.util.Comparator;
10  import gate.DataStore;
11  import gate.persist.PersistenceException;
12  import gate.security.SecurityException;
13  import gate.LanguageResource;
14  import gate.Resource;
15  import gate.FeatureMap;
16  
17  import java.util.*;
18  import com.hp.hpl.jena.daml.*;
19  import com.hp.hpl.jena.daml.common.*;
20  import com.hp.hpl.mesa.rdf.jena.model.*;
21  import com.hp.hpl.mesa.rdf.jena.common.*;
22  import com.hp.hpl.jena.vocabulary.*;
23  import com.hp.hpl.mesa.rdf.jena.common.prettywriter.*;
24  import com.hp.hpl.mesa.rdf.jena.vocabulary.*;
25  import com.hp.hpl.jena.rdf.arp.*;
26  
27  
28  public class OntologyImpl extends TaxonomyImpl implements Ontology {
29  
30    private static final boolean DEBUG = false;
31  
32    private Map instancesByName = new HashMap();
33    private List instances = new ArrayList();
34    private Set propertyDefinitionSet = new HashSet();
35  
36    public OInstance addInstance(String name, OClass theClass) {
37      if (instancesByName.containsKey(name))
38        return (OInstance) instancesByName.get(name);
39      OInstance newInstance = new OInstanceImpl(name, theClass);
40      instancesByName.put(name, newInstance);
41      instances.add(newInstance);
42      return newInstance;
43    }
44  
45    public void addInstance(OInstance theInstance) {
46      if (instancesByName.containsKey(theInstance.getName()))
47        return;
48      instancesByName.put(theInstance.getName(), theInstance);
49      instances.add(theInstance);
50    }
51  
52    public void removeInstance(OInstance theInstance) {
53      if (! instancesByName.containsKey(theInstance.getName()))
54        return;
55      instancesByName.remove(theInstance.getName());
56      instances.remove(theInstance);
57    }
58  
59    public List getInstances() {
60      return instances;
61    }
62  
63    public List getInstances(OClass aClass) {
64      List theInstances = new ArrayList();
65      Set subClasses;
66      try {
67        subClasses = aClass.getSubClasses(OClass.TRANSITIVE_CLOSURE);
68      } catch (NoSuchClosureTypeException ex){
69        subClasses = new HashSet();
70      }
71  
72      //iterate through all instances and only include those
73      //that either have the same class or their class is a subclass
74      //of the given class; not an efficient implementation but fine for now
75      for (int i=0; i< instances.size(); i++) {
76        OClass theClass = ((OInstance)instances.get(i)).getOClass();
77        if (theClass.equals(aClass) || subClasses.contains(theClass))
78          theInstances.add(instances.get(i));
79      }//for
80      return theInstances;
81    }
82  
83    public List getDirectInstances(OClass aClass) {
84      List theInstances = new ArrayList();
85      //iterate through all instances and only include those
86      //that have the same class; not an efficient implementation but fine for now
87      for (int i=0; i< instances.size(); i++) {
88        OClass theClass = ((OInstance)instances.get(i)).getOClass();
89        if (theClass.equals(aClass))
90          theInstances.add(instances.get(i));
91      }//for
92      return theInstances;
93    }
94  
95    public OInstance getInstanceByName(String aName) {
96      return (OInstance) instancesByName.get(aName);
97    }
98  
99    public TClass createClass(String aName, String aComment) {
100 
101     this.modified = true;
102     TClass theClass
103       = new OClassImpl(Long.toString(++lastGeneratedId),aName,aComment,this);
104     addClass(theClass);
105     nullBuffers = true;
106     fireObjectModificationEvent(this);
107     return theClass;
108   }
109 
110   public DatatypeProperty addDatatypeProperty(String name, OClass domain, String value){
111     DatatypeProperty theProperty =
112       new DatatypePropertyImpl(name, domain, value, this);
113     ((OClassImpl)domain).addProperty(theProperty);
114     return theProperty;
115   }
116 
117   public DatatypeProperty addDatatypeProperty(String name, OClass domain, Number value){
118     DatatypeProperty theProperty =
119       new DatatypePropertyImpl(name, domain, value, this);
120     ((OClassImpl)domain).addProperty(theProperty);
121     return theProperty;
122   }
123 
124   public FunctionalProperty addFunctionalProperty(String name, OClass domain, Object range){
125     System.out.println("Functional properties not supported yet");
126     return null;
127   }
128 
129   public ObjectProperty addObjectProperty(String name, OClass domain, OClass range){
130     ObjectProperty theProperty =
131       new ObjectPropertyImpl(name, domain, range, this);
132     ((OClassImpl)domain).addProperty(theProperty);
133     return theProperty;
134   }
135 
136   public SymmetricProperty addSymmetricProperty(String name, OClass domain, OClass range){
137     System.out.println("Symmetric properties not supported yet");
138     return null;
139   }
140 
141   public TransitiveProperty addTransitiveProperty(OClass domain, OClass range){
142     System.out.println("Transitive properties not supported yet");
143     return null;
144   }
145 
146   public void addPropertyDefinition(gate.creole.ontology.Property theProperty) {
147     this.propertyDefinitionSet.add(theProperty);
148   }
149 
150   public Set getPropertyDefinitions() {
151     return this.propertyDefinitionSet;
152   }
153 
154   public gate.creole.ontology.Property getPropertyDefinitionByName(String name){
155     if (name == null)
156       return null;
157     Iterator iter = this.propertyDefinitionSet.iterator();
158     while (iter.hasNext()) {
159       gate.creole.ontology.Property theProperty = (gate.creole.ontology.Property) iter.next();
160       if (name.equals(theProperty.getName()))
161         return theProperty;
162     }
163     return null;
164   }
165 
166 }