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 org.omwg.ontology.Concept;
21  import org.omwg.ontology.Instance;
22  import org.omwg.ontology.Parameter;
23  import org.omwg.ontology.Relation;
24  import org.omwg.ontology.RelationInstance;
25  import org.wsmo.common.exception.*;
26  
27  import test.wsmo4j.Wsmo4jTestCase;
28  
29  /**
30   * <p>Title: WSMO4J</p>
31   * <p>Description: WSMO API and a Reference Implementation</p>
32   * <p>Copyright:  Copyright (c) 2004-2005</p>
33   * <p>Company: OntoText Lab. / SIRMA </p>
34   * @author not attributable
35   * @version 1.0
36   */
37  public class RelationTest extends Wsmo4jTestCase {
38      
39      private Relation relation;
40      private Relation anotherRelation;
41      private Parameter param;
42      private Concept paramType;
43      private String ns = "http://example.org/";
44      
45      public void setUp() throws Exception {
46          super.setUp();
47          relation = factory.createRelation(createIRI(ns+"relaion1"+System.currentTimeMillis()));
48          anotherRelation = factory.createRelation(createIRI(ns+"relaion2"+System.currentTimeMillis()));
49          paramType = factory.createConcept(createIRI(ns+"paramConcept"+System.currentTimeMillis()));
50      }
51      
52      /**
53       * Test addSubRelation / addSuperRelation methods.
54       * @throws Exception
55       */
56      public void testRelationInheritance() throws Exception {
57          relation.addSubRelation(anotherRelation);
58          assertEquals("relations are not inherted correctly!", 
59                  relation.listSubRelations().size(), 1);
60          assertTrue("relations are not inherted correctly!", 
61                  relation.listSubRelations().contains(anotherRelation));
62          
63          relation.removeSubRelation(anotherRelation);
64          assertEquals("relations are not inherted correctly!", 
65                  relation.listSubRelations().size(), 0);
66          
67          // test the same functions with the "reverse" methods
68          anotherRelation.addSuperRelation(relation);
69          assertEquals("relations are not inherted correctly!", 
70                  anotherRelation.listSuperRelations().size(), 1);
71          assertTrue("relations are not inherted correctly!", 
72                  relation.listSubRelations().contains(anotherRelation));
73          
74          anotherRelation.removeSuperRelation(relation);
75          assertEquals("relations are not inherted correctly!", 
76                  anotherRelation.listSuperRelations().size(), 0);
77          
78          try {
79              relation.addSubRelation(anotherRelation);
80              anotherRelation.addSubRelation(relation);
81              fail("Cycles in the concept inheritance hierarchy allowed!");
82          }
83          catch (InvalidModelException e){
84              // restore back the concepts to the previous state
85              relation.removeSubRelation(anotherRelation);
86          }
87          
88          try {
89  
90              relation.addSuperRelation(anotherRelation);
91              anotherRelation.addSuperRelation(relation);
92              fail("Cycles in the concept inheritance hierarchy allowed!");
93          }
94          catch (InvalidModelException e){
95              // restore back the concepts to the previous state
96              relation.removeSuperRelation(anotherRelation);
97          }
98      }
99      
100     /**
101      * Test the handling of Parameters.
102      * @throws Exception
103      */
104     public void testRelationParameters() throws Exception {
105         param = relation.createParameter((byte) 0);        
106         assertEquals("Parameter counter is not incremented",
107                 relation.listParameters().size(), 1);
108         assertTrue("Parameters are not added correctly",
109                 relation.listParameters().contains(param));
110         
111         param.addType(paramType);
112         assertEquals("Parameter type is not added", 
113                 param.listTypes().size(), 1);
114         assertTrue("Parameter type is not added", 
115                 param.listTypes().contains(paramType));
116         
117         param.removeType(paramType);
118         assertEquals("Parameter type is not added", 
119                 param.listTypes().size(), 0);
120         assertFalse("Parameter type is not added", 
121                 param.listTypes().contains(paramType));
122         
123         relation.removeParameter((byte) 0);
124         assertEquals("Parameter counter is not incremented",
125                 relation.listParameters().size(), 0);
126         
127         param = relation.createParameter((byte) 0);
128         param.addType(paramType);
129         assertFalse("setConstraining is not false (defaut)", param.isConstraining());
130         param.setConstraining(true);
131         assertTrue("setConstraining is not settable", param.isConstraining());
132     }
133     
134     /**
135      * Test the handling of RelationInstance.
136      * @throws Exception
137      */
138     public void testRelationInstance() throws Exception {
139         relation.createParameter((byte)0);
140         RelationInstance relInst1 = factory.createRelationInstance(createIRI(ns+"relInst1"), relation);
141         relInst1.setRelation(relation);
142         assertTrue("Relation of relationInstance is incorrect", 
143                 relation.listRelationInstances().contains(relInst1));
144         assertEquals("Relation of relationInstance is incorrect", 
145                 relInst1.getRelation(), relation);
146         
147         RelationInstance relInst2 = factory.createRelationInstance(
148                 createIRI(ns+"relInst1"), relation);
149         assertTrue("Relation of relationInstance is incorrect", 
150                 relation.listRelationInstances().contains(relInst2));
151         assertEquals("Relation of relationInstance is incorrect", 
152                 relInst2.getRelation(), relation);
153                 
154         Instance instance = factory.createInstance(createIRI(ns+"paramValue"));
155         relInst1.setParameterValue((byte) 0, instance);
156         assertEquals("Counter is not incremented", relInst1.listParameterValues().size(), 1);
157         assertTrue("Value is not set", relInst1.listParameterValues().contains(instance));
158         assertEquals("Parameter value is not correctly returned", 
159                 relInst1.getParameterValue((byte) 0), instance);
160     }
161     
162     
163     public void testRealtionWithArityOnly () throws Exception{
164         Parameter param1 = relation.createParameter((byte)0);
165         Parameter param2 = relation.createParameter((byte)1);
166         assertEquals(relation.listParameters().size(),2);
167         assertEquals(param1.listTypes().size(),0);
168         assertEquals(param2.listTypes().size(),0);
169         //relation.g
170         //param1.
171         //assertEquals()
172     }
173 }
174 
175 /*
176  * $Log$
177  * Revision 1.13  2005/11/29 15:25:56  holgerlausen
178  * removed dependency on order of execution of test methods
179  *
180  * Revision 1.12  2005/11/04 06:14:26  holgerlausen
181  * fixed unit tests relying on relative iris
182  *
183  * Revision 1.11  2005/09/15 15:00:59  vassil_momtchev
184  * added test to try cycles in the concept hierarchy
185  *
186  * Revision 1.10  2005/09/13 10:25:20  holgerlausen
187  * updated tests for relation with arity only
188  *
189  * Revision 1.9  2005/09/13 09:01:12  vassil_momtchev
190  * use WsmoFactory.createRelationInstance(Identifier, Relation) instead of WsmoFactory.createRelationInstance(Identifier)  RelationInstance.setRelation(Relation)
191  *
192  * Revision 1.8  2005/09/12 14:30:13  vassil_momtchev
193  * extend Wsmo4jTestCase; new tests added
194  *
195  */