add n_ary operators
This commit is contained in:
parent
698eff0a50
commit
daaa54b6f0
9 changed files with 146 additions and 2 deletions
43
app/models/logic/n_ary_operator.rb
Normal file
43
app/models/logic/n_ary_operator.rb
Normal file
|
@ -0,0 +1,43 @@
|
|||
class Logic::NAryOperator < Logic::Term
|
||||
attr_reader :operands
|
||||
|
||||
def initialize(operands)
|
||||
@operands = operands
|
||||
end
|
||||
|
||||
def to_h
|
||||
{
|
||||
"op" => self.class.name,
|
||||
"operands" => @operands.map(&:to_h)
|
||||
}
|
||||
end
|
||||
|
||||
def self.from_h(h)
|
||||
self.new(h['operands'].map { |operand_h| Logic.from_h(operand_h) })
|
||||
end
|
||||
|
||||
def errors(stable_ids = [])
|
||||
errors = []
|
||||
|
||||
if @operands.empty?
|
||||
errors += ["opérateur '#{operator_name}' vide"]
|
||||
end
|
||||
|
||||
not_booleans = @operands.filter { |operand| operand.type != :boolean }
|
||||
if not_booleans.present?
|
||||
errors += ["'#{operator_name}' ne contient pas que des booléens : #{not_booleans.map(&:to_s).join(', ')}"]
|
||||
end
|
||||
|
||||
errors + @operands.flat_map { |operand| operand.errors(stable_ids) }
|
||||
end
|
||||
|
||||
def type = :boolean
|
||||
|
||||
def ==(other)
|
||||
self.class == other.class &&
|
||||
@operands.count == other.operands.count &&
|
||||
@operands.all? do |operand|
|
||||
@operands.count { |o| o == operand } == other.operands.count { |o| o == operand }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue