Class: ActiveFacts::CQL::Compiler::Sum

Inherits:
Operation
  • Object
show all
Defined in:
lib/activefacts/cql/compiler/expression.rb

Instance Attribute Summary collapse

Attributes inherited from Operation

#binding, #certainty, #clause, #fact_type, #player, #role, #role_ref

Instance Method Summary collapse

Methods inherited from Operation

#bind, #conjunction, #identify_other_players, #identify_players_with_role_name, #is_equality_comparison, #is_naked_object_type, #leading_adjective, #literal, #match_existing_fact_type, #nested_clauses, #objectification_of, #objectified_as, #operands, #result_value_type, #role_name, #side_effects, #trailing_adjective, #value_constraint

Constructor Details

#initialize(*terms) ⇒ Sum

Returns a new instance of Sum.



230
231
232
# File 'lib/activefacts/cql/compiler/expression.rb', line 230

def initialize *terms
  @terms = terms
end

Instance Attribute Details

#termsObject

Returns the value of attribute terms.



229
230
231
# File 'lib/activefacts/cql/compiler/expression.rb', line 229

def terms
  @terms
end

Instance Method Details

#compile(context) ⇒ Object



264
265
266
# File 'lib/activefacts/cql/compiler/expression.rb', line 264

def compile(context)
  compile_terms(context, @terms)
end

#compile_terms(context, terms) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/activefacts/cql/compiler/expression.rb', line 268

def compile_terms(context, terms)
  if terms.size == 1
    terms[0].compile(context)
  else
    lhs = terms.shift
    lhs_expr = lhs.compile(context)
    rhs_expr = compile_terms(context, terms)
    context.vocabulary.constellation.Expression(
        :new, :expression_type => 'Binary', :operator => operator,
        :first_operand_expression => lhs_expr, :second_operand_expression => rhs_expr
    )
  end
end

#identify_player(context) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/activefacts/cql/compiler/expression.rb', line 242

def identify_player context
  @player || begin
    # The players in the @terms have already been identified
    # REVISIT: Check compliance of all units in @terms, and apply conversions where necessary
    # REVISIT: The type of this result should be derived from type promotion rules. Here, we take the left-most.
    # REVISIT: We should define a subtype of the result type here, and apply the units to it.
    v = context.vocabulary
    @player = @terms[0].player
    @player
  end
end

#inspectObject



258
# File 'lib/activefacts/cql/compiler/expression.rb', line 258

def inspect; to_s; end

#operatorObject



238
239
240
# File 'lib/activefacts/cql/compiler/expression.rb', line 238

def operator
  '+'
end

#refsObject



234
235
236
# File 'lib/activefacts/cql/compiler/expression.rb', line 234

def refs
  @terms
end

#result_type_name(context) ⇒ Object



254
255
256
# File 'lib/activefacts/cql/compiler/expression.rb', line 254

def result_type_name(context)
  "SUM_OF<#{ @terms.map{|f| f.player.name}*', ' }>"
end

#to_sObject



260
261
262
# File 'lib/activefacts/cql/compiler/expression.rb', line 260

def to_s
  'SUM(' + @terms.map{|term| "#{term.to_s}" } * ' PLUS ' + ')'
end