source: ruby/trunk/lib/open_ehr/serializer.rb@ 411

Last change on this file since 411 was 411, checked in by KOBAYASHI, Shinji, 14 years ago

fixed archetype ontology bugs

File size: 7.2 KB
Line 
1require 'rexml/document'
2require 'builder'
3
4module OpenEHR
5 module Serializer
6 NL = "\r\n"
7 INDENT = ' '
8
9 class BaseSerializer
10 def initialize(archetype)
11 @archetype = archetype
12 end
13
14 def serialize
15 return self.merge
16 end
17 end
18
19 class ADLSerializer < BaseSerializer
20 def header
21 hd = 'archetype'
22 unless @archetype.adl_version.nil?
23 hd << " (adl_version = #{@archetype.adl_version})"
24 end
25 hd << NL+INDENT + "#{@archetype.archetype_id.value}"+NL*2
26 hd << 'concept'+NL+ INDENT+"[#{@archetype.concept}]"+NL
27 hd << NL+'language'+NL+INDENT+'original_language = <['+
28 @archetype.original_language.terminology_id.value+'::'+
29 @archetype.original_language.code_string+']>'+NL
30 return hd
31 end
32
33 def description
34 desc = ''
35 if @archetype.description
36 ad = @archetype.description
37 desc << 'description' + NL
38 desc << INDENT + 'original_author = <' + NL
39 ad.original_author.each do |k,v|
40 desc << INDENT+INDENT+'["'+k+'"] = <"'+v+'">'+NL
41 end
42 desc << INDENT+'>'+NL
43 desc << INDENT+'lifecycle_state = <"'+ad.lifecycle_state+'">'+NL
44 desc << INDENT+'details = <'+NL
45 ad.details.each do |lang,item|
46 desc << INDENT*2+'["'+lang+'"] = <'+NL
47 desc << INDENT*3+'language = <['+
48 item.language.terminology_id.value+'::'+
49 item.language.code_string+']>'+NL
50 desc << INDENT*3+'purpose = <"'+item.purpose+'">'+NL
51 if item.keywords then
52 desc << INDENT*3+'keywords = <'
53 item.keywords.each do |word|
54 desc << '"'+word+'",'
55 end
56 desc.chop! << '>'+NL
57 end
58 desc << INDENT*3+'use = <"'+item.use+'">'+NL if item.use
59 desc << INDENT*3+'misuse = <"'+item.misuse+'">'+NL if item.misuse
60 desc << INDENT*3+'copyright = <"'+item.copyright+'">'+NL if item.copyright
61 if item.original_resource_uri
62 desc << INDENT*3 + 'original_resource_uri = <'
63 item.original_resource_uri.each do |k,v|
64 desc << INDENT*4+'["'+k+'"] = <"'+v+'">'+NL
65 end
66 desc << INDENT*3+'>'+NL
67 end
68 if item.other_details
69 desc << INDENT*3 + 'other_details = <'
70 item.original_resource_uri.each do |k,v|
71 desc << INDENT*4+'["'+k+'"] = <"'+v+'">'+NL
72 end
73 desc << INDENT*3+'>'+NL
74 end
75 desc << INDENT*2+'>'+NL
76 end
77 desc << INDENT+'>'+NL
78 end
79 return desc
80 end
81
82 def definition
83 ad = @archetype.definition
84 definition = 'definition'+NL
85 definition << INDENT+ad.rm_type_name+"[#{ad.node_id}] matches {"
86 if ad.any_allowed?
87 definition << '*}'+NL
88 else
89 definition << NL
90 if ad.attributes
91 attributes = ad.attributes
92 indents = 2
93 while attributes
94 definition << INDENT*indents+attributes.rm_type_name
95 definition << "[#{attributes.node_id}] "
96 definition << existence(attributes.existence)
97 definition << " matches {"
98 end
99 end
100 end
101 end
102
103 def ontology
104 ao = @archetype.ontology
105 ontology = 'ontology'+NL
106 ontology << INDENT + 'term_definitions = <' + NL
107 ao.term_codes.each do |term, items|
108 ontology << INDENT*2 + "[\"#{term}\"] = <" + NL
109 ontology << INDENT*3 + 'items = <' + NL
110 items.each do |code, attribute|
111 ontology << INDENT*4 + "[\"#{code}\] = <" + NL
112 terms.each do |name, description|
113 ontology << INDENT*5 + "#{name} = <\"#{description}\">" +NL
114 end
115 ontology << INDENT*4 + '>'+NL
116 end
117 ontogoly << INDENT*3 + '>' + NL
118 ontology << INDENT*2 + '>' + NL
119 end
120 ontology << INDENT + '>' + NL
121 end
122
123 def envelope
124 end
125
126 def merge
127 return header + description + definition + ontology
128 end
129
130 private
131 def c_object
132 end
133
134 def existence(existence)
135 "existence matches {#{existence.lower}..#{existence.upper}}"
136 end
137 end
138
139 class XMLSerializer < BaseSerializer
140
141 def header
142 header = ''
143 xml = Builder::XmlMarkup.new(:indent => 2, :target => header)
144 xml.archetype_id do
145 xml.value @archetype.archetype_id.value
146 end
147 xml.concept @archetype.concept
148 xml.original_language do
149 xml.terminology_id do
150 xml.value @archetype.original_language.terminology_id.value
151 end
152 xml.code_string @archetype.original_language.code_string
153 end
154 return header
155 end
156
157 def description
158 desc = ''
159 xml = Builder::XmlMarkup.new(:indent => 2, :target => desc)
160 ad = @archetype.description
161 if ad
162 xml.description do
163 ad.original_author.each do |key,value|
164 xml.original_author(value,"id"=>key)
165 end
166 if ad.other_contributors
167 ad.other_contributors.each do |co|
168 xml.other_contributors co
169 end
170 end
171 xml.lifecycle_state ad.lifecycle_state
172 xml.details do
173 ad.details.each do |lang, item|
174 xml.language do
175 xml.terminology_id do
176 xml.value item.language.terminology_id.value
177 end
178 xml.code_string lang
179 end
180 xml.purpose item.purpose
181 if item.keywords then
182 item.keywords.each do |word|
183 xml.keywords word
184 end
185 end
186 xml.use item.use if item.use
187 xml.misuse item.misuse if item.misuse
188 xml.copyright item.copyright if item.copyright
189 if ad.other_details
190 ad.other_details.each do |key,value|
191 xml.other_details(value, "id"=>key)
192 end
193 end
194 end
195 end
196 end
197 end
198 return desc
199 end
200
201 def definition
202 definition = ''
203 ad = @archetype.definition
204 xml = Builder::XmlMarkup.new(:indent => 2, :target => definition)
205 xml.definition do
206 xml.rm_type_name ad.rm_type_name
207 xml.occurrence do
208 oc = ad.occurrences
209 xml.lower_included oc.lower_included? unless oc.lower_included?.nil?
210 xml.upper_included oc.upper_included? unless oc.upper_included?.nil?
211 xml.lower_unbounded oc.lower_unbounded?
212 xml.upper_unbounded oc.upper_unbounded?
213 xml.lower oc.lower
214 xml.upper oc.lower
215 end
216 xml.node_id ad.node_id
217 end
218 end
219
220 def merge
221
222 end
223 end
224 end
225end
226
227class Publisher
228 def initialize(serializer)
229 @serializer = serializer
230 end
231
232 def publish(writer)
233 writer.out(@serializer.serialize)
234 end
235end
236
237class Writer
238 def initialize(target)
239 @target = target
240 end
241 def out
242 end
243end
Note: See TracBrowser for help on using the repository browser.