View Javadoc

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  
19  package com.ontotext.wsmo4j.examples;
20  
21  import java.io.*;
22  import java.util.*;
23  
24  import org.omwg.ontology.*;
25  import org.wsmo.common.*;
26  import org.wsmo.common.exception.*;
27  import org.wsmo.factory.*;
28  import org.wsmo.wsml.*;
29  
30  /**
31   * <p>Title: </p>
32   * <p>Description: </p>
33   * <p>Copyright: Copyright (c) 2004-2005</p>
34   * <p>Company: Ontotext Lab., Sirma AI</p>
35   * @author not attributable
36   * @version 1.0
37   */
38  
39  public class LoadOntologyExample {
40  
41      private WsmoFactory factory;
42  
43      private LogicalExpressionFactory leFactory;
44  
45      private DataFactory dataFactory;
46  
47      private Parser parser;
48  
49      private Serializer serializer;
50  
51      public static void showExample() throws InvalidModelException {
52          LoadOntologyExample example = new LoadOntologyExample();
53          example.createFactoryParserSerializer();
54          example.serializeParseOntology();
55      }
56  
57      private void createFactoryParserSerializer() {
58          HashMap <String, Object> props = new HashMap <String, Object>();
59  
60          // use default implementation for factory
61          factory = Factory.createWsmoFactory(null);
62          leFactory = Factory.createLogicalExpressionFactory(null);
63          dataFactory = Factory.createDataFactory(null);
64  
65          props.put(Factory.WSMO_FACTORY, factory);
66          props.put(Factory.LE_FACTORY, leFactory);
67          props.put(Factory.DATA_FACTORY, dataFactory);
68  
69          parser = Factory.createParser(props);
70          serializer = Factory.createSerializer(null);
71      }
72  
73      private void serializeParseOntology() throws InvalidModelException {
74  
75          // create ontology to be serialzed
76          Ontology ontology = CreateOntologyExample.showExample();
77  
78          try {
79              // tries to serialize ontology with all its elements to a text
80              StringWriter writer = new StringWriter();
81              serializer.serialize(new TopEntity[] { ontology }, writer);
82              System.out.println(writer.getBuffer().toString());
83  
84              // tries to parse it back to the memmory
85              TopEntity[] entities = parser.parse(new StringReader(writer.getBuffer().toString()));
86              if (entities.length != 1) {
87                  throw new RuntimeException("The number of parsed entities is not 1!");
88              }
89          }
90          catch (IOException ex) {
91              // should never happen using StringWriter and StringReader objects;
92          }
93          catch (ParserException ex) {
94              System.out.println("Invalid WSML token encountered at line " + ex.getErrorLine()
95                      + " position " + ex.getErrorPos());
96          }
97      }
98      
99      public static void main(String[] args) throws InvalidModelException {
100         showExample();
101     }
102 }
103 
104 /*
105  * $Log$
106  * Revision 1.4  2007/06/07 08:24:45  lcekov
107  * fix examples http://jira.sirma.bg/jira/browse/WSMO4J-20
108  *
109  * Revision 1.3  2007/04/02 12:13:27  morcen
110  * Generics support added to wsmo-api, wsmo4j and wsmo-test
111  *
112  * Revision 1.2  2006/01/11 13:02:41  marin_dimitrov
113  * common constants moved to Factory
114  *
115  * Revision 1.1  2005/09/19 10:20:22  vassil_momtchev
116  * inherite OntologyExample
117  *
118 */