View Javadoc

1   /*
2    * @author Giorgos Anestis, TUC/MUSIC
3    *  
4    * @version 2.0
5    */
6   package org.dbe.studio.tools.ontologyviewer.views;
7   
8   
9   import java.io.IOException;
10  import java.util.Collection;
11  import java.util.Iterator;
12  import java.util.Vector;
13  
14  import javax.jmi.xmi.MalformedXMIException;
15  
16  import org.dbe.kb.client.ServiceInvoker;
17  import org.dbe.kb.mdrman.JMItool;
18  import org.dbe.kb.metamodel.odm.OdmPackage;
19  import org.dbe.kb.metamodel.odm.classes.Odmclass;
20  import org.dbe.kb.metamodel.odm.classes.Restriction;
21  import org.dbe.kb.metamodel.odm.classes.Value;
22  import org.dbe.kb.metamodel.odm.classes.ValueRange;
23  import org.dbe.kb.metamodel.odm.core.AnnotationProperty;
24  import org.dbe.kb.metamodel.odm.datatypes.DataRange;
25  import org.dbe.kb.metamodel.odm.datatypes.Datatype;
26  import org.dbe.kb.metamodel.odm.datatypes.DepricatedDatatype;
27  import org.dbe.kb.metamodel.odm.datatypes.Enumeration;
28  import org.dbe.kb.metamodel.odm.datatypes.Literal;
29  import org.dbe.kb.metamodel.odm.datatypes.NonNegativeInteger;
30  import org.dbe.kb.metamodel.odm.datatypes.PrimitiveType;
31  import org.dbe.kb.metamodel.odm.datatypes.TypedLiteral;
32  import org.dbe.kb.metamodel.odm.individuals.ClassThing;
33  import org.dbe.kb.metamodel.odm.properties.DatatypeProperty;
34  import org.dbe.kb.metamodel.odm.properties.FunctionalDatatypeProperty;
35  import org.dbe.kb.metamodel.odm.properties.FunctionalObjectProperty;
36  import org.dbe.kb.metamodel.odm.properties.InverseFunctionalProperty;
37  import org.dbe.kb.metamodel.odm.properties.ObjectProperty;
38  import org.dbe.kb.metamodel.odm.properties.Property;
39  import org.dbe.kb.metamodel.odm.properties.SymmetricProperty;
40  import org.dbe.kb.metamodel.odm.properties.TransitiveProperty;
41  import org.dbe.kb.service.KBI;
42  //import org.dbe.kb.toolkit.ODI;
43  //import org.dbe.kb.toolkit.ODMproxy;
44  
45  import org.dbe.studio.tools.ontologyviewer.OntologyViewerPlugin;
46  import org.dbe.studio.tools.ontologyviewer.listeners.OntologyTreeListener;
47  import org.dbe.studio.tools.ontologyviewer.listeners.OntologyTreeNodeSelectionChangeListener;
48  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmAnnotationProperty;
49  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmComplementClass;
50  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmDataRange;
51  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmDataType;
52  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmDataValue;
53  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmDatatypeProperty;
54  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmEnumeratedClass;
55  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmEnumeration;
56  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmIntersectionClass;
57  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmObjectProperty;
58  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmOntology;
59  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmOntologyClass;
60  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmRestrictionClass;
61  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmSimpleNamedClass;
62  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmSimpleNamedClassInst;
63  import org.dbe.studio.tools.ontologyviewer.metamodels.odm.OdmUnionClass;
64  import org.dbe.studio.tools.ontologyviewer.utils.Constants;
65  import org.dbe.studio.tools.ontologyviewer.utils.IRunner;
66  import org.dbe.studio.tools.ontologyviewer.utils.Runner;
67  import org.eclipse.jface.dialogs.MessageDialog;
68  import org.eclipse.jface.preference.IPreferenceStore;
69  import org.eclipse.jface.viewers.DoubleClickEvent;
70  import org.eclipse.jface.viewers.IDoubleClickListener;
71  import org.eclipse.jface.viewers.IStructuredSelection;
72  import org.eclipse.jface.viewers.TreeViewer;
73  import org.eclipse.swt.SWT;
74  import org.eclipse.swt.custom.SashForm;
75  import org.eclipse.swt.events.MouseEvent;
76  import org.eclipse.swt.events.MouseListener;
77  import org.eclipse.swt.layout.FillLayout;
78  import org.eclipse.swt.layout.GridData;
79  import org.eclipse.swt.layout.GridLayout;
80  import org.eclipse.swt.widgets.Button;
81  import org.eclipse.swt.widgets.Combo;
82  import org.eclipse.swt.widgets.Composite;
83  import org.eclipse.swt.widgets.Group;
84  import org.eclipse.swt.widgets.Label;
85  import org.eclipse.swt.widgets.TabFolder;
86  import org.eclipse.swt.widgets.Text;
87  import org.eclipse.ui.IViewSite;
88  import org.eclipse.ui.PartInitException;
89  import org.eclipse.ui.part.ViewPart;
90  
91  /***
92   * The main view of the OntologyViewer
93   */
94  public class OntologyViewer extends ViewPart implements IRunner {
95  	
96  	public KBI theKBIservice = null;
97  	public JMItool theODMtool = null;
98  	
99  	String fadaNodeAddress;	
100 	boolean connectionProblem = false;
101 	
102 	TreeViewer ontologyTreeViewer;
103 	SashForm theMainForm;
104 	TabFolder additionalInfoTabFolder;
105 	public static Vector listOfAvailableOntologies;
106 	Combo  availableOntologies;
107 	Text searchValue;
108 	Button goSearch;
109 			
110 	OdmOntology theOntology;
111 	OdmOntology theOntologyPart;
112 	OntologyTree theTree;
113 		
114 	/***
115 	 * List of registered ontology tree listeners 
116 	 */
117 	private Vector ontologyTreeListeners = new Vector();
118 	
119 	/***
120 	 * Default constructor 
121 	 */
122 	public OntologyViewer() {
123 		super();			
124 	}
125 
126 	/***
127 	 * Initializes this view with the given view site. This method is 
128 	 * automatically called by the workbench shortly after the part is 
129 	 * instantiated. It marks the start of the views's lifecycle. Clients must 
130 	 * not call this method. 
131 	 */
132 	public void init(IViewSite site) throws PartInitException {
133 
134 		super.init(site);
135 		
136 		fadaNodeAddress = readFADAinfo();						
137 		theOntology = null;		
138 		
139 		clearView();
140 		theKBIservice = null;
141 		theODMtool = null;
142 		
143 		try {
144 			(new Runner()).run(this, Constants.TRYING_CONNECT + fadaNodeAddress);
145 		}
146 		catch (Exception e) {
147 			System.out.println( e.getMessage() );
148 			connectionProblem = true;
149 		}
150 	}
151 	
152 	/* (non-Javadoc)
153 	 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
154 	 */
155 	public void createPartControl(Composite parent) {							
156 				
157 		theMainForm = new SashForm(parent, SWT.VERTICAL);		
158 		
159 		createSearchArea();
160 		createTreeViewArea();
161 										
162 		theMainForm.setWeights( new int[] {2, 8, 3});									
163 	}
164 	
165 	public Object run() throws Exception {		
166 		getODMtool();
167 		return null;
168 	}
169 		
170 	protected void clearView() {
171 		listOfAvailableOntologies = null;
172 		
173 		if (availableOntologies != null ) {
174 			availableOntologies.removeAll();
175 		}
176 		
177 		//clear the ontology tree viewer
178 		if (ontologyTreeViewer != null) {
179 			Object o = ontologyTreeViewer.getInput();
180 			if ( o != null ) {
181 				ontologyTreeViewer.remove(o);
182 			}
183 		}
184 		//clear the additional info tab folder
185 		if (additionalInfoTabFolder != null) {
186 			int itemCount = additionalInfoTabFolder.getItemCount(); 		
187 			for(int i = 0; i < itemCount; ++i) {
188 				additionalInfoTabFolder.getItem(0).dispose();			
189 			}
190 		}
191 		
192 		//clear JMItool cache
193 		if (theODMtool != null) {						
194 			theODMtool.clearCache();
195 		}
196 	}
197 	
198 	private void getODMtool() {		
199 		try {
200 			ServiceInvoker si = new ServiceInvoker("OntologyViewerClient");
201 			theKBIservice = (KBI)si.searchService(fadaNodeAddress,"KB", KBI.class);
202 			
203 			if (theKBIservice != null) {			
204 				theODMtool = new JMItool();										
205 				theODMtool.initRepository("ODM", "ODMmodels", theKBIservice.getODM());
206 			}
207 		} 		
208 		catch (Exception e) {
209 			e.printStackTrace();
210 		}	
211 	}	
212 	
213 	/***
214 	 * It creates the Search area
215 	 */
216 	protected void createSearchArea() {
217 		
218 			if (!connectionProblem) {
219 				getAvailableOntologies();
220 			}
221 			
222 			GridLayout layout = new GridLayout(3,false);
223 			GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
224 	    	
225 	    	Group searchGroup = new Group(theMainForm, SWT.SHADOW_ETCHED_IN);
226 	    	searchGroup.setLayout(layout);
227 	    	searchGroup.setLayoutData(layoutData);
228 	    	searchGroup.setText(Constants.BORDER_SEARCH);
229 	    	    	
230 	    	//First row
231 	    	(new Label(searchGroup, SWT.NONE)).setText(Constants.LABEL_SEARCH);
232 	    	searchValue = new Text(searchGroup, SWT.BORDER);
233 	    	searchValue.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));    	
234 	    	
235 	    	goSearch = new Button(searchGroup, SWT.PUSH);
236 	    	goSearch.setText(Constants.BUTTON_LABEL_GO);
237 	    	goSearch.addMouseListener(new MouseListener() {
238 	    	
239 	    	public void mouseDoubleClick(MouseEvent e) {}
240 	    	
241 	    		public void mouseUp(MouseEvent e) {}
242 	    		
243 	    		public void mouseDown(MouseEvent e) {
244 	    			if (availableOntologies.getItemCount() > 0) {
245 		    			theOntologyPart = searchForOntologyConcept(searchValue.getText());	    			
246 		    			theTree = new OntologyTree(theOntologyPart);
247 		    			theOntologyPart.setTheTree(theTree);
248 						ontologyTreeViewer.setInput(theTree);
249 						ontologyTreeViewer.expandToLevel(2);
250 	    			}
251 	    		}
252 	    	});	 
253 	    	    	    
254 	    	//Second row
255 	    	(new Label(searchGroup, SWT.NONE)).setText(Constants.LABEL_ONTOLOGY);
256 	    	availableOntologies = new Combo(searchGroup, SWT.BORDER);
257 	    	GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
258 	    	gridData .horizontalSpan = 2;
259 	    	availableOntologies.setLayoutData(gridData);
260 	    	
261 	    	if (listOfAvailableOntologies != null ) {
262 		    	for (int i=0; i < listOfAvailableOntologies.size(); ++i ) {
263 		    		availableOntologies.add(
264 		    					((AvailableOntology)listOfAvailableOntologies.get(i)).name);
265 		    	}
266 		    	availableOntologies.select(0);		 
267 	    	}
268 		}
269 	
270 	/***
271 	 * Create the Tree View area
272 	 */
273 	protected void createTreeViewArea() {
274 		
275 		//Create the group into which the tree viewer will be located
276 		Group treeViewerGroup = new Group(theMainForm, SWT.SHADOW_ETCHED_IN);
277 		treeViewerGroup.setText(Constants.ONT_TREE);			
278 		
279 		treeViewerGroup.setLayout(new FillLayout());
280 		
281 		//add the Ontology Tree Viewer to the View via adding it to the
282 		//treeViewer Group
283 		ontologyTreeViewer = new TreeViewer( treeViewerGroup, SWT.SINGLE );		
284 		ontologyTreeViewer.setContentProvider( new OntologyTreeContentProvider(ontologyTreeViewer) );
285 		ontologyTreeViewer.setLabelProvider(new OntologyTreeLabelProvider() );				
286 		//ontologyTreeViewer.expandAll();
287 		getSite().setSelectionProvider(ontologyTreeViewer); 
288 		
289 	    // It creates the additional info area group
290 		Group additionalInfoGroup;
291 		additionalInfoGroup = new Group(theMainForm, SWT.SHADOW_ETCHED_IN);
292 		additionalInfoGroup.setLayout(new FillLayout());
293 		additionalInfoGroup.setText(Constants.ADD_INFO);
294 		
295 		additionalInfoTabFolder = new TabFolder(additionalInfoGroup, SWT.NONE);						
296 		// end of additional info area
297 		
298 		ontologyTreeViewer.addSelectionChangedListener(new OntologyTreeNodeSelectionChangeListener(additionalInfoGroup));
299 		ontologyTreeViewer.addDoubleClickListener(new DoubleClickListener()); 
300 		ontologyTreeViewer.getTree().addTreeListener(new OntologyTreeListener());
301 	}	
302 	
303 	static public Vector getListOfAvailableOntologies() {
304 		
305 		Vector ontListVector = new Vector();
306 		
307 		if (listOfAvailableOntologies != null ) {
308 			for (int i=0; i < listOfAvailableOntologies.size(); ++i ) {
309 				String[] ont = new String[2];
310 				
311     			ont[1] = ((AvailableOntology)listOfAvailableOntologies.get(i)).name;
312     			ont[0] = ((AvailableOntology)listOfAvailableOntologies.get(i)).id;
313     			
314     			ontListVector.add(ont);
315 			}
316 		}
317 		
318 		return ontListVector;
319 	}
320 	    	          
321 	/* (non-Javadoc)
322 	 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
323 	 */
324 	public void setFocus() {		
325 	}
326 	
327 	protected class AvailableOntology {
328 		String name;
329 		String id;
330 		
331 		AvailableOntology(String name, String id) {
332 			this.name = name;
333 			this.id = id;
334 		}
335 	}
336 
337 	/***
338 	 * It uses the ODM proxy and gets the names (URIs) of the available 
339 	 * ontologies
340 	 */
341 	protected void getAvailableOntologies() {		
342 		
343 		if (theODMtool != null) {					
344 			Iterator iter = theKBIservice.listODMmodels().iterator();
345 						
346 			if (iter.hasNext()) {
347 				listOfAvailableOntologies = new Vector();
348 			}
349 			
350 			String ontologyName,ontologyID;
351 		 	for (; iter.hasNext() ; ) {	    	
352 		 		String[] list = (String[])iter.next();	        	
353 	        	ontologyID = list[0];
354 	        	ontologyName = list[1];
355 		 		listOfAvailableOntologies.add(new AvailableOntology(ontologyName,
356 		 															ontologyID)
357 											  );
358 		 	}	 			
359 		}
360 		else {
361 			MessageDialog.openWarning( null, "Warning", 
362 									  "KB connection problem. Please choose another " + 
363 									  "KB address from preferences pages");
364 		}
365 	}
366 			
367 	protected OdmOntology searchForOntologyConcept(String key) {
368 		
369 		int index = availableOntologies.getSelectionIndex();
370 		String ontName = ((AvailableOntology)listOfAvailableOntologies.get(index)).name;
371 		String ontID = ((AvailableOntology)listOfAvailableOntologies.get(index)).id;
372 		
373 		if ( theOntology == null ||
374 			 !theOntology.getId().equals(ontID) ) { 	// read the ontology from the repository
375 			try {
376 				readOntology(ontName, ontID);								
377 			}
378 			catch (MalformedXMIException e) {
379 				e.printStackTrace();
380 			}
381 			catch(Exception e) {
382 				e.printStackTrace();
383 			}
384 		}
385 		
386 		if ( key.equals(Constants.EMPTY_STRING) ) {	// return the whole ontology
387 			
388 			return theOntology;
389 		}
390 		else {  							// return the part of the ontology that was found
391 			OdmOntology tmpOntology = new OdmOntology(theOntology.getName(), theOntology.getId() );
392 			Iterator classesIter = (theOntology.getClasses().values()).iterator();
393 			
394 			for (; classesIter.hasNext() ;) {
395 				OdmOntologyClass currentClass = (OdmOntologyClass)classesIter.next(); 
396 				String className = currentClass.getName();
397 				
398 				if ( className.toLowerCase().startsWith(key.toLowerCase()) ) {
399 					tmpOntology.addClass(currentClass);
400 				}
401 			}
402 				
403 			return tmpOntology;
404 		}
405 	}
406 	/***
407 	 * @param ontName
408 	 * @param ontID
409 	 * 
410 	 * It reads an ontology from the ODM repository. The way it reads from OMD
411 	 * and it creates the ontology has as follows:
412 	 * 
413 	 */
414 	public void readOntology(String ontName, String ontID)
415 					throws MalformedXMIException, IOException {		
416 		
417 		theODMtool.clearCache();
418 		theODMtool.importModel(theKBIservice.retrieveODMmodel(ontID));
419 		OdmPackage odmPackage = (OdmPackage)theODMtool.getModelPackage();
420 	 	
421 	 	//create the Ontology object
422 	 	theOntology = new OdmOntology(ontName, ontID);
423 	 	
424 	 	//first iteration: It creates all the Ontology classes and adds them in 
425 	 	// the Ontology
426 	 	readOntologyClasses(odmPackage);	 		 	
427 	 	
428 	 	//get data ranges
429 	 	readOntologyDataRanges(odmPackage);
430 	 	// get the individuals	 	
431 	 	readOntologyIndividuals(odmPackage);
432 	 		 	
433 	 	// get the ObjectProperties
434 	 	readOntologyObjectProperties(odmPackage);	 	
435 	 	
436 	 	//get the DatatypeProperties
437 	 	readOntologyDatatypeProperties(odmPackage);
438 	 	
439 	 	//second iteration: complete the remaining works
440 	 	//Get all the (remaining) ontology classes and class axioms 
441 	 	readOntologyClassesSecond(odmPackage);
442 	 	readOntologyObjectPropertiesSecond(odmPackage);
443 	 	readOntologyDatatypePropertiesSecond(odmPackage);
444 	}
445 	
446 	/***
447 	 * It reads the Ontology classes
448 	 * 
449 	 * @param odmPackage
450 	 *  
451 	 */
452 	protected void readOntologyClasses(OdmPackage odmPackage) {
453 		
454 		//Get all the ontology classes
455 	 	Iterator iter = odmPackage.getClasses().getOdmclass().refAllOfType()
456 															 .iterator();
457 	 		 	
458 		OdmOntologyClass ontologyClass = null;
459 	 	for (; iter.hasNext() ;){
460 	 		Odmclass odmClass = (Odmclass)iter.next();	 			 		
461 	 		
462 	 		//Find what type it is and create the corresponding object
463 	 		Collection union = odmClass.getTheMember();
464 	 		Collection intersection = odmClass.getTheIntersected(); 
465 	 		Odmclass complement = odmClass.getTheComplemented();
466 	 		Collection enumeration = odmClass.getTheInstance();	 		
467 	 		
468 	 		if (!union.isEmpty()){  // is union
469 	 			ontologyClass = new OdmUnionClass( theOntology, 
470 	 											   odmClass.getName(), 
471 												   odmClass.getId()
472 												   );
473 	 		}
474 	 		else if (!intersection.isEmpty()){ // is intersection
475 	 			ontologyClass = new OdmIntersectionClass( theOntology, 
476 	 													  odmClass.getName(), 
477 														  odmClass.getId()
478 														  );
479 	 		}
480 	 		else if (complement != null){ // is complement
481 	 			ontologyClass = new OdmComplementClass( theOntology, 
482 	 													odmClass.getName(), 
483 														odmClass.getId(), 
484 														null
485 														);
486 	 		}
487 	 		else if (!enumeration.isEmpty()){ // is enumerated class
488 	 			ontologyClass = new OdmEnumeratedClass( theOntology, 
489 	 													odmClass.getName(), 
490 														odmClass.getId()
491 														);
492 	 		}
493 	 		else if (Restriction.class.isInstance(odmClass)){ // is restriction	 				 			
494 	 			ontologyClass = new OdmRestrictionClass( theOntology, 
495 	 													 null, 
496 														 odmClass.getName(), 
497 														 odmClass.getId()
498 														 );
499 	 		}
500 	 		else{  // is simle named class
501 	 			ontologyClass = new OdmSimpleNamedClass( theOntology, 
502 	 													 odmClass.getName(), 
503 														 odmClass.getId()
504 														 );	 			
505 	 		}
506 	 		
507 	 		//Get the annotation of the class	 		
508 	 		Iterator aiter = odmClass.getTheAnnotationProperty().iterator();
509 	 		for(; aiter.hasNext(); ){
510 	 			AnnotationProperty annotation = (AnnotationProperty)aiter.next();
511 	 			OdmAnnotationProperty annotationProperty;
512 	 			annotationProperty = new OdmAnnotationProperty(
513 	 									annotation.getName(),
514 										annotation.getId(),
515 										annotation
516 											.getTheAnnotationObject()
517 											.toString()
518 										);
519 	 			
520 	 			ontologyClass.addAnnotationProp(annotationProperty);				
521 	 		}	 		
522 	 	}
523 	}
524 	
525 	/***
526 	 * It reads the Ontology classes and class axioms
527 	 * @param odmPackage
528 	 */
529 	protected void readOntologyClassesSecond(OdmPackage odmPackage) {
530 		Iterator iter1;
531 		Iterator iter = odmPackage.getClasses().getOdmclass().refAllOfType().iterator();
532 	 	OdmOntologyClass ontologyClass;
533 	 	
534 	 	for (; iter.hasNext() ;) {
535 	 		Odmclass odmClass = (Odmclass)iter.next();	 			 		
536 	 	//	String className = odmClass.getName();
537 	 		String classID = odmClass.getId();	 			 	
538 	 		
539 	 		//Find what type it is and create the corresponding object
540 	 		Collection union = odmClass.getTheMember();
541 	 		Collection intersection = odmClass.getTheIntersected(); 
542 	 		Odmclass complement = odmClass.getTheComplemented();
543 	 		Collection enumeration = odmClass.getTheInstance();
544 	 		
545 	 		if (!union.isEmpty()){  // is union
546 	 			ontologyClass = (OdmOntologyClass)theOntology.findClass(classID);
547 	 			iter1 = union.iterator();
548 	 			for(; iter1.hasNext(); ) {
549 	 				Odmclass unionClass = (Odmclass)iter1.next();
550 	 				OdmOntologyClass ontClass = (OdmOntologyClass)theOntology.findClass(unionClass.getId());
551 	 				((OdmUnionClass)ontologyClass).addClass(ontClass);	
552 	 			}
553 	 		}
554 	 		else if (!intersection.isEmpty()){ // is intersection
555 	 			ontologyClass = (OdmOntologyClass)theOntology.findClass(classID);
556 	 			iter1 = intersection.iterator(); 
557 	 			for(; iter1.hasNext(); ) {
558 	 				Odmclass intersectionClass = (Odmclass)iter1.next();
559 	 				OdmOntologyClass ontClass = (OdmOntologyClass)theOntology.findClass(intersectionClass.getId());
560 	 				((OdmIntersectionClass)ontologyClass).addClass(ontClass);	
561 	 			}
562 	 		}
563 	 		else if (complement != null){ // is complement
564 	 			ontologyClass = (OdmOntologyClass)theOntology.findClass(classID);	 			
565 	 			OdmOntologyClass ontClass = (OdmOntologyClass)theOntology.findClass(complement.getId());
566  				((OdmComplementClass)ontologyClass).setComplementOf(ontClass);
567 	 		}
568 	 		else if (!enumeration.isEmpty()){ // is enumerated class
569 	 			ontologyClass = (OdmEnumeratedClass)theOntology.findClass(classID);
570 	 			iter1 = enumeration.iterator(); 
571 	 			for(; iter1.hasNext(); ) {
572 	 				String id = ((ClassThing)iter1.next()).getId();
573 	 				((OdmEnumeratedClass)ontologyClass).addIndividual(theOntology.findClassInst(id));
574 	 			}	 			
575 	 		}
576 	 		else if (Restriction.class.isInstance(odmClass)){ // is restriction	 				 			
577 	 			ontologyClass = theOntology.findClass(classID);	 			
578 	 			Property p = ((Restriction)odmClass).getTheProperty();
579 	 			if (ObjectProperty.class.isInstance(p)) {
580 	 				((OdmRestrictionClass)ontologyClass).setOnProperty(
581 	 											theOntology.findObjectProperty(p.getId()));
582 	 			} else {
583 	 				iter1 = p.getTheDomain().iterator();
584 	 				OdmOntologyClass c = theOntology.findClass( ((Odmclass)iter1.next()).getId() );
585 					((OdmRestrictionClass)ontologyClass).setOnProperty(
586 							       c.findDatatypeProperty( p.getId() ));
587 	 			}
588 	 				 			
589 	 			//Some values from	 		
590 	 			ValueRange vr = ((Restriction)odmClass).getTheSvaluesRange();
591 	 			if ( vr != null ) {
592 		 			if (Odmclass.class.isInstance(vr)) {
593 		 				((OdmRestrictionClass)ontologyClass).setSomeValuesFrom(						
594 		 									theOntology.findClass(((Odmclass)vr).getId()));	 				
595 		 			} else if (Enumeration.class.isInstance(vr)) {
596 		 				((OdmRestrictionClass)ontologyClass).setSomeValuesFrom(
597 									theOntology.findDataRange( ((Enumeration)vr).getId())); 
598 		 			} else if (Datatype.class.isInstance(vr)) {
599 		 				((OdmRestrictionClass)ontologyClass).setSomeValuesFrom(
600 								theOntology.findDataRange( ((Datatype)vr).getId()));
601 		 			} else {
602 		 				System.out.println("FATAL ERROR");
603 		 			}
604 	 			}
605 	 			
606 	 			//All values from
607 	 			vr = ((Restriction)odmClass).getTheAvalueRange();
608 	 			if ( vr != null ) {
609 		 			if (Odmclass.class.isInstance(vr)) {
610 		 				((OdmRestrictionClass)ontologyClass).setAllValuesFrom(
611 		 									theOntology.findClass(((Odmclass)vr).getId()));	 				
612 		 			} else if (Enumeration.class.isInstance(vr)) {
613 		 				((OdmRestrictionClass)ontologyClass).setAllValuesFrom(
614 									theOntology.findDataRange( ((Enumeration)vr).getId())); 
615 		 			} else if (Datatype.class.isInstance(vr)) {
616 		 				((OdmRestrictionClass)ontologyClass).setAllValuesFrom(
617 								theOntology.findDataRange( ((Datatype)vr).getId()));
618 		 			} else {
619 		 				System.out.println("FATAL ERROR");
620 		 			}
621 	 			}
622 	 			
623 	 			//has value
624 	 			Value v = ((Restriction)odmClass).getTheValue();
625 	 			if (v != null) {
626 	 				if (Literal.class.isInstance(v) ) {	 					
627 	 					OdmDataValue v1 = new OdmDataValue(((Literal)v).getLexicalForm());
628 	 					((OdmRestrictionClass)ontologyClass).setHasValue(v1);
629 	 				} else if (TypedLiteral.class.isInstance(v) ) {
630 	 					OdmDataType dt = (OdmDataType)theOntology.findDataRangeByURI(((TypedLiteral)v).getTheType().getLexicalForm());
631 	 					OdmDataValue v1 = new OdmDataValue(((TypedLiteral)v).getLexicalForm(), dt);
632 	 					((OdmRestrictionClass)ontologyClass).setHasValue(v1);
633 	 				}else if (ClassThing.class.isInstance(v) ) {
634 	 					((OdmRestrictionClass)ontologyClass).setHasValue(
635 	 							theOntology.findClassInst( ((ClassThing)v).getId()) );
636 	 				}
637 	 			}
638 	 			
639 	 			//cardinality constraint
640 	 			NonNegativeInteger nni = ((Restriction)odmClass).getTheCardinality();
641 	 			if (nni != null) {
642 	 				((OdmRestrictionClass)ontologyClass).setCardinality(nni.getLexicalForm());
643 	 			}
644 	 			
645 	 			//min cardinality constraint
646 	 			nni = ((Restriction)odmClass).getTheMinCardinality();
647 	 			if (nni != null) {
648 	 				((OdmRestrictionClass)ontologyClass).setMinCardinality(nni.getLexicalForm());
649 	 			}
650 	 			
651 	 			//max cardinality constraint
652 	 			nni = ((Restriction)odmClass).getTheMaxCardinality();
653 	 			if (nni != null) {
654 	 				((OdmRestrictionClass)ontologyClass).setMaxCardinality(nni.getLexicalForm());
655 	 			}
656 	 		}	 			 	
657 	 		//subClassOf
658 	 		Collection subClassCol = odmClass.getTheSuperClass();
659 	 		if (!subClassCol.isEmpty()){  // is sub class of
660 	 			ontologyClass = (OdmOntologyClass)theOntology.findClass(classID);	 			
661 	 			iter1 = subClassCol.iterator(); 
662 	 			for(; iter1.hasNext(); ) {
663 	 				Odmclass subClass = (Odmclass)iter1.next();	 				
664 	 				OdmOntologyClass ontClass = (OdmOntologyClass)theOntology.findClass(subClass.getId());	 				
665 	 				ontologyClass.addSubClass(ontClass);
666 	 				ontClass.addSuperClass(ontologyClass);
667 	 			}
668 	 		}
669 	 		
670 	 		//equivalentClass 
671 	 		Collection equivClassCol = odmClass.getTheTargetEquivalent();
672 	 		if (!equivClassCol.isEmpty()){  // is equivalent class of
673 	 			ontologyClass = (OdmOntologyClass)theOntology.findClass(classID);
674 	 			iter1 = equivClassCol.iterator(); 
675 	 			for(; iter1.hasNext(); ) {
676 	 				Odmclass equivClass = (Odmclass)iter1.next();
677 	 				OdmOntologyClass ontClass = (OdmOntologyClass)theOntology.findClass(equivClass.getId());
678 	 				ontologyClass.addEquivalentClass(ontClass);	
679 	 			}
680 	 		}
681 	 		//disjointWithClass
682 	 		Collection disjointClassCol = odmClass.getTheTargetDisjointClass();
683 	 		if (!disjointClassCol.isEmpty()){  // is disjoint class with
684 	 			ontologyClass = (OdmOntologyClass)theOntology.findClass(classID);
685 	 			iter1 = disjointClassCol.iterator(); 
686 	 			for(; iter1.hasNext(); ) {
687 	 				Odmclass disjointClass = (Odmclass)iter1.next();
688 	 				OdmOntologyClass ontClass = (OdmOntologyClass)theOntology.findClass(disjointClass.getId());
689 	 				ontologyClass.addDisjointWithClass(ontClass);	
690 	 			}
691 	 		}
692 	 	}
693 	}
694 	
695 	/***
696 	 *  It reads the Ontology data ranges
697 	 * @param odmPackage
698 	 */
699 	protected void readOntologyDataRanges(OdmPackage odmPackage) {
700 		
701 		// Get all the ontology data ranges. For future use
702 	 	Iterator iter = odmPackage.getDatatypes().getDataRange().refAllOfType().iterator();
703 	 	
704 	 	for(; iter.hasNext() ;) {
705 	 		DataRange dr = (DataRange)iter.next();
706 	 		OdmDataRange dataRange;
707 	 		if (Enumeration.class.isInstance(dr)) {
708 	 			dataRange = new OdmEnumeration(theOntology, ((Enumeration)dr).getName(), ((Enumeration)dr).getId());
709 	 		} 
710 	 		else if (Datatype.class.isInstance(dr)) {
711 	 			dataRange = new OdmDataType(theOntology, ((Datatype)dr).getName(), ((Datatype)dr).getId()); 
712 	 		}	 		
713 	 	}	 
714 	}
715 	
716 	/***
717 	 * It reads the Ontology individuals
718 	 * 
719 	 * @param odmPackage
720 	 */
721 	protected void readOntologyIndividuals(OdmPackage odmPackage) {
722 		
723 		// Get all the ontology individuals. For future use
724 		Iterator iter = odmPackage.getIndividuals().getClassThing().refAllOfType().iterator();
725 	 	for (; iter.hasNext() ;) {
726 	 		ClassThing classThing = (ClassThing)iter.next();
727 
728 	 		// TODO:this must be changed. The getType must return no collection
729 	 		Collection type = classThing.getType();
730 	 		
731 	 		Odmclass c1 = (Odmclass)type.iterator().next();
732 	 		
733 	 		OdmSimpleNamedClassInst intstance = new OdmSimpleNamedClassInst(
734 	 												theOntology, 
735 													classThing.getName(),
736 													classThing.getId(),
737 													theOntology.findClass(c1.getId()));	 			 	
738 	 	}	
739 	}
740 	
741 	/***
742 	 * It reads the Ontology ObjectProperties
743 	 * @param odmPackage
744 	 */
745 	protected void readOntologyObjectProperties(OdmPackage odmPackage) {
746 		
747 		Iterator iter = odmPackage.getProperties().getObjectProperty().refAllOfType().iterator();
748 	 	
749 		for (; iter.hasNext() ;) {
750 	 		ObjectProperty prop = (ObjectProperty)iter.next();
751 	 		OdmObjectProperty objPro = new OdmObjectProperty(theOntology, prop.getName(), prop.getId());	 			 			 	
752 	 		
753 	 		objPro.setIsFunctional(FunctionalObjectProperty.class.isInstance(prop));
754 	 		objPro.setIsInverseFunctional(InverseFunctionalProperty.class.isInstance(prop));
755 	 		objPro.setIsSymmetric(SymmetricProperty.class.isInstance(prop));
756 	 		objPro.setIsTransitive(TransitiveProperty.class.isInstance(prop));
757 	 		
758 	 		Iterator domain = prop.getTheDomain().iterator();	 		
759 	 		for(; domain.hasNext(); ) {
760 	 			String classID = ((Odmclass)domain.next()).getId();
761 	 			OdmOntologyClass ontClass = theOntology.findClass(classID);
762 	 			objPro.addDomain(ontClass);	 			
763 	 			ontClass.addObjectProperty(objPro);
764 	 		}
765 	 		
766 	 		Iterator range = prop.getTheRange().iterator();
767 	 		for(; range.hasNext(); ) {
768 	 			String classID = ((Odmclass)range.next()).getId();
769 	 			OdmOntologyClass ontClass = theOntology.findClass(classID);
770 	 			objPro.addRange(ontClass);
771 	 			ontClass.addObjectProperty(objPro);
772 	 		}
773 	 		
774 	 		//Get the annotation of the property	 		
775 	 		Iterator aiter = prop.getTheAnnotationProperty().iterator();
776 	 		for(; aiter.hasNext(); ){
777 	 			AnnotationProperty annotation = (AnnotationProperty)aiter.next();
778 	 			OdmAnnotationProperty annotationProperty = new OdmAnnotationProperty(
779 	 									annotation.getName(),
780 										annotation.getId(),
781 										annotation.getTheAnnotationObject().toString());
782 	 			
783 	 			objPro.addAnnotationProp(annotationProperty);
784 	 		}	
785 	 	}
786 	}
787 	
788 	/***
789 	 * It read the Ontology Object Properties axioms
790 	 * @param odmPackage
791 	 */
792 	protected void readOntologyObjectPropertiesSecond(OdmPackage odmPackage) {
793 		
794 		Iterator iter = odmPackage.getProperties().getObjectProperty().refAllOfType().iterator();
795 	 	for (; iter.hasNext() ;) {
796 	 		ObjectProperty prop = (ObjectProperty)iter.next();
797 	 		OdmObjectProperty objProp = theOntology.findObjectProperty(prop.getId());		 			 	
798 	 		
799 	 		//supPropertyOf
800 	 		Iterator iter1 = prop.getTheSuperProperty().iterator();
801 	 		ObjectProperty prop1;
802 	 		for(; iter1.hasNext() ;) {
803 	 			prop1 = (ObjectProperty)iter1.next();
804 	 			OdmObjectProperty objProp1 = theOntology.findObjectProperty(prop1.getId());
805 	 			objProp.addSubproperty(objProp1);
806 	 			objProp1.addSuperProperty(objProp);	 					
807 	 		}
808 	 		//equivalnetProperty
809 	 		iter1 = prop.getTheTargetEquivalent().iterator();
810 	 		for(; iter1.hasNext() ;) {
811 	 			prop1 = (ObjectProperty)iter1.next();
812 	 			objProp.addEquivalentProperty(theOntology.findObjectProperty(prop1.getId()));
813 	 		}
814 	 		//invserseOf
815 	 		prop1 = prop.getTheTargetInverse();
816 	 		if (prop1 != null) {
817 	 			objProp.setInverseOf(theOntology.findObjectProperty(prop1.getId()));
818 	 		}
819 	 	}
820 	}
821 	
822 	/***
823 	 * It reads the Ontology Datatype Properties
824 	 * @param odmPackage
825 	 */
826 	protected void readOntologyDatatypeProperties(OdmPackage odmPackage) {
827 		
828 		Iterator iter = odmPackage.getProperties().getDatatypeProperty().refAllOfClass().iterator();
829 	 	for (; iter.hasNext() ;) {
830 	 		DatatypeProperty prop = (DatatypeProperty)iter.next();
831 	 		
832 	 		OdmDatatypeProperty datatypePro = new OdmDatatypeProperty(theOntology, prop.getName(), prop.getId());	 		
833 	 		
834 	 		datatypePro.setIsFunctional(FunctionalDatatypeProperty.class.isInstance(prop));
835 	 			 			 		 		
836 	 		Iterator domain = prop.getTheDomain().iterator();
837 	 		for(; domain.hasNext(); ) {
838 	 			String classID = ((Odmclass)domain.next()).getId();
839 	 			OdmOntologyClass ontClass = theOntology.findClass(classID);
840 	 			datatypePro.addDomain(ontClass);	 			
841 	 			ontClass.addDatatypeProperty(datatypePro);
842 	 		}
843 	 		
844 	 		DataRange range = prop.getTheDataRange();
845 	 		if (Enumeration.class.isInstance(range)) {
846 	 			OdmEnumeration enumeration = new OdmEnumeration(theOntology, 
847 	 												((Enumeration)range).getName(),
848 													((Enumeration)range).getId());
849 	 			
850 	 				 			
851 	 			Iterator iter1 = ((Enumeration)range).getEnumerator().iterator();
852 	 			for(; iter1.hasNext() ; ) {	 			
853 					enumeration.addElm(new OdmDataValue( ((Literal)iter1.next()).getLexicalForm())); 
854 	 			}
855 	 			datatypePro.addRange(enumeration);	 			
856 	 		}
857 	 		else {	 		
858 	 			String name = "";
859 	 			
860 	 			// TODO: the DataType getId does not exist. Nikos	 		 			
861 	 			if (range instanceof PrimitiveType ) {
862 	 				name = ((PrimitiveType)range).getName();
863 	 			} 
864 	 			else if (range instanceof DepricatedDatatype) {
865 	 				name = ((DepricatedDatatype)range).getName();
866 	 			}
867 	 			else if ( range instanceof Datatype) {
868 	 				name = ((Datatype)range).getName();	 			
869 	 			}
870 	 			OdmDataType dataType = new OdmDataType(theOntology, 	 												   
871 	 												   	name,
872 	 												  	""+1
873 													    );
874 	 			datatypePro.addRange(dataType);	 
875 	 		}
876 	 	}
877 	}
878 	
879 	/***
880 	 *  It reads the Ontology Datatype Properties axioms
881 	 * @param odmPackage
882 	 */
883 	protected void readOntologyDatatypePropertiesSecond(OdmPackage odmPackage) {
884 		
885 		Iterator iter = odmPackage.getProperties().getDatatypeProperty().refAllOfClass().iterator();
886 	 	
887 		for (; iter.hasNext() ;) {
888 	 		DatatypeProperty prop = (DatatypeProperty)iter.next();	 		
889 	 		Iterator domain = prop.getTheDomain().iterator();
890 	 		String classID = ((Odmclass)domain.next()).getId();
891 	 		OdmOntologyClass ontClass = theOntology.findClass(classID);
892 	 		
893 	 		OdmDatatypeProperty datatypePro = ontClass.findDatatypeProperty(prop.getId());
894 				 		
895 	 		DatatypeProperty prop1;
896 	 		Iterator domain1;
897 	 		String classID1;
898 	 		OdmOntologyClass ontClass1;
899 	 		OdmDatatypeProperty datatypePro1;
900 	 		
901 	 		//sup property of
902 	 		Iterator iter1 = prop.getTheSuperProperty().iterator();
903 	 		for(; iter1.hasNext() ;) {
904 	 			prop1 = (DatatypeProperty)iter1.next();
905 	 			domain1 = prop1.getTheDomain().iterator();
906 		 		classID1 = ((Odmclass)domain1.next()).getId();
907 		 		ontClass1 = theOntology.findClass(classID1);
908 		 		
909 		 		datatypePro1 = ontClass1.findDatatypeProperty(prop1.getId());
910 		 		datatypePro.addSubproperty(datatypePro1);
911 	 		}
912 			
913 			//equivalent property of	 		
914 	 		iter1 = prop.getTheTargetEquivalent().iterator();
915 	 		for(; iter1.hasNext() ;) {
916 	 			prop1 = (DatatypeProperty)iter1.next();
917 	 			domain1 = prop1.getTheDomain().iterator();
918 		 		classID1 = ((Odmclass)domain1.next()).getId();
919 		 		ontClass1 = theOntology.findClass(classID1);
920 		 		
921 		 		datatypePro1 = ontClass1.findDatatypeProperty(prop1.getId());
922 		 		datatypePro.addEquivalentProperty(datatypePro1);
923 	 		}
924 	 	}	
925 	}
926 	
927 	
928 	/***
929 	 * 
930 	 * Inner class that implement the DoubleClickListener interface
931 	 * It is used in order to provide an 'interface' for the usage of
932 	 * the Ontology Viewer from other S/W components, e.g. BML Editor,
933 	 * SDL Editor, etc.
934 	 */
935 	
936 	class DoubleClickListener implements IDoubleClickListener {
937 		
938 		DoubleClickListener() {
939 			
940 		}
941 		
942 		public void doubleClick(DoubleClickEvent event) {			
943 			
944 			if( !event.getSelection().isEmpty() ) {	           				
945 				if( event.getSelection() instanceof IStructuredSelection ) {
946 					IStructuredSelection selection = (IStructuredSelection)event
947 													.getSelection();
948 		           		        
949 					Iterator iterator = selection.iterator();
950 					for (; iterator.hasNext();) {						
951 						Object src = iterator.next();		            		        
952 						if ( src instanceof OdmOntologyClass ) {  // Restriction class		        							
953 							int size = ontologyTreeListeners.size();
954 							
955 							for(int i = 0; i < size; ++i) {															
956 								OdmOntologyClass ontClass = (OdmOntologyClass)src;											
957 								((IOntologyTreeListener)ontologyTreeListeners.get(i))
958 											.classSelected(
959 												ontClass.getOntology().getId(),
960 												ontClass.getOntology().getName(),
961 												ontClass.getId(),
962 												ontClass.getName(),
963 												ontClass.getClass().getName()
964 											);
965 							}
966 						}
967 					}
968 				}
969 			}
970 		}
971 	}
972 	
973 	/***
974 	 * Adds a listener for double click event that correspond to the
975 	 * selection of an ontology class in this Ontology Viewer
976 	 * 
977 	 * @param listener
978 	 *           an ontology tree viewer listener
979 	 */
980 	
981 	public void addOntologyTreeListener(IOntologyTreeListener listener) {
982 		ontologyTreeListeners.add(listener);
983 	}
984 	
985 	/***
986 	 * Removes a listener for double click event that correspond to the
987 	 * selection of an ontology class in the Ontology viewer
988 	 * 
989 	 * @param listener
990 	 *           an ontology tree viewer listener
991 	 */
992 	public void removeOntologyTreeListener(IOntologyTreeListener listener) {
993 		ontologyTreeListeners.remove(listener);
994 	}	
995 	
996 	/*** 
997 	 * It reads the Ontology Viewer property file
998 	 */
999 	public String readFADAinfo() {
1000 		
1001 		IPreferenceStore store;
1002 		store = OntologyViewerPlugin.getDefault().getPreferenceStore();
1003 		String address = store.getString(Constants.FADA_NODE_ADDRESS);			
1004 		return address;
1005 	}
1006 	
1007 	public OdmOntology getOntology() {
1008 		return theOntology;
1009 	}
1010 }