Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 312   Methods: 11
NCLOC: 208   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
OntologyUtil.java 0% 0% 0% 0%
coverage
 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  0 public static List getConcepts(Ontology o)
 64    throws SynchronisationException, InvalidModelException{
 65  0 conceptList = new ArrayList <Term> (15);
 66  0 collectConcepts(o);
 67  0 collectInstances(o);
 68  0 checkLogicalExpressions(o);
 69  0 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  0 public static List getRelations(Ontology o)
 81    throws SynchronisationException, InvalidModelException{
 82  0 relationList = new ArrayList <Term> (15);
 83  0 collectRelations(o);
 84  0 checkLogicalExpressions(o);
 85  0 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  0 public static List getAttributes(Term c, Ontology o)
 99    throws SynchronisationException, InvalidModelException{
 100  0 attributeList = new ArrayList <Term> (15);
 101  0 collectConceptAttributes(c, o);
 102  0 checkLogicalExpressions(o);
 103  0 if (!attributeMap.isEmpty()) {
 104  0 Set entrySet = attributeMap.entrySet();
 105  0 Iterator itSet = entrySet.iterator();
 106  0 while (itSet.hasNext()) {
 107  0 Entry entry = (Entry) itSet.next();
 108  0 Term conId = (Term) entry.getKey();
 109  0 Term attId = (Term) entry.getValue();
 110  0 if (conId.equals(c)) {
 111  0 if (!attributeList.contains(attId)) {
 112  0 attributeList.add(attId);
 113    }
 114    }
 115    }
 116    }
 117  0 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  0 public static List getAttributes(Concept c)
 134    throws SynchronisationException, InvalidModelException{
 135  0 attributeList = new ArrayList <Term> (15);
 136  0 collectConceptAttributes(c);
 137  0 checkLogicalExpressions(c.getOntology());
 138  0 if (!attributeMap.isEmpty()) {
 139  0 Set entrySet = attributeMap.entrySet();
 140  0 Iterator itSet = entrySet.iterator();
 141  0 while (itSet.hasNext()) {
 142  0 Entry entry = (Entry) itSet.next();
 143  0 Term conId = (Term) entry.getKey();
 144  0 Term attId = (Term) entry.getValue();
 145  0 if (conId.equals(c)) {
 146  0 if (!attributeList.contains(attId)) {
 147  0 attributeList.add(attId);
 148    }
 149    }
 150    }
 151    }
 152  0 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  0 public static List getInstances(Ontology o)
 164    throws SynchronisationException, InvalidModelException{
 165  0 instanceList = new ArrayList <Term> (15);
 166  0 collectInstances(o);
 167  0 checkLogicalExpressions(o);
 168  0 return instanceList;
 169    }
 170   
 171  0 private static void collectConcepts(Ontology o) {
 172  0 Iterator it = o.listConcepts().iterator();
 173  0 while (it.hasNext()) {
 174  0 Concept concept = (Concept) it.next();
 175  0 if (!conceptList.contains(concept.getIdentifier()))
 176  0 conceptList.add(concept.getIdentifier());
 177   
 178    //checking for SuperConcepts and adding them to the list
 179  0 Iterator it2 = concept.listSuperConcepts().iterator();
 180  0 while (it2.hasNext()) {
 181  0 Concept superConcept = (Concept) it2.next();
 182  0 if (!conceptList.contains(superConcept.getIdentifier()))
 183  0 conceptList.add(superConcept.getIdentifier());
 184    }
 185   
 186    //checking attribute types and adding them evtl. to the list
 187  0 Iterator it3 = concept.listAttributes().iterator();
 188  0 while (it3.hasNext()) {
 189  0 Attribute attribute = (Attribute) it3.next();
 190  0 Iterator it4 = attribute.listTypes().iterator();
 191  0 while (it4.hasNext()) {
 192  0 Type type = (Type) it4.next();
 193  0 if (type instanceof Concept) {
 194  0 if (!conceptList.contains(((Concept) type).getIdentifier()))
 195  0 conceptList.add(((Concept) type).getIdentifier());
 196    }
 197    }
 198    }
 199    }
 200    }
 201   
 202  0 private static void collectConceptAttributes(Term t, Ontology o) {
 203  0 Concept concept = null;
 204  0 Instance instance = null;
 205  0 if (t instanceof Identifier) {
 206  0 concept = o.findConcept((Identifier) t);
 207  0 instance = o.findInstance((Identifier) t);
 208    }
 209  0 if (concept != null) {
 210  0 Iterator it = concept.listAttributes().iterator();
 211  0 while (it.hasNext()) {
 212  0 Attribute attribute = (Attribute) it.next();
 213  0 if (!attributeList.contains(attribute.getIdentifier()))
 214  0 attributeList.add(attribute.getIdentifier());
 215    }
 216    }
 217  0 if (instance != null) {
 218  0 Map instanceMap = instance.listAttributeValues();
 219  0 Set keySet = instanceMap.keySet();
 220  0 Iterator it = keySet.iterator();
 221  0 while (it.hasNext()) {
 222  0 Identifier attId = (Identifier) it.next();
 223  0 if (!attributeList.contains(attId))
 224  0 attributeList.add(attId);
 225    }
 226    }
 227    }
 228   
 229  0 private static void collectConceptAttributes(Concept c) {
 230  0 Iterator it = c.listAttributes().iterator();
 231  0 while (it.hasNext()) {
 232  0 Attribute attribute = (Attribute) it.next();
 233  0 if (!attributeList.contains(attribute.getIdentifier()))
 234  0 attributeList.add(attribute.getIdentifier());
 235    }
 236    }
 237   
 238  0 private static void collectInstances(Ontology o) {
 239  0 Iterator it = o.listInstances().iterator();
 240  0 while (it.hasNext()) {
 241  0 Instance instance = (Instance) it.next();
 242  0 if (!instanceList.contains(instance.getIdentifier()))
 243  0 instanceList.add(instance.getIdentifier());
 244   
 245    //checking for memberOf concepts and adding them to the concept list
 246  0 Iterator it2 = instance.listConcepts().iterator();
 247  0 while (it2.hasNext()) {
 248  0 Concept concept = (Concept) it2.next();
 249  0 if (!conceptList.contains(concept.getIdentifier()))
 250  0 conceptList.add(concept.getIdentifier());
 251    }
 252    }
 253    }
 254   
 255  0 private static void collectRelations(Ontology o) {
 256  0 Iterator it = o.listRelations().iterator();
 257  0 while (it.hasNext()) {
 258  0 Relation relation = (Relation) it.next();
 259  0 if (!relationList.contains(relation.getIdentifier()))
 260  0 relationList.add(relation.getIdentifier());
 261   
 262    //checking for SuperConcepts and adding them to the list
 263  0 Iterator it2 = relation.listSuperRelations().iterator();
 264  0 while (it2.hasNext()) {
 265  0 Relation superRelation = (Relation) it2.next();
 266  0 if (!relationList.contains(superRelation.getIdentifier()))
 267  0 relationList.add(superRelation.getIdentifier());
 268    }
 269    }
 270    }
 271   
 272  0 private static void checkLogicalExpressions(Ontology o)
 273    throws SynchronisationException, InvalidModelException {
 274  0 Iterator it = o.listAxioms().iterator();
 275  0 while (it.hasNext()) {
 276  0 Axiom axiom = (Axiom) it.next();
 277  0 Iterator itLogExp = axiom.listDefinitions().iterator();
 278  0 while (itLogExp.hasNext()) {
 279  0 LogicalExpression logExpr = (LogicalExpression) itLogExp.next();
 280  0 logExpr.accept(helper);
 281    }
 282    // collect concepts from logical expressions
 283  0 Iterator itCon = helper.getConceptIds().iterator();
 284  0 while (itCon.hasNext()) {
 285  0 Term conIdentifier = (Term) itCon.next();
 286  0 if (!conceptList.contains(conIdentifier)) {
 287  0 conceptList.add(conIdentifier);
 288    }
 289    }
 290    // collect instances from logical expressions
 291  0 Iterator itIns = helper.getInstanceIds().iterator();
 292  0 while (itIns.hasNext()) {
 293  0 Term insIdentifier = (Term) itIns.next();
 294  0 if (!instanceList.contains(insIdentifier)) {
 295  0 instanceList.add(insIdentifier);
 296    }
 297    }
 298    // collect relations from logical expressions
 299  0 Iterator itRel = helper.getRelationIds().iterator();
 300  0 while (itRel.hasNext()) {
 301  0 Term relIdentifier = (Term) itRel.next();
 302  0 if (!relationList.contains(relIdentifier)) {
 303  0 relationList.add(relIdentifier);
 304    }
 305    }
 306    // collect attributes from logical expressions
 307  0 attributeMap = helper.getAttributeIds();
 308    }
 309    }
 310   
 311    }
 312