1   /*
2    * OntologyResourceImpl.java
3    *
4    * Copyright (c) 2005, The University of Sheffield.
5    *
6    * This file is part of GATE (see http://gate.ac.uk/), and is free
7    * software, licenced under the GNU Library General Public License,
8    * Version 2, June1991.
9    *
10   * A copy of this licence is included in the distribution in the file
11   * licence.html, and is also available at http://gate.ac.uk/gate/licence.html.
12   *
13   * Valentin Tablan 15-Sep-2005
14   *
15   *
16   *  $Id$
17   */
18  package gate.creole.ontology;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Set;
24  
25  /**
26   * This is an implementation for ontology resource. It provides implementations
27   * for the methods on the {@link gate.creole.ontology.OntologyResource}
28   * interface and is intended to be used as a base class for other classes
29   * implementing that interface or its sub-interfaces.
30   */
31  public class OntologyResourceImpl implements OntologyResource {
32    protected String uri;
33    protected String comment;
34    protected String name;
35    protected Taxonomy taxonomy;
36    protected Ontology ontology;
37    protected HashMap instanceProperties;
38  
39    public OntologyResourceImpl(String uri, String name, String comment,
40            Taxonomy taxonomy) {
41      this.comment = comment;
42      this.name = name;
43      this.taxonomy = taxonomy;
44      this.ontology = taxonomy instanceof Ontology ? (Ontology)taxonomy : null;
45      this.uri = uri;
46      this.instanceProperties = new HashMap();
47    }
48  
49    /**
50     * Constructor variant using the name as the local URI.
51     * 
52     * @param name
53     * @param comment
54     * @param ontology
55     */
56    public OntologyResourceImpl(String name, String comment, Taxonomy taxonomy) {
57      this(name, name, comment, taxonomy);
58    }
59  
60    public boolean addPropertyValue(String propertyName, Object theValue) {
61      // this means that we look for a property with the same name
62      // in the class. If such cannot be found, i.e. the propSet is
63      // is empty, then we just return without adding the value
64      Property prop = ((Ontology)ontology)
65              .getPropertyDefinitionByName(propertyName);
66      if(prop == null) return false;
67      if(prop.isValidDomain(this)) {
68        List values = (List)instanceProperties.get(propertyName);
69        if(values == null) {
70          values = new ArrayList();
71          instanceProperties.put(propertyName, values);
72        }
73        values.add(theValue);
74        return true;
75      } else return false;
76    }
77  
78    public Set getSetPropertiesNames() {
79      return instanceProperties.keySet();
80    }
81  
82    public List getPropertyValues(String propertyName) {
83      return (List)instanceProperties.get(propertyName);
84    }
85  
86    public boolean removePropertyValue(String propertyName, Object theValue) {
87      List values = (List)instanceProperties.get(propertyName);
88      if(values != null) {
89        return values.remove(theValue);
90      } else return false;
91    }
92  
93    public void removePropertyValues(String propertyName) {
94      instanceProperties.remove(propertyName);
95    }
96  
97    public Object getPropertyValue(String propertyName) {
98      if(instanceProperties == null || instanceProperties.isEmpty()) return null;
99      return instanceProperties.get(propertyName);
100   }
101 
102   /**
103    * @return Returns the comment.
104    */
105   public String getComment() {
106     return comment;
107   }
108 
109   /**
110    * @param comment
111    *          The comment to set.
112    */
113   public void setComment(String comment) {
114     this.comment = comment;
115   }
116 
117   /**
118    * @return Returns the name.
119    */
120   public String getName() {
121     return name;
122   }
123 
124   /**
125    * @param name
126    *          The name to set.
127    */
128   public void setName(String name) {
129     this.name = name;
130   }
131 
132   /**
133    * @return Returns the taxonomy.
134    */
135   public Taxonomy getTaxonomy() {
136     return taxonomy;
137   }
138 
139   /**
140    * @return Returns the ontology.
141    */
142   public Ontology getOntology() {
143     return ontology;
144   }
145 
146   /**
147    * @param ontology
148    *          The ontology to set.
149    */
150   public void setOntology(Taxonomy ontology) {
151     this.taxonomy = ontology;
152   }
153 
154   /**
155    * @return Returns the URI.
156    */
157   public String getURI() {
158     return uri;
159   }
160 
161   /**
162    * @param uri
163    *          The URI to set.
164    */
165   public void setURI(String uri) {
166     this.uri = uri;
167   }
168 }
169