View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3    Copyright (c) 2005, University of Innsbruck, Austria
4    This library is free software; you can redistribute it and/or modify it under
5    the terms of the GNU Lesser General Public License as published by the Free
6    Software Foundation; either version 2.1 of the License, or (at your option)
7    any later version.
8    This library is distributed in the hope that it will be useful, but WITHOUT
9    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11   details.
12   You should have received a copy of the GNU Lesser General Public License along
13   with this library; if not, write to the Free Software Foundation, Inc.,
14   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15   */
16  package org.deri.wsmo4j.logicalexpression.util;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  import java.util.Map.Entry;
25  
26  import org.omwg.logicalexpression.LogicalExpression;
27  import org.omwg.logicalexpression.terms.Term;
28  import org.omwg.ontology.*;
29  import org.wsmo.common.Identifier;
30  import org.wsmo.common.exception.InvalidModelException;
31  import org.wsmo.common.exception.SynchronisationException;
32  
33  
34  /**
35   * Utility class to extract concepts, instances, relations and attributes 
36   * from a given ontology.
37   * 
38   * @author Martin Tanler, DERI Innsbruck
39   * @author Nathalie Steinmetz, DERI Innsbruck
40   */
41  public class OntologyUtil { 
42  	
43  	private static ItemCollectHelper helper = new ItemCollectHelper();
44  
45  	private static List <Term> conceptList = new ArrayList <Term> (15);
46  	
47  	private static List <Term> instanceList = new ArrayList <Term> (15);
48  	
49  	private static List <Term> relationList = new ArrayList <Term> (15);
50  	
51  	private static List <Term> attributeList = new ArrayList <Term> (15);
52  	
53  	private static Map attributeMap = new HashMap();
54  	
55  	/**
56  	 * Collecting all concepts from a given ontology.
57  	 * 
58  	 * @param o Ontology to be checked for contained concepts
59  	 * @return List of terms, identifying all concepts from the ontology
60  	 * @throws InvalidModelException 
61  	 * @throws SynchronisationException 
62  	 */
63  	public static List getConcepts(Ontology o) 
64  			throws SynchronisationException, InvalidModelException{
65  		conceptList = new ArrayList <Term> (15);
66  		collectConcepts(o);
67  		collectInstances(o);
68  		checkLogicalExpressions(o);
69  		return conceptList;
70  	}
71  	
72  	/**
73  	 * Collecting all relations from a given ontology.
74  	 * 
75  	 * @param o Ontolgoy to be checked for contained relations
76  	 * @return List of terms, identifying all relations from the ontology
77  	 * @throws InvalidModelException 
78  	 * @throws SynchronisationException 
79  	 */
80  	public static List getRelations(Ontology o) 
81  			throws SynchronisationException, InvalidModelException{
82  		relationList = new ArrayList <Term> (15);
83  		collectRelations(o);
84  		checkLogicalExpressions(o);
85  		return relationList;
86  	}
87  
88  	/**
89  	 * Collecting all attributes from a given concept.
90  	 * 
91  	 * @param c Identifier of the concept to be checked for belonging attributes.
92  	 * @param o Ontology to be checked for concepts and their attributes.
93  	 * @return List of terms, identifying all Attributes belonging to 
94  	 * 		  a concept
95  	 * @throws InvalidModelException 
96  	 * @throws SynchronisationException 
97  	 */
98  	public static List getAttributes(Term c, Ontology o) 
99  			throws SynchronisationException, InvalidModelException{
100 		attributeList = new ArrayList <Term> (15);
101 		collectConceptAttributes(c, o);
102 		checkLogicalExpressions(o);
103 		if (!attributeMap.isEmpty()) {
104 			Set entrySet = attributeMap.entrySet();
105 			Iterator itSet = entrySet.iterator();
106 			while (itSet.hasNext()) {
107 				Entry entry = (Entry) itSet.next();
108 				Term conId = (Term) entry.getKey();
109 				Term attId = (Term) entry.getValue();
110 				if (conId.equals(c)) {
111 					if (!attributeList.contains(attId)) {
112 						attributeList.add(attId);
113 					}
114 				}
115 			}
116 		}
117 		return attributeList;
118 	}	
119 	
120 	/**
121 	 * Collecting all attributes from a given concept. It is preferebly to 
122 	 * use the method getAttributes(Term c, Ontology o) for collecting 
123 	 * the attributes from a concept, as that method is more complete in 
124 	 * regard to implicitly defined concepts.
125 	 * 
126 	 * @param c Concept to be checked for belonging attributes.
127 	 * @return List of terms, identifying all Attributes belonging to 
128 	 * 		  a concept
129 	 * @throws InvalidModelException 
130 	 * @throws SynchronisationException 
131 	 * @see #getAttributes(Term, Ontology)
132 	 */
133 	public static List getAttributes(Concept c) 
134 			throws SynchronisationException, InvalidModelException{
135 		attributeList = new ArrayList <Term> (15);
136 		collectConceptAttributes(c);
137 		checkLogicalExpressions(c.getOntology());
138 		if (!attributeMap.isEmpty()) {
139 			Set entrySet = attributeMap.entrySet();
140 			Iterator itSet = entrySet.iterator();
141 			while (itSet.hasNext()) {
142 				Entry entry = (Entry) itSet.next();
143 				Term conId = (Term) entry.getKey();
144 				Term attId = (Term) entry.getValue();
145 				if (conId.equals(c)) {
146 					if (!attributeList.contains(attId)) {
147 						attributeList.add(attId);
148 					}
149 				}
150 			}
151 		}
152 		return attributeList;
153 	}	
154 
155 	/**
156 	 * Collecting all instances from a given ontology.
157 	 * 
158 	 * @param o Ontology to be checked for contained instances
159 	 * @return List of terms, identifying all instances from the ontology
160 	 * @throws InvalidModelException 
161 	 * @throws SynchronisationException 
162 	 */
163 	public static List getInstances(Ontology o) 
164 			throws SynchronisationException, InvalidModelException{
165 		instanceList = new ArrayList <Term> (15);
166 		collectInstances(o);
167 		checkLogicalExpressions(o);
168 		return instanceList;
169 	}
170 	
171 	private static void collectConcepts(Ontology o) {
172 		Iterator it = o.listConcepts().iterator();
173 		while (it.hasNext()) {
174 			Concept concept = (Concept) it.next();
175 			if (!conceptList.contains(concept.getIdentifier()))
176 				conceptList.add(concept.getIdentifier());
177 			
178 	        //checking for SuperConcepts and adding them to the list
179 			Iterator it2 = concept.listSuperConcepts().iterator();
180 			while (it2.hasNext()) {
181 				Concept superConcept = (Concept) it2.next();
182 				if (!conceptList.contains(superConcept.getIdentifier()))
183 					conceptList.add(superConcept.getIdentifier());
184 			}
185 			
186 			//checking attribute types and adding them evtl. to the list
187 			Iterator it3 = concept.listAttributes().iterator();
188 			while (it3.hasNext()) {
189 				Attribute attribute = (Attribute) it3.next();
190 				Iterator it4 = attribute.listTypes().iterator();
191 				while (it4.hasNext()) {
192 					Type type = (Type) it4.next();
193 					if (type instanceof Concept) {
194 						if (!conceptList.contains(((Concept) type).getIdentifier()))
195 							conceptList.add(((Concept) type).getIdentifier());
196 					}
197 				}
198 			}
199 		}
200 	}
201 	
202 	private static void collectConceptAttributes(Term t, Ontology o) {
203 		Concept concept = null;
204 		Instance instance = null;
205 		if (t instanceof Identifier) {
206 			concept = o.findConcept((Identifier) t);
207 			instance = o.findInstance((Identifier) t);
208 		}
209 		if (concept != null) {
210 			Iterator it = concept.listAttributes().iterator();
211 			while (it.hasNext()) {
212 				Attribute attribute = (Attribute) it.next();
213 				if (!attributeList.contains(attribute.getIdentifier()))
214 					attributeList.add(attribute.getIdentifier());
215 			}
216 		}
217 		if (instance != null) {
218 			Map instanceMap = instance.listAttributeValues();
219 			Set keySet = instanceMap.keySet();
220 			Iterator it = keySet.iterator();
221 			while (it.hasNext()) {
222 				Identifier attId = (Identifier) it.next();
223 				if (!attributeList.contains(attId))
224 					attributeList.add(attId);
225 			}
226 		}
227 	}
228 	
229 	private static void collectConceptAttributes(Concept c) {
230 		Iterator it = c.listAttributes().iterator();
231 		while (it.hasNext()) {
232 			Attribute attribute = (Attribute) it.next();
233 			if (!attributeList.contains(attribute.getIdentifier()))
234 				attributeList.add(attribute.getIdentifier());
235 		}
236 	}
237 	
238 	private static void collectInstances(Ontology o) {
239 		Iterator it = o.listInstances().iterator();
240 		while (it.hasNext()) {
241 			Instance instance = (Instance) it.next();
242 			if (!instanceList.contains(instance.getIdentifier()))
243 				instanceList.add(instance.getIdentifier());
244 			
245 	        //checking for memberOf concepts and adding them to the concept list
246 			Iterator it2 = instance.listConcepts().iterator();
247 			while (it2.hasNext()) {
248 				Concept concept = (Concept) it2.next();
249 				if (!conceptList.contains(concept.getIdentifier()))
250 					conceptList.add(concept.getIdentifier());
251 			}
252 		}
253 	}
254 	
255 	private static void collectRelations(Ontology o) {
256 		Iterator it = o.listRelations().iterator();
257 		while (it.hasNext()) {
258 			Relation relation = (Relation) it.next();
259 			if (!relationList.contains(relation.getIdentifier()))
260 				relationList.add(relation.getIdentifier());
261 			
262 	        //checking for SuperConcepts and adding them to the list
263 			Iterator it2 = relation.listSuperRelations().iterator();
264 			while (it2.hasNext()) {
265 				Relation superRelation = (Relation) it2.next();
266 				if (!relationList.contains(superRelation.getIdentifier()))
267 					relationList.add(superRelation.getIdentifier());
268 			}
269 		}
270 	}
271 	
272 	private static void checkLogicalExpressions(Ontology o) 
273 			throws SynchronisationException, InvalidModelException {
274 		Iterator it = o.listAxioms().iterator();
275 		while (it.hasNext()) {
276 			Axiom axiom = (Axiom) it.next();
277 			Iterator itLogExp = axiom.listDefinitions().iterator();
278 			while (itLogExp.hasNext()) {
279 				LogicalExpression logExpr = (LogicalExpression) itLogExp.next();
280 				logExpr.accept(helper);
281 			}
282 			// collect concepts from logical expressions
283 			Iterator itCon = helper.getConceptIds().iterator();
284 			while (itCon.hasNext()) {
285 				Term conIdentifier = (Term) itCon.next();
286 				if (!conceptList.contains(conIdentifier)) {
287 					conceptList.add(conIdentifier);
288 				}
289 			}
290 			// collect instances from logical expressions
291 			Iterator itIns = helper.getInstanceIds().iterator();
292 			while (itIns.hasNext()) {
293 				Term insIdentifier = (Term) itIns.next();
294 				if (!instanceList.contains(insIdentifier)) {
295 					instanceList.add(insIdentifier);
296 				}
297 			}
298 			// collect relations from logical expressions
299 			Iterator itRel = helper.getRelationIds().iterator();
300 			while (itRel.hasNext()) {
301 				Term relIdentifier = (Term) itRel.next();
302 				if (!relationList.contains(relIdentifier)) {
303 					relationList.add(relIdentifier);
304 				}
305 			}
306 			// collect attributes from logical expressions
307 			attributeMap = helper.getAttributeIds();
308 		}
309 	}
310 	
311 }
312