1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package olr.rdf;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 import olr.rdf.util.SortedStringList;
28
29
30 public abstract class AbstractStatementPool implements StatementPool
31 {
32
33 public AbstractStatementPool()
34 {
35 namespaceURIs = new HashMap();
36 resources = new HashMap();
37 }
38
39 public boolean isParsedNamespaceURI(String namespaceURI)
40 {
41 return namespaceURIs.get(namespaceURI) != null;
42 }
43
44 public void addParsedNamespaceURI(String namespaceURI)
45 {
46 addParsedNamespaceURI(namespaceURI, namespaceURI);
47 }
48
49 public void addParsedNamespaceURI(String namespaceURI, String title)
50 {
51 if(!namespaceURI.endsWith("#") && !namespaceURI.endsWith("/"))
52 namespaceURI = String.valueOf(namespaceURI) + String.valueOf('#');
53 namespaceURIs.put(namespaceURI, title);
54 }
55
56 public void removeParsedNamespaceURI(String namespaceURI)
57 {
58 namespaceURIs.remove(namespaceURI);
59 }
60
61 private String getParsedNamespaceURItitle(String namespaceURI)
62 {
63 Object title = namespaceURIs.get(namespaceURI);
64 if(title != null)
65 return title.toString();
66 else
67 return null;
68 }
69
70 public void addStatement(Statement statement)
71 {
72 addAttributeAbout(statement.getSubject(), statement);
73 }
74
75 public void addAttributeAbout(String subject, Attribute attribute)
76 {
77 subject = Tools.correctURI(subject);
78 String predicate = attribute.getPredicate();
79 String object = attribute.getObject();
80 Resource resource = (Resource)resources.get(subject);
81 if(resource == null)
82 {
83 resource = new Resource(subject);
84 resources.put(subject, resource);
85 }
86 resource.addAttribute(attribute);
87 if(Definitions.isSubClassOf(predicate))
88 addAttributeAbout(object, new Attribute(Definitions.getSubClassPredicate(), subject, attribute.getID()));
89 else
90 if(Definitions.isDomain(predicate))
91 addAttributeAbout(object, new Attribute(Definitions.getDomainForPredicate(), subject, attribute.getID()));
92 else
93 if(Definitions.isType(predicate))
94 addAttributeAbout(object, new Attribute(Definitions.getInstancePredicate(), subject, attribute.getID()));
95 }
96
97 public void removeStatement(Statement statement)
98 {
99 removeAttributeAbout(statement.getSubject(), statement);
100 }
101
102 public void removeAttributeAbout(String subject, Attribute attribute)
103 {
104 String predicate = attribute.getPredicate();
105 String object = attribute.getObject();
106 Resource resource = getResource(subject);
107 if(resource != null)
108 {
109 resource.removeAttribute(attribute);
110 if(resource.isEmpty())
111 resources.remove(subject);
112 }
113 if(Definitions.isSubClassOf(predicate))
114 removeAttributeAbout(object, new Attribute(Definitions.getSubClassPredicate(), subject, attribute.getID()));
115 else
116 if(Definitions.isDomain(predicate))
117 removeAttributeAbout(object, new Attribute(Definitions.getDomainForPredicate(), subject, attribute.getID()));
118 else
119 if(Definitions.isType(predicate))
120 removeAttributeAbout(object, new Attribute(Definitions.getInstancePredicate(), subject, attribute.getID()));
121 }
122
123 public List getAttributesAbout(String subject)
124 {
125 Resource resource = getResource(subject);
126 if(resource != null)
127 return resource.getAttributes();
128 else
129 return new ArrayList();
130 }
131
132 public List getSpecificAttributesAbout(String subject, String predicate)
133 {
134 Resource resource = getResource(subject);
135 if(resource != null)
136 return resource.getAttributes(predicate);
137 else
138 return new ArrayList();
139 }
140
141 public Resource getResource(String subject)
142 {
143 return (Resource)resources.get(subject);
144 }
145
146 public boolean hasAttribute(String subject, String predicate, String object)
147 {
148 Resource resource = getResource(subject);
149 return resource.hasAttribute(predicate, object);
150 }
151
152 public boolean isClass(String resource)
153 {
154 return isClass(getResource(resource));
155 }
156
157 public static boolean isClass(Resource resource)
158 {
159 return resource != null ? Definitions.isClass(resource.getType()) : false;
160 }
161
162 public boolean isProperty(String resource)
163 {
164 return isProperty(getResource(resource));
165 }
166
167 public boolean isProperty(Resource resource)
168 {
169 return resource != null ? isInstanceOf(resource, Definitions.RDF_PROPERTY) : false;
170 }
171
172 public String getResourceTitle(String resource)
173 {
174 try
175 {
176 if(Tools.isURI(resource))
177 {
178 String ns = Tools.getNS(resource);
179 if(ns.equals(Definitions.HOME_NS))
180 {
181 Resource resourceObj = getResource(resource);
182 if(resourceObj != null)
183 {
184 String s = resourceObj.getTitle();
185 return s;
186 }
187 } else
188 {
189 String title = getParsedNamespaceURItitle(ns);
190 if(title != null)
191 {
192 String s1 = String.valueOf(String.valueOf((new StringBuffer(String.valueOf(String.valueOf(title)))).append(":").append(Tools.getName(resource))));
193 return s1;
194 }
195 }
196 }
197 }
198 catch(Exception exception) { }
199 return resource;
200 }
201
202 public String getResourceTitle(Resource resource)
203 {
204 try
205 {
206 String ns = resource.getNS();
207 if(ns.equals(Definitions.HOME_NS))
208 {
209 String s = resource.getTitle();
210 return s;
211 }
212 String title = getParsedNamespaceURItitle(ns);
213 if(title != null)
214 {
215 String s1 = String.valueOf(String.valueOf((new StringBuffer(String.valueOf(String.valueOf(title)))).append(":").append(resource.getName())));
216 return s1;
217 }
218 }
219 catch(Exception exception) { }
220 return resource.getURI();
221 }
222
223 public boolean isSubClassOf(String class1, String class2)
224 {
225 label0:
226 {
227 if(class1.equals(class2))
228 return true;
229 Resource resource = getResource(class1);
230 if(!isClass(resource))
231 break label0;
232 Iterator it = resource.getSubClassOf().iterator();
233 do
234 if(!it.hasNext())
235 break label0;
236 while(!isSubClassOf(it.next().toString(), class2));
237 return true;
238 }
239 return false;
240 }
241
242 public boolean isInstanceOf(String resource, String classResource)
243 {
244 return isInstanceOf(getResource(resource), classResource);
245 }
246
247 public boolean isInstanceOf(Resource resource, String classResource)
248 {
249 if(resource != null)
250 {
251 String type = resource.getType();
252 if(type.length() > 0)
253 return isSameType(type, classResource);
254 }
255 return false;
256 }
257
258
259 public boolean isCourse(String uri)
260 {
261 return uri.equals(OLR3Definitions.OLR3_HAS_COURSE);
262 }
263
264 private boolean isSameType(String resourceType, String classResource)
265 {
266 if(resourceType.equals(classResource))
267 return true;
268 Resource classObj = getResource(classResource);
269 if(classObj != null && isClass(classObj))
270 {
271 Iterator it = classObj.getSubClass().iterator();
272 if(it.hasNext())
273 return isSameType(resourceType, it.next().toString());
274 }
275 return false;
276 }
277
278 public String createInstanceOf(String namespace, String classResource)
279 {
280 String resource = Tools.getURI(namespace, "".concat(String.valueOf(getInstanceId())));
281 Attribute attribute = new Attribute(Definitions.RDF_TYPE, classResource);
282 addAttributeAbout(resource, attribute);
283 return resource;
284 }
285
286 public List getAllInstances(String subject)
287 {
288 ArrayList instances = new ArrayList();
289 getAllInstances(subject, instances);
290 return instances;
291 }
292
293 private void getAllInstances(String subject, ArrayList list)
294 {
295 Resource clss = getResource(subject);
296 if(!isClass(clss))
297 return;
298 list.addAll(clss.getInstance());
299 for(Iterator it = clss.getSubClass().iterator(); it.hasNext(); getAllInstances(it.next().toString(), list));
300 }
301
302 public List getAllSubClasses(List classResources)
303 {
304 List subClasses = new SortedStringList();
305 for(Iterator it = classResources.iterator(); it.hasNext(); subClasses.addAll(getAllSubClasses(it.next().toString())));
306 return subClasses;
307 }
308
309 public List getAllSubClasses(String classResource)
310 {
311 List subClasses = new SortedStringList();
312 subClasses.add(classResource);
313 getAllSubClasses(classResource, subClasses);
314 return subClasses;
315 }
316
317 private void getAllSubClasses(String classResource, List subClasses)
318 {
319 final Resource res = getResource(classResource);
320 if(res != null)
321 {
322 final List newSubClasses = res.getSubClass();
323 subClasses.addAll(newSubClasses);
324 for(Iterator it = newSubClasses.iterator(); it.hasNext(); getAllSubClasses(it.next().toString(), subClasses));
325 }
326 }
327
328 public List getAllSuperClasses(String classResource)
329 {
330 List superClasses = new ArrayList();
331 superClasses.add(classResource);
332 getAllSuperClasses(classResource, superClasses);
333 return superClasses;
334 }
335
336 private void getAllSuperClasses(String classResource, List superClasses)
337 {
338 Resource res = getResource(classResource);
339 if(res != null)
340 {
341 List newSuperClasses = res.getSubClassOf();
342 superClasses.addAll(newSuperClasses);
343 for(Iterator it = newSuperClasses.iterator(); it.hasNext(); getAllSuperClasses(it.next().toString(), superClasses));
344 }
345 }
346
347 public List getAllPropertiesForClass(String classResource)
348 {
349 List properties = new ArrayList();
350 for(Iterator it = getAllSuperClasses(classResource).iterator(); it.hasNext(); properties.addAll(getResource(it.next().toString()).getProperty()));
351 return properties;
352 }
353
354 public List getAllDomains(String property)
355 {
356 ArrayList domains = new ArrayList();
357 getAllDomains(property, domains);
358 return domains;
359 }
360
361 private void getAllDomains(String property, List domains)
362 {
363 Resource propertyObj = getResource(property);
364 if(propertyObj != null)
365 {
366 domains.addAll(propertyObj.getDomain());
367 for(Iterator it = propertyObj.getSubPropertyOf().iterator(); it.hasNext(); getAllDomains(it.next().toString(), domains));
368 }
369 }
370
371 public List getAllRanges(String property)
372 {
373 List ranges = new ArrayList();
374 getAllRanges(property, ranges);
375 return ranges;
376 }
377
378 private void getAllRanges(String property, List ranges)
379 {
380 Resource propertyObj = getResource(property);
381 if(propertyObj != null)
382 {
383 ranges.addAll(propertyObj.getRange());
384 for(Iterator it = propertyObj.getSubPropertyOf().iterator(); it.hasNext(); getAllRanges(it.next().toString(), ranges));
385 }
386 }
387
388 public List getAvailableProperties4Resource(String subject)
389 {
390 List properties = new ArrayList();
391 Resource resource = getResource(subject);
392 List domainClasses;
393 if(resource != null)
394 domainClasses = getAllSuperClasses(resource.getType());
395 else
396 domainClasses = new ArrayList();
397 Iterator res = resources.values().iterator();
398 label0:
399 do
400 {
401 if(!res.hasNext())
402 break;
403 Resource item = (Resource)res.next();
404 if(!isProperty(item))
405 continue;
406 List domains = getAllDomains(item.getURI());
407 if(domains.isEmpty())
408 {
409 properties.add(item.toString());
410 continue;
411 }
412 Iterator dom = domains.iterator();
413 do
414 if(!dom.hasNext())
415 continue label0;
416 while(!domainClasses.contains(dom.next().toString()));
417 properties.add(item.toString());
418 } while(true);
419 return properties;
420 }
421
422 protected abstract long getInstanceId();
423
424 private Map namespaceURIs;
425 protected Map resources;
426 }