rtm-javatmapi 0.3.1-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. data/DISCLAIMER +15 -0
  2. data/LICENSE +201 -0
  3. data/README +4 -0
  4. data/lib/rtm-javatmapi.rb +4 -0
  5. data/lib/rtm/javatmapi.rb +229 -0
  6. data/lib/rtm/javatmapi/aliases.rb +43 -0
  7. data/lib/rtm/javatmapi/core.rb +16 -0
  8. data/lib/rtm/javatmapi/core/association.rb +71 -0
  9. data/lib/rtm/javatmapi/core/construct.rb +92 -0
  10. data/lib/rtm/javatmapi/core/datatype_aware.rb +60 -0
  11. data/lib/rtm/javatmapi/core/locator.rb +20 -0
  12. data/lib/rtm/javatmapi/core/name.rb +127 -0
  13. data/lib/rtm/javatmapi/core/occurrence.rb +32 -0
  14. data/lib/rtm/javatmapi/core/reifiable.rb +38 -0
  15. data/lib/rtm/javatmapi/core/role.rb +49 -0
  16. data/lib/rtm/javatmapi/core/scoped.rb +93 -0
  17. data/lib/rtm/javatmapi/core/topic.rb +504 -0
  18. data/lib/rtm/javatmapi/core/topic_map.rb +677 -0
  19. data/lib/rtm/javatmapi/core/typed.rb +31 -0
  20. data/lib/rtm/javatmapi/core/variant.rb +19 -0
  21. data/lib/rtm/javatmapi/ext.rb +4 -0
  22. data/lib/rtm/javatmapi/ext/java_util_set.rb +10 -0
  23. data/lib/rtm/javatmapi/javalibs/aduna-commons-collections-2.5.0.jar +0 -0
  24. data/lib/rtm/javatmapi/javalibs/aduna-commons-concurrent-2.4.0.jar +0 -0
  25. data/lib/rtm/javatmapi/javalibs/aduna-commons-i18n-1.1.0.jar +0 -0
  26. data/lib/rtm/javatmapi/javalibs/aduna-commons-io-2.6.0.jar +0 -0
  27. data/lib/rtm/javatmapi/javalibs/aduna-commons-iteration-2.5.0.jar +0 -0
  28. data/lib/rtm/javatmapi/javalibs/aduna-commons-lang-2.6.0.jar +0 -0
  29. data/lib/rtm/javatmapi/javalibs/aduna-commons-net-2.4.0.jar +0 -0
  30. data/lib/rtm/javatmapi/javalibs/aduna-commons-text-2.4.0.jar +0 -0
  31. data/lib/rtm/javatmapi/javalibs/aduna-commons-xml-2.4.0.jar +0 -0
  32. data/lib/rtm/javatmapi/javalibs/commons-collections-3.2.1.jar +0 -0
  33. data/lib/rtm/javatmapi/javalibs/ctm-writer-1.0.8-SNAPSHOT.jar +0 -0
  34. data/lib/rtm/javatmapi/javalibs/java-tools-0.0.3-SNAPSHOT.jar +0 -0
  35. data/lib/rtm/javatmapi/javalibs/jcl-over-slf4j-1.6.1.jar +0 -0
  36. data/lib/rtm/javatmapi/javalibs/mio-syntaxpack-nodeps-0.1.1.jar +0 -0
  37. data/lib/rtm/javatmapi/javalibs/sesame-model-2.3-pr1.jar +0 -0
  38. data/lib/rtm/javatmapi/javalibs/sesame-query-2.3-pr1.jar +0 -0
  39. data/lib/rtm/javatmapi/javalibs/sesame-queryalgebra-evaluation-2.3-pr1.jar +0 -0
  40. data/lib/rtm/javatmapi/javalibs/sesame-queryalgebra-model-2.3-pr1.jar +0 -0
  41. data/lib/rtm/javatmapi/javalibs/sesame-queryparser-api-2.3-pr1.jar +0 -0
  42. data/lib/rtm/javatmapi/javalibs/sesame-queryparser-sparql-2.3-pr1.jar +0 -0
  43. data/lib/rtm/javatmapi/javalibs/sesame-queryresultio-api-2.3-pr1.jar +0 -0
  44. data/lib/rtm/javatmapi/javalibs/sesame-queryresultio-sparqlxml-2.3-pr1.jar +0 -0
  45. data/lib/rtm/javatmapi/javalibs/sesame-repository-api-2.3-pr1.jar +0 -0
  46. data/lib/rtm/javatmapi/javalibs/sesame-repository-sail-2.3-pr1.jar +0 -0
  47. data/lib/rtm/javatmapi/javalibs/sesame-rio-api-2.3-pr1.jar +0 -0
  48. data/lib/rtm/javatmapi/javalibs/sesame-rio-n3-2.3-pr1.jar +0 -0
  49. data/lib/rtm/javatmapi/javalibs/sesame-rio-rdfxml-2.2.4.jar +0 -0
  50. data/lib/rtm/javatmapi/javalibs/sesame-rio-turtle-2.3-pr1.jar +0 -0
  51. data/lib/rtm/javatmapi/javalibs/sesame-sail-api-2.3-pr1.jar +0 -0
  52. data/lib/rtm/javatmapi/javalibs/sesame-sail-inferencer-2.3-pr1.jar +0 -0
  53. data/lib/rtm/javatmapi/javalibs/sesame-sail-memory-2.3-pr1.jar +0 -0
  54. data/lib/rtm/javatmapi/javalibs/slf4j-api-1.6.1.jar +0 -0
  55. data/lib/rtm/javatmapi/javalibs/slf4j-nop-1.6.1.jar +0 -0
  56. data/lib/rtm/javatmapi/javalibs/tmapi-2.0.3-SNAPSHOT.jar +0 -0
  57. data/lib/rtm/javatmapi/javalibs/tmapix-core-0.2.0-SNAPSHOT.jar +0 -0
  58. data/lib/rtm/javatmapi/javalibs/tmapix-io-1.0.0.jar +0 -0
  59. data/lib/rtm/javatmapi/javalibs/trove-2.0.4.jar +0 -0
  60. data/lib/rtm/javatmapi/superiseable.rb +33 -0
  61. metadata +137 -0
@@ -0,0 +1,677 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ module Java::OrgTmapiCore::TopicMap
5
+
6
+ URI_PATTERN = ":"
7
+
8
+ include RTM::TopicMap
9
+ extend Superiseable
10
+
11
+ attr_reader :prefixes #hash
12
+ attr_reader :engine
13
+
14
+ def set_engine(engine)
15
+ @engine = engine
16
+ end
17
+
18
+ # returns the base iri of this topic map
19
+ def base_iri
20
+ getLocator.reference
21
+ end
22
+ alias :base_locator :base_iri
23
+
24
+ def prefixes
25
+ @prefixes ||= {}
26
+ end
27
+
28
+
29
+ # Adds the identifier (key) and reference (value) to the prefixes-Hash and
30
+ # returns all prefixes. Qnames consisting of "identifier:localpart"
31
+ # will be converted to "referencelocalpart".
32
+ #
33
+ # @param [String] identifier
34
+ # @param [String] reference
35
+ # @example
36
+ # add_prefix("tml", "http://www.topicmapslab.de/")
37
+ # @return void
38
+ def add_prefix(identifier, reference)
39
+ unless identifier.is_a?(String) && reference.is_a?(String)
40
+ raise("add_prefix: identifier and reference must be Strings")
41
+ # TODO @raise in yardoc
42
+ end
43
+ if identifier.empty? || reference.empty?
44
+ raise("add_prefix: identifier or reference may not be empty")
45
+ end
46
+ if (identifier == "sl") || (identifier == "si") || (identifier == "ii")
47
+ raise("add_prefix: identifier 'sl', 'si' or 'ii' not allowed for prefixes")
48
+ end
49
+ unless reference.include?(URI_PATTERN)
50
+ raise("add_prefix: reference must be absulute")
51
+ end
52
+ # prefix xsd is allowed
53
+ prefixes[identifier]= reference
54
+ prefixes
55
+ end
56
+
57
+ # Deletes a prefix (given the identifier) from the prefixes-Hash and
58
+ # returnes the remaining prefixes.
59
+ #
60
+ # If reference is given, it must match the value of the identifier (key)
61
+ # in the prefixes-Hash -
62
+ # if not, the prefix is not deleted.
63
+ #
64
+ # The result may be empty.
65
+ #
66
+ # :call-seq:
67
+ # delete_prefix(identifier) -> Hash
68
+ # delete_prefix(identifier,reference) -> Hash
69
+ #
70
+ def delete_prefix(identifier,reference = :any)
71
+ #warn("delete_prefix: identifier not found") unless prefixes.has_key?(identifier)
72
+ #warn("delete_prefix: reference does not match") unless reference == :any || reference == prefixes[identifier]
73
+ if reference == :any
74
+ prefixes.delete(identifier)
75
+ else
76
+ prefixes.delete_if{|k,v| k == identifier && v == reference}
77
+ end
78
+ prefixes
79
+ end
80
+ alias :del_prefix :delete_prefix
81
+
82
+ # Returns the prefixes that match the given the
83
+ # identifier and/or reference.
84
+ #
85
+ # :call-seq:
86
+ # find_prefix -> Hash
87
+ # find_prefix({:identifier => String}) -> Hash
88
+ # find_prefix({:reference => String}) -> Hash
89
+ # find_prefix({:identifier => String, :reference => String}) -> Hash
90
+ #
91
+ def find_prefix(filter = {})
92
+ #TODO Specs
93
+ identifier = filter[:identifier]
94
+ reference = filter[:reference]
95
+ if identifier && reference
96
+ return prefixes.select{|k,v| k == identifier && v == reference}
97
+ end
98
+ if identifier
99
+ return prefixes.select{|k,v| k == identifier}
100
+ elsif reference
101
+ return prefixes.select{|k,v| v == reference}
102
+ else
103
+ prefixes
104
+ end
105
+ end
106
+
107
+ private
108
+ def show_prefixes
109
+ #TODO to be implemented?
110
+ prefixes
111
+ end
112
+ public
113
+
114
+ superised
115
+ # Returns nil.
116
+ #
117
+ # :call-seq:
118
+ # parent -> nil
119
+ #
120
+ def parent
121
+ getParent
122
+ end
123
+ alias :reverse_children :parent
124
+
125
+ # Returns all Associations and Topics contained in this TopicMap.
126
+ #
127
+ # :call-seq:
128
+ # children -> Array of Associations and/or Topics
129
+ #
130
+ def children
131
+ getAssociations.to_a + getTopics.to_a
132
+ end
133
+ alias :reverse_parent :children
134
+
135
+ superised
136
+ # Creates an Association in this TopicMap.
137
+ #
138
+ # Type specifies the Association type.
139
+ #
140
+ # If given, scope specifies the association scope;
141
+ # else the Association is in the unconstrained scope.
142
+ # Scope must be an Array of identifiers.
143
+ #
144
+ # The last argument - a roles-Hash - is optional. If given,
145
+ # all specified roles will be created additionally. A role is
146
+ # specified by the role type: the key in the Hash and
147
+ # the role player: the corresponding value in the Hash.
148
+ #
149
+ # Each identifier (type, scope-identifier, roletype-identifier and
150
+ # player-identifier) may be a topic reference.
151
+ #
152
+ # :call-seq:
153
+ # create_association(type) -> Association
154
+ # create_association(type, scope-Array) -> Association
155
+ # create_association(type, roletype-identifier => player-identifier, ...) -> Association
156
+ # create_association(type, scope-Array, roletype-identifier => player-identifier, ...) -> Association
157
+ #
158
+ def create_association(type, *args)
159
+ roles = {}
160
+ if args.size == 0
161
+ # no args -> no scope and no roles given
162
+ scope = :ucs
163
+ elsif args.size == 1
164
+ # args may be an array (scope) or hash (roles)
165
+ argument = args[0]
166
+ if argument.is_a?(Array)
167
+ scope = argument
168
+ elsif argument.is_a?(Hash)
169
+ scope = :ucs
170
+ roles = argument
171
+ else
172
+ raise("create_association: arguments after type may be an Array or Hash")
173
+ end
174
+ else
175
+ # several arguments after type
176
+ argument1 = args[0]
177
+ argument2 = args[1]
178
+ if argument1.is_a?(Array)
179
+ scope = argument1
180
+ roles = argument2
181
+ elsif argument1.is_a?(Hash)
182
+ scope = :ucs
183
+ roles = argument1
184
+ else
185
+ raise("create_association: arguments after type may be an Array or Hash")
186
+ end
187
+ end
188
+ raise("create_association: type must be a Topic or Topic-Reference") unless type.is_a?(Java::OrgTmapiCore::Topic) || type.is_a?(String) || type.is_a?(Java::OrgTmapiCore::Locator)
189
+ if scope == :ucs
190
+ assoc = createAssociation(get!(type),[].to_java(Java::OrgTmapiCore::Topic))
191
+ else
192
+ raise("create_association: scope must be an Array") unless scope.is_a?(Array)
193
+ assoc = scope.empty? ? createAssociation(get!(type),[].to_java(Java::OrgTmapiCore::Topic)) : createAssociation(get!(type),get!(scope))
194
+ end
195
+ raise("create_association: roles must be a Hash") unless roles.is_a?(Hash)
196
+ roles.each do |k,v|
197
+ if v.is_a?(Array)
198
+ v.each{|player| assoc.create_role(k,player)}
199
+ else
200
+ assoc.create_role(k,v)
201
+ end
202
+ end
203
+ yield assoc if block_given?
204
+ return assoc
205
+ end
206
+
207
+ # Calls TMAPI TopicMap.getIndex which
208
+ # returns the index for the TypeInstanceIndex.
209
+ #
210
+ # :call-seq:
211
+ # type_instance_index -> TypeInstanceIndex
212
+ #
213
+ def type_instance_index
214
+ # Equals _index(:TypeInstanceIndex), which doesn't work yet
215
+ index = getIndex(Java::OrgTmapiIndex::TypeInstanceIndex.java_class)
216
+ index.open unless index.is_open
217
+ return index
218
+ end
219
+
220
+ # Calls TMAPI TopicMap.getIndex which
221
+ # returns the index for the LiteralIndex.
222
+ #
223
+ # :call-seq:
224
+ # literal_index -> LiteralIndex
225
+ #
226
+ def literal_index
227
+ # Equals _index(:LiteralIndex), which doesn't work yet
228
+ index = getIndex(Java::OrgTmapiIndex::LiteralIndex.java_class)
229
+ index.open unless index.is_open
230
+ return index
231
+ end
232
+
233
+ # Calls TMAPI TopicMap.getIndex which
234
+ # returns the index for the ScopedIndex.
235
+ #
236
+ # :call-seq:
237
+ # scoped_index -> ScopedIndex
238
+ #
239
+ def scoped_index
240
+ # Equals _index(:ScopedIndex), which doesn't work yet
241
+ index = getIndex(Java::OrgTmapiIndex::ScopedIndex.java_class)
242
+ index.open unless index.is_open
243
+ return index
244
+ end
245
+
246
+ # Returns all topics in the topic map that are used as instance
247
+ # in an "type-instance"-relationship.
248
+ #
249
+ # The optional argument specifies the type the instances should have.
250
+ # Type may be a topic reference. This method equals topics(type).
251
+ #
252
+ # The result may be empty.
253
+ #
254
+ # :call-seq:
255
+ # instances -> Array of Topics
256
+ # instances(type) -> Array of Topics
257
+ #
258
+ def instances(type = :any)
259
+ return types.map{|t| t.instances.to_a}.flatten.uniq if type == :any
260
+ type = self.get(type) unless type.is_a?(RTM::Topic)
261
+ return type ? type_instance_index.getTopics(type).to_a : []
262
+ end
263
+
264
+ # Returns all topics in the topic map that are used as type
265
+ # in an "type-instance"-relationship.
266
+ #
267
+ # :call-seq:
268
+ # types -> Set of Topics
269
+ #
270
+ def types
271
+ type_instance_index.getTopicTypes
272
+ end
273
+
274
+ superised
275
+ # Returns all associations contained in this topic map.
276
+ #
277
+ # The optional argument specifies the type the associations should have,
278
+ # i.e. all instances of the specified type are returned. Type may be a
279
+ # topic reference.
280
+ #
281
+ # The return value may be empty.
282
+ #
283
+ # :call-seq:
284
+ # associations -> Set of Associations
285
+ # associations(type) -> Array of Associations
286
+ #
287
+ def associations(type = :any)
288
+ return getAssociations if type == :any
289
+ raise("associations(type): type must be a topic reference.") unless (type.is_a?(RTM::Topic) || type.is_a?(RTM::Locator) || type.is_a?(String))
290
+ type = self.get(type) unless type.is_a?(RTM::Topic)
291
+ return type ? type_instance_index.getAssociations(type).to_a : []
292
+ end
293
+
294
+ superised
295
+ # Returns all topics contained in this topic map.
296
+ #
297
+ # The optional argument specifies the type of the topics should have,
298
+ # i.e. all instances of the specified type are returned. Type may be a
299
+ # topic reference. This method equals instances (type).
300
+ #
301
+ # The return value may be empty.
302
+ #
303
+ # :call_spec:
304
+ # topics -> Set of Topics
305
+ # topics(type) -> Array of Topics
306
+ #
307
+ def topics(type = :any)
308
+ return getTopics if type == :any
309
+ return instances(type)
310
+ end
311
+
312
+ superised
313
+ # Returns a Locator representing the identifier.
314
+ # Identifier may be a String (IRI).
315
+ #
316
+ # :call-seq:
317
+ # create_locator(identifier) -> Locator
318
+ #
319
+ def create_locator(identifier)
320
+ return identifier if identifier.is_a?(Java::OrgTmapiCore::Locator)
321
+ raise("create_locator: identifier must be a String") unless identifier.is_a?(String)
322
+ qname = check_for_qname(identifier)
323
+ if qname
324
+ identifier = qname
325
+ else
326
+ unless identifier.include?(URI_PATTERN)
327
+ begin
328
+ identifier = getLocator.resolve(identifier)
329
+ rescue org.tmapi.core.MalformedIRIException => mie
330
+ raise(mie.message)
331
+ end
332
+ return identifier
333
+ end
334
+ end
335
+ begin
336
+ identifier = createLocator(identifier)
337
+ rescue org.tmapi.core.MalformedIRIException => mie
338
+ raise(mie.message)
339
+ end
340
+ return identifier
341
+ end
342
+
343
+ superised
344
+ # Assumes identifier is an item identifier (Locator or String)
345
+ # or an Array of item identifiers (Locators and/or Strings)
346
+ # and returns a Construct or an Array of Constructs
347
+ # by its/their item identifier/s.
348
+ #
349
+ # :call-seq:
350
+ # get_construct_by_item_identifier(identifier) -> Construct
351
+ # get_construct_by_item_identifier(identifier-Array) -> Array of Constructs
352
+ #
353
+ def get_construct_by_item_identifier(identifier)
354
+ identifier = create_locator(identifier) if identifier.is_a?(String)
355
+ return getConstructByItemIdentifier(identifier) if identifier.is_a?(Java::OrgTmapiCore::Locator)
356
+ return identifier.map{|i| get_topic_by_subject_locator(i)} if identifier.is_a?(Array)
357
+ return nil
358
+ end
359
+
360
+ superised
361
+ # Assumes identifier is a subject identifier (Locator or String)
362
+ # or an Array of subject identifiers (Locators and/or Strings)
363
+ # and returns a Topic or an Array of Topics
364
+ # by its/their subject identifier/s.
365
+ #
366
+ # :call-seq:
367
+ # get_topic_by_subject_identifier(identifier) -> Topic
368
+ # get_topic_by_subject_identifier(identifier-Array) -> Array of Topics
369
+ #
370
+ def get_topic_by_subject_identifier(identifier)
371
+ identifier = create_locator(identifier) if identifier.is_a?(String)
372
+ return getTopicBySubjectIdentifier(identifier) if identifier.is_a?(Java::OrgTmapiCore::Locator)
373
+ return identifier.map{|i| get_topic_by_subject_locator(i)} if identifier.is_a?(Array)
374
+ return nil
375
+ end
376
+
377
+ superised
378
+ # Assumes identifier is a subject locator (Locator or String)
379
+ # or an Array of subject locators (Locators and/or Strings)
380
+ # and returns a Topic or an Array of Topics
381
+ # by its/their subject locator/s.
382
+ #
383
+ # :call-seq:
384
+ # get_topic_by_subject_locator(identifier) -> Topic
385
+ # get_topic_by_subject_locator(identifier-Array) -> Array of Topics
386
+ #
387
+ def get_topic_by_subject_locator(identifier)
388
+ identifier = create_locator(identifier) if identifier.is_a?(String)
389
+ return getTopicBySubjectLocator(identifier) if identifier.is_a?(Java::OrgTmapiCore::Locator)
390
+ return identifier.map{|i| get_topic_by_subject_locator(i)} if identifier.is_a?(Array)
391
+ return nil
392
+ end
393
+
394
+ superised
395
+ # Assumes identifier is a subject identifier (Locator or String)
396
+ # or an Array of subject identifiers (Locators and/or Strings).
397
+ # Either returns an existing Topic (or Array of Topics) or creates a/
398
+ # new Topic instance/s with the specified subject identifier/s.
399
+ #
400
+ # :call-seq:
401
+ # create_topic_by_subject_identifier(identifier) -> Topic
402
+ # create_topic_by_subject_identifier(identifier-Array) -> Array of Topics
403
+ #
404
+ # If a topic with the specified subject identifier exists in the topic map,
405
+ # that topic is returned. If a topic with an item identifier equals to the
406
+ # specified subject identifier exists, the specified subject identifier is
407
+ # added to that topic and the topic is returned. If neither a topic with
408
+ # the specified subject identifier nor with an item identifier equals to
409
+ # the subject identifier exists, a topic with the subject identifier is
410
+ # created.
411
+ def create_topic_by_subject_identifier(identifier)
412
+ get_stuff(identifier){|x| createTopicBySubjectIdentifier(x)}
413
+ end
414
+
415
+ superised
416
+ # Assumes identifier is a subject locator (Locator or String)
417
+ # or an Array of subject locators (Locators and/or Strings).
418
+ # Either returns an existing Topic (or Array of Topics) or creates a/
419
+ # new Topic instance/s with the specified subject locators/s.
420
+ #
421
+ # :call-seq:
422
+ # create_topic_by_subject_locator(identifier) -> Topic
423
+ # create_topic_by_subject_locator(identifier-Array) -> Array of Topics
424
+ #
425
+ def create_topic_by_subject_locator(identifier)
426
+ get_stuff(identifier){|x| createTopicBySubjectLocator(x)}
427
+ end
428
+
429
+ superised
430
+ # Assumes identifier is a item identifier (Locator or String)
431
+ # or an Array of item identifiers (Locators and/or Strings).
432
+ # Either returns an existing Topic (or Array of Topics) or creates a/
433
+ # new Topic instance/s with the specified item identifier/s.
434
+ #
435
+ # :call-seq:
436
+ # create_topic_by_item_identifier(identifier) -> Topic
437
+ # create_topic_by_item_identifier(identifier-Array) -> Array of Topics
438
+ #
439
+ # If a topic with the specified item identifier exists in the topic map,
440
+ # that topic is returned. If a topic with a subject identifier equals to
441
+ # the specified item identifier exists, the specified item identifier is
442
+ # added to that topic and the topic is returned. If neither a topic with
443
+ # the specified item identifier nor with a subject identifier equals to
444
+ # the subject identifier exists, a topic with the item identifier is created.
445
+ def create_topic_by_item_identifier(identifier)
446
+ get_stuff(identifier){|x| createTopicByItemIdentifier(x)}
447
+ end
448
+
449
+ # Assumes identifier is an IRI (Locator or String) or an Array of IRIs.
450
+ # Returns an existing or created Topic or an Array of Topics according
451
+ # to the nature of the IRI.
452
+ #
453
+ # :call-seq:
454
+ # create_topic_by(identifier) -> Topic
455
+ # create_topic_by(identifier-Array) -> Array of Topics
456
+ #
457
+ def create_topic_by(identifier)
458
+ case identifier
459
+ when Java::OrgTmapiCore::Locator
460
+ return create_topic_by_subject_identifier(identifier)
461
+ when String
462
+ reroute_for_create(identifier)
463
+ when Array
464
+ return identifier.map{|i| create_topic_by(i)}
465
+ else
466
+ return nil
467
+ end
468
+ end
469
+
470
+ # If identifier is a Topic, returns this Topic.
471
+ #
472
+ # If identifier is a Locator, returns the Topic specified by the Locator (as item
473
+ # identifier or subject identifier).
474
+ #
475
+ # If identifier is a String, returnes the Topic which has this
476
+ # String as subject identifier (by default or if the String starts with "si:")
477
+ # or as subject locator (if the String starts with a "=" or "sl:")
478
+ # or as item identifier (if the String starts with a "^" or "ii:").
479
+ #
480
+ # Returns an Array of Topics if an Array of identifiers is given according to the
481
+ # rules above.
482
+ #
483
+ # Given one identifier, if no Topic is found, returns nil.
484
+ #
485
+ # Given an Array of identifiers, if no Topics are found, returnes an empty Array.
486
+ #
487
+ # :call-seq:
488
+ # get(identifier) -> Topic or nil
489
+ # get(identifier-Array) -> Array of Topics or empty Array
490
+ #
491
+ def get(identifier)
492
+ self.cached(self, :get, identifier) {
493
+ case identifier
494
+ when nil
495
+ nil
496
+ when Java::OrgTmapiCore::Topic
497
+ identifier
498
+ when Java::OrgTmapiCore::Locator
499
+ if get_construct_by_item_identifier(identifier).is_a?(Java::OrgTmapiCore::Topic)
500
+ get_construct_by_item_identifier(identifier)
501
+ elsif get_topic_by_subject_identifier(identifier)
502
+ get_topic_by_subject_identifier(identifier)
503
+ else
504
+ #return get_topic_by_subject_locator(identifier) if get_topic_by_subject_locator(identifier) #not allowed because Topics with equals si and ii are merged but sl not
505
+ nil
506
+ end
507
+ when String
508
+ reroute_for_get(identifier)
509
+ when Array
510
+ # in: multi array -> out: multi array
511
+ identifier.map{|i| get(i)}
512
+ #when Hash
513
+ #TODO: use case for getting Topics if argument is a hash
514
+ else
515
+ raise("get(#{identifier.inspect}): arguments do not match")
516
+ end
517
+ }
518
+ end
519
+ alias :topic :get
520
+ # alias :[] :get # not sure if this is a good idea, for now we leave it out
521
+
522
+ # Calls get(identifier) and returns the Topic or Array of Topics according
523
+ # to the nature of the identifier (Topic/Topic-References or
524
+ # Array of those types).
525
+ #
526
+ # Calls create_topic_by(identifier) if a Topic specified by identifier does
527
+ # not exist in the TopicMap. Returns the created and/our found Topic/s.
528
+ #
529
+ # If identifier is a String, returnes/creates the Topic which has this
530
+ # String as subject identifier (by default or if the String starts with "si:")
531
+ # or as subject locator (if the String starts with a "=" or "sl:")
532
+ # or as item identifier (if the String starts with a "^" or "ii:").
533
+ #
534
+ # :call-seq:
535
+ # get!(identifier) -> Topic
536
+ # get!(identifier-Array) -> Array of Topics
537
+ #
538
+ def get!(identifier)
539
+ return nil unless identifier
540
+ if identifier.is_a?(Array)
541
+ return identifier.map{|i| get!(i)}
542
+ end
543
+ found = get(identifier)
544
+ return found ? found : create_topic_by(identifier)
545
+ end
546
+ alias :topic! :get!
547
+
548
+ def construct_by_id(id)
549
+ getConstructById(id)
550
+ end
551
+
552
+ # Returns all names occurring in the topic map
553
+ def names
554
+ name_types.map{|type| type.typed_names.to_a}.flatten
555
+ end
556
+
557
+ # Returns all occurrences occurring in the topic map
558
+ def occurrences
559
+ occurrence_types.map{|type| type.typed_occurrences.to_a}.flatten
560
+ end
561
+
562
+ # Returns all roles occurring in the topic map
563
+ def roles
564
+ role_types.map{|type| type.typed_roles.to_a}.flatten
565
+ end
566
+
567
+ # Returns all variants occurring in the topic map
568
+ def variants
569
+ names.map{|name| name.variants.to_a}.flatten
570
+ end
571
+
572
+ private
573
+
574
+ # If the identifier is a qname (= "prefix_identifier" + ":" + "localpart"),
575
+ # substitutes the prefix_identifier with the reference
576
+ # (=> "reference" + "localpart") if the prefix_identifier is
577
+ # found in the prefixes-Hash.
578
+ #
579
+ # Returnes the enhanced identifier if successfull.
580
+ # Returns the nil if no substitution occured.
581
+ #
582
+ # :call-seq:
583
+ # check_for_qname(identifier) -> String
584
+ #
585
+ def check_for_qname(identifier)
586
+ return nil if prefixes.empty?
587
+
588
+ sections = identifier.split(":")
589
+ prefix_identifier = sections[0]
590
+ if (sections.size > 1) && prefixes.has_key?(prefix_identifier)
591
+ localpart = identifier.sub("#{prefix_identifier}:", "")
592
+ # check localpart
593
+ unless localpart.include?("/") # no / allowed in localpart
594
+ return prefixes[prefix_identifier] + localpart
595
+ end
596
+ end
597
+ return nil
598
+ end
599
+
600
+ # Calls TMAPI TopicMap.getIndex which
601
+ # returns the specified index for the
602
+ # indexInterface i.
603
+ #
604
+ # :call-seq:
605
+ # _index(i) -> extended index
606
+ #
607
+ # doesn't work;
608
+ # JRuby doesn't seem to like "send" there;
609
+ # solution: use getIndex directly
610
+ def _index(i)
611
+ index = getIndex(Java::OrgTmapiIndex::send(i).java_class)
612
+ index.open unless index.is_open
613
+ return index
614
+ end
615
+
616
+ # If identifier is not a String or Locator -> assumes it is an Array.
617
+ # If Array entries are not locators -> create_locator(entry).
618
+ # Calls yield on each Array entry and returns Array.
619
+ #
620
+ # If identifier is not a Locator -> assumes it is a String and
621
+ # calls create_locator(idenifier).
622
+ # Executes yield on locator and returns the result.
623
+ def get_stuff(identifier)
624
+ unless (identifier.is_a?(String)) || (identifier.is_a?(Java::OrgTmapiCore::Locator))
625
+ identifier_in_use = identifier.map{|x|(x.is_a? Java::OrgTmapiCore::Locator)? x : create_locator(x)}
626
+ return identifier_in_use.map{|y| yield y}
627
+ else
628
+ unless identifier.is_a? Java::OrgTmapiCore::Locator
629
+ identifier = create_locator(identifier)
630
+ end
631
+ return (yield identifier)
632
+ end
633
+ end
634
+
635
+ # Identifies the identifier as item identifier, subject locator or
636
+ # subject identifier and calls create_topic_by_.. accordingly. Creates
637
+ # new topic if topic does not exist yet. Returns the topic.
638
+ #
639
+ # :call-seq:
640
+ # reroute_for_create(identifier) -> Topic
641
+ #
642
+ def reroute_for_create(identifier)
643
+ case identifier
644
+ when /^(\^|ii:)\s*(.*)/ #identifiers starts with ^ or ii:
645
+ create_topic_by_item_identifier(create_locator($2))
646
+ when /^(=|sl:)\s*(.*)/ #identifier starts with = or sl:
647
+ create_topic_by_subject_locator(create_locator($2))
648
+ when /^(si:)\s*(.*)/ #identifier starts with si:
649
+ create_topic_by_subject_identifier(create_locator($2))
650
+ else #identifier does not start with a special character
651
+ create_topic_by_subject_identifier(create_locator(identifier.lstrip))#lstrip: leading whitespace chars removed
652
+ end
653
+ end
654
+
655
+ # Identifies the identifier as item identifier, subject locator or
656
+ # subject identifier and calls get_construct/topic_by_.. accordingly.
657
+ # Returns the topic.
658
+ #
659
+ # :call-seq:
660
+ # reroute_for_get(identifier) -> Topic
661
+ #
662
+ def reroute_for_get(identifier)
663
+ case identifier
664
+ when /^(si:|sl:|ii:|=|\^)$/
665
+ return nil
666
+ when /^(\^|ii:)\s*(.*)/ #identifiers starts with ^ or ii:
667
+ get_construct_by_item_identifier(create_locator($2))
668
+ when /^(=|sl:)\s*(.+)/ #identifier starts with = or sl:
669
+ get_topic_by_subject_locator(create_locator($2))
670
+ when /^(si:)\s*(.+)/ #identifier starts with si:
671
+ get_topic_by_subject_identifier(create_locator($2))
672
+ else #identifier does not start with a special character
673
+ get_topic_by_subject_identifier(create_locator(identifier.lstrip)) #lstrip: leading whitespace chars removed
674
+ end
675
+ end
676
+
677
+ end