openwferu 0.9.16 → 0.9.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. data/examples/about_state.rb +81 -0
  2. data/examples/engine_template.rb +7 -0
  3. data/lib/openwfe/contextual.rb +2 -2
  4. data/lib/openwfe/def.rb +2 -3
  5. data/lib/openwfe/{util/schedulers.rb → engine.rb} +3 -39
  6. data/lib/openwfe/engine/engine.rb +202 -251
  7. data/lib/openwfe/engine/process_status.rb +359 -0
  8. data/lib/openwfe/expool/errorjournal.rb +6 -6
  9. data/lib/openwfe/expool/expressionpool.rb +161 -239
  10. data/lib/openwfe/expool/expstorage.rb +185 -55
  11. data/lib/openwfe/expool/journal.rb +1 -2
  12. data/lib/openwfe/expool/parser.rb +233 -0
  13. data/lib/openwfe/expool/threadedexpstorage.rb +6 -18
  14. data/lib/openwfe/expool/wfidgen.rb +25 -7
  15. data/lib/openwfe/expool/yamlexpstorage.rb +60 -37
  16. data/lib/openwfe/expressions/condition.rb +49 -12
  17. data/lib/openwfe/expressions/environment.rb +45 -15
  18. data/lib/openwfe/expressions/expressionmap.rb +39 -19
  19. data/lib/openwfe/expressions/fe_concurrence.rb +24 -13
  20. data/lib/openwfe/expressions/fe_cron.rb +19 -18
  21. data/lib/openwfe/expressions/fe_cursor.rb +69 -28
  22. data/lib/openwfe/expressions/fe_define.rb +4 -1
  23. data/lib/openwfe/expressions/fe_do.rb +1 -3
  24. data/lib/openwfe/expressions/fe_equals.rb +131 -20
  25. data/lib/openwfe/expressions/fe_fqv.rb +27 -3
  26. data/lib/openwfe/expressions/fe_iterator.rb +14 -7
  27. data/lib/openwfe/expressions/fe_listen.rb +7 -2
  28. data/lib/openwfe/expressions/fe_misc.rb +187 -20
  29. data/lib/openwfe/expressions/fe_participant.rb +8 -7
  30. data/lib/openwfe/expressions/fe_reserve.rb +105 -33
  31. data/lib/openwfe/expressions/fe_save.rb +55 -5
  32. data/lib/openwfe/expressions/{fe_value.rb → fe_set.rb} +6 -82
  33. data/lib/openwfe/expressions/fe_sleep.rb +25 -15
  34. data/lib/openwfe/expressions/fe_subprocess.rb +2 -2
  35. data/lib/openwfe/expressions/fe_wait.rb +3 -2
  36. data/lib/openwfe/expressions/fe_when.rb +7 -15
  37. data/lib/openwfe/expressions/flowexpression.rb +90 -49
  38. data/lib/openwfe/expressions/merge.rb +7 -1
  39. data/lib/openwfe/expressions/raw.rb +261 -63
  40. data/lib/openwfe/expressions/{raw_prog.rb → rprocdef.rb} +94 -179
  41. data/lib/openwfe/expressions/time.rb +36 -12
  42. data/lib/openwfe/expressions/timeout.rb +9 -7
  43. data/lib/openwfe/expressions/value.rb +126 -0
  44. data/lib/openwfe/flowexpressionid.rb +52 -22
  45. data/lib/openwfe/listeners/listeners.rb +3 -3
  46. data/lib/openwfe/listeners/socketlisteners.rb +8 -5
  47. data/lib/openwfe/logging.rb +6 -3
  48. data/lib/openwfe/omixins.rb +8 -6
  49. data/lib/openwfe/orest/xmlcodec.rb +16 -12
  50. data/lib/openwfe/participants.rb +38 -0
  51. data/lib/openwfe/participants/participant.rb +1 -1
  52. data/lib/openwfe/participants/participantmap.rb +24 -10
  53. data/lib/openwfe/participants/participants.rb +4 -3
  54. data/lib/openwfe/participants/soapparticipants.rb +1 -1
  55. data/lib/openwfe/participants/socketparticipants.rb +1 -1
  56. data/lib/openwfe/rudefinitions.rb +7 -5
  57. data/lib/openwfe/storage/yamlcustom.rb +10 -10
  58. data/lib/openwfe/storage/yamlfilestorage.rb +12 -12
  59. data/lib/openwfe/tools/flowtracer.rb +6 -5
  60. data/lib/openwfe/util/dollar.rb +42 -85
  61. data/lib/openwfe/util/ometa.rb +1 -3
  62. data/lib/openwfe/util/workqueue.rb +1 -1
  63. data/lib/openwfe/utils.rb +33 -11
  64. data/lib/openwfe/version.rb +2 -2
  65. data/lib/openwfe/workitem.rb +76 -14
  66. data/lib/openwfe/worklist/storelocks.rb +9 -4
  67. data/lib/openwfe/worklist/storeparticipant.rb +1 -1
  68. data/test/back_0916_test.rb +101 -0
  69. data/test/bm/ft_26_load.rb +1 -1
  70. data/test/bm/ft_26b_load.rb +1 -1
  71. data/test/bm/ft_26c_load.rb +3 -2
  72. data/test/bm/ft_26d_load.rb +97 -0
  73. data/test/bm/ft_recu.rb +71 -0
  74. data/test/concurrence_test.rb +1 -1
  75. data/test/condition_test.rb +152 -0
  76. data/test/description_test.rb +12 -7
  77. data/test/eno_test.rb +1 -1
  78. data/test/expool_20031219_0916.tgz +0 -0
  79. data/test/fe_lookup_att_test.rb +1 -1
  80. data/test/fei_test.rb +16 -0
  81. data/test/file_persistence_test.rb +8 -12
  82. data/test/filep_cancel_test.rb +116 -0
  83. data/test/flowtestbase.rb +47 -25
  84. data/test/ft_0.rb +1 -1
  85. data/test/ft_10_loop.rb +29 -14
  86. data/test/{ft_10b_loop2.rb → ft_10b_loop.rb} +2 -11
  87. data/test/ft_11_ppd.rb +6 -17
  88. data/test/ft_11b_ppd.rb +1 -4
  89. data/test/ft_12_blockparticipant.rb +1 -1
  90. data/test/ft_13_eno.rb +1 -1
  91. data/test/ft_15_iterator.rb +1 -1
  92. data/test/ft_15b_iterator.rb +1 -1
  93. data/test/ft_17_condition.rb +6 -6
  94. data/test/ft_18_pname.rb +1 -1
  95. data/test/ft_20_cron.rb +1 -1
  96. data/test/ft_21_cron.rb +6 -4
  97. data/test/ft_22_history.rb +1 -1
  98. data/test/ft_23_when.rb +1 -1
  99. data/test/ft_23b_when.rb +18 -6
  100. data/test/ft_23c_wait.rb +8 -6
  101. data/test/ft_25_cancel.rb +7 -5
  102. data/test/ft_27_getflowpos.rb +22 -17
  103. data/test/ft_28_fileparticipant.rb +1 -2
  104. data/test/ft_2_concurrence.rb +1 -1
  105. data/test/ft_2b_concurrence.rb +25 -20
  106. data/test/ft_30_socketlistener.rb +0 -3
  107. data/test/ft_34_cancelwfid.rb +9 -9
  108. data/test/ft_35_localdefs.rb +0 -1
  109. data/test/ft_36_subprocids.rb +6 -6
  110. data/test/ft_38_tag.rb +3 -2
  111. data/test/ft_38b_tag.rb +229 -0
  112. data/test/ft_39_reserve.rb +3 -18
  113. data/test/ft_39b_reserve.rb +34 -5
  114. data/test/ft_3b_lookup_vf.rb +83 -0
  115. data/test/ft_40_defined.rb +2 -11
  116. data/test/ft_42_environments.rb +4 -6
  117. data/test/ft_44b_restore.rb +88 -22
  118. data/test/ft_45_citerator.rb +57 -11
  119. data/test/ft_49_condition.rb +4 -2
  120. data/test/ft_4_misc.rb +24 -3
  121. data/test/ft_50_xml_attribute.rb +17 -20
  122. data/test/ft_54_listen.rb +1 -1
  123. data/test/ft_54b_listen.rb +2 -2
  124. data/test/ft_56_timeout.rb +8 -1
  125. data/test/ft_57_a.rb +10 -10
  126. data/test/ft_59_ps.rb +49 -16
  127. data/test/ft_60_ecancel.rb +52 -10
  128. data/test/ft_63_pause.rb +8 -8
  129. data/test/ft_65_stringlaunch.rb +4 -6
  130. data/test/ft_67_schedlaunch.rb +4 -4
  131. data/test/ft_69_cancelmissing.rb +4 -2
  132. data/test/ft_70_lookupvar.rb +2 -2
  133. data/test/ft_72_lookup_processes.rb +2 -2
  134. data/test/ft_73_cancel_sub.rb +8 -8
  135. data/test/ft_77_segments.rb +38 -0
  136. data/test/ft_78_eval.rb +154 -0
  137. data/test/ft_79_tticket.rb +185 -0
  138. data/test/ft_80_spname.rb +95 -0
  139. data/test/ft_81_exp.rb +64 -0
  140. data/test/ft_82_trecu.rb +48 -0
  141. data/test/ft_83_badpause.rb +62 -0
  142. data/test/ft_84_updateexp.rb +125 -0
  143. data/test/ft_9b_cursor.rb +105 -0
  144. data/test/ft_tests.rb +14 -1
  145. data/test/hash_test.rb +7 -7
  146. data/test/hparticipant_test.rb +4 -4
  147. data/test/lookup_vf_test.rb +94 -0
  148. data/test/misc_test.rb +5 -3
  149. data/test/orest_test.rb +4 -3
  150. data/test/param_test.rb +12 -16
  151. data/test/participant_test.rb +36 -0
  152. data/test/pending.rb +10 -10
  153. data/test/rake_ltest.rb +1 -10
  154. data/test/rake_qtest.rb +7 -6
  155. data/test/raw_prog_test.rb +89 -121
  156. data/test/restart_cron_test.rb +84 -36
  157. data/test/restart_paused_test.rb +100 -0
  158. data/test/restart_sleep_test.rb +1 -1
  159. data/test/restart_tests.rb +1 -0
  160. data/test/restart_when_test.rb +33 -22
  161. data/test/ruby_procdef_test.rb +19 -18
  162. data/test/sec_test.rb +74 -35
  163. data/test/storage_test.rb +44 -0
  164. data/test/test.rb +3 -0
  165. data/test/timeout_test.rb +7 -18
  166. data/test/wfid_test.rb +2 -1
  167. data/test/wi_test.rb +29 -18
  168. metadata +121 -57
  169. data/lib/openwfe/expressions/raw_xml.rb +0 -176
  170. data/lib/openwfe/expressions/simplerep.rb +0 -266
  171. data/lib/openwfe/util/kotoba.rb +0 -236
  172. data/lib/openwfe/util/lru.rb +0 -171
  173. data/lib/openwfe/util/otime.rb +0 -246
  174. data/lib/openwfe/util/safe.rb +0 -160
  175. data/lib/openwfe/util/scheduler.rb +0 -1158
  176. data/test/cron_test.rb +0 -113
  177. data/test/cronline_test.rb +0 -60
  178. data/test/dollar_test.rb +0 -90
  179. data/test/kotoba_test.rb +0 -72
  180. data/test/lru_test.rb +0 -79
  181. data/test/safely_test.rb +0 -84
  182. data/test/scheduler_1_test.rb +0 -88
  183. data/test/scheduler_test.rb +0 -363
  184. data/test/time_test.rb +0 -84
@@ -1,176 +0,0 @@
1
- #
2
- #--
3
- # Copyright (c) 2006-2007, John Mettraux, OpenWFE.org
4
- # All rights reserved.
5
- #
6
- # Redistribution and use in source and binary forms, with or without
7
- # modification, are permitted provided that the following conditions are met:
8
- #
9
- # . Redistributions of source code must retain the above copyright notice, this
10
- # list of conditions and the following disclaimer.
11
- #
12
- # . Redistributions in binary form must reproduce the above copyright notice,
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- #
16
- # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
- # used to endorse or promote products derived from this software without
18
- # specific prior written permission.
19
- #
20
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
- # POSSIBILITY OF SUCH DAMAGE.
31
- #++
32
- #
33
-
34
- #
35
- # "made in Japan"
36
- #
37
- # John Mettraux at openwfe.org
38
- #
39
-
40
- require 'rexml/document'
41
-
42
- require 'openwfe/expressions/raw'
43
-
44
-
45
- module OpenWFE
46
-
47
- #
48
- # Handling process definition whose representation is in XML
49
- # (the classical OpenWFE case).
50
- #
51
- class XmlRawExpression < RawExpression
52
-
53
- def initialize \
54
- (fei, parent_id, env_id, application_context, raw_representation)
55
-
56
- super
57
-
58
- @raw_representation_s = raw_representation.to_s
59
- end
60
-
61
- def raw_representation
62
-
63
- unless @raw_representation
64
-
65
- @raw_representation = \
66
- REXML::Document.new(@raw_representation_s).root
67
- end
68
-
69
- @raw_representation
70
- end
71
-
72
- protected
73
-
74
- def extract_attributes ()
75
- result = {}
76
- raw_representation.attributes.each_attribute do |a|
77
- result[a.name] = a.value
78
- end
79
- return result
80
- end
81
-
82
- def extract_descriptions ()
83
-
84
- result = []
85
- raw_representation.each_child do |child|
86
-
87
- next unless child.is_a?(REXML::Element)
88
- next if child.name.intern != :description
89
-
90
- lang = child.attributes["language"]
91
- lang = child.attributes["lang"] unless lang
92
- lang = "default" unless lang
93
-
94
- result << [ lang, child.children[0] ]
95
- end
96
- result
97
- end
98
-
99
- def extract_children ()
100
-
101
- c = []
102
- i = 0
103
-
104
- raw_representation.each_child do |elt|
105
-
106
- if elt.kind_of?(REXML::Element)
107
-
108
- ename = elt.name.intern
109
-
110
- next if ename == :param
111
- next if ename == :parameter
112
- next if ename == :description
113
-
114
- cfei = @fei.dup
115
-
116
- efei = @environment_id
117
-
118
- cfei.expression_name = elt.name
119
- cfei.expression_id = "#{cfei.expression_id}.#{i}"
120
-
121
- rawchild = XmlRawExpression\
122
- .new(cfei, @fei, efei, @application_context, elt)
123
-
124
- get_expression_pool().update(rawchild)
125
- c << rawchild.fei
126
-
127
- i = i+1
128
-
129
- elsif elt.kind_of?(REXML::Comment)
130
-
131
- next
132
-
133
- else
134
-
135
- s = elt.to_s.strip
136
- c << s if s.length > 0
137
-
138
- end
139
- end
140
-
141
- c
142
- end
143
-
144
- def extract_text_children ()
145
-
146
- raw_representation.children.collect do |elt|
147
-
148
- next if elt.is_a? REXML::Element
149
- next if elt.is_a? REXML::Comment
150
- s = elt.to_s.strip
151
- next if s.length < 1
152
- s
153
- end
154
- end
155
-
156
- def extract_parameters ()
157
-
158
- r = []
159
- raw_representation.children.each do |child|
160
-
161
- next unless child.is_a? REXML::Element
162
-
163
- cname = child.name.intern
164
- next unless (cname == :param or cname == :parameter)
165
-
166
- r << Parameter.new(
167
- child.attributes["field"],
168
- child.attributes["match"],
169
- child.attributes["default"],
170
- child.attributes["type"])
171
- end
172
- r
173
- end
174
- end
175
- end
176
-
@@ -1,266 +0,0 @@
1
- #
2
- #--
3
- # Copyright (c) 2007, John Mettraux, OpenWFE.org
4
- # All rights reserved.
5
- #
6
- # Redistribution and use in source and binary forms, with or without
7
- # modification, are permitted provided that the following conditions are met:
8
- #
9
- # . Redistributions of source code must retain the above copyright notice, this
10
- # list of conditions and the following disclaimer.
11
- #
12
- # . Redistributions in binary form must reproduce the above copyright notice,
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- #
16
- # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
- # used to endorse or promote products derived from this software without
18
- # specific prior written permission.
19
- #
20
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
- # POSSIBILITY OF SUCH DAMAGE.
31
- #++
32
- #
33
-
34
- #
35
- # "made in Japan"
36
- #
37
- # John Mettraux at openwfe.org
38
- #
39
-
40
- require 'rexml/document'
41
-
42
- require 'openwfe/expressions/raw_prog'
43
-
44
-
45
- module OpenWFE
46
-
47
- #
48
- # A raw representation for a process definition, programmatic
49
- # process definitions are turned into trees of instances of this class.
50
- #
51
- # It's just an s-expression somehow.
52
- #
53
- class SimpleExpRepresentation
54
-
55
- attr_reader \
56
- :name,
57
- :attributes
58
-
59
- attr_accessor \
60
- :children
61
-
62
- def initialize (name, attributes)
63
-
64
- super()
65
- @name = name
66
- @attributes = attributes
67
- @children = []
68
- end
69
-
70
- #
71
- # Adds a child to this expression representation.
72
- #
73
- def << (child)
74
- @children << child
75
- end
76
-
77
- #
78
- # Always return the ProgRawExpression class.
79
- #
80
- def raw_expression_class
81
-
82
- ProgRawExpression
83
- end
84
-
85
- #
86
- # Finds the first child named 'description' and returns its
87
- # first child as a String.
88
- #
89
- # TODO : manage languages, as specified in OpenWFE.
90
- #
91
- def get_description
92
-
93
- return @children.first.to_s if @name == 'description'
94
-
95
- @children.each do |child|
96
- d = child.get_description
97
- return d if d
98
- end
99
-
100
- nil
101
- end
102
-
103
- #
104
- # Returns an XML string, containing the equivalent process definition
105
- # in the classical OpenWFE process definition language.
106
- #
107
- def to_s
108
-
109
- doc = REXML::Document.new()
110
- doc << to_xml
111
- s = ""
112
- doc.write(s, 0)
113
-
114
- s
115
- end
116
-
117
- #
118
- # Returns this representation tree as an XML element (and its children).
119
- #
120
- def to_xml
121
-
122
- elt = REXML::Element.new(@name)
123
-
124
- #elt.attributes.update(@attributes)
125
- @attributes.each do |k, v|
126
- elt.attributes[k] = v
127
- end
128
-
129
- @children.each do |child|
130
- if child.kind_of? SimpleExpRepresentation
131
- elt << child.to_xml
132
- else
133
- elt << REXML::Text.new(child.to_s)
134
- end
135
- end
136
-
137
- elt
138
- end
139
-
140
- #
141
- # Turns an XML tree into a simple representation
142
- # (beware embedded XML, should do something to stop that,
143
- # CDATA is perhaps sufficient).
144
- #
145
- def self.from_xml (xml)
146
-
147
- xml = REXML::Document.new(xml) \
148
- if xml.is_a?(String)
149
-
150
- xml = xml.root \
151
- if xml.is_a?(REXML::Document)
152
-
153
- if xml.is_a?(REXML::Text)
154
- #s = xml.to_s
155
- #return s if s.strip.length > 1
156
- s = xml.to_s.strip
157
- return s if s.length > 0
158
- return nil
159
- end
160
-
161
- # xml element thus...
162
-
163
- name = xml.name
164
-
165
- attributes = {}
166
-
167
- xml.attributes.each do |k, v|
168
- attributes[k] = v
169
- end
170
-
171
- rep = SimpleExpRepresentation.new(name, attributes)
172
-
173
- xml.children.each do |c|
174
- r = from_xml(c)
175
- rep << r if r
176
- end
177
-
178
- rep
179
- end
180
-
181
- #
182
- # Evals the given code (string) into a SimpleExpRepresentation.
183
- #
184
- def self.from_code (code)
185
-
186
- ProcessDefinition.eval_ruby_process_definition code
187
- end
188
-
189
- #
190
- # Evals the given string a return its SimpleExpRepresentation
191
- # equivalent, ready for evaluation or rendering (fluo).
192
- #
193
- def self.from_s (s)
194
-
195
- s = s.strip
196
-
197
- if s[0, 1] == "<"
198
-
199
- from_xml s
200
- else
201
-
202
- from_code s
203
- end
204
- end
205
-
206
- #
207
- # Returns a string containing the ruby code that generated this
208
- # raw representation tree.
209
- #
210
- def to_code_s (indentation = 0)
211
-
212
- s = ""
213
- tab = " "
214
- ind = tab * indentation
215
-
216
- s << ind
217
- s << OpenWFE::make_safe(@name)
218
-
219
- sa = ""
220
- @attributes.each do |k, v|
221
- sa << ", :#{OpenWFE::to_underscore(k)} => '#{v}'"
222
- end
223
- s << sa[1..-1] if sa.length > 0
224
-
225
- if @children.length > 0
226
- s << " do\n"
227
- @children.each do |child|
228
- if child.respond_to?(:to_code_s)
229
- s << child.to_code_s(indentation + 1)
230
- else
231
- s << ind
232
- s << tab
233
- s << "'#{child.to_s}'"
234
- end
235
- s << "\n"
236
- end
237
- s << ind
238
- s << "end"
239
- end
240
-
241
- s
242
- end
243
-
244
- #
245
- # Turns this simple representation into an array
246
- # (something suitable for to_json()).
247
- #
248
- def to_a
249
-
250
- cs = @children.collect do |child|
251
-
252
- if child.respond_to?(:to_a)
253
-
254
- child.to_a
255
- else
256
-
257
- child.to_s
258
- end
259
- end
260
-
261
- [ @name, @attributes, cs ]
262
- end
263
- end
264
-
265
- end
266
-
@@ -1,236 +0,0 @@
1
- #
2
- #--
3
- # Copyright (c) 2007, John Mettraux OpenWFE.org
4
- # All rights reserved.
5
- #
6
- # Redistribution and use in source and binary forms, with or without
7
- # modification, are permitted provided that the following conditions are met:
8
- #
9
- # . Redistributions of source code must retain the above copyright notice, this
10
- # list of conditions and the following disclaimer.
11
- #
12
- # . Redistributions in binary form must reproduce the above copyright notice,
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- #
16
- # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
- # used to endorse or promote products derived from this software without
18
- # specific prior written permission.
19
- #
20
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
- # POSSIBILITY OF SUCH DAMAGE.
31
- #++
32
- #
33
- # $Id: definitions.rb 2725 2006-06-02 13:26:32Z jmettraux $
34
- #
35
-
36
- #
37
- # "made in Japan"
38
- #
39
- # John Mettraux at openwfe.org
40
- #
41
-
42
- #
43
- # = Kotoba
44
- #
45
- # This module contains methods for converting plain integers (base 10)
46
- # into words that are easier to read and remember.
47
- #
48
- # For example, the equivalent of the (base 10) integer 1329724967 is
49
- # "takeshimaya".
50
- #
51
- # Kotoba uses 70 of the syllables of the Japanese language, it is thus
52
- # a base 10 to base 70 converter.
53
- #
54
- # Kotoba is meant to be used for generating human readable (or more
55
- # easily rememberable) identifiers. Its first usage is within the
56
- # OpenWFEru Ruby workflow and bpm engine for generating 'kawaii'
57
- # business process intance ids.
58
- #
59
- # == Kotoba from the command line
60
- #
61
- # You can use Kotoba directly from the command line :
62
- #
63
- # $ ruby kotoba.rb kotoba
64
- # 141260
65
- # $ ruby kotoba.rb rubi
66
- # 3432
67
- # $ ruby kotoba.rb 2455
68
- # nada
69
- #
70
- # might be useful when used from some scripts.
71
- #
72
- module Kotoba
73
-
74
- V = %w{ a e i o u }
75
- C = %w{ b d g h j k m n p r s t z }
76
-
77
- SYL = []
78
-
79
- C.each do |s|
80
- V.each do |v|
81
- SYL << s + v
82
- end
83
- end
84
-
85
- SYL << "wa"
86
- SYL << "wo"
87
-
88
- SYL << "ya"
89
- SYL << "yo"
90
- SYL << "yu"
91
-
92
- SPECIAL = [
93
- [ "hu", "fu" ],
94
- [ "si", "shi" ],
95
- [ "ti", "chi" ],
96
- [ "tu", "tsu" ],
97
- [ "zi", "tzu" ]
98
- ]
99
-
100
- #SYL2 = SYL.collect do |syl|
101
- # s = syl
102
- # SPECIAL.each do |a, b|
103
- # if s == a
104
- # s = b
105
- # break
106
- # end
107
- # end
108
- # s
109
- #end
110
-
111
- #
112
- # Turns the given integer into a Kotoba word.
113
- #
114
- def Kotoba.from_integer (integer)
115
- s = from_i(integer)
116
- to_special(s)
117
- end
118
-
119
- #
120
- # Turns the given Kotoba word to its equivalent integer.
121
- #
122
- def Kotoba.to_integer (string)
123
- s = from_special(string)
124
- to_i(s)
125
- end
126
-
127
- #
128
- # Turns a simple syllable into the equivalent number.
129
- # For example Kotoba::to_number("fu") will yield 19.
130
- #
131
- def Kotoba.to_number (syllable)
132
- SYL.each_with_index do |s, index|
133
- return index if syllable == s
134
- end
135
- raise "did not find syllable '#{syllable}'"
136
- end
137
-
138
- #
139
- # Given a Kotoba 'word', will split into its list of syllables.
140
- # For example, "tsunashima" will be split into
141
- # [ "tsu", "na", "shi", "ma" ]
142
- #
143
- def Kotoba.split (word)
144
- word = from_special(word)
145
- a = string_split(word)
146
- a_to_special(a)
147
- end
148
-
149
- #
150
- # Returns if the string is a Kotoba word, like "fugu" or
151
- # "toriyamanobashi".
152
- #
153
- def Kotoba.is_kotoba_word (string)
154
- begin
155
- to_integer(string)
156
- true
157
- rescue #Exception => e
158
- false
159
- end
160
- end
161
-
162
- protected
163
-
164
- def Kotoba.string_split (s, result=[])
165
- return result if s.length < 1
166
- result << s[0, 2]
167
- string_split(s[2..-1], result)
168
- end
169
-
170
- def Kotoba.a_to_special (a)
171
- a.collect do |syl|
172
- SPECIAL.each do |a, b|
173
- if syl == a
174
- syl = b
175
- break
176
- end
177
- end
178
- syl
179
- end
180
- end
181
-
182
- def Kotoba.to_special (s)
183
- SPECIAL.each do |a, b|
184
- s = s.gsub(a, b)
185
- end
186
- s
187
- end
188
-
189
- def Kotoba.from_special (s)
190
- SPECIAL.each do |a, b|
191
- s = s.gsub(b, a)
192
- end
193
- s
194
- end
195
-
196
- def Kotoba.from_i (integer)
197
-
198
- return '' if integer == 0
199
-
200
- mod = integer % SYL.length
201
- rest = integer / SYL.length
202
-
203
- return from_i(rest) + SYL[mod]
204
- end
205
-
206
- def Kotoba.to_i (s)
207
- return 0 if s.length == 0
208
- return SYL.length * to_i(s[0..-3]) + to_number(s[-2, 2])
209
- end
210
- end
211
-
212
- #
213
- # command line interface for Kotoba
214
-
215
- if __FILE__ == $0
216
- arg = ARGV[0]
217
- if arg and arg != "-h" and arg != "--help"
218
- begin
219
- puts Kotoba::from_integer(Integer(arg))
220
- rescue
221
- puts Kotoba::to_integer(arg)
222
- end
223
- else
224
- puts
225
- puts "ruby #{$0} {arg}"
226
- puts
227
- puts " If the arg is a 'Kotoba' word, will turn it into the equivalent"
228
- puts " integer."
229
- puts " Else, it will consider the arg as an integer and attempt at"
230
- puts " turning it into a Kotoba [word]."
231
- puts
232
- puts " Kotoba uses #{Kotoba::SYL.length} syllables."
233
- puts
234
- end
235
- end
236
-