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