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