Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 236   Methods: 5
NCLOC: 115   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ParsingAndSerializingFile.java 0% 0% 0% 0%
coverage
 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  0 public ParsingAndSerializingFile() {
 91   
 92    /* create parameters for the parser */
 93  0 HashMap <String, Object> createParams = new HashMap <String, Object> ();
 94  0 createParams.put(Factory.PROVIDER_CLASS, "org.deri.wsmo4j.factory.LogicalExpressionFactoryImpl");
 95  0 leFactory = Factory.createLogicalExpressionFactory(createParams);
 96  0 factory = Factory.createWsmoFactory(null);
 97  0 createParams = new HashMap <String, Object> ();
 98  0 createParams.put(Factory.WSMO_FACTORY, factory);
 99  0 createParams.put(Factory.LE_FACTORY, leFactory);
 100   
 101    /* create parser */
 102  0 parser = Factory.createParser(createParams);
 103   
 104    /* create serializer */
 105  0 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  0 parseFile();
 112  0 try {
 113  0 addLogExp();
 114    }
 115    catch (Exception e) {}
 116  0 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  0 public void parseFile() {
 124    /* open file, read and parse it */
 125  0 try {
 126  0 FileReader fReader = new FileReader(file);
 127    /* assuming first topentity in file is an ontology */
 128  0 ontology = (Ontology)parser.parse(fReader)[0];
 129    }
 130    catch (FileNotFoundException ef) {
 131  0 ef.printStackTrace();
 132    }
 133    catch (IOException eio) {
 134  0 eio.printStackTrace();
 135    }
 136    catch (ParserException pe) {
 137  0 pe.printStackTrace();
 138    }
 139    catch (InvalidModelException ime) {
 140  0 ime.printStackTrace();
 141    }
 142   
 143    /* get number of concepts, axioms and instances from the ontology */
 144  0 int x = ontology.listConcepts().size();
 145  0 System.out.println("Number of concepts: " + x);
 146  0 int y = ontology.listAxioms().size();
 147  0 System.out.println("Number of axioms: " + y);
 148  0 int i = ontology.listInstances().size();
 149  0 System.out.println("Number of instances: " + i);
 150   
 151    /* search for a specified axiom by indicating the default namespace as identifier */
 152  0 Axiom a = ontology.findAxiom(
 153    factory.createIRI("http://www.example.org/example/validDistance"));
 154  0 if (a != null) {
 155  0 System.out.println("axiom found " + a.getIdentifier());
 156  0 a.getOntology();
 157    }
 158   
 159    /* search for a specified namespace by indicating the searched-for prefix */
 160  0 Namespace n = ontology.findNamespace("dc");
 161  0 if (n != null) {
 162  0 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  0 public void addLogExp()
 171    throws ParserException {
 172    /* create Molecules (simple logical expressions) */
 173  0 try {
 174  0 Molecule m1 = leFactory.createMemberShipMolecule(
 175    leFactory.createVariable("x"),
 176    factory.createIRI("http://www.example.org/example/animal"));
 177  0 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  0 Binary b1 = leFactory.createInverseImplication(m1, m2);
 183   
 184    /* Alternative way of creating the same logical expression from a String */
 185  0 Binary b2 = (Binary)leFactory.createLogicalExpression(
 186    "?x memberOf animal impliedBy ?x memberOf human", ontology);
 187   
 188    /* Check if both Binary Versions are equal */
 189  0 if (b1.equals(b2)) {
 190  0 System.out.println(b1 + " is equal to " + b2);
 191    }
 192    else {
 193  0 System.out.println(b1 + " is unequal to " + b2);
 194    }
 195   
 196    /* creating an axiom and add it to the ontology */
 197  0 Axiom a = factory.createAxiom(factory.createIRI(
 198    "http://www.example.org/example/animalImpliedByHuman"));
 199   
 200  0 a.addDefinition(b1);
 201    /* add axiom */
 202  0 ontology.addAxiom(a);
 203    }
 204    catch (InvalidModelException ime) {
 205  0 ime.getStackTrace();
 206    }
 207   
 208    /* get number of axioms */
 209  0 int y = ontology.listAxioms().size();
 210  0 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  0 public void serializeToFile() {
 218  0 StringBuffer str = new StringBuffer();
 219  0 TopEntity[] tops = {ontology};
 220  0 serializer.serialize(tops, str);
 221  0 try {
 222  0 BufferedWriter out = new BufferedWriter(
 223    new FileWriter(fileOut));
 224  0 out.write(str.toString());
 225  0 out.close();
 226    }
 227    catch (IOException ioe) {
 228  0 ioe.getStackTrace();
 229    }
 230    }
 231   
 232  0 public static void main(String[] args) {
 233  0 new ParsingAndSerializingFile();
 234    }
 235   
 236    }