1   /*
2    *  Copyright (c) 1998-2001, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Angel Kirilov 18/04/2002
10   *
11   *  $Id: ProtegeProjectName.java,v 1.11 2003/03/14 00:01:19 kalina Exp $
12   *
13   */
14  package gate.creole;
15  
16  import java.net.URL;
17  import java.util.*;
18  
19  import gate.*;
20  import gate.creole.ontology.*;
21  import gate.gui.ProtegeWrapper;
22  import com.ontotext.gate.ontology.*;
23  // Protege import
24  import edu.stanford.smi.protege.model.*;
25  import edu.stanford.smi.protege.event.*;
26  
27  
28  /** Dummy Protege LR. Just keep the Protege project file name */
29  public class ProtegeProjectName extends AbstractLanguageResource
30                                  implements ProtegeProject, Ontology {
31    /** Debug flag */
32    private static final boolean DEBUG = false;
33  
34    /** Protege project file name */
35    private URL projectName;
36  
37    /** Protege ontology */
38    private edu.stanford.smi.protege.model.KnowledgeBase knBase = null;
39  
40    /** Ontotext Ontology object */
41    private Ontology ontotextOntology = null;
42    private URL ontotextOntologyUrl = null;
43  
44    /** Keep visual resource to refresh Ontotext Editor if any */
45    ProtegeWrapper visualResource = null;
46  
47    /** Track changes in Protege KnowledgeBase to transffer in Ontotext Editor */
48    private KnowledgeBaseListener _knowledgeBaseListener = null;
49  
50    public ProtegeProjectName() {
51      projectName = null;
52    }
53  
54    public void setProjectName(URL name) {
55      projectName = name;
56    } // setProjectName(String name)
57  
58    public URL getProjectName() {
59      return projectName;
60    } // getProjectName()
61  
62    public void setViewResource(ProtegeWrapper visual) {
63      visualResource = visual;
64    } // setViewResource(AbstractVisualResource visual)
65  
66    public void setKnowledgeBase(edu.stanford.smi.protege.model.KnowledgeBase base) {
67      knBase = base;
68      fillOntotextOntology();
69      createKBListener();
70    } // setKnowledgeBase(KnowledgeBase base)
71  
72    public edu.stanford.smi.protege.model.KnowledgeBase getKnowledgeBase() {
73      return knBase;
74    } // getKnowledgeBase()
75  
76    private void createKBListener() {
77      _knowledgeBaseListener = new KnowledgeBaseAdapter() {
78        public void clsCreated(KnowledgeBaseEvent event) {
79          fillOntotextOntology();
80          visualResource.refreshOntoeditor(ontotextOntology);
81        } // clsCreated(KnowledgeBaseEvent event)
82  
83        public void clsDeleted(KnowledgeBaseEvent event) {
84          fillOntotextOntology();
85          visualResource.refreshOntoeditor(ontotextOntology);
86        } // clsDeleted(KnowledgeBaseEvent event)
87  
88        public void frameNameChanged(KnowledgeBaseEvent event) {
89          Frame frame = event.getFrame();
90          if(frame instanceof Cls) {
91            fillOntotextOntology();
92            visualResource.refreshOntoeditor(ontotextOntology);
93          } // if
94        } // frameNameChanged(KnowledgeBaseEvent event)
95      };
96      knBase.addKnowledgeBaseListener(_knowledgeBaseListener);
97    } // createKBListener()
98  
99    private void fillOntotextOntology() {
100     Collection coll = knBase.getRootClses();
101     Iterator it = coll.iterator();
102     Cls cls;
103     OClass oCls;
104 
105     ontotextOntology = new DAMLOntology();
106     ontotextOntology.setURL(ontotextOntologyUrl);
107 
108     while(it.hasNext()) {
109       cls = (Cls) it.next();
110       oCls = ontotextOntology.createClass(cls.getName(), "Protege class");
111       oCls.setURI("");
112       ontotextOntology.addClass(oCls);
113       createSubClasses(cls, oCls);
114     }
115 
116   } // fillOntotextOntology()
117 
118   private void createSubClasses(Cls protegeClass, OClass ontotextClass) {
119     Cls cls;
120     OClass oCls;
121 
122     Collection coll = protegeClass.getDirectSubclasses();
123     Iterator it = coll.iterator();
124     while(it.hasNext()) {
125       cls = (Cls) it.next();
126       oCls = ontotextOntology.createClass(cls.getName(), "Protege class");
127       ontotextClass.addSubClass(oCls);
128       createSubClasses(cls, oCls);
129     }
130   } // createSubClasses(Cls protegeClass, OClass ontotextClass)
131 
132 //------------------------------------------------------------------------------
133 //  Ontology interface methods
134 
135   public Ontology getOntology(URL someUrl)
136         throws ResourceInstantiationException {
137     return ontotextOntology.getOntology(someUrl);
138   }
139 
140   public String getLabel() {
141      return ontotextOntology.getLabel();
142   }
143 
144   public void setLabel(String label) {
145     ontotextOntology.setLabel(label);
146   }
147 
148   public URL getURL() {
149     return ontotextOntologyUrl;
150   }
151   public void setURL(URL aUrl) {
152     ontotextOntologyUrl = aUrl;
153     if(ontotextOntology != null) {
154       ontotextOntology.setURL(aUrl);
155       fillOntotextOntology();
156       visualResource.refreshOntoeditor(ontotextOntology);
157     } // if
158   }
159   public void setSourceURI(String theURI) {
160     ontotextOntology.setSourceURI(theURI);
161   }
162   public String getSourceURI() {
163     return ontotextOntology.getSourceURI();
164   }
165   public void setVersion(String theVersion) {
166     ontotextOntology.setVersion(theVersion);
167   }
168   public String getVersion() {
169     return ontotextOntology.getVersion();
170   }
171   public void load() throws ResourceInstantiationException {
172     if(ontotextOntology != null) {
173       ontotextOntology.setURL(ontotextOntologyUrl);
174       ontotextOntology.load();
175     } // if
176   }
177   public void store() throws ResourceInstantiationException {
178     if(ontotextOntology != null) {
179       ontotextOntology.setURL(ontotextOntologyUrl);
180       ontotextOntology.store();
181     } // if
182   }
183   public String getId() {
184     return ontotextOntology.getId();
185   }
186   public void setId(String theId) {
187     ontotextOntology.setId(theId);
188   }
189   public String getComment() {
190     return ontotextOntology.getComment();
191   }
192   public void setComment(String theComment) {
193     ontotextOntology.setComment(theComment);
194   }
195 
196   public OClass createClass(String aName, String aComment) {
197     return ontotextOntology.createClass(aName, aComment);
198   }
199 
200   public void removeClass(OClass theClass) {
201     ontotextOntology.removeClass(theClass);
202   }
203 
204   public void addClass(OClass theClass) {
205     ontotextOntology.addClass(theClass);
206   }
207 
208   public OClass getClassByName(String theName) {
209     return ontotextOntology.getClassByName(theName);
210   }
211 
212   public boolean containsClassByName(String theName) {
213     return ontotextOntology.containsClassByName(theName);
214   }
215 
216   public Set getClasses() {
217     return ontotextOntology.getClasses();
218   }
219 
220   public Iterator getClasses(Comparator comp) {
221     return ontotextOntology.getClasses(comp);
222   }
223 
224   public Set getTopClasses() {
225     if(ontotextOntology != null)
226       return ontotextOntology.getTopClasses();
227     else return new HashSet();
228   }
229 
230   public int getTaxonomicDistance(OClass class1,OClass class2) {
231     return ontotextOntology.getTaxonomicDistance(class1, class2);
232   }
233 
234   public boolean equals(Object o) {
235     boolean result = false;
236 
237     if(o instanceof ProtegeProjectName) {
238       ProtegeProjectName prj = (ProtegeProjectName) o;
239       if(ontotextOntology != null) {
240         result = ontotextOntology.equals(prj.ontotextOntology);
241       }
242       else { // ontology is null
243         result = prj.ontotextOntology == null;
244       }
245     }
246 
247     return result;
248   }
249 
250   public void setModified(boolean isModified) {
251     ontotextOntology.setModified(isModified);
252   }
253 
254   public boolean isModified() {
255     if(ontotextOntology == null) return false;
256     return ontotextOntology.isModified();
257   }
258 
259 } // class ProtegeProjectName extends AbstractLanguageResource