1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
70
71
72
73
74
75
76
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
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
101
102
103
104
105
106
107 public Entity lookup(Identifier id, Class clazz) throws SynchronisationException {
108 return findEntity(wsmlEntities, id);
109 }
110
111
112
113
114
115
116
117
118
119 public Set<Entity> lookup(Identifier id) throws SynchronisationException {
120 throw new UnsupportedOperationException();
121 }
122
123
124
125
126
127
128
129
130
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
145 return null;
146 }
147
148
149
150
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
167 Locator locator = LocatorManager.createLocator(locatorInit);
168
169
170
171 Factory.getLocatorManager().addLocator(locator);
172
173
174 WsmoFactory factory = Factory.createWsmoFactory(null);
175
176
177 Instance instance = factory.getInstance(factory.createIRI(lacateIRI));
178
179 try {
180
181
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209