Class: ActiveFacts::CQL::Compiler

Inherits:
Parser
  • Object
show all
Defined in:
lib/activefacts/cql/compiler.rb,
lib/activefacts/cql/compiler/fact.rb,
lib/activefacts/cql/compiler/query.rb,
lib/activefacts/cql/compiler/clause.rb,
lib/activefacts/cql/compiler/shared.rb,
lib/activefacts/cql/compiler/fact_type.rb,
lib/activefacts/cql/compiler/constraint.rb,
lib/activefacts/cql/compiler/expression.rb,
lib/activefacts/cql/compiler/value_type.rb,
lib/activefacts/cql/compiler/entity_type.rb

Defined Under Namespace

Classes: Binding, Clause, ClauseMatchSideEffect, ClauseMatchSideEffects, Comparison, CompilationContext, Constraint, ContextNote, Definition, Enforcement, EntityType, Fact, FactType, Import, Literal, Negate, ObjectType, Operation, PresenceConstraint, Product, Quantifier, Query, Reciprocal, Reference, ReferenceMode, RingConstraint, SetComparisonConstraint, SetConstraint, SetEqualityConstraint, SetExclusionConstraint, SubsetConstraint, Sum, Unit, ValueConstraint, ValueType, Vocabulary

Constant Summary collapse

LANGUAGES =
{
  'en' => 'English',
  'fr' => 'French',
  'cn' => 'Mandarin'
}

Constants included from ActiveFacts

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Parser

#context, #parse, #parse_all

Constructor Details

#initialize(*a) ⇒ Compiler

Returns a new instance of Compiler.



27
28
29
30
31
32
33
# File 'lib/activefacts/cql/compiler.rb', line 27

def initialize *a
  @filename = a.shift || "stdio"
  super *a
  @constellation = ActiveFacts::API::Constellation.new(ActiveFacts::Metamodel)
  @language = nil
  debug :file, "Parsing '#{@filename}'"
end

Instance Attribute Details

#vocabularyObject (readonly)

Returns the value of attribute vocabulary.



25
26
27
# File 'lib/activefacts/cql/compiler.rb', line 25

def vocabulary
  @vocabulary
end

Instance Method Details

#compile(input) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/activefacts/cql/compiler.rb', line 59

def compile input
  include_language

  @string = input

  # The syntax tree created from each parsed CQL statement gets passed to the block.
  # parse_all returns an array of the block's non-nil return values.
  ok = parse_all(@string, :definition) do |node|
    debug :parse, "Parsed '#{node.text_value.gsub(/\s+/,' ').strip}'" do
      begin
        ast = node.ast
        next unless ast
        debug :ast, ast.inspect
        ast.tree = node
        ast.constellation = @constellation
        ast.vocabulary = @vocabulary
        value = compile_definition ast
        debug :definition, "Compiled to #{value.is_a?(Array) ? value.map{|v| v.verbalise}*', ' : value.verbalise}" if value
        @vocabulary = value if ast.is_a?(Compiler::Vocabulary)
      rescue => e
        # Augment the exception message, but preserve the backtrace
        start_line = @string.line_of(node.interval.first)
        end_line = @string.line_of(node.interval.last-1)
        lines = start_line != end_line ? "s #{start_line}-#{end_line}" : " #{start_line.to_s}"
        ne = StandardError.new("at line#{lines} #{e.message.strip}")
        ne.set_backtrace(e.backtrace)
        raise ne
      end
    end
  end
  raise failure_reason unless ok
  vocabulary
end

#compile_definition(ast) ⇒ Object



119
120
121
# File 'lib/activefacts/cql/compiler.rb', line 119

def compile_definition ast
  ast.compile
end

#compile_file(filename) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/activefacts/cql/compiler.rb', line 35

def compile_file filename
  old_filename = @filename
  @filename = filename
  File.open(filename) do |f|
    compile(f.read)
  end
  @filename = old_filename
  @vocabulary
end

#compile_import(file, aliases) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/activefacts/cql/compiler.rb', line 93

def compile_import file, aliases
  saved_index = @index
  saved_block = @block
  saved_string = @string
  saved_input_length = @input_length
  old_filename = @filename
  @filename = File.dirname(old_filename)+'/'+file+'.cql'

  # REVISIT: Save and use another @vocabulary for this file?
  File.open(@filename) do |f|
    ok = parse_all(f.read, nil, &@block)
  end

rescue => e
  ne = StandardError.new("In #{@filename} #{e.message.strip}")
  ne.set_backtrace(e.backtrace)
  raise ne
ensure
  @block = saved_block
  @index = saved_index
  @input_length = saved_input_length
  @string = saved_string
  @filename = old_filename
  nil
end

#detect_languageObject

Load the appropriate natural language module



46
47
48
49
50
# File 'lib/activefacts/cql/compiler.rb', line 46

def detect_language
  @filename =~ /.*\.(..)\.cql$/i
  language_code = $1
  @language = LANGUAGES[language_code] || 'English'
end

#include_languageObject



52
53
54
55
56
57
# File 'lib/activefacts/cql/compiler.rb', line 52

def include_language
  detect_language unless @langage
  require 'activefacts/cql/Language/'+@language
  language_module = ActiveFacts::CQL.const_get(@language)
  extend language_module
end

#unit?(s) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
# File 'lib/activefacts/cql/compiler.rb', line 123

def unit? s
  name = @constellation.Name[s]
  units = (!name ? [] : Array(name.unit) + Array(name.unit_as_plural_name)).uniq
  debug :units, "Looking for unit #{s}, got #{units.map{|u|u.name}.inspect}"
  units.size > 0
end