Class: ActiveFacts::CQL::Compiler::ValueType

Inherits:
ObjectType show all
Defined in:
lib/activefacts/cql/compiler/value_type.rb

Instance Attribute Summary

Attributes inherited from ObjectType

#name

Attributes inherited from Definition

#constellation, #tree, #vocabulary

Instance Method Summary collapse

Methods inherited from Definition

#all_bindings_in_clauses, #build_all_steps, #build_step, #build_variables, #source

Constructor Details

#initialize(name, base, parameters, unit, value_constraint, pragmas, context_note, auto_assigned_at) ⇒ ValueType

Returns a new instance of ValueType.



90
91
92
93
94
95
96
97
98
99
# File 'lib/activefacts/cql/compiler/value_type.rb', line 90

def initialize name, base, parameters, unit, value_constraint, pragmas, context_note, auto_assigned_at
  super name
  @base_type_name = base
  @parameters = parameters
  @unit = unit
  @value_constraint = value_constraint
  @pragmas = pragmas
  @context_note = context_note
  @auto_assigned_at = auto_assigned_at
end

Instance Method Details

#compileObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/activefacts/cql/compiler/value_type.rb', line 101

def compile
  length, scale = *@parameters

  # Create the base type unless it already exists:
  base_type = nil
  if (@base_type_name != @name)
    unless base_type = @vocabulary.valid_value_type_name(@base_type_name)
      base_type = @constellation.ValueType(@vocabulary, @base_type_name, :concept => :new)
      return base_type if @base_type_name == @name
    end
  end

  # Create and initialise the ValueType:
  vt = @vocabulary.valid_value_type_name(@name) ||
    @constellation.ValueType(@vocabulary, @name, :concept => :new)
  vt.is_independent = true if @pragmas.delete('independent')
  @pragmas.each do |p|
    @constellation.ConceptAnnotation(:concept => vt.concept, :mapping_annotation => p)
  end if @pragmas
  vt.supertype = base_type if base_type
  vt.length = length if length
  vt.scale = scale if scale
  vt.transaction_phase = @auto_assigned_at

  unless @unit.empty?
    unit_name, exponent = *@unit[0]
    unit = @constellation.Name[unit_name].unit ||
      @constellation.Name[unit_name].plural_named_unit
    raise "Unit #{unit_name} for value type #{@name} is not defined" unless unit
    if exponent != 1
      base_unit = unit
      unit_name = base_unit.name+"^#{exponent}"
      unless unit = @constellation.Unit.detect{|k,v| v.name == unit_name } 
        # Define a derived unit (these are skipped on output)
        unit = @constellation.Unit(:new,
              :vocabulary => @vocabulary,
              :name => unit_name,
              :is_fundamental => false
            )
        @constellation.Derivation(unit, base_unit).exponent = exponent
      end
    end
    vt.unit = unit
  end

  if @value_constraint
    @value_constraint.constellation = @constellation
    vt.value_constraint = @value_constraint.compile
  end

  if @context_note
    @context_note.compile(@constellation, vt)
  end

  vt
end

#to_sObject



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/activefacts/cql/compiler/value_type.rb', line 158

def to_s
  "ValueType: #{super} is written as #{
      @base_type_name
    }#{
      @parameters.size > 0 ? "(#{ @parameters.map{|p|p.to_s}*', ' })" : ''
    }#{
      @unit && @unit.length > 0 ? " in #{@unit.inspect}" : ''
    }#{
      @value_constraint ? " "+@value_constraint.to_s : ''
    }#{
      @pragmas.size > 0 ? ", pragmas [#{@pragmas*','}]" : ''
    };"
end