View Javadoc

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  
17  package org.deri.wsmo4j.examples;
18  
19  
20  import java.io.*;
21  import java.util.*;
22  
23  import org.omwg.logicalexpression.*;
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  /**
32   * <h2>Parsing and serializing a file</h2>
33   * <p>Description: Example - How to parse, create and serialize logical expressions</p>
34   * <h4>How to parse logical expressions from a file</h4>
35   * <p>
36   * A Parser is created using the <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">
37   * Factory Pattern</a> (org.wsmo.factory.Factory). In order to use the logical
38   * expression extension a LogicalExpressionFactory has to be created and passed as parameter
39   * when creating the parser.
40   * </p>
41   * <p>
42   * If we don't pass specific parameters, the parser is created
43   * using wsmo4j default factories. The Logical Expressions factory used by default
44   * handles logical expressions as plain strings, instead of objects
45   * (variables, molecules, operators,...).
46   * </p>
47   * <p>
48   * Now we we use the parser to parse the wsml file. As
49   * result we obtain an array of TopEntities (Ontologies, Goals, Mediators or Web
50   * Services). In our test case we can cast the first TopEntity to an ontology, which can be searched for specified
51   * concepts, axioms, instances, namespaces,...
52   * </p>
53   * <h4>How to create logical expressions</h4>
54   * <p>
55   * To create logical expressions, one needs to use the LogicalExpressionsFactory. This one
56   * allows to build logical expressions in two different ways. Either from a string
57   * (ex: ?x memberOf _"http://example.org/animal"), or by building it up directly
58   * using different objects like variables, IRIs, molecules, etc. No matter how an expression is
59   * created it will be equal.
60   * </p>
61   * <h4>How to serialize logical expressions</h4>
62   * <p>
63   * First we need to create a serializer. This one is, analogous to the parser, created by the
64   * Factory, with as parameter simply "null".
65   * The TopEntity, consisting of the ontology and the ontology items is serialized to a string.
66   * This one can be written into a wsml file.
67   * </p>
68   * <p>
69   * With the toString(), it is also possible to convert a logical expression
70   * directly to a string, without using the serializer.
71   * </p>
72   * @author Nathalie.Steinmetz@deri.org
73   */
74  public class ParsingAndSerializingFile {
75  
76      private String file = "wsmo4j/org/deri/wsmo4j/examples/locations.wsml";
77  
78      private String fileOut = "wsmo4j/org/deri/wsmo4j/examples/locations2.wsml";
79  
80      protected Parser parser;
81  
82      protected Serializer serializer;
83  
84      protected WsmoFactory factory;
85  
86      protected LogicalExpressionFactory leFactory;
87  
88      protected Ontology ontology;
89  
90      public ParsingAndSerializingFile() {
91  
92          /* create parameters for the parser */
93          HashMap <String, Object> createParams = new HashMap <String, Object> ();
94          createParams.put(Factory.PROVIDER_CLASS, "org.deri.wsmo4j.factory.LogicalExpressionFactoryImpl");
95          leFactory = Factory.createLogicalExpressionFactory(createParams);
96          factory = Factory.createWsmoFactory(null);
97          createParams = new HashMap <String, Object> ();
98          createParams.put(Factory.WSMO_FACTORY, factory);
99          createParams.put(Factory.LE_FACTORY, leFactory);
100 
101         /* create parser */
102         parser = Factory.createParser(createParams);
103 
104         /* create serializer */
105         serializer = Factory.createSerializer(null);
106 
107         /*
108                  * parse the existing wsml-file, add and remove some logical expressions and then serialize the ontology into a new
109          * file.
110          */
111         parseFile();
112         try {
113             addLogExp();
114         }
115         catch (Exception e) {}
116         serializeToFile();
117     }
118 
119     /**
120      * Read a wsml-file and parse it. See how many concepts, axioms and instances are in the
121      * resulting ontology. Search for a specific axiom and for a specific namespace.
122      */
123     public void parseFile() {
124         /* open file, read and parse it */
125         try {
126             FileReader fReader = new FileReader(file);
127             /* assuming first topentity in file is an ontology */
128             ontology = (Ontology)parser.parse(fReader)[0];
129         }
130         catch (FileNotFoundException ef) {
131             ef.printStackTrace();
132         }
133         catch (IOException eio) {
134             eio.printStackTrace();
135         }
136         catch (ParserException pe) {
137             pe.printStackTrace();
138         }
139         catch (InvalidModelException ime) {
140             ime.printStackTrace();
141         }
142 
143         /* get number of concepts, axioms and instances from the ontology */
144         int x = ontology.listConcepts().size();
145         System.out.println("Number of concepts: " + x);
146         int y = ontology.listAxioms().size();
147         System.out.println("Number of axioms: " + y);
148         int i = ontology.listInstances().size();
149         System.out.println("Number of instances: " + i);
150 
151         /* search for a specified axiom by indicating the default namespace as identifier */
152         Axiom a = ontology.findAxiom(
153                 factory.createIRI("http://www.example.org/example/validDistance"));
154         if (a != null) {
155             System.out.println("axiom found " + a.getIdentifier());
156             a.getOntology();
157         }
158 
159         /* search for a specified namespace by indicating the searched-for prefix */
160         Namespace n = ontology.findNamespace("dc");
161         if (n != null) {
162             System.out.println("found Namespace with prefix \"dc\": " + n.getIRI().toString());
163         }
164     }
165 
166     /**
167      * A simple axiom is created and added to the ontology. The axiom is defined by a binary
168      * logical expression. The LogicalExpressionFactory is used to build the axiom.
169      */
170     public void addLogExp()
171             throws ParserException {
172         /* create Molecules (simple logical expressions) */
173         try {
174             Molecule m1 = leFactory.createMemberShipMolecule(
175                     leFactory.createVariable("x"),
176                     factory.createIRI("http://www.example.org/example/animal"));
177             Molecule m2 = leFactory.createMemberShipMolecule(
178                     leFactory.createVariable("x"),
179                     factory.createIRI("http://www.example.org/example/human"));
180 
181             /* create Binaries (binary logical expressions) */
182             Binary b1 = leFactory.createInverseImplication(m1, m2);
183 
184             /* Alternative way of creating the same logical expression from a String */
185             Binary b2 = (Binary)leFactory.createLogicalExpression(
186                     "?x memberOf animal impliedBy ?x memberOf human", ontology);
187 
188             /* Check if both Binary Versions are equal */
189             if (b1.equals(b2)) {
190                 System.out.println(b1 + " is equal to " + b2);
191             }
192             else {
193                 System.out.println(b1 + " is unequal to " + b2);
194             }
195 
196             /* creating an axiom and add it to the ontology */
197             Axiom a = factory.createAxiom(factory.createIRI(
198                     "http://www.example.org/example/animalImpliedByHuman"));
199 
200             a.addDefinition(b1);
201             /* add axiom */
202             ontology.addAxiom(a);
203         }
204         catch (InvalidModelException ime) {
205             ime.getStackTrace();
206         }
207 
208         /* get number of axioms */
209         int y = ontology.listAxioms().size();
210         System.out.println("Number of axioms: " + y);
211     }
212 
213     /**
214      * The ontology with the added axiom is serialized and written into the file
215      * "SerializedLocations.wsml"
216      */
217     public void serializeToFile() {
218         StringBuffer str = new StringBuffer();
219         TopEntity[] tops = {ontology};
220         serializer.serialize(tops, str);
221         try {
222             BufferedWriter out = new BufferedWriter(
223                     new FileWriter(fileOut));
224             out.write(str.toString());
225             out.close();
226         }
227         catch (IOException ioe) {
228             ioe.getStackTrace();
229         }
230     }
231 
232     public static void main(String[] args) {
233         new ParsingAndSerializingFile();
234     }
235 
236 }