pione 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/History.txt +12 -1
  2. data/example/AbstractRule/AbstractRule.pione +40 -0
  3. data/example/Fib/Fib.pione +12 -5
  4. data/example/LucasNumber/LucasNumber.pione +1 -1
  5. data/example/MakePair/MakePair.pione +21 -6
  6. data/example/OddSelector/OddSelector.pione +17 -0
  7. data/example/OddSelector/data/1.i +0 -0
  8. data/example/OddSelector/data/10.i +0 -0
  9. data/example/OddSelector/data/2.i +0 -0
  10. data/example/OddSelector/data/3.i +0 -0
  11. data/example/OddSelector/data/4.i +0 -0
  12. data/example/OddSelector/data/5.i +0 -0
  13. data/example/OddSelector/data/6.i +0 -0
  14. data/example/OddSelector/data/7.i +0 -0
  15. data/example/OddSelector/data/8.i +0 -0
  16. data/example/OddSelector/data/9.i +0 -0
  17. data/example/SequentialParameter/SequentialParameter.pione +4 -0
  18. data/example/SieveOfEratosthenes/SieveOfEratosthenes.pione +31 -43
  19. data/example/SingleParticlesWithRef/SingleParticlesWithRef.pione +1 -1
  20. data/example/Touch/Touch.pione +3 -0
  21. data/lib/pione/command/pione-syntax-checker.rb +4 -4
  22. data/lib/pione/model/assignment.rb +6 -1
  23. data/lib/pione/model/basic-model.rb +92 -278
  24. data/lib/pione/model/binary-operator.rb +5 -1
  25. data/lib/pione/model/block.rb +17 -0
  26. data/lib/pione/model/boolean.rb +54 -22
  27. data/lib/pione/model/constraints.rb +34 -0
  28. data/lib/pione/model/data-expr.rb +184 -297
  29. data/lib/pione/model/feature-expr.rb +13 -4
  30. data/lib/pione/model/float.rb +24 -41
  31. data/lib/pione/model/integer.rb +75 -41
  32. data/lib/pione/model/keyed-sequence.rb +143 -0
  33. data/lib/pione/model/list.rb +12 -8
  34. data/lib/pione/model/message.rb +8 -4
  35. data/lib/pione/model/ordinal-sequence.rb +75 -0
  36. data/lib/pione/model/package.rb +6 -2
  37. data/lib/pione/model/parameters.rb +61 -9
  38. data/lib/pione/model/pione-method.rb +146 -0
  39. data/lib/pione/model/rule-expr.rb +44 -38
  40. data/lib/pione/model/rule-io.rb +11 -3
  41. data/lib/pione/model/rule.rb +105 -155
  42. data/lib/pione/model/sequence.rb +273 -0
  43. data/lib/pione/model/string.rb +75 -29
  44. data/lib/pione/model/ticket-expr.rb +17 -29
  45. data/lib/pione/model/type.rb +242 -0
  46. data/lib/pione/model/variable-table.rb +52 -53
  47. data/lib/pione/model/variable.rb +8 -4
  48. data/lib/pione/model.rb +34 -0
  49. data/lib/pione/parser/block-parser.rb +44 -20
  50. data/lib/pione/parser/common-parser.rb +2 -1
  51. data/lib/pione/parser/document-parser.rb +6 -1
  52. data/lib/pione/parser/expr-parser.rb +57 -11
  53. data/lib/pione/parser/flow-element-parser.rb +2 -2
  54. data/lib/pione/parser/rule-definition-parser.rb +23 -1
  55. data/lib/pione/patch/rinda-patch.rb +1 -5
  56. data/lib/pione/rule-handler/action-handler.rb +5 -5
  57. data/lib/pione/rule-handler/basic-handler.rb +30 -7
  58. data/lib/pione/rule-handler/empty-handler.rb +14 -0
  59. data/lib/pione/rule-handler/flow-handler.rb +132 -115
  60. data/lib/pione/rule-handler/root-handler.rb +6 -2
  61. data/lib/pione/rule-handler/update-criteria.rb +152 -0
  62. data/lib/pione/rule-handler.rb +14 -0
  63. data/lib/pione/system/identifier.rb +9 -9
  64. data/lib/pione/transformer/block-transformer.rb +4 -0
  65. data/lib/pione/transformer/expr-transformer.rb +1 -1
  66. data/lib/pione/transformer/flow-element-transformer.rb +4 -2
  67. data/lib/pione/transformer/literal-transformer.rb +14 -3
  68. data/lib/pione/transformer/rule-definition-transformer.rb +17 -5
  69. data/lib/pione/tuple-space/data-finder.rb +15 -52
  70. data/lib/pione/version.rb +1 -1
  71. data/lib/pione.rb +12 -38
  72. data/test/agent/spec_task-worker.rb +13 -12
  73. data/test/model/spec_assignment.rb +2 -2
  74. data/test/model/spec_binary-operator.rb +10 -10
  75. data/test/model/spec_block.rb +8 -8
  76. data/test/model/spec_boolean.rb +1 -72
  77. data/test/model/spec_boolean.yml +134 -0
  78. data/test/model/spec_data-expr.rb +50 -237
  79. data/test/model/spec_data-expr.yml +16 -45
  80. data/test/model/spec_data-expr_match.yml +45 -0
  81. data/test/model/spec_feature-expr.rb +2 -43
  82. data/test/model/spec_feature-expr.yml +0 -28
  83. data/test/model/spec_feature-expr_decide.yml +28 -0
  84. data/test/model/spec_float.rb +1 -119
  85. data/test/model/spec_float.yml +17 -0
  86. data/test/model/spec_integer.rb +1 -119
  87. data/test/model/spec_integer.yml +57 -0
  88. data/test/model/spec_keyed-sequence.rb +5 -0
  89. data/test/model/spec_keyed-sequence.yml +22 -0
  90. data/test/model/spec_message.rb +7 -7
  91. data/test/model/spec_parameters.rb +50 -63
  92. data/test/model/spec_pione-method.rb +56 -0
  93. data/test/model/spec_rule-expr.rb +18 -8
  94. data/test/model/spec_rule.rb +12 -12
  95. data/test/model/spec_sequence.rb +5 -0
  96. data/test/model/spec_sequence.yml +60 -0
  97. data/test/model/spec_string.rb +3 -70
  98. data/test/model/spec_string.yml +83 -0
  99. data/test/model/spec_ticket-expr.rb +4 -54
  100. data/test/model/spec_ticket-expr.yml +11 -0
  101. data/test/model/spec_variable-table.rb +41 -42
  102. data/test/model/spec_variable.rb +20 -22
  103. data/test/parser/spec_block-parser.yml +9 -0
  104. data/test/parser/spec_expr-parser.yml +0 -17
  105. data/test/parser/spec_flow-element-parser.yml +1 -1
  106. data/test/parser/spec_rule-definition-parser.yml +0 -4
  107. data/test/rule-handler/spec_update-criteria.pione +39 -0
  108. data/test/rule-handler/spec_update-criteria.rb +53 -0
  109. data/test/rule-handler/spec_update-criteria.yml +158 -0
  110. data/test/test-util.rb +25 -0
  111. data/test/transformer/spec_block-transformer.rb +7 -0
  112. data/test/transformer/spec_expr-transformer.rb +64 -19
  113. data/test/transformer/spec_flow-element-transformer.rb +11 -11
  114. data/test/transformer/spec_literal-transformer.rb +29 -29
  115. data/test/transformer/spec_rule-definition-transformer.rb +39 -21
  116. metadata +57 -11
  117. data/lib/pione/model/undefined-value.rb +0 -24
  118. data/lib/pione/tuple-space/update-criteria.rb +0 -97
  119. data/test/model/spec_list.rb +0 -26
  120. data/test/model/spec_rule-io.rb +0 -32
  121. data/test/spec_update-criteria.rb +0 -83
@@ -13,8 +13,8 @@ module Pione
13
13
  # @api private
14
14
  def message
15
15
  args = [
16
- @type.type_string,
17
- @obj.pione_model_type.type_string,
16
+ @type.name,
17
+ @obj.pione_model_type.name,
18
18
  @obj.line,
19
19
  @obj.column
20
20
  ]
@@ -39,267 +39,95 @@ module Pione
39
39
 
40
40
  # @api private
41
41
  def message
42
- str = nil
43
- begin
44
- str = @obj.call_pione_method("as_string")
45
- rescue => e
46
- str = @obj.to_s
47
- end
48
- "method \"%s\" is not found in %s" % [@name, str]
42
+ "PIONE method \"%s\" is not found in %s" % [@name, @obj.inspect]
49
43
  end
50
44
  end
51
45
 
52
- # Type is a class for type expression of PIONE model objects.
53
- class Type < System::PioneObject
54
- attr_reader :type_string
55
- attr_reader :method_interface
56
- attr_reader :method_body
57
-
58
- # Creates a type for PIONE model object.
59
- # @param [Symbol] type_string
60
- # PIONE model type
61
- def initialize(type_string)
62
- @type_string = type_string
63
- @method_interface = {}
64
- @method_body = {}
65
- end
66
-
67
- # Return true if the type or the pione model object matches.
68
- # @param [Type, BasicModel] other
69
- # type or object for match test target
70
- # @return [Boolean]
71
- # true if it matches, or false
72
- def match(other)
73
- case other
74
- when Type
75
- other == TypeAny || @type_string == other.type_string
76
- when BasicModel
77
- match(other.pione_model_type)
78
- when nil
79
- # do nothing
80
- else
81
- raise ArgumentError.new(other)
82
- end
83
- end
84
-
85
- # Defines PIONE model object methods.
86
- # @param [String] name
87
- # method name
88
- # @param [Array<Type>] inputs
89
- # input types of the method
90
- # @param [Type] output
91
- # output type of the method
92
- # @param [Proc] b
93
- # @return [void]
94
- def define_pione_method(name, inputs, output, &b)
95
- raise ArgumentError.new(inputs) unless inputs.kind_of?(Array)
96
- raise ArgumentError.new(inputs) unless inputs.all?{|input|
97
- input.kind_of?(Type)
98
- }
99
- raise ArgumentError.new(output) unless output.kind_of?(Type)
100
- @method_interface[name] = PioneMethodInterface.new(inputs, output)
101
- @method_body[name] = b
102
- end
103
-
104
- # Returns true if the data has the type.
105
- # @return [void]
106
- def check(data)
107
- unless match(data.pione_model_type)
108
- raise PioneModelTypeError.new(data, self)
109
- end
110
- end
111
-
112
- # @api private
113
- def to_s
114
- "#<Type %s>" % @type_string
115
- end
116
-
117
- # @api private
118
- def ==(other)
119
- @type_string == other.type_string
120
- end
121
-
122
- # @api private
123
- def hash
124
- @type_string.hash
125
- end
126
- end
127
-
128
- class VariableType
129
- def initialize(name)
130
- @name = name
131
- end
132
- end
133
-
134
- # TypeList represetnts list type of element type.
135
- class TypeList < Type
136
- attr_reader :element_type
137
-
138
- # @api private
139
- @table = {}
140
-
46
+ # BasicModel is a class for pione model object.
47
+ class BasicModel < Pione::PioneObject
141
48
  class << self
142
- def [](type)
143
- if @table.has_key?(type)
144
- return @table[type]
145
- else
146
- t = new(type)
147
- @table[type] = t
148
- return t
149
- end
49
+ # Return true if the object is atomic.
50
+ #
51
+ # @return [Boolean]
52
+ # true if the object is atom, or false.
53
+ def atomic?
54
+ @atomic ||= true
150
55
  end
151
- end
152
56
 
153
- def initialize(element_type)
154
- @element_type = element_type
155
- super("[%s]" % [element_type.type_string])
57
+ def set_atomic(b)
58
+ @atomic = b
59
+ end
156
60
  end
157
61
 
158
- def match(other)
159
- return false unless other.kind_of?(TypeList)
160
- @element_type.match(other.element_type)
161
- end
62
+ forward :class, :atomic?
162
63
 
163
- def method_body
164
- if self == self.class[TypeAny]
165
- @method_body
166
- else
167
- self.class[TypeAny].method_body
168
- end
64
+ # Creates a model object.
65
+ def initialize(&b)
66
+ instance_eval(&b) if block_given?
169
67
  end
170
68
 
171
- def method_interface
172
- if self == self.class[TypeAny]
173
- @method_interface
174
- else
175
- self.class[TypeAny].method_interface
176
- end
69
+ # Evaluates the model object in the variable table.
70
+ # @param [VariableTable] vtable
71
+ # variable table for evaluation
72
+ # @return [BasicModel]
73
+ # evaluated object
74
+ def eval(vtable=VariableTable.new)
75
+ return self
177
76
  end
178
- end
179
77
 
180
- # PioneMethodInterface represents type of PIONE object's methods.
181
- class PioneMethodInterface < Pione::PioneObject
182
- attr_reader :inputs
183
- attr_reader :output
184
-
185
- # Creates an interface for a pione method.
186
- # @param [Array<Type>] inputs
187
- # inputs type definition
188
- # @param [Type] output
189
- # ouutput type definition
190
- def initialize(inputs, output)
191
- @inputs = inputs
192
- @output = output
78
+ # Returns true if the object has pione variables.
79
+ # @return [Boolean]
80
+ # true if the object has pione variables, or false
81
+ def include_variable?
82
+ false
193
83
  end
194
84
 
195
- # Validates inputs data types for the method.
85
+ # Returns rule definition document path.
196
86
  # @return [void]
197
- def validate_inputs(*args)
198
- @inputs.each_with_index do |input, i|
199
- unless input.match(args[i].pione_model_type)
200
- raise PioneModelTypeError.new(args[i], input)
201
- end
202
- end
87
+ def set_document_path(path)
88
+ @__document_path__ = path
203
89
  end
204
90
 
205
- # Validates output data type for the method.
91
+ # Sets line and column number of the definition.
206
92
  # @return [void]
207
- def validate_output(value)
208
- @output.match(value.pione_model_type)
93
+ def set_line_and_column(line_and_column)
94
+ @__line__, @__column__ = line_and_column
209
95
  end
210
- end
211
-
212
- # boolean type for PIONE system
213
- TypeBoolean = Type.new("boolean")
214
-
215
- # integer type for PIONE system
216
- TypeInteger = Type.new("integer")
217
-
218
- # float type for PIONE system
219
- TypeFloat = Type.new("float")
220
-
221
- # string type for PIONE system
222
- TypeString = Type.new("string")
223
-
224
- # data expression type for PIONE system
225
- TypeDataExpr = Type.new("data-expr")
226
-
227
- # feature type for PIONE system
228
- TypeFeature = Type.new("feature")
229
-
230
- # rule expression type for PIONE system
231
- TypeRuleExpr = Type.new("rule-expr")
232
-
233
- # parameters type for PIONE system
234
- TypeParameters = Type.new("parameters")
235
-
236
- # assignment type for PIONE system
237
- TypeAssignment = Type.new("assignment")
238
-
239
- # variable table type for PIONE system
240
- TypeVariableTable = Type.new("variable-table")
241
96
 
242
- # package type for PIONE system
243
- TypePackage = Type.new("package")
244
-
245
- # undefined value type for PIONE system
246
- TypeUndefinedValue = Type.new("undefined-value")
247
-
248
- # rule io list type for PIONE system
249
- TypeRuleIOList = Type.new("rule-io-list")
250
-
251
- # rule io element type for PIONE system
252
- TypeRuleIOElement = Type.new("rule-io-element")
253
-
254
- # ticket expression type
255
- TypeTicketExpr = Type.new("ticket-expr")
256
-
257
- # any type for PIONE system
258
- TypeAny = Type.new("any")
259
-
260
- def TypeAny.match(other)
261
- true
262
- end
97
+ # Returns line number of the model in definition document.
98
+ # @return [Integer]
99
+ # line number
100
+ def line
101
+ @__line__
102
+ end
263
103
 
264
- TypeAny.instance_eval do
265
- define_pione_method("==", [TypeAny], TypeBoolean) do |res, other|
266
- if other.pione_model_type == TypeAny && res.name == other.name
267
- PioneBoolean.true
268
- else
269
- raise UnboundVariableError.new(res)
270
- end
104
+ # Returns coloumn number of the model in definition document.
105
+ # @return [Integer]
106
+ # column number
107
+ def column
108
+ @__column__
271
109
  end
272
110
 
273
- define_pione_method("!=", [TypeAny], TypeBoolean) do |res, other|
274
- PioneBoolean.not(res.call_pione_method("==", other))
111
+ # Returns itself.
112
+ # @return [BasicModel]
113
+ def to_pione
114
+ self
275
115
  end
276
116
  end
277
117
 
278
- # BasicModel is a class for pione model object.
279
- class BasicModel < Pione::PioneObject
118
+ class Callable < BasicModel
280
119
  class << self
281
- # Sets pione model type of the model.
282
- # @param [Symbol] type
120
+ attr_reader :pione_model_type
121
+
122
+ # Set pione model type of the model.
123
+ #
124
+ # @param [Type] type
283
125
  # pione model type
284
126
  # @return [void]
285
127
  def set_pione_model_type(type)
286
- raise ArgumentError unless type.kind_of?(Type)
287
128
  @pione_model_type = type
288
129
  end
289
130
 
290
- # Returns the pione model type of the model.
291
- # @return [Symbol]
292
- # pione model type
293
- def pione_model_type
294
- @pione_model_type
295
- end
296
-
297
- # Defines a pione method.
298
- # @return [void]
299
- def define_pione_method(*args, &b)
300
- @pione_model_type.define_pione_method(*args, &b)
301
- end
302
-
303
131
  # @api private
304
132
  def inherited(subclass)
305
133
  if @pione_model_type
@@ -308,34 +136,23 @@ module Pione
308
136
  end
309
137
  end
310
138
 
139
+ forward :class, :pione_model_type
140
+
311
141
  # Creates a model object.
312
142
  def initialize(&b)
313
143
  instance_eval(&b) if block_given?
314
144
  end
315
145
 
316
- # Returns PIONE model type.
317
- # @return [Symbol]
318
- # PIONE model type
319
- def pione_model_type
320
- self.class.pione_model_type
321
- end
322
-
323
- # Evaluates the model object in the variable table.
146
+ # Evaluate the model object in the variable table.
147
+ #
324
148
  # @param [VariableTable] vtable
325
149
  # variable table for evaluation
326
150
  # @return [BasicModel]
327
- # evaluated object
151
+ # evaluated result
328
152
  def eval(vtable=VariableTable.new)
329
153
  return self
330
154
  end
331
155
 
332
- # Returns true if the object is atomic.
333
- # @return [Boolean]
334
- # true if the object is atom, or false.
335
- def atomic?
336
- true
337
- end
338
-
339
156
  # Returns true if the object has pione variables.
340
157
  # @return [Boolean]
341
158
  # true if the object has pione variables, or false
@@ -343,33 +160,8 @@ module Pione
343
160
  false
344
161
  end
345
162
 
346
- # Returns rule definition document path.
347
- # @return [void]
348
- def set_document_path(path)
349
- @__document_path__ = path
350
- end
351
-
352
- # Sets line and column number of the definition.
353
- # @return [void]
354
- def set_line_and_column(line_and_column)
355
- @__line__, @__column__ = line_and_column
356
- end
357
-
358
- # Returns line number of the model in definition document.
359
- # @return [Integer]
360
- # line number
361
- def line
362
- @__line__
363
- end
364
-
365
- # Returns coloumn number of the model in definition document.
366
- # @return [Integer]
367
- # column number
368
- def column
369
- @__column__
370
- end
371
-
372
- # Calls pione model object method.
163
+ # Call pione model object method.
164
+ #
373
165
  # @param [String] name
374
166
  # method name
375
167
  # @param [Array] args
@@ -377,12 +169,8 @@ module Pione
377
169
  # @return [Object]
378
170
  # method's result
379
171
  def call_pione_method(name, *args)
380
- name = name.to_s
381
- if method = pione_model_type.method_interface[name]
382
- method.validate_inputs(*args)
383
- output = pione_model_type.method_body[name].call(self, *args)
384
- pione_model_type.method_interface[name].validate_output(output)
385
- return output
172
+ if pione_method = pione_model_type.find_method(name, self, *args)
173
+ pione_method.call(self, *args)
386
174
  else
387
175
  raise MethodNotFound.new(name, self)
388
176
  end
@@ -394,5 +182,31 @@ module Pione
394
182
  self
395
183
  end
396
184
  end
185
+
186
+ class Element < BasicModel
187
+ class << self
188
+ attr_reader :sequence_class
189
+
190
+ def set_sequence_class(sequence_class)
191
+ @sequence_class = sequence_class
192
+ end
193
+ end
194
+
195
+ forward :class, :sequence_class
196
+
197
+ def to_seq
198
+ sequence_class.new([self])
199
+ end
200
+ end
201
+
202
+ class Value < Element
203
+ attr_reader :value
204
+
205
+ # @param value [Integer]
206
+ # value in ruby
207
+ def initialize(value)
208
+ @value = value
209
+ end
210
+ end
397
211
  end
398
212
  end
@@ -46,7 +46,11 @@ module Pione
46
46
 
47
47
  # @api private
48
48
  def pione_model_type
49
- @left.pione_model_type.method_interface[@symbol].output
49
+ if pione_method = @left.pione_model_type.find_method(@symbol, @left, @right)
50
+ return pione_method.get_output_type(@left)
51
+ else
52
+ raise MethodNotFound.new(@name.to_s, self)
53
+ end
50
54
  end
51
55
 
52
56
  # Evaluate the application expression and returns application result.
@@ -241,5 +241,22 @@ module Pione
241
241
  @condition.hash + @blocks.hash
242
242
  end
243
243
  end
244
+
245
+ # EmptyBlock is no elements block.
246
+ class EmptyBlock < BasicModel
247
+ include Singleton
248
+
249
+ def eval(vtable)
250
+ return self
251
+ end
252
+
253
+ def include_variable?
254
+ false
255
+ end
256
+
257
+ def textize
258
+ "empty_block()"
259
+ end
260
+ end
244
261
  end
245
262
  end
@@ -1,9 +1,7 @@
1
1
  module Pione
2
2
  module Model
3
3
  # PioneBoolean representes truth value in PIONE system.
4
- class PioneBoolean < BasicModel
5
- set_pione_model_type TypeBoolean
6
-
4
+ class PioneBoolean < Value
7
5
  # Returns the value in ruby.
8
6
  attr_reader :value
9
7
 
@@ -58,8 +56,8 @@ module Pione
58
56
  # @param value [Boolean]
59
57
  # true or false
60
58
  def initialize(value)
59
+ raise ArgumentError.new(value) unless value == true or value == false
61
60
  @value = value
62
- super()
63
61
  end
64
62
 
65
63
  # @api private
@@ -96,6 +94,10 @@ module Pione
96
94
  return @value
97
95
  end
98
96
 
97
+ def to_seq
98
+ BooleanSequence.new([self])
99
+ end
100
+
99
101
  # @api private
100
102
  def ==(other)
101
103
  return false unless other.kind_of?(self.class)
@@ -109,40 +111,70 @@ module Pione
109
111
  @value.hash
110
112
  end
111
113
 
112
- #
113
- # pione methods
114
- #
115
-
116
- define_pione_method("==", [TypeBoolean], TypeBoolean) do |rec, other|
117
- PioneBoolean.new(rec.value == other.value)
114
+ def inspect
115
+ "#<PioneBoolean %s>" % @value
118
116
  end
117
+ end
119
118
 
120
- define_pione_method("!=", [TypeBoolean], TypeBoolean) do |rec, other|
121
- PioneBoolean.not(rec.call_pione_method("==", other))
122
- end
119
+ class BooleanSequence < OrdinalSequence
120
+ set_pione_model_type TypeBoolean
121
+ set_element_class PioneBoolean
122
+ set_shortname "BSeq"
123
123
 
124
- define_pione_method("&&", [TypeBoolean], TypeBoolean) do |rec, other|
125
- PioneBoolean.new(rec.value && other.value)
124
+ def value
125
+ @value ||= @elements.inject(true){|b, elt| b and elt.value}
126
126
  end
127
+ end
127
128
 
128
- define_pione_method("||", [TypeBoolean], TypeBoolean) do |rec, other|
129
- PioneBoolean.new(rec.value || other.value)
130
- end
129
+ #
130
+ # pione methods
131
+ #
131
132
 
133
+ TypeBoolean.instance_eval do
132
134
  define_pione_method("and", [TypeBoolean], TypeBoolean) do |rec, other|
133
- rec.call_pione_method("&&", other)
135
+ sequential_map2(TypeBoolean, rec, other) do |rec_elt, other_elt|
136
+ rec_elt.value && other_elt.value
137
+ end
134
138
  end
135
139
 
136
140
  define_pione_method("or", [TypeBoolean], TypeBoolean) do |rec, other|
137
- rec.call_pione_method("||", other)
141
+ sequential_map2(TypeBoolean, rec, other) do |rec_elt, other_elt|
142
+ rec_elt.value || other_elt.value
143
+ end
144
+ end
145
+
146
+ define_pione_method("as_integer", [], TypeInteger) do |rec|
147
+ sequential_map1(TypeInteger, rec) {|rec| rec.value ? 1 : 0}
148
+ end
149
+
150
+ define_pione_method("as_float", [], TypeFloat) do |rec|
151
+ sequential_map1(TypeFloat, rec) {|rec| rec.value ? 1.0 : 0.0}
138
152
  end
139
153
 
140
154
  define_pione_method("as_string", [], TypeString) do |rec|
141
- PioneString.new(rec.value.to_s)
155
+ sequential_map1(TypeString, rec) {|rec| rec.value.to_s}
156
+ end
157
+
158
+ define_pione_method("as_data_expr", [], TypeDataExpr) do |rec|
159
+ sequential_map1(TypeDataExpr, rec) {|rec| rec.value.to_s}
142
160
  end
143
161
 
144
162
  define_pione_method("not", [], TypeBoolean) do |rec|
145
- PioneBoolean.not(rec)
163
+ sequential_map1(TypeBoolean, rec) do |elt|
164
+ not(elt.value)
165
+ end
166
+ end
167
+
168
+ define_pione_method("every?", [], TypeBoolean) do |rec|
169
+ PioneBoolean.new(not(rec.elements.include?(PioneBoolean.false))).to_seq
170
+ end
171
+
172
+ define_pione_method("any?", [], TypeBoolean) do |rec|
173
+ PioneBoolean.new(rec.elements.include?(PioneBoolean.true)).to_seq
174
+ end
175
+
176
+ define_pione_method("one?", [], TypeBoolean) do |rec|
177
+ PioneBoolean.new(rec.elements.select{|elt| elt == PioneBoolean.true}.size == 1).to_seq
146
178
  end
147
179
  end
148
180
  end
@@ -0,0 +1,34 @@
1
+ module Pione
2
+ module Model
3
+ class Constraints
4
+ class << self
5
+ # Return the empty constraints.
6
+ #
7
+ # @return [Constraints]
8
+ # empty constraints
9
+ def empty
10
+ new([])
11
+ end
12
+ end
13
+
14
+ # @param exprs [Array<BasicModel>]
15
+ # constraint expressions
16
+ def initialize(exprs)
17
+ @exprs = exprs
18
+ end
19
+
20
+ # Return true if constraints satisfied.
21
+ #
22
+ # @param vtable [VariableTable]
23
+ # variable table
24
+ # @return [Boolean]
25
+ # true if constraints satisfied
26
+ def satisfy?(vtable)
27
+ @exprs.all? do |expr|
28
+ res = expr.eval(vtable)
29
+ res.kind_of?(BooleanSequence) and res.value
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end