rtm 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/DISCLAIMER +10 -33
  2. data/LICENSE +201 -0
  3. data/README +3 -3
  4. data/lib/rtm.rb +148 -74
  5. data/lib/rtm/axes.rb +295 -0
  6. data/lib/rtm/axes/association.rb +76 -0
  7. data/lib/rtm/axes/associations.rb +96 -0
  8. data/lib/rtm/axes/assocs_names_occs.rb +56 -0
  9. data/lib/rtm/axes/characteristic.rb +68 -0
  10. data/lib/rtm/axes/characteristics.rb +93 -0
  11. data/lib/rtm/axes/string.rb +76 -0
  12. data/lib/rtm/axes/strings.rb +87 -0
  13. data/lib/rtm/axes/topic.rb +233 -0
  14. data/lib/rtm/axes/topics.rb +280 -0
  15. data/lib/rtm/{backward_compatibility.rb → deprecated/index_property_set.rb} +3 -0
  16. data/lib/rtm/engine.rb +58 -0
  17. data/lib/rtm/extensions.rb +11 -1
  18. data/lib/rtm/{locator_helpers.rb → helpers/locator.rb} +15 -4
  19. data/lib/rtm/{helpers.rb → helpers/no_output.rb} +3 -0
  20. data/lib/rtm/helpers/uri.rb +13 -0
  21. data/lib/rtm/io.rb +18 -0
  22. data/lib/rtm/io/from_xtm2_libxml.rb +2 -1
  23. data/lib/rtm/io/ontopia_io.rb +25 -0
  24. data/lib/rtm/io/tmapix.rb +234 -0
  25. data/lib/rtm/io/to_hash.rb +116 -0
  26. data/lib/rtm/io/to_jtm.rb +53 -103
  27. data/lib/rtm/io/to_rdf.rb +30 -0
  28. data/lib/rtm/io/to_string.rb +8 -6
  29. data/lib/rtm/io/to_xtm1.rb +6 -4
  30. data/lib/rtm/io/to_xtm2.rb +10 -8
  31. data/lib/rtm/io/to_yaml.rb +29 -98
  32. data/lib/rtm/navigation.rb +37 -0
  33. data/lib/rtm/navigation/association/players.rb +25 -0
  34. data/lib/rtm/navigation/name/atomify.rb +19 -0
  35. data/lib/rtm/navigation/name/characteristics.rb +33 -0
  36. data/lib/rtm/navigation/occurrence/atomify.rb +19 -0
  37. data/lib/rtm/navigation/occurrence/characteristics.rb +33 -0
  38. data/lib/rtm/navigation/topic/characteristics.rb +33 -0
  39. data/lib/rtm/navigation/topic/indicators.rb +31 -0
  40. data/lib/rtm/navigation/topic/items.rb +31 -0
  41. data/lib/rtm/navigation/topic/locators.rb +31 -0
  42. data/lib/rtm/navigation/topic/players.rb +27 -0
  43. data/lib/rtm/navigation/topic/supertypes.rb +162 -0
  44. data/lib/rtm/navigation/topic/traverse.rb +51 -0
  45. data/lib/rtm/navigation/topic/types.rb +107 -0
  46. data/lib/rtm/psi.rb +33 -2
  47. data/lib/rtm/sugar.rb +34 -0
  48. data/lib/rtm/sugar/association/hash_access.rb +46 -0
  49. data/lib/rtm/sugar/occurrence/dynamic_value.rb +75 -0
  50. data/lib/rtm/{pimp_my_api.rb → sugar/occurrence/externalize.rb} +3 -0
  51. data/lib/rtm/sugar/role/counterparts.rb +133 -46
  52. data/lib/rtm/sugar/topic/characteristics.rb +126 -12
  53. data/lib/rtm/sugar/topic/counterparts.rb +130 -7
  54. data/lib/rtm/sugar/topic/hash_access.rb +140 -30
  55. data/lib/rtm/sugar/topic/scoped.rb +65 -0
  56. data/lib/rtm/sugar/topic/topic_ref.rb +35 -0
  57. data/lib/rtm/sugar/topic/typed.rb +159 -0
  58. data/lib/rtm/sugar/typed/types.rb +38 -0
  59. data/lib/rtm/validation.rb +8 -5
  60. data/lib/rtm/version.rb +18 -0
  61. data/spec/helpers/spec_exampleexamplegroup.rb +14 -0
  62. data/spec/rtm/axes/association_spec.rb +88 -0
  63. data/spec/rtm/axes/associations_spec.rb +60 -0
  64. data/spec/rtm/axes/assocs_names_occs_spec.rb +9 -0
  65. data/spec/rtm/axes/characteristic_spec.rb +90 -0
  66. data/spec/rtm/axes/characteristics_spec.rb +85 -0
  67. data/spec/rtm/axes/string_spec.rb +145 -0
  68. data/spec/rtm/axes/strings_spec.rb +168 -0
  69. data/spec/rtm/axes/topic_spec.rb +124 -0
  70. data/spec/rtm/axes/topics_spec.rb +103 -0
  71. data/spec/rtm/base_spec.rb +32 -0
  72. data/spec/rtm/io/tmapix_spec.rb +85 -0
  73. data/spec/rtm/navigation/association/players_spec.rb +58 -0
  74. data/spec/rtm/navigation/association_spec.rb +52 -0
  75. data/spec/rtm/navigation/name/atomify_spec.rb +27 -0
  76. data/spec/rtm/navigation/name/characteristics_spec.rb +34 -0
  77. data/spec/rtm/navigation/name_spec.rb +52 -0
  78. data/spec/rtm/navigation/occurrence/atomify_spec.rb +27 -0
  79. data/spec/rtm/navigation/occurrence/characteristics_spec.rb +34 -0
  80. data/spec/rtm/navigation/occurrence_spec.rb +52 -0
  81. data/spec/rtm/navigation/string_spec.rb +51 -0
  82. data/spec/rtm/navigation/topic/characteristics_spec.rb +55 -0
  83. data/spec/rtm/navigation/topic/indicators_spec.rb +43 -0
  84. data/spec/rtm/navigation/topic/items_spec.rb +44 -0
  85. data/spec/rtm/navigation/topic/locators_spec.rb +44 -0
  86. data/spec/rtm/navigation/topic/players_spec.rb +48 -0
  87. data/spec/rtm/navigation/topic/scope_spec.rb +41 -0
  88. data/spec/rtm/navigation/topic/supertypes_spec.rb +376 -0
  89. data/spec/rtm/navigation/topic/traverse_spec.rb +64 -0
  90. data/spec/rtm/navigation/topic/types_spec.rb +195 -0
  91. data/spec/rtm/navigation/topic_spec.rb +153 -0
  92. data/spec/rtm/sugar/association/hash_access_spec.rb +55 -0
  93. data/spec/rtm/sugar/occurrence/dynamic_value_spec.rb +16 -0
  94. data/spec/rtm/sugar/role/counterparts_spec.rb +191 -0
  95. data/spec/rtm/sugar/topic/characteristics_spec.rb +318 -0
  96. data/spec/rtm/sugar/topic/counterparts_spec.rb +184 -0
  97. data/spec/rtm/sugar/topic/hash_access_spec.rb +234 -0
  98. data/spec/rtm/sugar/topic/scoped_spec.rb +131 -0
  99. data/spec/rtm/sugar/topic/topic_ref_spec.rb +44 -0
  100. data/spec/rtm/sugar/topic/typed_spec.rb +155 -0
  101. data/spec/rtm/sugar/typed/types_spec.rb +24 -0
  102. data/spec/rtm/tmapi/core/association_spec.rb +169 -0
  103. data/spec/rtm/tmapi/core/construct_spec.rb +25 -0
  104. data/spec/rtm/tmapi/core/name_spec.rb +85 -0
  105. data/spec/rtm/tmapi/core/occurrence_spec.rb +96 -0
  106. data/spec/rtm/tmapi/core/reifiable_spec.rb +168 -0
  107. data/spec/rtm/tmapi/core/role_spec.rb +73 -0
  108. data/spec/rtm/tmapi/core/scoped_spec.rb +403 -0
  109. data/spec/rtm/tmapi/core/topic_map_spec.rb +648 -0
  110. data/spec/rtm/tmapi/core/topic_spec.rb +992 -0
  111. data/spec/rtm/tmapi/core/typed_spec.rb +112 -0
  112. data/spec/rtm/tmapi/core/variant_spec.rb +52 -0
  113. data/spec/rtm/tmapi/ext/java_util_set_spec.rb +34 -0
  114. data/spec/rtm/tmapi_spec.rb +44 -0
  115. data/spec/rtm/utils/sparql_spec.rb +26 -0
  116. data/spec/rtm_spec.rb +94 -0
  117. data/spec/spec_helper.rb +23 -0
  118. data/test/base_unit_test.rb +161 -0
  119. data/test/{base_test.rb → base_unit_test_tmapi.rb} +46 -43
  120. metadata +122 -66
  121. data/COPYRIGHT +0 -4
  122. data/lib/Rakefile.rb +0 -42
  123. data/lib/activetopicmaps.rb +0 -278
  124. data/lib/rtm/backend/active_record.rb +0 -58
  125. data/lib/rtm/backend/active_record/001_initial_schema.rb +0 -116
  126. data/lib/rtm/backend/active_record/association_and_role.rb +0 -33
  127. data/lib/rtm/backend/active_record/locators.rb +0 -55
  128. data/lib/rtm/backend/active_record/name_variant_occurrence.rb +0 -45
  129. data/lib/rtm/backend/active_record/quaaxtm2rtm.rb +0 -113
  130. data/lib/rtm/backend/active_record/quaaxtm2rtmviews.rb +0 -134
  131. data/lib/rtm/backend/active_record/set_wrapper.rb +0 -98
  132. data/lib/rtm/backend/active_record/tm_construct.rb +0 -62
  133. data/lib/rtm/backend/active_record/tm_delegator.rb +0 -345
  134. data/lib/rtm/backend/active_record/tm_set_delegator.rb +0 -195
  135. data/lib/rtm/backend/active_record/tmdm.rb +0 -298
  136. data/lib/rtm/backend/active_record/topic.rb +0 -87
  137. data/lib/rtm/backend/active_record/topic_map.rb +0 -314
  138. data/lib/rtm/backend/active_record/traverse_associations.rb +0 -87
  139. data/lib/rtm/base.rb +0 -92
  140. data/lib/rtm/connect.rb +0 -92
  141. data/lib/rtm/core_ext.rb +0 -6
  142. data/lib/rtm/io/from_xtm2.rb +0 -263
  143. data/lib/rtm/merging/merging.rb +0 -307
  144. data/lib/rtm/sugar/topic/identifier_direct.rb +0 -11
  145. data/lib/rtm/sugar/topic/predefined_associations.rb +0 -42
  146. data/lib/run_main_project.rb +0 -16
@@ -1,4 +1,5 @@
1
- # Ruby Topic Maps (RTM) http://rtm.rubyforge.org/
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
2
3
 
3
4
  module RTM
4
5
  def self.from_xtm2lx(*args)
@@ -0,0 +1,25 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+
5
+ module RTM::IO
6
+ # Import and Export of TMAPI Topic Maps
7
+ # Utilizing the Ontopia libary http://code.google.com/p/tmapix/
8
+ # Consequently the (de-) serializer provided here
9
+ # are only Wrapper
10
+
11
+ module Ontopia
12
+ module TopicMap
13
+ def to_cxtm(filename)
14
+ raise("Only supported for Ontopia.") unless self.kind_of?(net.ontopia.topicmaps.impl.tmapi2.TopicMapImpl)
15
+ out_file = java.io.FileOutputStream.new(filename)
16
+ writer = net.ontopia.topicmaps.xml.CanonicalXTMWriter.new(out_file)
17
+ writer.write(self.wrapped)
18
+ out_file.flush
19
+ out_file.close
20
+ end
21
+ end
22
+
23
+ RTM.register_extension(self)
24
+ end
25
+ end
@@ -0,0 +1,234 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ module RTM::IO
5
+ # Import and Export of TMAPI Topic Maps
6
+ # Utilizing the TMAPIX libary http://code.google.com/p/tmapix/
7
+ # Consequently the (de-) serializer provided here
8
+ # are only Wrapper
9
+
10
+ module TMAPIX
11
+ module TopicMap
12
+
13
+ # Reads a Topic Map from an LTM File
14
+ # whose location is given as String parameter
15
+ #
16
+ # :call-seq:
17
+ # from_ltm(filename)
18
+ #
19
+ def from_ltm(filename)
20
+ raise "Only supported for TMAPI backends!!" unless self.kind_of? Java::OrgTmapiCore::TopicMap
21
+ org.tmapix.io.LTMTopicMapReader.new(self, java.io.File.new(filename)).read
22
+ end
23
+
24
+ # Reads a Topic Map from an CTM File
25
+ # whose location is given as String parameter
26
+ #
27
+ # :call-seq:
28
+ # from_ctm(filename)
29
+ #
30
+ def from_ctm(filename)
31
+ raise "Only supported for TMAPI backends!!" unless self.kind_of? Java::OrgTmapiCore::TopicMap
32
+ org.tmapix.io.CTMTopicMapReader.new(self, java.io.File.new(filename)).read
33
+ end
34
+
35
+ # Reads a JSON Topic Maps (JTM) File
36
+ # whose location is given as String parameter
37
+ #
38
+ # :call-seq:
39
+ # from_jtm(filename)
40
+ #
41
+ def from_jtm(filename)
42
+ raise "Only supported for TMAPI backends!!" unless self.kind_of? Java::OrgTmapiCore::TopicMap
43
+ # seems not to acceped all files that are found to be valid by the JTM validator
44
+ org.tmapix.io.JTMTopicMapReader.new(self, java.io.File.new(filename)).read
45
+ end
46
+
47
+ # Reads a Friendly Topic Maps XML (TM/XML) File
48
+ # independent of the version
49
+ # whose location is given as String parameter
50
+ #
51
+ # :call-seq:
52
+ # from_tmxml(filename)
53
+ #
54
+ def from_tmxml(filename)
55
+ raise "Only supported for TMAPI backends!!" unless self.kind_of? Java::OrgTmapiCore::TopicMap
56
+ org.tmapix.io.TMXMLTopicMapReader.new(self, java.io.File.new(filename)).read
57
+ end
58
+
59
+ # Reads a XML Topic Maps (XTM) 1.0 File
60
+ # whose location is given as String parameter
61
+ #
62
+ # :call-seq:
63
+ # from_xtm10(filename)
64
+ #
65
+ def from_xtm10(filename)
66
+ raise "Only supported for TMAPI backends!!" unless self.kind_of? Java::OrgTmapiCore::TopicMap
67
+ org.tmapix.io.XTM10TopicMapReader.new(self, java.io.File.new(filename)).read
68
+ end
69
+
70
+ # Reads a XML Topic Maps (XTM) 2.0 File
71
+ # whose location is given as String parameter
72
+ #
73
+ # :call-seq:
74
+ # from_xtm20(filename)
75
+ #
76
+ def from_xtm20(filename)
77
+ from_xtm(filename)
78
+ end
79
+
80
+ # Reads a XML Topic Maps (XTM) File
81
+ # independent of the version
82
+ # whose location is given as String parameter
83
+ #
84
+ # :call-seq:
85
+ # from_xtm(filename)
86
+ #
87
+ def from_xtm(filename)
88
+ raise "Only supported for TMAPI backends!!" unless self.kind_of? Java::OrgTmapiCore::TopicMap
89
+ org.tmapix.io.XTMTopicMapReader.new(self, java.io.File.new(filename)).read
90
+ end
91
+
92
+ # Reads a Notation3 (N3) File
93
+ # whose location is given as String
94
+ # a second parameter has to be handed over
95
+ # providing the mapping vocabulary.
96
+ # The syntax of the vocabulary is guessed.
97
+ #
98
+ # :call-seq:
99
+ # from_n3(source vocabulary)
100
+ #
101
+ def from_n3(filename, vocab)
102
+ raise "Only supported for TMAPI backends!!" unless self.kind_of? Java::OrgTmapiCore::TopicMap
103
+ reader = org.tmapix.io.N3TopicMapReader.new self, java.io.File.new(filename)
104
+ reader.setMappingSource(java.io.File.new(vocab))
105
+ reader.read
106
+ end
107
+
108
+ # Reads a RDF/XML File
109
+ # whose location is given as String
110
+ # a second parameter has to be handed over
111
+ # providing the mapping vocabulary.
112
+ # The syntax of the vocabulary is guessed.
113
+ #
114
+ # :call-seq:
115
+ # from_rdfxml(source vocabulary)
116
+ #
117
+ def from_rdfxml(filename, vocab)
118
+ raise "Only supported for TMAPI backends!!" unless self.kind_of? Java::OrgTmapiCore::TopicMap
119
+ reader = org.tmapix.io.RDFXMLTopicMapReader.new self, java.io.File.new(filename)
120
+ reader.setMappingSource(java.io.File.new(vocab))
121
+ reader.read
122
+ end
123
+
124
+ # Serializes an RTM::TopicMap to XTM v. 2.0.
125
+ #
126
+ # The argument filename defines the absolute location of the file that is created.
127
+ # The argument base_iri is used to resolve IRIs against.
128
+ #
129
+ # :call-seq:
130
+ # to_xtm20(filename, base_iri)
131
+ #
132
+ def to_xtm20(filename, base_iri)
133
+ raise("Only supported for TMAPI backends.") unless self.kind_of?(Java::OrgTmapiCore::TopicMap)
134
+ out_file = java.io.FileOutputStream.new(filename)
135
+ writer = org.tmapix.io.XTM20TopicMapWriter.new(out_file, base_iri)
136
+ writer.write(self)
137
+ out_file.flush
138
+ out_file.close
139
+ end
140
+ alias :to_xtm :to_xtm20
141
+
142
+ # Serializes an RTM::TopicMap to XTM v. 1.0.
143
+ #
144
+ # The argument filename defines the absolute location of the file that is created.
145
+ # The argument base_iri is used to resolve IRIs against.
146
+ # Both arguments have to be Strings.
147
+ #
148
+ # :call-seq:
149
+ # to_xtm10(filename, base_iri)
150
+ #
151
+ def to_xtm10(filename, base_iri)
152
+ raise("Only supported for TMAPI backends.") unless self.kind_of?(Java::OrgTmapiCore::TopicMap)
153
+ out_file = java.io.FileOutputStream.new(filename)
154
+ writer = org.tmapix.io.XTM10TopicMapWriter.new(out_file, base_iri)
155
+ writer.write(self)
156
+ out_file.flush
157
+ out_file.close
158
+ end
159
+
160
+ # Serializes an RTM::TopicMap to JTM.
161
+ #
162
+ # The argument filename defines the absolute location of the file that is created.
163
+ # The argument base_iri is used to resolve IRIs against.
164
+ #
165
+ # :call-seq:
166
+ # to_jtm(filename, base_iri)
167
+ #
168
+ def to_jtm(filename, base_iri)
169
+ raise("Only supported for TMAPI backends.") unless self.kind_of?(Java::OrgTmapiCore::TopicMap)
170
+ out_file = java.io.FileOutputStream.new(filename)
171
+ writer = org.tmapix.io.JTMTopicMapWriter.new(out_file, base_iri)
172
+ writer.write(self)
173
+ out_file.flush
174
+ out_file.close
175
+ end
176
+ alias :to_json :to_jtm
177
+
178
+ # Serializes an RTM::TopicMap to LTM.
179
+ #
180
+ # The argument filename defines the absolute location of the file that is created.
181
+ # The argument base_iri is used to resolve IRIs against.
182
+ #
183
+ # :call-seq:
184
+ # to_ltm(filename, base_iri)
185
+ #
186
+ def to_ltm(filename, base_iri)
187
+ raise("Only supported for TMAPI backends.") unless self.kind_of?(Java::OrgTmapiCore::TopicMap)
188
+ out_file = java.io.FileOutputStream.new(filename)
189
+ writer = org.tmapix.io.LTMTopicMapWriter.new(out_file, base_iri)
190
+ writer.write(self)
191
+ out_file.flush
192
+ out_file.close
193
+ end
194
+
195
+ # Serializes an RTM::TopicMap to TM/XML.
196
+ #
197
+ # The argument filename defines the absolute location of the file that is created.
198
+ # The argument base_iri is used to resolve IRIs against.
199
+ #
200
+ # :call-seq:
201
+ # to_tmxml(filename, base_iri)
202
+ #
203
+ def to_tmxml(filename, base_iri)
204
+ raise("Only supported for TMAPI backends.") unless self.kind_of?(Java::OrgTmapiCore::TopicMap)
205
+ out_file = java.io.FileOutputStream.new(filename)
206
+ writer = org.tmapix.io.TMXMLTopicMapWriter.new(out_file, base_iri)
207
+ writer.write(self)
208
+ out_file.flush
209
+ out_file.close
210
+ end
211
+
212
+ # Serializes an RTM::TopicMap to CTM.
213
+ #
214
+ # The argument filename defines the absolute location of the file that is created.
215
+ # The argument base_iri is used to resolve IRIs against.
216
+ #
217
+ # :call-seq:
218
+ # to_ctm(filename, base_iri)
219
+ #
220
+ def to_ctm(filename, base_iri)
221
+ raise("Only supported for TMAPI backends.") unless self.kind_of?(Java::OrgTmapiCore::TopicMap)
222
+ out_file = java.io.FileOutputStream.new(filename)
223
+ line = "writer.features.export.itemidentifier = false, " + "writer.features.prefixDetection.enabled = false, " + "writer.features.templateDetection.enabled = false, " + "writer.features.templateDetection.topicTemplates = false , " + "writer.features.templateDetection.associationTemplates = false, " + "writer.features.templateMerger.enabled = false"
224
+ writer = de.topicmapslab.ctm.writer.core.CTMTopicMapWriter.new(out_file, base_iri, line)
225
+ writer.write(self)
226
+ out_file.flush
227
+ out_file.close
228
+ end
229
+
230
+ end
231
+
232
+ RTM.register_extension(self)
233
+ end
234
+ end
@@ -0,0 +1,116 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ module RTM::IO
5
+ # HASH Export.
6
+ # Each Topic Maps Construct gets a to_hash method.
7
+ module TOHASH
8
+
9
+ module TopicMap
10
+ # Returns the Hash representation of this RTM::TopicMap
11
+ def to_hash
12
+ h={}
13
+ h[:reifier] = reifier.to_ref if reifier
14
+ h[:item_identifiers] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
15
+ h[:topics] = topics.map{|i| i.to_hash} unless topics.empty?
16
+ h[:associations] = associations.map{|i| i.to_hash} unless associations.empty?
17
+ h
18
+ end
19
+ end
20
+
21
+
22
+ module Topic
23
+ # Returns the Hash representation of this RTM::Topic
24
+ def to_hash
25
+ h = {}
26
+ h[:item_identifiers] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
27
+ h[:subject_identifiers] = subject_identifiers.map{|i| i.reference} unless subject_identifiers.empty?
28
+ h[:subject_locators] = subject_locators.map{|i| i.reference} unless subject_locators.empty?
29
+ h[:names] = names.map{|i| i.to_hash} unless names.empty?
30
+ h[:occurrences] = occurrences.map{|i| i.to_hash} unless occurrences.empty?
31
+ h
32
+ end
33
+ # Returns the reference to the Topic
34
+ def to_ref
35
+ if subject_identifiers.first
36
+ return "si:#{subject_identifiers.first.reference.to_s}"
37
+ end
38
+ if item_identifiers.first
39
+ return "ii:#{item_identifiers.first.reference.to_s}"
40
+ end
41
+ # "t#{id}"
42
+ end
43
+ end
44
+
45
+ module Name
46
+ # Returns the Hash representation of this RTM::Name
47
+ def to_hash
48
+ h={}
49
+ h[:reifier] = reifier.to_ref if reifier
50
+ h[:item_identifiers] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
51
+ h[:value] = value if value
52
+ h[:type] = type.to_ref if type
53
+ h[:scope] = scope.map{|i| i.to_ref} unless scope.empty?
54
+ h[:variants] = variants.map{|i| i.to_hash} unless variants.empty?
55
+ h
56
+ end
57
+ end
58
+
59
+
60
+ module Association
61
+ # Returns the Hash representation of this RTM::Association
62
+ def to_hash
63
+ h={}
64
+ h[:reifier] = reifier.to_ref if reifier
65
+ h[:item_identifiers] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
66
+ h[:type] = type.to_ref if type
67
+ h[:scope] = scope.map{|i| i.to_ref} unless scope.empty?
68
+ h[:roles] = roles.map{|i| i.to_hash} unless roles.empty?
69
+ h
70
+ end
71
+ end
72
+
73
+
74
+
75
+ module Occurrence
76
+ # Returns the Hash representation of this RTM::Occurrence
77
+ def to_hash
78
+ h={}
79
+ h[:reifier] = reifier.to_ref if reifier
80
+ h[:item_identifiers] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
81
+ h[:value] = value if value
82
+ h[:datatype] = datatype.reference if datatype
83
+ h[:type] = type.to_ref if type
84
+ h[:scope] = scope.map{|i| i.to_ref} unless scope.empty?
85
+ h
86
+ end
87
+ end
88
+
89
+ module Role
90
+ # Returns the Hash representation of this RTM::Role
91
+ def to_hash
92
+ h={}
93
+ h[:reifier] = reifier.to_ref if reifier
94
+ h[:item_identifiers] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
95
+ h[:player] = player.to_ref if player
96
+ h[:type] = type.to_ref if type
97
+ h
98
+ end
99
+ end
100
+
101
+ module Variant
102
+ # Returns the Hash representation of this RTM::Variant
103
+ def to_hash(*a)
104
+ h={}
105
+ h[:reifier] = reifier.to_ref if reifier
106
+ h[:item_identifiers] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
107
+ h[:value] = value if value
108
+ h[:scope] = scope.map{|i| i.to_ref} unless scope.empty?
109
+ h
110
+ end
111
+ end
112
+
113
+ RTM.register_extension self
114
+
115
+ end
116
+ end
data/lib/rtm/io/to_jtm.rb CHANGED
@@ -1,144 +1,94 @@
1
- # Ruby Topic Maps (RTM) http://rtm.rubyforge.org/
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
2
4
  module RTM::IO
3
5
  # JTM Export.
4
6
  # Each Topic Maps Construct gets a to_jtm method.
5
- module TOJTM
7
+ module TOJTM
6
8
  begin
7
9
  require 'json/ext'
8
10
  rescue LoadError
9
11
  require 'json/pure'
10
- end
12
+ end unless Object.const_defined?(:JSON)
11
13
 
12
14
  module TopicMap
13
- # returns the JTM representation of this topic map
14
- def to_jtm_hash(*a)
15
- j={}
16
- #j['base_locator'] = base_locator, # with absolute item_identifiers its not needed
17
- j['reifier'] = reifier.to_jtm_ref if reifier
18
- j['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
19
- j['topics'] = topics.map{|i| i.to_jtm_hash} unless topics.empty?
20
- j['associations'] = associations.map{|i| i.to_jtm_hash} unless associations.empty?
21
- j
15
+ # Returns the JTM representation of this RTM::TopicMap
16
+ def to_jtm_str # only for export with no file and base_iri otherwise TMAPIX
17
+ h = to_hash
18
+ h[:version] = "1.0"
19
+ h[:item_type] = "topicmap"
20
+ h.to_json
22
21
  end
23
- def to_jtm(*a)
24
- to_jtm_hash.to_json(*a)
25
- end
26
- alias :to_json :to_jtm
22
+ # alias :to_json :to_jtm_str
27
23
  end
28
24
 
29
25
  module Topic
30
- # returns the JTM representation of this topic
31
- def to_jtm_hash(*a)
32
- j={}
33
- j['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
34
- j['subject_identifiers'] = subject_identifiers.map{|i| i.reference} unless subject_identifiers.empty?
35
- unless j['item_identifiers'] || j['subject_identifiers']
36
- j['item_identifiers'] = [to_jtm_ref]
37
- end
38
- j['subject_locators'] = subject_locators.map{|i| i.reference} unless subject_locators.empty?
39
- j['names'] = names.map{|i| i.to_jtm_hash} unless names.empty?
40
- j['occurrences'] = occurrences.map{|i| i.to_jtm_hash} unless occurrences.empty?
41
- j
42
- end
43
- def to_jtm(*a)
44
- to_jtm_hash.to_json(*a)
26
+ # Returns the JTM representation of this RTM::Topic
27
+ def to_jtm
28
+ h = to_hash
29
+ h[:version] = "1.0"
30
+ h[:item_type] = "topic"
31
+ h.to_json
45
32
  end
46
33
  alias :to_json :to_jtm
47
-
48
- def to_jtm_ref
49
- if subject_identifiers.first
50
- return subject_identifiers.first.reference
51
- end
52
- if item_identifiers.first
53
- return item_identifiers.first.reference
54
- end
55
- "t#{id}"
56
- end
57
34
  end
58
35
 
59
36
  module Association
60
- # returns the JTM representation of this association
61
- def to_jtm_hash(*a)
62
- j={}
63
- j['reifier'] = reifier.to_jtm_ref if reifier
64
- j['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
65
- j['type'] = type.to_jtm_ref if type
66
- j['scope'] = scope.map{|i| i.to_jtm_hash} unless scope.empty?
67
- j['roles'] = roles.map{|i| i.to_jtm_hash} unless roles.empty?
68
- j
69
- end
70
- def to_jtm(*a)
71
- to_jtm_hash.to_json(*a)
37
+ # Returns the JTM representation of this RTM::Association
38
+ def to_jtm
39
+ h = to_hash
40
+ h[:version] = "1.0"
41
+ h[:item_type] = "association"
42
+ h.to_json
72
43
  end
73
44
  alias :to_json :to_jtm
74
-
75
45
  end
76
46
 
77
- module TopicName
78
- def to_jtm_hash(*a)
79
- j={}
80
- j['reifier'] = reifier.to_jtm_ref if reifier
81
- j['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
82
- j['value'] = value if value
83
- j['type'] = type.to_jtm_ref if type
84
- j['scope'] = scope.map{|i| i.to_jtm_hash} unless scope.empty?
85
- j['variants'] = variants.map{|i| i.to_jtm_hash} unless variants.empty?
86
- j
87
- end
88
- def to_jtm(*a)
89
- to_jtm_hash.to_json(*a)
47
+ module Name
48
+ # Returns the JTM representation of this RTM::Name
49
+ def to_jtm
50
+ h = to_hash
51
+ h[:version] = "1.0"
52
+ h[:item_type] = "name"
53
+ h.to_json
90
54
  end
91
55
  alias :to_json :to_jtm
92
56
  end
93
57
 
94
58
  module Occurrence
95
- def to_jtm_hash(*a)
96
- j={}
97
- j['reifier'] = reifier.to_jtm_ref if reifier
98
- j['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
99
- j['value'] = value if value
100
- j['datatype'] = datatype if datatype
101
- j['type'] = type.to_jtm_ref if type
102
- j['scope'] = scope.map{|i| i.to_jtm_hash} unless scope.empty?
103
- j
104
- end
105
- def to_jtm(*a)
106
- to_jtm_hash.to_json(*a)
59
+ # Returns the JTM representation of this RTM::Occurrence
60
+ def to_jtm
61
+ h = to_hash
62
+ h[:version] = "1.0"
63
+ h[:item_type] = "occurrence"
64
+ h.to_json
107
65
  end
108
66
  alias :to_json :to_jtm
109
67
  end
110
68
 
111
- module AssociationRole
112
- def to_jtm_hash(*a)
113
- j={}
114
- j['reifier'] = reifier.to_jtm_ref if reifier
115
- j['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
116
- j['player'] = player.to_jtm_ref if player
117
- j['type'] = type.to_jtm_ref if type
118
- j
119
- end
120
- def to_jtm(*a)
121
- to_jtm_hash.to_json(*a)
69
+ module Role
70
+ # Returns the JTM representation of this RTM::Role
71
+ def to_jtm
72
+ h = to_hash
73
+ h[:version] = "1.0"
74
+ h[:item_type] = "role"
75
+ h.to_json
122
76
  end
123
77
  alias :to_json :to_jtm
124
78
  end
125
79
 
126
80
  module Variant
127
- def to_jtm_hash(*a)
128
- j={}
129
- j['reifier'] = reifier.to_jtm_ref if reifier
130
- j['item_identifiers'] = item_identifiers.map{|i| i.reference} unless item_identifiers.empty?
131
- j['value'] = value if value
132
- j['scope'] = scope.map{|i| i.to_jtm_hash} unless scope.empty?
133
- j
134
- end
135
- def to_jtm(*a)
136
- to_jtm_hash.to_json(*a)
81
+ # Returns the JTM representation of this RTM::Variant
82
+ def to_jtm
83
+ h = to_hash
84
+ h[:version] = "1.0"
85
+ h[:item_type] = "variant"
86
+ h.to_json
137
87
  end
138
88
  alias :to_json :to_jtm
139
89
  end
140
-
141
- RTM.register_extension( self )
90
+
91
+ RTM.register_extension self
92
+
142
93
  end
143
94
  end
144
-