Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 411   Methods: 13
NCLOC: 274   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AnonIdCollectHelper.java 0% 0% 0% 0%
coverage
 1    /*
 2    wsmo4j - a WSMO API and Reference Implementation
 3    Copyright (c) 2004, DERI Innsbruck
 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.validator;
 17   
 18    import java.util.*;
 19    import java.util.Map.Entry;
 20   
 21    import org.omwg.ontology.*;
 22    import org.wsmo.common.*;
 23    import org.wsmo.validator.ValidationError;
 24   
 25    /**
 26    * The AnonIdCollectHelper collects the anonym concepts, instances and relation, and
 27    * checks if they are used correctly. An object with an unnumbered anonymous identifier
 28    * cannot be referenced, and thus, the validator throws an error, if such an object is
 29    * referenced.
 30    *
 31    * <pre>
 32    * Created on March 24, 2006
 33    * Committed by $Author: morcen $
 34    * $Source$
 35    * </pre>
 36    *
 37    * @author nathalie.steinmetz@deri.org
 38    *
 39    * @version $Revision: 1946 $ $Date: 2007-04-02 15:13:28 +0300 (Mon, 02 Apr 2007) $
 40    */
 41    public class AnonIdCollectHelper {
 42   
 43    private final static String ERROR_ANON_IDS = "An object with an unnumbered " +
 44    "anonymous identifier cannot be referenced. To reference this object, name it.";
 45   
 46    private List <ValidationError> errors = null;
 47   
 48    private HashMap <Concept, Integer> concepts = null;
 49   
 50    private HashMap <Relation, Integer> relations = null;
 51   
 52    private HashMap <Instance, Integer> instances = null;
 53   
 54  0 public AnonIdCollectHelper(List <ValidationError> errors) {
 55  0 this.errors = errors;
 56  0 concepts = new HashMap <Concept, Integer> ();
 57  0 relations = new HashMap <Relation, Integer> ();
 58  0 instances = new HashMap <Instance, Integer> ();
 59    }
 60   
 61    /*
 62    * This method checks if the given concept has got an unnumbered
 63    * anonymous identifier and thus needs to be put into the concerned
 64    * map 'concepts'. If not, it is checked if there are any references
 65    * to such an anonymous concept, which then needs to be put in the map.
 66    */
 67  0 private void visitConcept(Concept concept){
 68  0 boolean checked = false;
 69  0 if (concept.getIdentifier() instanceof UnnumberedAnonymousID) {
 70  0 addToConcepts(concept);
 71  0 if (concept.listSuperConcepts().size() > 0) {
 72  0 if (checked) {
 73  0 checked = false;
 74  0 addToConcepts(concept);
 75    }
 76    else {
 77  0 checked = true;
 78    }
 79    }
 80  0 if (concept.listAttributes().size() > 0) {
 81  0 if (checked) {
 82  0 checked = false;
 83  0 addToConcepts(concept);
 84    }
 85    else {
 86  0 checked = true;
 87    }
 88    }
 89  0 if (concept.listInstances().size() > 0) {
 90  0 for (int i=0; i<concept.listInstances().size(); i++) {
 91  0 if (checked) {
 92  0 checked = false;
 93  0 addToConcepts(concept);
 94    }
 95    else {
 96  0 checked = true;
 97    }
 98    }
 99    }
 100  0 if (concept.listNFPValues().size() > 0) {
 101  0 for (int i=0; i<concept.listNFPValues().size(); i++) {
 102  0 if (checked) {
 103  0 checked = false;
 104  0 addToConcepts(concept);
 105    }
 106    else {
 107  0 checked = true;
 108    }
 109    }
 110    }
 111    }
 112  0 if (concept.listSuperConcepts().size() > 0) {
 113  0 Iterator it = concept.listSuperConcepts().iterator();
 114  0 while (it.hasNext()) {
 115  0 Concept tmp = (Concept) it.next();
 116  0 if (tmp.getIdentifier() instanceof UnnumberedAnonymousID) {
 117  0 addToConcepts(tmp, concept);
 118    }
 119    }
 120    }
 121  0 if (concept.listAttributes().size() > 0) {
 122  0 Iterator it = concept.listAttributes().iterator();
 123  0 while (it.hasNext()) {
 124  0 Attribute a = (Attribute) it.next();
 125  0 if (a.listTypes().size() > 0) {
 126  0 Iterator it2 = a.listTypes().iterator();
 127  0 while (it2.hasNext()) {
 128  0 Type t = (Type) it2.next();
 129  0 if (t instanceof Concept &&
 130    ((Concept) t).getIdentifier() instanceof UnnumberedAnonymousID) {
 131  0 addToConcepts((Concept) t, a);
 132    }
 133    }
 134    }
 135    }
 136    }
 137    }
 138   
 139    /*
 140    * This method checks if the given instance has got an unnumbered
 141    * anonymous identifier and thus needs to be put into the appropriate
 142    * map 'instances'. If not, it is checked if there are any references
 143    * to such an anonymous instance, which then needs to be put in the map.
 144    */
 145  0 private void visitInstance(Instance instance) {
 146  0 boolean checked = false;
 147  0 if (instance.getIdentifier() instanceof UnnumberedAnonymousID) {
 148  0 addToInstances(instance);
 149  0 if (instance.listAttributeValues().size() > 0) {
 150  0 if (checked) {
 151  0 checked = false;
 152  0 addToInstances(instance);
 153    }
 154    else {
 155  0 checked = true;
 156    }
 157    }
 158  0 if (instance.listConcepts().size() > 0) {
 159  0 for (int i=0; i<instance.listConcepts().size(); i++) {
 160  0 if (checked) {
 161  0 checked = false;
 162  0 addToInstances(instance);
 163    }
 164    else {
 165  0 checked = true;
 166    }
 167    }
 168    }
 169  0 if (instance.listNFPValues().size() > 0) {
 170  0 for (int i=0; i<instance.listNFPValues().size(); i++) {
 171  0 if (checked) {
 172  0 checked = false;
 173  0 addToInstances(instance);
 174    }
 175    else {
 176  0 checked = true;
 177    }
 178    }
 179    }
 180    }
 181  0 if (instance.listAttributeValues().size() > 0) {
 182  0 Iterator it = instance.listAttributeValues().entrySet().iterator();
 183  0 while (it.hasNext()) {
 184  0 Set set = (Set) ((Entry) it.next()).getValue();
 185  0 Iterator it2 = set.iterator();
 186  0 while (it2.hasNext()) {
 187  0 Object o = it2.next();
 188  0 if (o instanceof Instance &&
 189    ((Instance) o).getIdentifier() instanceof UnnumberedAnonymousID) {
 190  0 addToInstances((Instance) o, instance);
 191    }
 192    }
 193    }
 194    }
 195    }
 196   
 197    /*
 198    * This method checks if the given relation has got an unnumbered
 199    * anonymous identifier and thus needs to be put into the appropriate
 200    * map 'relations'. If not, it is checked if there are any references
 201    * to an anonymous relation or concept, which then needs to be put in
 202    * the appropriate map.
 203    */
 204  0 private void visitRelation(Relation relation) {
 205  0 if (relation.getIdentifier() instanceof UnnumberedAnonymousID) {
 206  0 addToRelations(relation);
 207  0 if (relation.listSuperRelations().size() > 0) {
 208  0 addToRelations(relation);
 209    }
 210  0 if (relation.listParameters().size() > 0) {
 211  0 Iterator it = relation.listParameters().iterator();
 212  0 while (it.hasNext()) {
 213  0 Parameter p = (Parameter) it.next();
 214  0 if (p.listTypes().size() > 0) {
 215  0 addToRelations(relation);
 216    }
 217    }
 218    }
 219  0 if (relation.listNFPValues().size() > 0) {
 220  0 for (int i=0; i<relation.listNFPValues().size(); i++) {
 221  0 addToRelations(relation);
 222    }
 223    }
 224    }
 225  0 if (relation.listSuperRelations().size() > 0) {
 226  0 Iterator it = relation.listSuperRelations().iterator();
 227  0 while(it.hasNext()) {
 228  0 Relation tmp = (Relation) it.next();
 229  0 if (tmp.getIdentifier() instanceof UnnumberedAnonymousID) {
 230  0 addToRelations(tmp, relation);
 231    }
 232    }
 233    }
 234  0 if (relation.listParameters().size() > 0) {
 235  0 Iterator it = relation.listParameters().iterator();
 236  0 while (it.hasNext()) {
 237  0 Parameter p = (Parameter) it.next();
 238  0 if (p.listTypes().size() > 0) {
 239  0 Iterator it2 = p.listTypes().iterator();
 240  0 while (it2.hasNext()) {
 241  0 Type t = (Type) it2.next();
 242  0 if (t instanceof Concept &&
 243    ((Concept) t).getIdentifier() instanceof UnnumberedAnonymousID) {
 244  0 addToConcepts((Concept) t, relation);
 245    // if an relation contains a reference to an anonymous concept,
 246    // this relation cannot have a sub- or superrelation.
 247  0 if (relation.listSuperRelations().size() > 0 ||
 248    relation.listSubRelations().size() > 0) {
 249  0 addError(relation, ValidationError.ANON_ID_ERR + ":\n" +
 250    ERROR_ANON_IDS);
 251    }
 252    }
 253    }
 254    }
 255    }
 256    }
 257    }
 258   
 259    /*
 260    * This method checks if the given relationInstance references
 261    * an anonymous relation or instance, which then needs to be put
 262    * in the appropriate map.
 263    */
 264  0 private void visitRelationInstance(RelationInstance relationInstance) {
 265  0 if (relationInstance.getRelation().getIdentifier() instanceof UnnumberedAnonymousID) {
 266  0 addError(relationInstance, ValidationError.ANON_ID_ERR + ":\n" +
 267    "A relationInstance cannot reference an anonymous relation");
 268    }
 269  0 if (relationInstance.listParameterValues().size() > 0) {
 270  0 Iterator it = relationInstance.listParameterValues().iterator();
 271  0 while (it.hasNext()) {
 272  0 Value v = (Value) it.next();
 273  0 if (v instanceof Instance &&
 274    ((Instance) v).getIdentifier() instanceof UnnumberedAnonymousID) {
 275  0 addToInstances((Instance) v, relationInstance);
 276    }
 277    }
 278    }
 279    }
 280   
 281    /*
 282    * see addToConcepts(Concept concept, Entity entity)
 283    */
 284  0 private void addToConcepts(Concept concept) {
 285  0 addToConcepts(concept, concept);
 286    }
 287   
 288    /*
 289    * This method adds a given concept to the concepts map. If
 290    * the concept is already contained in the map, the number of
 291    * references to it is increased and a ValidationError is added.
 292    *
 293    * param concept, concept to be added to the concepts map
 294    * param entity, entity to be added to the ValidationError
 295    */
 296  0 private void addToConcepts(Concept concept, Entity entity) {
 297  0 if (concepts.containsKey(concept)) {
 298  0 concepts.put(concept, new Integer(concepts.get(concept).intValue() + 1));
 299  0 addError(entity, ValidationError.ANON_ID_ERR + ":\n" +
 300    ERROR_ANON_IDS);
 301    }
 302    else {
 303  0 concepts.put(concept, new Integer(1));
 304    }
 305    }
 306   
 307    /*
 308    * see addToInstances(Instance instance, Entity entity)
 309    */
 310  0 private void addToInstances(Instance instance) {
 311  0 addToInstances(instance, instance);
 312    }
 313   
 314    /*
 315    * This method adds a given instance to the instances map. If
 316    * the instance is already contained in the map, the number of
 317    * references to it is increased and a ValidationError is added.
 318    *
 319    * param instance, instance to be added to the instances map
 320    * param entity, entity to be added to the ValidationError
 321    */
 322  0 private void addToInstances(Instance instance, Entity entity) {
 323  0 if (instances.containsKey(instance)) {
 324  0 instances.put(instance, new Integer(instances.get(instance).intValue() + 1));
 325  0 addError(entity, ValidationError.ANON_ID_ERR + ":\n" +
 326    ERROR_ANON_IDS);
 327    }
 328    else {
 329  0 instances.put(instance, new Integer(1));
 330    }
 331    }
 332   
 333    /*
 334    * see addToRelations(Relation relation)
 335    */
 336  0 private void addToRelations(Relation relation) {
 337  0 addToRelations(relation, relation);
 338    }
 339   
 340    /*
 341    * This method adds a given relation to the relations map. If
 342    * the relation is already contained in the map, the number of
 343    * references to it is increased and a ValidationError is added.
 344    *
 345    * param relation, relation to be added to the relations map
 346    * param entity, entity to be added to the ValidationError
 347    */
 348  0 private void addToRelations(Relation relation, Entity entity) {
 349  0 if (relations.containsKey(relation)) {
 350  0 relations.put(relation, new Integer(relations.get(relation).intValue() + 1));
 351  0 addError(entity, ValidationError.ANON_ID_ERR + ":\n" +
 352    ERROR_ANON_IDS);
 353    }
 354    else {
 355  0 relations.put(relation, new Integer(1));
 356    }
 357    }
 358   
 359    /**
 360    * This method takes the ontology and checks all its contained
 361    * elements for an erronous use of unnumbered anonymous identifiers.
 362    */
 363  0 public void checkAnonIds(Ontology ontology) {
 364  0 Iterator i = ontology.listConcepts().iterator();
 365  0 while (i.hasNext()) {
 366  0 Concept concept = (Concept) i.next();
 367  0 visitConcept(concept);
 368    }
 369  0 i = ontology.listInstances().iterator();
 370  0 while (i.hasNext()) {
 371  0 Instance instance = (Instance) i.next();
 372  0 visitInstance(instance);
 373    }
 374   
 375  0 i = ontology.listRelations().iterator();
 376  0 while (i.hasNext()) {
 377  0 Relation relation = (Relation) i.next();
 378  0 visitRelation(relation);
 379    }
 380   
 381  0 i = ontology.listRelationInstances().iterator();
 382  0 while (i.hasNext()) {
 383  0 RelationInstance relationInstance = (RelationInstance) i.next();
 384  0 visitRelationInstance(relationInstance);
 385    }
 386    }
 387   
 388  0 private void addError(Entity entity, String reason) {
 389  0 errors.add(new ValidationErrorImpl(entity, reason, WSML.WSML_FULL));
 390    }
 391   
 392    }
 393    /*
 394    * $Log$
 395    * Revision 1.5 2007/04/02 12:13:20 morcen
 396    * Generics support added to wsmo-api, wsmo4j and wsmo-test
 397    *
 398    * Revision 1.4 2006/04/26 13:11:49 nathaliest
 399    * fixed a bug at instances anonId check
 400    *
 401    * Revision 1.3 2006/03/28 08:16:36 nathaliest
 402    * added documentation
 403    *
 404    * Revision 1.2 2006/03/28 07:56:16 nathaliest
 405    * fix
 406    *
 407    * Revision 1.1 2006/03/27 16:35:01 nathaliest
 408    * changed anonId check
 409    *
 410    *
 411    */