1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  package test.wsmo4j.ontology;
19  
20  import java.util.Set;
21  
22  import org.omwg.ontology.*;
23  import org.wsmo.common.IRI;
24  import org.wsmo.common.exception.*;
25  
26  import test.wsmo4j.Wsmo4jTestCase;
27  
28  /**
29   * <p>Title: WSMO4J</p>
30   * <p>Description: WSMO API and a Reference Implementation</p>
31   * <p>Copyright:  Copyright (c) 2004-2005</p>
32   * <p>Company: OntoText Lab. / SIRMA </p>
33   * @author not attributable
34   * @version 1.0
35   */
36  public class ConceptTest extends Wsmo4jTestCase {
37  
38      private Concept concept;
39  
40      private Concept anotherConcept;
41  
42      private IRI attrIRI;
43  
44      private IRI instanceIRI;
45  
46      private IRI anotherInstanceIRI;
47  
48      private String ns = "http://example.org/" + System.currentTimeMillis() + "#";
49  
50      public void setUp() throws Exception {
51          super.setUp();
52          concept = factory.createConcept(createIRI(ns + "concept"));
53          anotherConcept = factory.createConcept(createIRI(ns + "anotherConcept"));
54          attrIRI = createIRI(ns + "attribute");
55          instanceIRI = createIRI(ns + "instance1");
56          anotherInstanceIRI = createIRI(ns + "instance2");
57      }
58  
59      /**
60       * Test the concept inheritance addSubConcept / addSuperConcept / 
61       * removeSubConcept / removeSuperConcept / listSubConcepts.
62       * @throws Exception
63       */
64      public void testConceptInheritance() throws Exception {
65          concept.addSubConcept(anotherConcept);
66          assertEquals("Concepts are not inherted correctly!", concept.listSubConcepts().size(), 1);
67          assertTrue("Concepts are not inherted correctly!", concept.listSubConcepts().contains(
68                  anotherConcept));
69  
70          concept.removeSubConcept(anotherConcept);
71          assertEquals("Concepts are not inherted correctly!", concept.listSubConcepts().size(), 0);
72  
73          // test the same functions with the "reverse" methods
74          anotherConcept.addSuperConcept(concept);
75          assertEquals("Concepts are not inherted correctly!", anotherConcept.listSuperConcepts()
76                  .size(), 1);
77          assertTrue("Concepts are not inherted correctly!", 
78                  concept.listSubConcepts().contains(anotherConcept));
79  
80          anotherConcept.removeSuperConcept(concept);
81          assertEquals("Concepts are not inherted correctly!", anotherConcept.listSuperConcepts()
82                  .size(), 0);
83  
84          try {
85              concept.addSubConcept(anotherConcept);
86              anotherConcept.addSubConcept(concept);
87              fail("Cycles in the concept inheritance hierarchy allowed!");
88          }
89          catch (InvalidModelException e) {
90              // restore back the concepts to the previous state
91              concept.removeSubConcept(anotherConcept);
92          }
93  
94          try {
95  
96              concept.addSuperConcept(anotherConcept);
97              anotherConcept.addSuperConcept(concept);
98              fail("Cycles in the concept inheritance hierarchy allowed!");
99          }
100         catch (InvalidModelException e) {
101             // restore back the concepts to the previous state
102             concept.removeSuperConcept(anotherConcept);
103         }
104     }
105 
106     /**
107      * Test the createAttribute / addType / removeAttribute + all attribute properties.
108      * @throws Exception
109      */
110     public void testAttributes() throws Exception {
111         Attribute attribute = concept.createAttribute(attrIRI);
112         //check default values
113         assertEquals(attribute.getMaxCardinality(), Integer.MAX_VALUE);
114         assertEquals(attribute.getMinCardinality(), 0);
115         attribute.addType(dataFactory.createWsmlDataType(WsmlDataType.WSML_DATE));
116         //check default values
117         assertEquals(attribute.getMaxCardinality(), Integer.MAX_VALUE);
118         assertEquals(attribute.getMinCardinality(), 0);
119         assertEquals("Attribute is not added correctly", 
120                 concept.findAttributes(attrIRI).iterator().next(), 
121                 attribute);
122         assertEquals("Attribute count is not correct", 
123                 concept.listAttributes().size(), 
124                 1);
125 
126         concept.removeAttribute(attribute);
127         assertEquals("Attribute count is not correct", 
128                 concept.listAttributes().size(), 
129                 0);
130         assertEquals("Attribute has Invalid reference to concept!", 
131                 null,attribute.getConcept());
132    
133         //add it again
134         attribute = concept.createAttribute(attribute.getIdentifier());
135         attribute.addType(concept);
136         assertEquals("Attribute is not added correctly", 
137                 concept, 
138                 attribute.getConcept());
139 
140         // inverseOf
141         attribute.setInverseOf(createIRI(ns + "inverseAttr"));
142         assertEquals("InverseOf attribute is not set correctly", attribute.getInverseOf(),
143                 createIRI(ns + "inverseAttr"));
144 
145         assertFalse("Attribute default properties are incorrect", attribute.isConstraining());
146         attribute.setConstraining(true);
147         assertTrue("Attribute properties are not set correctly", attribute.isConstraining());
148 
149         attribute.setMaxCardinality(3);
150         assertTrue("Attribute properties are not set correctly", attribute.getMaxCardinality() == 3);
151         attribute.setMinCardinality(2);
152         assertTrue("Attribute properties are not set correctly", attribute.getMinCardinality() == 2);
153 
154         assertFalse("Attribute default properties are incorrect", attribute.isReflexive());
155         attribute.setReflexive(true);
156         assertTrue("Attribute properties are not set correctly", attribute.isReflexive());
157 
158         assertFalse("Attribute default properties are incorrect", attribute.isSymmetric());
159         attribute.setSymmetric(true);
160         assertTrue("Attribute properties are not set correctly", attribute.isSymmetric());
161 
162         assertFalse("Attribute default properties are incorrect", attribute.isTransitive());
163         attribute.setTransitive(true);
164         assertTrue("Attribute properties are not set correctly", attribute.isTransitive());
165     }
166 
167     /**
168      * Test the correctness of the 3 possible ways to create instance.
169      * @throws Exception
170      */
171     public void testInstance() throws Exception {
172         Instance instance1 = factory.createInstance(instanceIRI);
173         instance1.addConcept(concept);
174         assertEquals("Instance is not added to concept", instance1.listConcepts().size(), 1);
175         assertTrue("Instance is not added to concept", instance1.listConcepts().contains(concept));
176 
177         // test the other way of creating instances
178         Instance instance2 = factory.createInstance(anotherInstanceIRI, concept);
179         assertEquals("Instance is not added to concept", instance2.listConcepts().size(), 1);
180         assertTrue("Instance is not added to concept", instance2.listConcepts().contains(concept));
181 
182         assertEquals("Instance is not added to concept", concept.listInstances().size(), 2);
183 
184         // test third way of assigning instance to concept
185         anotherConcept.addInstance(instance1);
186         assertEquals("Size of concept is not incremented", instance1.listConcepts().size(), 2);
187         assertTrue("Instance is not added to concept", instance1.listConcepts().contains(
188                 anotherConcept));
189         assertTrue("Instance is not added to concept", anotherConcept.listInstances().contains(
190                 instance1));
191     }
192 
193     /**
194      * Test addAttributeValue / removeAttributeValue / listAttributeValue.
195      * @throws Exception
196      */
197     public void testAttributeValues() throws Exception {
198         Instance instance = factory.getInstance(instanceIRI);
199         Instance instance2 = factory.createInstance(createIRI(ns + "attributeVal2"));
200         Instance instance3 = factory.createInstance(createIRI(ns + "attributeVal3"));
201 
202         factory.createConcept(createIRI(ns + "concept"));
203 
204         // attributeValue is added to Map<attribute, set of values>
205         instance.addAttributeValue(attrIRI, instance2);
206         assertEquals("Attribute values size is not incremented", instance.listAttributeValues()
207                 .size(), 1);
208         assertTrue("Attribute values are not added", ((Set) instance.listAttributeValues().get(
209                 attrIRI)).contains(instance2));
210 
211         instance.addAttributeValue(attrIRI, instance3);
212         assertEquals("Attribute values size is incorrectly incremented", instance
213                 .listAttributeValues().size(), 1);
214         assertEquals("Attribute values is not added to set", ((Set) instance.listAttributeValues()
215                 .get(attrIRI)).size(), 2);
216 
217         instance.removeAttributeValue(attrIRI, instance3);
218         assertEquals("Attribute values size is incorrectly decremented", instance
219                 .listAttributeValues().size(), 1);
220         assertFalse("Attribute values are not removed", ((Set) instance.listAttributeValues().get(
221                 attrIRI)).contains(instance3));
222 
223         instance.addAttributeValue(attrIRI, instance3);
224         instance.removeAttributeValues(attrIRI);
225         assertEquals("Attribute values size is not decremented", instance.listAttributeValues()
226                 .size(), 0);
227     }
228 }
229 
230 /*
231  * $Log$
232  * Revision 1.12  2006/08/07 10:51:21  vassil_momtchev
233  * test fixed
234  *
235  * Revision 1.11  2006/08/04 13:31:27  holgerlausen
236  * the reverse does not make sense here, need to recreate it.
237  *
238  * Revision 1.10  2006/08/04 13:27:21  holgerlausen
239  * bug in attribute imple?
240  *
241  * Revision 1.9  2006/02/16 10:21:18  vassil_momtchev
242  * setInverseOf(Attribute) changed to setInverseOf(Identifier); setInverseOf is not longer symmetric - some tests removed
243  *
244  * Revision 1.8  2006/02/15 11:49:59  holgerlausen
245  * added checks for default behavior regarding cardinality
246  *
247  * Revision 1.7  2006/02/10 15:08:34  vassil_momtchev
248  * test extended;
249  *
250  * Revision 1.6  2005/11/29 16:47:04  holgerlausen
251  * generated "random" ids, where intefearing with other tests
252  *
253  * Revision 1.5  2005/11/04 06:14:26  holgerlausen
254  * fixed unit tests relying on relative iris
255  *
256  * Revision 1.4  2005/09/26 13:05:14  vassil_momtchev
257  * comment that testAttributes() will fail removed
258  *
259  * Revision 1.3  2005/09/15 15:00:59  vassil_momtchev
260  * added test to try cycles in the concept hierarchy
261  *
262  * Revision 1.2  2005/09/13 11:29:09  vassil_momtchev
263  * mark that testAttributes() will fail
264  *
265  * Revision 1.1  2005/09/12 14:28:03  vassil_momtchev
266  * unit test of Concept, Instance, ConceptAttribute
267  *
268  */