Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 198   Methods: 6
NCLOC: 119   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LocatorExample.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    package org.deri.wsmo4j.examples;
 17   
 18    import java.io.InputStream;
 19    import java.io.InputStreamReader;
 20    import java.util.HashMap;
 21    import java.util.HashSet;
 22    import java.util.Iterator;
 23    import java.util.List;
 24    import java.util.Vector;
 25   
 26    import org.omwg.ontology.Concept;
 27    import org.omwg.ontology.Ontology;
 28    import org.wsmo.common.Identifier;
 29    import org.wsmo.common.exception.SynchronisationException;
 30    import org.wsmo.factory.Factory;
 31    import org.wsmo.locator.Locator;
 32    import org.wsmo.locator.LocatorManager;
 33    import org.wsmo.validator.ValidationError;
 34    import org.wsmo.validator.ValidationWarning;
 35    import org.wsmo.validator.WsmlValidator;
 36    import org.wsmo.wsml.Parser;
 37   
 38    /**
 39    * This class is meant to be used as example for how to use the default locator
 40    * implementation. This is shown in combination with the use of the validator,
 41    * that, by default, is enabled to also validate imported ontologies. This
 42    * feature can nevertheless be disabled.
 43    *
 44    * <pre>
 45    * Created on Nov 22, 2006
 46    * Committed by $Author: nathaliest $
 47    * $Source$,
 48    * </pre>
 49    *
 50    * @author nathalie.steinmetz@deri.org
 51    * @version $Revision: 2073 $ $Date: 2007-06-08 10:33:42 +0300 (Fri, 08 Jun 2007) $
 52    */
 53    public class LocatorExample {
 54   
 55    private Parser parser = null;
 56   
 57    private Locator locator = null;
 58   
 59    private WsmlValidator validator = null;
 60   
 61    public HashSet <Identifier> visitedOntologies = new HashSet <Identifier> ();
 62   
 63    private String phys_ex8 = this.getClass().getClassLoader().getResource(
 64    "org/deri/wsmo4j/examples/valid_wsml_rule_test.wsml").toString();
 65   
 66    private String phys_ex3 = this.getClass().getClassLoader().getResource(
 67    "org/deri/wsmo4j/examples/invalid_wsml_flight_test.wsml").toString();
 68   
 69    private List<ValidationError> errors = new Vector<ValidationError>();
 70   
 71    private List<ValidationWarning> warnings = new Vector<ValidationWarning>();
 72   
 73  0 private void doTestRun() throws Exception {
 74    // read test file and parse it
 75  0 InputStream is = this.getClass().getClassLoader().getResourceAsStream(
 76    "test/wsmo4j/validator/importsOntology.wsml");
 77    // assuming first topentity in file is an ontology
 78  0 Ontology ontology = (Ontology)parser.parse(new InputStreamReader(is))[0];
 79  0 visitedOntologies.add(ontology.getIdentifier());
 80   
 81    // set Locator
 82  0 Factory.getLocatorManager().addLocator(locator);
 83   
 84    // Creating the WSML Validator and disable feature to validate imported files
 85  0 HashMap <String, Object> prefs = new HashMap <String, Object> ();
 86  0 prefs.put(WsmlValidator.VALIDATE_IMPORTS, new Boolean(false));
 87  0 prefs.put(Factory.WSMO_FACTORY, Factory.createWsmoFactory(null));
 88  0 prefs.put(Factory.LE_FACTORY, Factory.createLogicalExpressionFactory(null));
 89  0 prefs.put(Parser.CACHE_LOGICALEXPRESSION_STRING, "true");
 90  0 validator = Factory.createWsmlValidator(prefs);
 91  0 prefs.clear();
 92  0 validator.isValid(ontology, errors, warnings);
 93  0 System.out.println("\n\nErrors and warnings without validation of imported files:\n");
 94  0 printError(errors);
 95  0 printWarning(warnings);
 96  0 errors.clear();
 97  0 warnings.clear();
 98   
 99    // check imported ontologies
 100  0 importOntologies(ontology);
 101   
 102    // Creating the WSML Validator with enabling the feature to validate imported files
 103  0 prefs.put(WsmlValidator.VALIDATE_IMPORTS, new Boolean(true));
 104  0 prefs.put(Factory.WSMO_FACTORY, Factory.createWsmoFactory(null));
 105  0 prefs.put(Factory.LE_FACTORY, Factory.createLogicalExpressionFactory(null));
 106  0 prefs.put(Parser.CACHE_LOGICALEXPRESSION_STRING, "true");
 107  0 validator = Factory.createWsmlValidator(prefs);
 108  0 validator.isValid(ontology, errors, warnings);
 109  0 System.out.println("\n\nErrors and warnings with validation of imported files:\n");
 110  0 printError(errors);
 111  0 printWarning(warnings);
 112  0 Factory.getLocatorManager().removeLocator(locator);
 113    }
 114   
 115  0 private void importOntologies(Ontology ont) {
 116  0 System.out.println("Number of ontologies: " + ont.listOntologies().size());
 117  0 for (Iterator it = ont.listOntologies().iterator(); it.hasNext();){
 118  0 Ontology ontology = (Ontology) it.next();
 119  0 if (!visitedOntologies.contains(ontology.getIdentifier())){
 120  0 visitedOntologies.add(ontology.getIdentifier());
 121  0 try {
 122  0 ontology.getWsmlVariant();
 123  0 System.out.println("Loaded imported ontology: " + ontology.getIdentifier());
 124  0 System.out.println("Number of concepts: " + ontology.listConcepts().size());
 125  0 Iterator iterator = ontology.listConcepts().iterator();
 126  0 while(iterator.hasNext()) {
 127  0 Concept concept = (Concept) iterator.next();
 128  0 System.out.println(" concept: " + concept.getIdentifier());
 129    }
 130  0 if (ontology.listOntologies().size() > 0) {
 131  0 for(Iterator ite = ontology.listOntologies().iterator(); ite.hasNext();) {
 132  0 Ontology o = (Ontology) ite.next();
 133  0 importOntologies(o);
 134    }
 135    }
 136    } catch (SynchronisationException e) {
 137  0 System.out.println("Could not load ontology " + ontology.getIdentifier());
 138    }
 139    }
 140    }
 141    }
 142   
 143  0 private void setup() {
 144    // This map contains a mapping from locigal URIs to physical locations.
 145  0 HashMap <String, String> mapping = new HashMap <String, String> ();
 146  0 mapping.put("http://www.example.org/ontologies/example8", phys_ex8);
 147  0 mapping.put("http://www.example.org/ontologies/example3", phys_ex3);
 148   
 149    // This map contains preferences for the creation of the wsml parser,
 150    // the locator and the wsml validator
 151  0 HashMap <String, Object> prefs = new HashMap <String, Object> ();
 152   
 153    // Creating a WSML Parser
 154  0 prefs.put(Factory.WSMO_FACTORY, Factory.createWsmoFactory(null));
 155  0 prefs.put(Factory.LE_FACTORY, Factory.createLogicalExpressionFactory(null));
 156  0 parser = Factory.createParser(prefs);
 157  0 prefs.clear();
 158   
 159    // Creating the Locator
 160  0 prefs.put(Factory.PROVIDER_CLASS, "org.deri.wsmo4j.locator.LocatorImpl");
 161  0 prefs.put(Locator.URI_MAPPING, mapping);
 162  0 prefs.put(Factory.WSMO_PARSER, parser);
 163  0 locator = LocatorManager.createLocator(prefs);
 164  0 prefs.clear();
 165    }
 166   
 167  0 private void printError(java.util.List l){
 168  0 for (int i=0; i<l.size(); i++) {
 169  0 System.out.println("\nError " + (i+1) + ":\n" + l.get(i) + "\n\n --------------------------------");
 170    }
 171    }
 172   
 173  0 private void printWarning(List l) {
 174  0 for (int i=0; i<l.size(); i++) {
 175  0 System.out.println("\nWarning " + (i+1) + ":\n" + l.get(i) + "\n\n -------------------------------");
 176    }
 177    }
 178   
 179  0 public static void main(String[] args) throws Exception {
 180  0 LocatorExample ex = new LocatorExample();
 181  0 ex.setup();
 182  0 ex.doTestRun();
 183    }
 184   
 185    }
 186    /*
 187    * $Log$
 188    * Revision 1.3 2007/06/08 07:33:42 nathaliest
 189    * changed example to load files from package
 190    *
 191    * Revision 1.2 2007/04/02 12:13:22 morcen
 192    * Generics support added to wsmo-api, wsmo4j and wsmo-test
 193    *
 194    * Revision 1.1 2006/11/27 11:36:55 nathaliest
 195    * added an example to show how to use the locator to import ontologies and the validator to either validate imported ontologies or not
 196    *
 197    *
 198    */