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 test.wsmo4j.parser.rdf;
17  
18  import java.io.*;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.deri.wsmo4j.io.parser.rdf.RDFParserError;
25  import org.deri.wsmo4j.io.parser.rdf.RDFParserImpl;
26  import org.deri.wsmo4j.io.parser.rdf.RDFParserWarning;
27  import org.omwg.ontology.*;
28  import org.openrdf.model.Statement;
29  import org.wsmo.common.TopEntity;
30  import org.wsmo.common.WSML;
31  import org.wsmo.factory.Factory;
32  import org.wsmo.wsml.Parser;
33  import org.wsmo.wsml.Serializer;
34  
35  import test.wsmo4j.Wsmo4jTestCase;
36  import static test.wsmo4j.Utils.*;
37  /**
38  * Test method for RDF parsing.
39  *
40  * <pre>
41  *  Created on May 02, 2006
42  *  Committed by $Author: nathaliest $
43  *  $Source$
44  * </pre>
45  *
46  * @author nathalie.steinmetz@deri.org
47  * @version $Revision: 2643 $ $Date: 2008-08-08 01:32:53 +0300 (Fri, 08 Aug 2008) $
48  */
49  public class RDFParserTest extends Wsmo4jTestCase {
50  
51      protected Parser parser;
52      
53      protected Serializer serializer;
54      
55      private List warnings;
56      
57      private List errors;
58      
59      protected void setUp() throws Exception {
60          super.setUp();
61          HashMap <String, Object> properties = new HashMap <String, Object> ();
62          properties.put(Factory.PROVIDER_CLASS, "org.deri.wsmo4j.io.parser.rdf.RDFParserImpl");
63          properties.put(Factory.WSMO_FACTORY, factory);
64          properties.put(Factory.LE_FACTORY, leFactory);
65          properties.put(Factory.DATA_FACTORY, dataFactory);
66          properties.put(RDFParserImpl.RDF_PROPERTIES, RDFParserImpl.RDF_PROP_AS_ATTR);
67          parser = Factory.createParser(properties);
68          
69          properties = new HashMap <String, Object> ();
70          properties.put(Factory.PROVIDER_CLASS,
71                  "com.ontotext.wsmo4j.serializer.wsml.WSMLSerializerImpl");
72          serializer = Factory.createSerializer(properties);
73      }
74  
75      protected void tearDown() throws Exception {
76          super.tearDown();
77          parser = null;
78          serializer = null;
79          System.gc(); // tries to reinitialize all singletons in WSMO!
80      }
81      
82      private void printWarnings() {
83          int index = 0;
84          Iterator it = warnings.iterator();
85          while (it.hasNext()) {
86              RDFParserWarning w  = (RDFParserWarning) it.next();
87              System.out.println("Warning " + (index+1) + ": " + w);
88              index = index+1;
89              printTriples(w.getTriples());
90          }
91      }
92      
93      private void printErrors() {
94          int index = 0;
95          Iterator it = errors.iterator();
96          while (it.hasNext()) {
97              RDFParserError e  = (RDFParserError) it.next();
98              System.out.println("Error " + (index+1) + ": " + e);
99              index = index+1;
100         }
101     }
102     
103     private void printTriples(Set triples) {
104         int index = 0;
105         Iterator it = triples.iterator();
106         while (it.hasNext()) {
107            Statement statement = (Statement) it.next();     
108            System.out.println(statement.getSubject().toString());
109            System.out.println(statement.getPredicate().toString());
110            System.out.println(statement.getObject().toString());
111            System.out.println("------------------------------------");
112            index = index+1;   
113         }
114     }
115       
116     public void testCorrectWSMLVariant() throws Exception {
117     	InputStreamReader reader = new InputStreamReader(getStream("test/wsmo4j/parser/rdf/test.rdf"));
118         TopEntity[] entities = parser.parse(reader);    
119         assertEquals(WSML.WSML_CORE, ((Ontology) entities[0]).getWsmlVariant());
120         
121         reader = new InputStreamReader(getStream("test/wsmo4j/parser/rdf/wood_class.rdf"));
122         entities = parser.parse(reader);    
123         assertEquals(WSML.WSML_FLIGHT, ((Ontology) entities[0]).getWsmlVariant());
124         
125 //        Serializer serializer = Factory.createSerializer(null);
126 //        StringBuffer buf = new StringBuffer();
127 //        serializer.serialize(entities, buf);
128 //        System.out.println(buf);
129     }
130     
131     public void testRDFSFileParsing() throws Exception {
132         InputStreamReader reader = new InputStreamReader(getStream("test/wsmo4j/parser/rdf/test.rdfs"));
133         TopEntity[] entities = parser.parse(reader);
134         
135         Serializer serializer = Factory.createSerializer(null);
136         StringBuffer buf = new StringBuffer();
137         serializer.serialize(entities, buf);
138         System.out.println(buf);
139         
140         warnings = parser.getWarnings();
141         errors = parser.getErrors();
142         assertTrue(entities.length == 1);
143         
144         Ontology onto = (Ontology)entities[0];
145         assertTrue(onto.listConcepts().size() == 4);
146         assertTrue(onto.listInstances().size() == 0);
147         assertTrue(onto.listAxioms().size() == 2);
148         
149         Concept testB = onto.findConcept(factory.createIRI("http://test.example.org/wsmo4j#TestB"));
150         assertNotNull(testB);
151         assertTrue(testB.listSuperConcepts().size() == 1);
152         assertTrue(testB.listAttributes().size() == 2);
153         
154     }
155     
156     public void testRDFFileParsing() throws Exception {
157         InputStreamReader reader = new InputStreamReader(getStream("test/wsmo4j/parser/rdf/test.rdf"));
158         TopEntity[] entities = parser.parse(reader);
159         assertTrue(entities.length == 1);
160         
161         Ontology onto = (Ontology)entities[0];
162         assertTrue(onto.listInstances().size() == 3);
163         
164         Instance instanceA = onto.findInstance(
165                 factory.createIRI("http://test.example.org/wsmo4j#instanceA"));
166         assertNotNull(instanceA);
167         assertTrue(instanceA.listAttributeValues().size() == 2);
168         assertTrue(instanceA.listAttributeValues(
169                 factory.createIRI("http://test.example.org/wsmo4j#propertyB")).size() == 2);
170         assertTrue(instanceA.listConcepts().size() == 1);
171         
172         Value val = (Value)instanceA.listAttributeValues(
173                 factory.createIRI("http://test.example.org/wsmo4j#propertyA"))
174                     .iterator().next();
175         
176         assertTrue(val instanceof Instance);
177         
178         Instance instanceB = onto.findInstance(
179                 factory.createIRI("http://test.example.org/wsmo4j#instanceB"));
180         assertNotNull(instanceB);
181         assertTrue(instanceB.listAttributeValues().size() == 3);
182         assertTrue(instanceB.listAttributeValues(
183                 factory.createIRI("http://test.example.org/wsmo4j#propertyA")).size() == 1);
184         assertTrue(instanceB.listConcepts().size() == 2);
185 
186     }
187 }
188 /*
189  * $Log$
190  * Revision 1.10  2007/04/02 12:13:23  morcen
191  * Generics support added to wsmo-api, wsmo4j and wsmo-test
192  *
193  * Revision 1.9  2007/01/11 14:13:24  nathaliest
194  * added test for metamodelling check
195  *
196  * Revision 1.8  2006/11/30 13:51:24  nathaliest
197  * removed test file
198  *
199  * Revision 1.7  2006/11/27 11:20:19  nathaliest
200  * *** empty log message ***
201  *
202  * Revision 1.6  2006/11/17 16:43:33  ohamano
203  * *** empty log message ***
204  *
205  * Revision 1.5  2006/11/16 14:33:46  ohamano
206  * *** empty log message ***
207  *
208  * Revision 1.4  2006/11/16 13:26:29  ohamano
209  * *** empty log message ***
210  *
211  * Revision 1.3  2006/11/16 09:53:28  nathaliest
212  * added RDFParserError
213  *
214  * Revision 1.2  2006/11/10 11:08:54  nathaliest
215  * added getWarnings() and getErrors() methods to Parser interface, implemented them in the rdf parser implementation and added UnsupportedOperationException to the other parser implementations
216  *
217  * Revision 1.1  2006/05/03 13:32:49  nathaliest
218  * adding RDF parser
219  */