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  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  	private void doTestRun() throws Exception {
74  		// read test file and parse it 
75          InputStream is = this.getClass().getClassLoader().getResourceAsStream(
76                  "test/wsmo4j/validator/importsOntology.wsml");
77          // assuming first topentity in file is an ontology  
78          Ontology ontology  = (Ontology)parser.parse(new InputStreamReader(is))[0]; 
79      	visitedOntologies.add(ontology.getIdentifier());
80      	
81      	// set Locator
82      	Factory.getLocatorManager().addLocator(locator);
83      	
84          // Creating the WSML Validator and disable feature to validate imported files
85          HashMap <String, Object> prefs = new HashMap <String, Object> ();
86  		prefs.put(WsmlValidator.VALIDATE_IMPORTS, new Boolean(false));
87  		prefs.put(Factory.WSMO_FACTORY, Factory.createWsmoFactory(null));
88  		prefs.put(Factory.LE_FACTORY, Factory.createLogicalExpressionFactory(null));
89  		prefs.put(Parser.CACHE_LOGICALEXPRESSION_STRING, "true");
90  		validator = Factory.createWsmlValidator(prefs);
91  		prefs.clear();
92          validator.isValid(ontology, errors, warnings);
93          System.out.println("\n\nErrors and warnings without validation of imported files:\n");
94          printError(errors);
95          printWarning(warnings);
96          errors.clear();
97          warnings.clear();
98          
99          // check imported ontologies
100     	importOntologies(ontology);
101         
102         // Creating the WSML Validator with enabling the feature to validate imported files
103         prefs.put(WsmlValidator.VALIDATE_IMPORTS, new Boolean(true));
104 		prefs.put(Factory.WSMO_FACTORY, Factory.createWsmoFactory(null));
105 		prefs.put(Factory.LE_FACTORY, Factory.createLogicalExpressionFactory(null));
106 		prefs.put(Parser.CACHE_LOGICALEXPRESSION_STRING, "true");
107 		validator = Factory.createWsmlValidator(prefs);
108         validator.isValid(ontology, errors, warnings);
109         System.out.println("\n\nErrors and warnings with validation of imported files:\n");
110         printError(errors);
111         printWarning(warnings);      
112     	Factory.getLocatorManager().removeLocator(locator);
113 	}
114 	
115 	private void importOntologies(Ontology ont) {
116 		System.out.println("Number of ontologies: " + ont.listOntologies().size());
117     	for (Iterator it = ont.listOntologies().iterator(); it.hasNext();){
118     		Ontology ontology = (Ontology) it.next();
119     		if (!visitedOntologies.contains(ontology.getIdentifier())){
120     			visitedOntologies.add(ontology.getIdentifier());
121     			try {
122     				ontology.getWsmlVariant();
123 					System.out.println("Loaded imported ontology: " + ontology.getIdentifier());
124 					System.out.println("Number of concepts: " + ontology.listConcepts().size());
125 					Iterator iterator = ontology.listConcepts().iterator();
126 					while(iterator.hasNext()) {
127 			        	Concept concept = (Concept) iterator.next();
128 			        	System.out.println("   concept: " + concept.getIdentifier());
129 			        }
130 					if (ontology.listOntologies().size() > 0) {
131 	    				for(Iterator ite = ontology.listOntologies().iterator(); ite.hasNext();) {
132 	    					Ontology o = (Ontology) ite.next();
133 	    					importOntologies(o);
134 	    				}
135 	    			}
136     			} catch (SynchronisationException e) {
137     				System.out.println("Could not load ontology " + ontology.getIdentifier());
138     			}
139     		}
140     	}
141 	}
142 	
143 	private void setup() {
144 		// This map contains a mapping from locigal URIs to physical locations. 
145         HashMap <String, String> mapping = new HashMap <String, String> ();
146         mapping.put("http://www.example.org/ontologies/example8", phys_ex8);
147         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         HashMap <String, Object> prefs = new HashMap <String, Object> ();
152         
153         // Creating a WSML Parser
154         prefs.put(Factory.WSMO_FACTORY, Factory.createWsmoFactory(null));
155     	prefs.put(Factory.LE_FACTORY, Factory.createLogicalExpressionFactory(null));
156     	parser = Factory.createParser(prefs);
157     	prefs.clear();
158     	
159     	// Creating the Locator
160     	prefs.put(Factory.PROVIDER_CLASS, "org.deri.wsmo4j.locator.LocatorImpl");
161     	prefs.put(Locator.URI_MAPPING, mapping);
162     	prefs.put(Factory.WSMO_PARSER, parser);		
163     	locator = LocatorManager.createLocator(prefs); 	
164 		prefs.clear();
165 	}
166 	
167     private void printError(java.util.List l){
168         for (int i=0; i<l.size(); i++) {
169             System.out.println("\nError " + (i+1) + ":\n" + l.get(i) + "\n\n --------------------------------");
170         }
171     }
172     
173     private void printWarning(List l) {
174         for (int i=0; i<l.size(); i++) {
175             System.out.println("\nWarning " + (i+1) + ":\n" + l.get(i) + "\n\n -------------------------------");
176         }
177     }
178 	
179 	public static void main(String[] args) throws Exception {
180 		LocatorExample ex = new LocatorExample();
181 		ex.setup();
182 		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  */