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.File;
22 import java.io.FileNotFoundException;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Set;
29
30 import org.omwg.ontology.Instance;
31 import org.omwg.ontology.Ontology;
32 import org.wsmo.common.Entity;
33 import org.wsmo.common.Identifier;
34 import org.wsmo.common.TopEntity;
35 import org.wsmo.common.exception.InvalidModelException;
36 import org.wsmo.common.exception.SynchronisationException;
37 import org.wsmo.factory.Factory;
38 import org.wsmo.factory.LogicalExpressionFactory;
39 import org.wsmo.factory.WsmoFactory;
40 import org.wsmo.locator.Locator;
41 import org.wsmo.locator.LocatorManager;
42 import org.wsmo.wsml.Parser;
43 import org.wsmo.wsml.ParserException;
44
45 /**
46 * <p>
47 * Title:
48 * </p>
49 * <p>
50 * Description:
51 * </p>
52 * <p>
53 * Copyright: Copyright (c) 2004-2005
54 * </p>
55 * <p>
56 * Company: Ontotext Lab., Sirma AI
57 * </p>
58 *
59 * @author not attributable
60 * @version 1.0
61 */
62
63 public class LocatorExample implements Locator {
64 public final static String FILE = "File";
65
66 private TopEntity[] wsmlEntities = new TopEntity[0];
67
68 /**
69 * Constructor of InstanceLocator.
70 *
71 * @param map
72 * map containing the directory to be used
73 * @throws InvalidModelException
74 * @throws ParserException
75 * @throws IOException
76 * @throws FileNotFoundException
77 */
78 public LocatorExample(Map<String, File> map) throws FileNotFoundException, IOException, ParserException, InvalidModelException {
79 if (map == null || map.get(FILE) == null) {
80 throw new IllegalArgumentException();
81 }
82
83 File wsmlFile = map.get(FILE);
84
85 // set up the wsmo factory and parser
86 HashMap<String, Object> props = new HashMap<String, Object>();
87 WsmoFactory factory = Factory.createWsmoFactory(null);
88 LogicalExpressionFactory leFactory = Factory.createLogicalExpressionFactory(null);
89 props.put(Factory.WSMO_FACTORY, factory);
90 props.put(Factory.LE_FACTORY, leFactory);
91 Parser parser = Factory.createParser(props);
92
93
94 if (wsmlFile.exists()) {
95 wsmlEntities = parser.parse(new FileReader(wsmlFile));
96 }
97 }
98
99 /**
100 * Method to retrieve Entity based on Identifier; It's supposed that the
101 * Entity is located in a file with name corresponding to it's namespace.
102 *
103 * @param id
104 * the id to search for
105 * @return instance or null if the identifier was not found in the directory
106 */
107 public Entity lookup(Identifier id, Class clazz) throws SynchronisationException {
108 return findEntity(wsmlEntities, id);
109 }
110
111 /**
112 * Method to retrieve Entity based on Identifier; It's supposed that the
113 * Entity is located in a file with name corresponding to it's namespace.
114 *
115 * @param id
116 * the id to search for
117 * @return instance or null if the identifier was not found in the directory
118 */
119 public Set<Entity> lookup(Identifier id) throws SynchronisationException {
120 throw new UnsupportedOperationException();
121 }
122
123 /**
124 * Scan all top entities for instances
125 *
126 * @param entities
127 * the entities to scan
128 * @param id
129 * the identifier of an instance to search for
130 * @return instance with the same identifier or null
131 */
132 private Entity findEntity(TopEntity[] entities, Identifier id) {
133 for (int i = 0; i < entities.length; i++) {
134 if (entities[i] instanceof Ontology) {
135 Ontology ontology = (Ontology) entities[i];
136 for (Iterator j = ontology.listInstances().iterator(); j.hasNext();) {
137 if (((Instance) j.next()).getIdentifier().equals(id)) {
138 return (Instance) j.next();
139 }
140 }
141 }
142 }
143
144 // instance was not found
145 return null;
146 }
147
148 /**
149 * Runs the example
150 * @param args[0] - path to a wsml file, args[1] an identifier IRI of and instance
151 */
152 public static void main(String[] args) {
153 if (args.length == 2) {
154 test(new File(args[0]), args[1]);
155 } else {
156 test(new File("testWsml.wsml"), "http://testIRI#test");
157 }
158
159 }
160
161 public static void test(File wsmlFile, String lacateIRI) {
162 HashMap<String, Object> locatorInit = new HashMap<String, Object>();
163 locatorInit.put(Factory.PROVIDER_CLASS, "com.ontotext.wsmo4j.examples.LocatorExample");
164 locatorInit.put(LocatorExample.FILE, wsmlFile);
165
166 // create Locator
167 Locator locator = LocatorManager.createLocator(locatorInit);
168
169
170 // register the locator to the locator manager
171 Factory.getLocatorManager().addLocator(locator);
172
173 // create wsmo factory
174 WsmoFactory factory = Factory.createWsmoFactory(null);
175
176 // get the Instance Test from Ontology file locatorExample
177 Instance instance = factory.getInstance(factory.createIRI(lacateIRI));
178
179 try {
180 // ensure that we have the implementation but not a dummy proxy by calling
181 // any method except getIdentifier() f.e. getOntology()
182 System.out.println("Instance is part from ontology: " + instance.getOntology().getIdentifier());
183 } catch (SynchronisationException e) {
184 throw new RuntimeException("The locator did not succed to find the isntance!");
185 }
186 }
187 }
188
189 /*
190 * $Log$
191 * Revision 1.10 2007/06/07 08:24:45 lcekov
192 * fix examples http://jira.sirma.bg/jira/browse/WSMO4J-20
193 * Revision 1.9 2007/04/02 12:13:27 morcen
194 * Generics support added to wsmo-api, wsmo4j and wsmo-test
195 *
196 * Revision 1.8 2006/01/11 13:02:41 marin_dimitrov common constants moved to
197 * Factory
198 *
199 * Revision 1.7 2005/10/18 09:13:34 marin_dimitrov lookup() returns a Set (was
200 * Entity[])
201 *
202 * Revision 1.6 2005/10/18 08:58:30 vassil_momtchev example updated
203 *
204 * Revision 1.5 2005/09/20 19:41:01 holgerlausen removed superflouis interfaces
205 * for IO in logical expression (since intgeration not necessary)
206 *
207 * Revision 1.4 2005/09/19 10:18:45 vassil_momtchev header and log added
208 *
209 */